Custom table name for sqlite3

This commit is contained in:
Anatoliy Chakkaev 2012-03-10 14:32:14 +04:00
parent b4de14b811
commit c894694c7f
3 changed files with 32 additions and 65 deletions

View File

@ -46,7 +46,7 @@ MySQL.prototype.query = function (sql, callback) {
*/
MySQL.prototype.create = function (model, data, callback) {
var fields = this.toFields(model, data);
var sql = 'INSERT ' + this.tableEscaped(model);
var sql = 'INSERT INTO ' + this.tableEscaped(model);
if (fields) {
sql += ' SET ' + fields;
} else {

View File

@ -61,7 +61,7 @@ SQLite3.prototype.query = function (method, args) {
SQLite3.prototype.save = function (model, data, callback) {
var queryParams = [];
var sql = 'UPDATE ' + model + ' SET ' +
var sql = 'UPDATE ' + this.tableEscaped(model) + ' SET ' +
Object.keys(data).map(function (key) {
queryParams.push(data[key]);
return key + ' = ?';
@ -82,7 +82,7 @@ SQLite3.prototype.create = function (model, data, callback) {
questions.push('?');
return data[key];
});
var sql = 'INSERT INTO ' + model + ' (' + Object.keys(data).join(',') + ') VALUES ('
var sql = 'INSERT INTO ' + this.tableEscaped(model) + ' (' + Object.keys(data).join(',') + ') VALUES ('
sql += questions.join(',');
sql += ')';
this.command(sql, values, function (err) {
@ -148,7 +148,7 @@ SQLite3.prototype.escapeName = function (name) {
};
SQLite3.prototype.exists = function (model, id, callback) {
var sql = 'SELECT 1 FROM ' + this.escapeName(model) + ' WHERE id = ' + id + ' LIMIT 1';
var sql = 'SELECT 1 FROM ' + this.tableEscaped(model) + ' WHERE id = ' + id + ' LIMIT 1';
this.queryOne(sql, function (err, data) {
if (err) return callback(err);
callback(null, data && data['1'] === 1);
@ -156,7 +156,7 @@ SQLite3.prototype.exists = function (model, id, callback) {
};
SQLite3.prototype.find = function find(model, id, callback) {
var sql = 'SELECT * FROM ' + model + ' WHERE id = ' + id + ' LIMIT 1';
var sql = 'SELECT * FROM ' + this.tableEscaped(model) + ' WHERE id = ' + id + ' LIMIT 1';
this.queryOne(sql, function (err, data) {
if (data) {
data.id = id;
@ -167,16 +167,9 @@ SQLite3.prototype.find = function find(model, id, callback) {
}.bind(this));
};
SQLite3.prototype.destroy = function destroy(model, id, callback) {
var sql = 'DELETE FROM ' + model + ' WHERE id = ' + id;
this.command(sql, function (err) {
callback(err);
});
};
SQLite3.prototype.all = function all(model, filter, callback) {
var sql = 'SELECT * FROM ' + model;
var sql = 'SELECT * FROM ' + this.tableEscaped(model);
var self = this;
var props = this._models[model].properties;
var queryParams = [];
@ -260,21 +253,14 @@ SQLite3.prototype.all = function all(model, filter, callback) {
};
SQLite3.prototype.destroyAll = function destroyAll(model, callback) {
this.command('DELETE FROM ' + model, function (err) {
if (err) {
return callback(err, []);
}
callback(err);
}.bind(this));
};
SQLite3.prototype.count = function count(model, callback, where) {
var self = this;
var props = this._models[model].properties;
var queryParams = [];
this.queryOne('SELECT count(*) as cnt FROM ' + model + buildWhere(where), queryParams, function (err, res) {
this.queryOne('SELECT count(*) as cnt FROM ' +
this.tableEscaped(model) + ' ' + buildWhere(where), queryParams, function (err, res) {
if (err) return callback(err);
callback(err, err ? null : res.cnt);
});
@ -293,41 +279,16 @@ SQLite3.prototype.count = function count(model, callback, where) {
}
};
SQLite3.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
data.id = id;
this.save(model, data, cb);
};
SQLite3.prototype.disconnect = function disconnect() {
this.client.close();
};
SQLite3.prototype.automigrate = function (cb) {
var self = this;
var wait = 0;
Object.keys(this._models).forEach(function (model) {
wait += 1;
self.dropTable(model, function () {
self.createTable(model, function (err) {
if (err) console.log(err);
done();
});
});
});
function done() {
if (--wait === 0 && cb) {
cb();
}
}
};
SQLite3.prototype.autoupdate = function (cb) {
var self = this;
var wait = 0;
Object.keys(this._models).forEach(function (model) {
wait += 1;
self.queryAll('SHOW FIELDS FROM ' + model, function (err, fields) {
self.queryAll('SHOW FIELDS FROM ' + this.tableEscaped(model), function (err, fields) {
self.alterTable(model, fields, done);
});
});
@ -374,7 +335,7 @@ SQLite3.prototype.alterTable = function (model, actualFields, done) {
});
if (sql.length) {
this.command('ALTER TABLE `' + model + '` ' + sql.join(',\n'), done);
this.command('ALTER TABLE ' + this.tableEscaped(model) + ' ' + sql.join(',\n'), done);
} else {
done();
}
@ -394,15 +355,6 @@ SQLite3.prototype.alterTable = function (model, actualFields, done) {
}
};
SQLite3.prototype.dropTable = function (model, cb) {
this.command('DROP TABLE IF EXISTS ' + model, cb);
};
SQLite3.prototype.createTable = function (model, cb) {
this.command('CREATE TABLE ' + model +
' (\n ' + this.propertiesSQL(model) + '\n)', cb);
};
SQLite3.prototype.propertiesSQL = function (model) {
var self = this;
var sql = ['`id` INTEGER PRIMARY KEY'];

View File

@ -2,6 +2,21 @@ module.exports = BaseSQL;
function BaseSQL() {}
BaseSQL.prototype.query = function () {
throw new Error('query method should be declared in adapter');
};
BaseSQL.prototype.command = function (sql, callback) {
return this.query(sql, callback);
};
BaseSQL.prototype.queryOne = function (sql, callback) {
return this.query(sql, function (err, data) {
if (err) return callback(err);
callback(err, data[0]);
});
};
BaseSQL.prototype.table = function (model) {
return this._models[model].model.schema.tableName(model);
};
@ -60,13 +75,13 @@ BaseSQL.prototype.destroy = function destroy(model, id, callback) {
var sql = 'DELETE FROM ' +
this.tableEscaped(model) + ' WHERE ' + this.escapeName('id') + ' = ' + id;
this.query(sql, function (err) {
this.command(sql, function (err) {
callback(err);
});
};
BaseSQL.prototype.destroyAll = function destroyAll(model, callback) {
this.query('DELETE FROM ' + this.tableEscaped(model), function (err) {
this.command('DELETE FROM ' + this.tableEscaped(model), function (err) {
if (err) {
return callback(err, []);
}
@ -78,10 +93,10 @@ BaseSQL.prototype.count = function count(model, callback, where) {
var self = this;
var props = this._models[model].properties;
this.query('SELECT count(*) as cnt FROM ' +
this.queryOne('SELECT count(*) as cnt FROM ' +
this.tableEscaped(model) + ' ' + buildWhere(where), function (err, res) {
if (err) return callback(err);
callback(err, res && res[0] && res[0].cnt);
callback(err, res && res.cnt);
});
function buildWhere(conds) {
@ -128,11 +143,11 @@ BaseSQL.prototype.automigrate = function (cb) {
};
BaseSQL.prototype.dropTable = function (model, cb) {
this.query('DROP TABLE IF EXISTS ' + this.tableEscaped(model), cb);
this.command('DROP TABLE IF EXISTS ' + this.tableEscaped(model), cb);
};
BaseSQL.prototype.createTable = function (model, cb) {
this.query('CREATE TABLE ' + this.tableEscaped(model) +
this.command('CREATE TABLE ' + this.tableEscaped(model) +
' (\n ' + this.propertiesSQL(model) + '\n)', cb);
};