Load schema at require time and use transactions.

This commit is contained in:
Young Hahn
2013-12-05 14:57:53 -05:00
parent 850efc5d38
commit 9868c9aca4
2 changed files with 6 additions and 8 deletions

View File

@@ -22,6 +22,7 @@ function hash(z, x, y) {
// insertion, etc.)
module.exports = MBTiles;
MBTiles.utils = require('./utils');
MBTiles.schema = fs.readFileSync(__dirname + '/schema.sql', 'utf8');
// Provides access to an mbtiles database file.
// - uri: A parsed URL hash, the only relevant part is `pathname`.
@@ -124,14 +125,7 @@ MBTiles.prototype._integrity = function(callback) {
// Setup schema, indices, views for a new mbtiles database.
// - @param {Function(err)} callback
MBTiles.prototype._setup = function(callback) {
var mbtiles = this;
mbtiles._exists('tiles', function(err, exists) {
if (exists) return callback(null);
fs.readFile(__dirname + '/schema.sql', 'utf8', function(err, sql) {
if (err) return callback(err);
mbtiles._db.exec(sql, callback);
});
});
this._db.exec(MBTiles.schema, callback);
};
// Select a tile from an mbtiles database. Scheme is XYZ.

View File

@@ -1,5 +1,7 @@
-- MBTiles schema
BEGIN;
CREATE TABLE IF NOT EXISTS map (
zoom_level INTEGER,
tile_column INTEGER,
@@ -70,3 +72,5 @@ CREATE VIEW IF NOT EXISTS grid_data AS
FROM map
JOIN grid_key ON map.grid_id = grid_key.grid_id
JOIN keymap ON grid_key.key_name = keymap.key_name;
COMMIT;