Unhashish, escape names, start moving common parts to sql.js
This commit is contained in:
parent
093edba65d
commit
13dce54a14
|
@ -4,6 +4,7 @@ var safeRequire = require('../utils').safeRequire;
|
|||
* Module dependencies
|
||||
*/
|
||||
var mysql = safeRequire('mysql');
|
||||
var BaseSQL = require('../sql');
|
||||
|
||||
exports.initialize = function initializeSchema(schema, callback) {
|
||||
if (!mysql) return;
|
||||
|
@ -28,14 +29,7 @@ function MySQL(client) {
|
|||
this.client = client;
|
||||
}
|
||||
|
||||
MySQL.prototype.define = function (descr) {
|
||||
if (!descr.settings) descr.settings = {};
|
||||
this._models[descr.model.modelName] = descr;
|
||||
};
|
||||
|
||||
MySQL.prototype.defineProperty = function (model, prop, params) {
|
||||
this._models[model].properties[prop] = params;
|
||||
};
|
||||
require('util').inherits(MySQL, BaseSQL);
|
||||
|
||||
MySQL.prototype.query = function (sql, callback) {
|
||||
var time = Date.now();
|
||||
|
@ -47,13 +41,8 @@ MySQL.prototype.query = function (sql, callback) {
|
|||
});
|
||||
};
|
||||
|
||||
MySQL.prototype.table = function (model) {
|
||||
return this._models[model].settings.table || model;
|
||||
};
|
||||
|
||||
MySQL.prototype.save = function (model, data, callback) {
|
||||
var sql = 'UPDATE ' + this.table(model) + ' SET ' + this.toFields(model, data) +
|
||||
' WHERE id = ' + data.id;
|
||||
var sql = 'UPDATE ' + this.tableEscaped(model) + ' SET ' + this.toFields(model, data) + ' WHERE id = ' + data.id;
|
||||
|
||||
this.query(sql, function (err) {
|
||||
callback(err);
|
||||
|
@ -65,7 +54,7 @@ MySQL.prototype.save = function (model, data, callback) {
|
|||
*/
|
||||
MySQL.prototype.create = function (model, data, callback) {
|
||||
var fields = this.toFields(model, data);
|
||||
var sql = 'INSERT ' + this.table(model);
|
||||
var sql = 'INSERT ' + this.tableEscaped(model);
|
||||
if (fields) {
|
||||
sql += ' SET ' + fields;
|
||||
} else {
|
||||
|
@ -144,8 +133,12 @@ MySQL.prototype.fromDatabase = function (model, data) {
|
|||
return data;
|
||||
};
|
||||
|
||||
MySQL.prototype.escapeName = function (name) {
|
||||
return '`' + name + '`';
|
||||
};
|
||||
|
||||
MySQL.prototype.exists = function (model, id, callback) {
|
||||
var sql = 'SELECT 1 FROM ' + this.table(model) + ' WHERE id = ' + id + ' LIMIT 1';
|
||||
var sql = 'SELECT 1 FROM ' + this.tableEscaped(model) + ' WHERE id = ' + id + ' LIMIT 1';
|
||||
this.query(sql, function (err, data) {
|
||||
if (err) return callback(err);
|
||||
callback(null, data.length === 1);
|
||||
|
@ -153,7 +146,7 @@ MySQL.prototype.exists = function (model, id, callback) {
|
|||
};
|
||||
|
||||
MySQL.prototype.find = function find(model, id, callback) {
|
||||
var sql = 'SELECT * FROM ' + this.table(model) + ' WHERE id = ' + id + ' LIMIT 1';
|
||||
var sql = 'SELECT * FROM ' + this.tableEscaped(model) + ' WHERE id = ' + id + ' LIMIT 1';
|
||||
this.query(sql, function (err, data) {
|
||||
if (data && data.length === 1) {
|
||||
data[0].id = id;
|
||||
|
@ -165,7 +158,7 @@ MySQL.prototype.find = function find(model, id, callback) {
|
|||
};
|
||||
|
||||
MySQL.prototype.destroy = function destroy(model, id, callback) {
|
||||
var sql = 'DELETE FROM ' + this.table(model) + ' WHERE id = ' + id + ' LIMIT 1';
|
||||
var sql = 'DELETE FROM ' + this.tableEscaped(model) + ' WHERE id = ' + id + ' LIMIT 1';
|
||||
this.query(sql, function (err) {
|
||||
callback(err);
|
||||
});
|
||||
|
@ -173,7 +166,7 @@ MySQL.prototype.destroy = function destroy(model, id, callback) {
|
|||
|
||||
MySQL.prototype.all = function all(model, filter, callback) {
|
||||
|
||||
var sql = 'SELECT * FROM ' + this.table(model);
|
||||
var sql = 'SELECT * FROM ' + this.tableEscaped(model);
|
||||
var self = this;
|
||||
var props = this._models[model].properties;
|
||||
|
||||
|
@ -264,7 +257,7 @@ MySQL.prototype.all = function all(model, filter, callback) {
|
|||
};
|
||||
|
||||
MySQL.prototype.destroyAll = function destroyAll(model, callback) {
|
||||
this.query('DELETE FROM ' + this.table(model), function (err) {
|
||||
this.query('DELETE FROM ' + this.tableEscaped(model), function (err) {
|
||||
if (err) {
|
||||
return callback(err, []);
|
||||
}
|
||||
|
@ -276,7 +269,7 @@ MySQL.prototype.count = function count(model, callback, where) {
|
|||
var self = this;
|
||||
var props = this._models[model].properties;
|
||||
|
||||
this.query('SELECT count(*) as cnt FROM ' + this.table(model) + buildWhere(where), function (err, res) {
|
||||
this.query('SELECT count(*) as cnt FROM ' + this.tableEscaped(model) + buildWhere(where), function (err, res) {
|
||||
callback(err, err ? null : res[0].cnt);
|
||||
});
|
||||
|
||||
|
@ -328,7 +321,7 @@ MySQL.prototype.autoupdate = function (cb) {
|
|||
var wait = 0;
|
||||
Object.keys(this._models).forEach(function (model) {
|
||||
wait += 1;
|
||||
self.query('SHOW FIELDS FROM ' + self.table(model), function (err, fields) {
|
||||
self.query('SHOW FIELDS FROM ' + self.tableEscaped(model), function (err, fields) {
|
||||
if (!err && fields.length) {
|
||||
self.alterTable(model, fields, done);
|
||||
} else {
|
||||
|
@ -428,11 +421,11 @@ MySQL.prototype.alterTable = function (model, actualFields, done, checkOnly) {
|
|||
};
|
||||
|
||||
MySQL.prototype.dropTable = function (model, cb) {
|
||||
this.query('DROP TABLE IF EXISTS ' + this.table(model), cb);
|
||||
this.query('DROP TABLE IF EXISTS ' + this.tableEscaped(model), cb);
|
||||
};
|
||||
|
||||
MySQL.prototype.createTable = function (model, cb) {
|
||||
this.query('CREATE TABLE ' + this.table(model) +
|
||||
this.query('CREATE TABLE ' + this.tableEscaped(model) +
|
||||
' (\n ' + this.propertiesSQL(model) + '\n)', cb);
|
||||
};
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ var safeRequire = require('../utils').safeRequire;
|
|||
* Module dependencies
|
||||
*/
|
||||
var pg = safeRequire('pg');
|
||||
var Hash = require('hashish');
|
||||
var BaseSQL = require('../sql');
|
||||
|
||||
exports.initialize = function initializeSchema(schema, callback) {
|
||||
if (!pg) return;
|
||||
|
@ -35,26 +35,19 @@ function PG(client) {
|
|||
this.client = client;
|
||||
}
|
||||
|
||||
PG.prototype.define = function (descr) {
|
||||
if (!descr.settings) descr.settings = {};
|
||||
this._models[descr.model.modelName] = descr;
|
||||
};
|
||||
require('util').inherits(PG, BaseSQL);
|
||||
|
||||
PG.prototype.query = function (sql, callback) {
|
||||
var time = Date.now();
|
||||
var log = this.log;
|
||||
this.client.query(sql, function (err, data) {
|
||||
log(sql, time);
|
||||
callback(err, data ? Hash(data.rows) : null);
|
||||
callback(err, data ? data.rows : null);
|
||||
});
|
||||
};
|
||||
|
||||
PG.prototype.table = function (model) {
|
||||
return this._models[model].settings.table || model;
|
||||
};
|
||||
|
||||
PG.prototype.save = function (model, data, callback) {
|
||||
var sql = 'UPDATE "' + this.table(model) + '" SET ' + this.toFields(model, data) +
|
||||
var sql = 'UPDATE ' + this.tableEscaped(model) + ' SET ' + this.toFields(model, data) +
|
||||
' WHERE "id" = ' + data.id;
|
||||
|
||||
this.query(sql, function (err) {
|
||||
|
@ -67,7 +60,7 @@ PG.prototype.save = function (model, data, callback) {
|
|||
*/
|
||||
PG.prototype.create = function (model, data, callback) {
|
||||
var fields = this.toFields(model, data,true);
|
||||
var sql = 'INSERT INTO "' + this.table(model) + '"';
|
||||
var sql = 'INSERT INTO ' + this.tableEscaped(model) + '';
|
||||
if (fields) {
|
||||
sql += ' ' + fields;
|
||||
} else {
|
||||
|
@ -76,7 +69,7 @@ PG.prototype.create = function (model, data, callback) {
|
|||
sql += ' RETURNING id';
|
||||
this.query(sql, function (err, info) {
|
||||
if (err) return callback(err);
|
||||
callback(err, info && info.items[0] && info.items[0].id);
|
||||
callback(err, info && info[0] && info[0].id);
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -153,28 +146,32 @@ PG.prototype.fromDatabase = function (model, data) {
|
|||
return data;
|
||||
};
|
||||
|
||||
PG.prototype.escapeName = function (name) {
|
||||
return '"' + name + '"';
|
||||
};
|
||||
|
||||
PG.prototype.exists = function (model, id, callback) {
|
||||
var sql = 'SELECT 1 FROM "' + this.table(model) + '" WHERE "id" = ' + id + ' LIMIT 1';
|
||||
var sql = 'SELECT 1 FROM ' + this.tableEscaped(model) + ' WHERE "id" = ' + id + ' LIMIT 1';
|
||||
this.query(sql, function (err, data) {
|
||||
if (err) return callback(err);
|
||||
callback(null, data.items.length === 1);
|
||||
callback(null, data.length === 1);
|
||||
});
|
||||
};
|
||||
|
||||
PG.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.query(sql, function (err, data) {
|
||||
if (data && data.items && data.items.length === 1) {
|
||||
data.items[0].id = id;
|
||||
if (data && data.length === 1) {
|
||||
data[0].id = id;
|
||||
} else {
|
||||
data = { items: [null] };
|
||||
}
|
||||
callback(err, this.fromDatabase(model, data.items[0]));
|
||||
callback(err, this.fromDatabase(model, data[0]));
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
PG.prototype.destroy = function destroy(model, id, callback) {
|
||||
var sql = 'DELETE FROM "' + this.table(model) + '" WHERE "id" = ' + id;
|
||||
var sql = 'DELETE FROM ' + this.tableEscaped(model) + ' WHERE "id" = ' + id;
|
||||
this.query(sql, function (err) {
|
||||
callback(err);
|
||||
});
|
||||
|
@ -182,11 +179,11 @@ PG.prototype.destroy = function destroy(model, id, callback) {
|
|||
|
||||
// TODO: hook up where, order, limit and offset conditions
|
||||
PG.prototype.all = function all(model, filter, callback) {
|
||||
this.query('SELECT * FROM "' + this.table(model) + '"' + this.toFilter(model, filter), function (err, data) {
|
||||
this.query('SELECT * FROM ' + this.tableEscaped(model) + '' + this.toFilter(model, filter), function (err, data) {
|
||||
if (err) {
|
||||
return callback(err, []);
|
||||
}
|
||||
callback(err, data.items);
|
||||
callback(err, data);
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
|
@ -252,7 +249,7 @@ PG.prototype.toFilter = function (model, filter) {
|
|||
};
|
||||
|
||||
PG.prototype.destroyAll = function destroyAll(model, callback) {
|
||||
this.query('DELETE FROM "' + this.table(model) + '"', function (err) {
|
||||
this.query('DELETE FROM ' + this.tableEscaped(model) + '', function (err) {
|
||||
if (err) {
|
||||
return callback(err, []);
|
||||
}
|
||||
|
@ -264,9 +261,9 @@ PG.prototype.count = function count(model, callback, where) {
|
|||
var self = this;
|
||||
var props = this._models[model].properties;
|
||||
|
||||
this.query('SELECT count(*) as cnt FROM "' + this.table(model) + '"' + buildWhere(where), function (err, res) {
|
||||
this.query('SELECT count(*) as cnt FROM ' + this.tableEscaped(model) + '' + buildWhere(where), function (err, res) {
|
||||
if (err) return callback(err);
|
||||
callback(err, res && res.items[0] && res.items[0].cnt);
|
||||
callback(err, res && res[0] && res[0].cnt);
|
||||
});
|
||||
|
||||
function buildWhere(conds) {
|
||||
|
@ -316,7 +313,7 @@ PG.prototype.autoupdate = function (cb) {
|
|||
var wait = 0;
|
||||
Object.keys(this._models).forEach(function (model) {
|
||||
wait += 1;
|
||||
self.query('SELECT column_name as Field, udt_name as Type, is_nullable as Null, column_default as Default FROM information_schema.COLUMNS WHERE table_name = \''+this.table(model)+'\'', function (err, fields) {
|
||||
self.query('SELECT column_name as Field, udt_name as Type, is_nullable as Null, column_default as Default FROM information_schema.COLUMNS WHERE table_name = \''+ this.table(model) + '\'', function (err, fields) {
|
||||
self.alterTable(model, fields, done);
|
||||
});
|
||||
});
|
||||
|
@ -363,7 +360,7 @@ PG.prototype.alterTable = function (model, actualFields, done) {
|
|||
});
|
||||
|
||||
if (sql.length) {
|
||||
this.query('ALTER TABLE "' + this.table(model) + '" ' + sql.join(',\n'), done);
|
||||
this.query('ALTER TABLE ' + this.tableEscaped(model) + ' ' + sql.join(',\n'), done);
|
||||
} else {
|
||||
done();
|
||||
}
|
||||
|
@ -384,12 +381,11 @@ PG.prototype.alterTable = function (model, actualFields, done) {
|
|||
};
|
||||
|
||||
PG.prototype.dropTable = function (model, cb) {
|
||||
this.query('DROP TABLE IF EXISTS "' + this.table(model) + '"', cb);
|
||||
this.query('DROP TABLE IF EXISTS ' + this.tableEscaped(model) + '', cb);
|
||||
};
|
||||
|
||||
PG.prototype.createTable = function (model, cb) {
|
||||
this.query('CREATE TABLE "' + this.table(model) +
|
||||
'" (\n ' + this.propertiesSQL(model) + '\n)', cb);
|
||||
this.query('CREATE TABLE ' + this.tableEscaped(model) + ' (\n ' + this.propertiesSQL(model) + '\n)', cb);
|
||||
};
|
||||
|
||||
PG.prototype.propertiesSQL = function (model) {
|
||||
|
|
|
@ -4,6 +4,7 @@ var safeRequire = require('../utils').safeRequire;
|
|||
* Module dependencies
|
||||
*/
|
||||
var sqlite3 = safeRequire('sqlite3');
|
||||
var BaseSQL = require('../sql');
|
||||
|
||||
exports.initialize = function initializeSchema(schema, callback) {
|
||||
if (!sqlite3) return;
|
||||
|
@ -26,13 +27,7 @@ function SQLite3(client) {
|
|||
this.client = client;
|
||||
}
|
||||
|
||||
SQLite3.prototype.define = function (descr) {
|
||||
this._models[descr.model.modelName] = descr;
|
||||
};
|
||||
|
||||
SQLite3.prototype.defineProperty = function (model, prop, params) {
|
||||
this._models[model].properties[prop] = params;
|
||||
};
|
||||
require('util').inherits(SQLite3, BaseSQL);
|
||||
|
||||
SQLite3.prototype.command = function () {
|
||||
this.query('run', [].slice.call(arguments));
|
||||
|
@ -148,8 +143,12 @@ SQLite3.prototype.fromDatabase = function (model, data) {
|
|||
return data;
|
||||
};
|
||||
|
||||
SQLite3.prototype.escapeName = function (name) {
|
||||
return '`' + name + '`';
|
||||
};
|
||||
|
||||
SQLite3.prototype.exists = function (model, id, callback) {
|
||||
var sql = 'SELECT 1 FROM ' + model + ' WHERE id = ' + id + ' LIMIT 1';
|
||||
var sql = 'SELECT 1 FROM ' + this.escapeName(model) + ' WHERE id = ' + id + ' LIMIT 1';
|
||||
this.queryOne(sql, function (err, data) {
|
||||
if (err) return callback(err);
|
||||
callback(null, data && data['1'] === 1);
|
||||
|
|
|
@ -188,6 +188,10 @@ Schema.prototype.define = function defineClass(className, properties, settings)
|
|||
|
||||
};
|
||||
|
||||
Schema.prototype.tableName = function (modelName) {
|
||||
return this.definitions[modelName].settings.table = this.definitions[modelName].settings.table || modelName
|
||||
};
|
||||
|
||||
Schema.prototype.defineForeignKey = function defineForeignKey(className, key) {
|
||||
// return if already defined
|
||||
if (this.definitions[className].properties[key]) return;
|
||||
|
|
|
@ -14,7 +14,6 @@
|
|||
"node >= 0.4.0"
|
||||
],
|
||||
"dependencies": {
|
||||
"hashish": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"redis": ">= 0.6.7",
|
||||
|
|
Loading…
Reference in New Issue