change tests to new interface
This commit is contained in:
@@ -4,9 +4,14 @@ var _ = require('underscore'),
|
||||
crypto = require('crypto'),
|
||||
zlib = require('zlib'),
|
||||
path = require('path'),
|
||||
url = require('url'),
|
||||
sm = new (require('sphericalmercator')),
|
||||
sqlite3 = require('sqlite3');
|
||||
|
||||
function noop(err) {
|
||||
if (err) throw err;
|
||||
}
|
||||
|
||||
// MBTiles
|
||||
// -------
|
||||
// MBTiles class for doing common operations (schema setup, tile reading,
|
||||
@@ -18,6 +23,9 @@ module.exports = MBTiles;
|
||||
// - callback: Will be called when the resources have been acquired
|
||||
// or acquisition failed.
|
||||
function MBTiles(uri, callback) {
|
||||
if (typeof callback !== 'function') callback = noop;
|
||||
if (typeof uri === 'string') uri = url.parse(uri);
|
||||
|
||||
this.filename = uri.pathname;
|
||||
this.db = new sqlite3.cached.Database(uri.pathname, function(err) {
|
||||
if (err) return callback(err);
|
||||
@@ -27,6 +35,10 @@ function MBTiles(uri, callback) {
|
||||
|
||||
// Finds all mbtiles file in the filepath and returns their tilesource URI.
|
||||
MBTiles.list = function(filepath, callback) {
|
||||
if (typeof callback !== 'function') {
|
||||
throw new Error('Callback required as second argument');
|
||||
}
|
||||
|
||||
filepath = path.resolve(filepath);
|
||||
fs.readdir(filepath, function(err, files) {
|
||||
if (err) return callback(err);
|
||||
@@ -41,6 +53,10 @@ MBTiles.list = function(filepath, callback) {
|
||||
// Finds an mbtiles file with the given ID in the filepath and returns a
|
||||
// tilesource URI.
|
||||
MBTiles.findID = function(filepath, id, callback) {
|
||||
if (typeof callback !== 'function') {
|
||||
throw new Error('Callback required as third argument');
|
||||
}
|
||||
|
||||
filepath = path.resolve(filepath);
|
||||
var file = path.join(filepath, id + '.mbtiles');
|
||||
fs.stat(file, function(err, stats) {
|
||||
@@ -52,6 +68,10 @@ MBTiles.findID = function(filepath, id, callback) {
|
||||
// Retrieve the schema of the current mbtiles database and inform the caller of
|
||||
// whether the specified table exists.
|
||||
MBTiles.prototype.exists = function(table, callback) {
|
||||
if (typeof callback !== 'function') {
|
||||
throw new Error('Callback required as second argument');
|
||||
}
|
||||
|
||||
if (this.schema) {
|
||||
return callback(null, _(this.schema).include(table));
|
||||
} else {
|
||||
@@ -71,6 +91,10 @@ MBTiles.prototype.exists = function(table, callback) {
|
||||
|
||||
// DB integrity check.
|
||||
MBTiles.prototype.integrity = function(callback) {
|
||||
if (typeof callback !== 'function') {
|
||||
throw new Error('Callback required as first argument');
|
||||
}
|
||||
|
||||
this.db.get('PRAGMA quick_check(1)', function(err, row) {
|
||||
if (!(row && row.integrity_check && row.integrity_check === 'ok')) {
|
||||
callback(new Error('Corrupted database.'));
|
||||
@@ -84,6 +108,10 @@ MBTiles.prototype.integrity = function(callback) {
|
||||
// Sets the synchronous flag to OFF for (much) faster inserts.
|
||||
// See http://www.sqlite3.org/pragma.html#pragma_synchronous
|
||||
MBTiles.prototype.setup = function(callback) {
|
||||
if (typeof callback !== 'function') {
|
||||
throw new Error('Callback required as second argument');
|
||||
}
|
||||
|
||||
fs.readFile(__dirname + '/schema.sql', 'utf8', function(err, sql) {
|
||||
if (err) return callback(err);
|
||||
this.db.serialize(function() {
|
||||
@@ -100,6 +128,8 @@ MBTiles.prototype.setup = function(callback) {
|
||||
// has key/value pairs as a hash corresponding to column name and row value.
|
||||
// - `callback` Function.
|
||||
MBTiles.prototype.insert = function(table, objects, callback) {
|
||||
if (typeof callback !== 'function') callback = noop;
|
||||
|
||||
if (!objects.length) return callback(null);
|
||||
var keys = _(objects[0]).keys();
|
||||
var placeholders = [];
|
||||
@@ -120,6 +150,8 @@ MBTiles.prototype.insert = function(table, objects, callback) {
|
||||
// - @param {Object} metadata key, value hash of metadata to be inserted.
|
||||
// - @param {Function} callback
|
||||
MBTiles.prototype.insertMetadata = function(data, callback) {
|
||||
if (typeof callback !== 'function') callback = noop;
|
||||
|
||||
var metadata = _(data).map(function(value, key) {
|
||||
return { name: key, value: value};
|
||||
});
|
||||
@@ -168,6 +200,8 @@ MBTiles.prototype.insertTiles = function(data, callback) {
|
||||
// be an object of the form { z: z, x: x, y: y, data: [Image buffer], keys: [] }.
|
||||
// - @param {Function} callback
|
||||
MBTiles.prototype.insertGrids = function(data, callback) {
|
||||
if (typeof callback !== 'function') callback = noop;
|
||||
|
||||
var that = this,
|
||||
map = [],
|
||||
grids = [],
|
||||
@@ -221,6 +255,8 @@ MBTiles.prototype.insertGrids = function(data, callback) {
|
||||
// - @param {Object} tile tile object to be inserted.
|
||||
// - @param {Function} callback
|
||||
MBTiles.prototype.insertGridTiles = function(map, callback) {
|
||||
if (typeof callback !== 'function') callback = noop;
|
||||
|
||||
var stmt = this.db.prepare('UPDATE OR REPLACE map SET grid_id = ? WHERE ' +
|
||||
' zoom_level = ? AND tile_column = ? AND tile_row = ?');
|
||||
|
||||
@@ -243,6 +279,8 @@ MBTiles.prototype.insertGridTiles = function(map, callback) {
|
||||
// - @param {Number} z tile z coordinate.
|
||||
// - @param {Function} callback
|
||||
MBTiles.prototype.getTile = function(x, y, z, callback) {
|
||||
if (typeof callback !== 'function') callback = noop;
|
||||
|
||||
var mbtiles = this;
|
||||
this.db.get('SELECT tile_data FROM tiles WHERE ' +
|
||||
'zoom_level = ? AND tile_column = ? AND tile_row = ?',
|
||||
@@ -261,6 +299,8 @@ MBTiles.prototype.getTile = function(x, y, z, callback) {
|
||||
// - @param {Number} z tile z coordinate
|
||||
// - @param {Function} callback
|
||||
MBTiles.prototype.getGrid = function(x, y, z, callback) {
|
||||
if (typeof callback !== 'function') callback = noop;
|
||||
|
||||
var that = this;
|
||||
Step(
|
||||
function() {
|
||||
@@ -276,7 +316,9 @@ MBTiles.prototype.getGrid = function(x, y, z, callback) {
|
||||
);
|
||||
},
|
||||
function(err, row, rows) {
|
||||
if ((!row || !row.grid) || (err && err.errno == 1)) return callback('Grid does not exist');
|
||||
if ((!row || !row.grid) || (err && err.errno == 1)) {
|
||||
return callback(new Error('Grid does not exist'));
|
||||
}
|
||||
if (err) return callback(err);
|
||||
|
||||
try {
|
||||
@@ -301,6 +343,8 @@ MBTiles.prototype.getGrid = function(x, y, z, callback) {
|
||||
//
|
||||
// - @param {Function} callback
|
||||
MBTiles.prototype.metadata = function(key, callback) {
|
||||
if (typeof callback !== 'function') callback = noop;
|
||||
|
||||
this.db.get('SELECT value FROM metadata WHERE name = ?',
|
||||
key,
|
||||
function(err, row) {
|
||||
@@ -314,6 +358,8 @@ MBTiles.prototype.metadata = function(key, callback) {
|
||||
// performing fallback queries if certain keys (like `bounds`, `minzoom`,
|
||||
// `maxzoom`) have not been provided.
|
||||
MBTiles.prototype.getInfo = function(callback) {
|
||||
if (typeof callback !== 'function') callback = noop;
|
||||
|
||||
var that = this;
|
||||
var info = {};
|
||||
info.basename = path.basename(that.filename);
|
||||
|
||||
Reference in New Issue
Block a user