From 7ae592ec9aac119c17bfc6d683462bd2e6921624 Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Sat, 10 Mar 2012 12:39:39 +0400 Subject: [PATCH] DRY sql adapters --- lib/adapters/mysql.js | 156 +++++++-------------------------------- lib/adapters/postgres.js | 107 +-------------------------- 2 files changed, 27 insertions(+), 236 deletions(-) diff --git a/lib/adapters/mysql.js b/lib/adapters/mysql.js index 3654e609..cdbd0841 100644 --- a/lib/adapters/mysql.js +++ b/lib/adapters/mysql.js @@ -41,14 +41,6 @@ MySQL.prototype.query = function (sql, callback) { }); }; -MySQL.prototype.save = function (model, data, callback) { - var sql = 'UPDATE ' + this.tableEscaped(model) + ' SET ' + this.toFields(model, data) + ' WHERE id = ' + data.id; - - this.query(sql, function (err) { - callback(err); - }); -}; - /** * Must invoke callback(err, id) */ @@ -99,12 +91,12 @@ MySQL.prototype.toDatabase = function (prop, val) { ' AND ' + this.toDatabase(prop, val[1]); } else if (operator == 'inq' || operator == 'nin') { - if (!(val.propertyIsEnumerable('length')) && typeof val === 'object' && typeof val.length === 'number') { //if value is array - return val.join(','); - } else { - return val; - } - } + if (!(val.propertyIsEnumerable('length')) && typeof val === 'object' && typeof val.length === 'number') { //if value is array + return val.join(','); + } else { + return val; + } + } } if (prop.type.name === 'Number') return val; if (prop.type.name === 'Date') { @@ -134,34 +126,7 @@ MySQL.prototype.fromDatabase = function (model, data) { }; MySQL.prototype.escapeName = function (name) { - return '`' + name + '`'; -}; - -MySQL.prototype.exists = function (model, id, callback) { - 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); - }); -}; - -MySQL.prototype.find = function find(model, id, callback) { - 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; - } else { - data = [null]; - } - callback(err, this.fromDatabase(model, data[0])); - }.bind(this)); -}; - -MySQL.prototype.destroy = function destroy(model, id, callback) { - var sql = 'DELETE FROM ' + this.tableEscaped(model) + ' WHERE id = ' + id + ' LIMIT 1'; - this.query(sql, function (err) { - callback(err); - }); + return '`' + name.replace(/\./g, '`.`') + '`'; }; MySQL.prototype.all = function all(model, filter, callback) { @@ -209,29 +174,29 @@ MySQL.prototype.all = function all(model, filter, callback) { var sqlCond = keyEscaped; switch (condType) { case 'gt': - sqlCond += ' > '; - break; + sqlCond += ' > '; + break; case 'gte': - sqlCond += ' >= '; - break; + sqlCond += ' >= '; + break; case 'lt': - sqlCond += ' < '; - break; + sqlCond += ' < '; + break; case 'lte': - sqlCond += ' <= '; - break; + sqlCond += ' <= '; + break; case 'between': - sqlCond += ' BETWEEN '; - break; - case 'inq': - sqlCond += ' IN '; - break; - case 'nin': - sqlCond += ' NOT IN '; - break; - case 'neq': - sqlCond + ' != '; - break; + sqlCond += ' BETWEEN '; + break; + case 'inq': + sqlCond += ' IN '; + break; + case 'nin': + sqlCond += ' NOT IN '; + break; + case 'neq': + sqlCond + ' != '; + break; } sqlCond += (condType == 'inq' || condType == 'nin') ? '(' + val + ')' : val; cs.push(sqlCond); @@ -256,66 +221,6 @@ MySQL.prototype.all = function all(model, filter, callback) { }; -MySQL.prototype.destroyAll = function destroyAll(model, callback) { - this.query('DELETE FROM ' + this.tableEscaped(model), function (err) { - if (err) { - return callback(err, []); - } - callback(err); - }.bind(this)); -}; - -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.tableEscaped(model) + buildWhere(where), function (err, res) { - callback(err, err ? null : res[0].cnt); - }); - - function buildWhere(conds) { - var cs = []; - Object.keys(conds || {}).forEach(function (key) { - var keyEscaped = '`' + key.replace(/\./g, '`.`') + '`' - if (conds[key] === null) { - cs.push(keyEscaped + ' IS NULL'); - } else { - cs.push(keyEscaped + ' = ' + self.toDatabase(props[key], conds[key])); - } - }); - return cs.length ? ' WHERE ' + cs.join(' AND ') : ''; - } -}; - -MySQL.prototype.updateAttributes = function updateAttrs(model, id, data, cb) { - data.id = id; - this.save(model, data, cb); -}; - -MySQL.prototype.disconnect = function disconnect() { - this.client.end(); -}; - -MySQL.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(); - } - } -}; - MySQL.prototype.autoupdate = function (cb) { var self = this; var wait = 0; @@ -420,15 +325,6 @@ MySQL.prototype.alterTable = function (model, actualFields, done, checkOnly) { } }; -MySQL.prototype.dropTable = function (model, cb) { - this.query('DROP TABLE IF EXISTS ' + this.tableEscaped(model), cb); -}; - -MySQL.prototype.createTable = function (model, cb) { - this.query('CREATE TABLE ' + this.tableEscaped(model) + - ' (\n ' + this.propertiesSQL(model) + '\n)', cb); -}; - MySQL.prototype.propertiesSQL = function (model) { var self = this; var sql = ['`id` INT(11) NOT NULL AUTO_INCREMENT UNIQUE PRIMARY KEY']; diff --git a/lib/adapters/postgres.js b/lib/adapters/postgres.js index 92752de1..69f3a940 100644 --- a/lib/adapters/postgres.js +++ b/lib/adapters/postgres.js @@ -46,15 +46,6 @@ PG.prototype.query = function (sql, callback) { }); }; -PG.prototype.save = function (model, data, callback) { - var sql = 'UPDATE ' + this.tableEscaped(model) + ' SET ' + this.toFields(model, data) + - ' WHERE "id" = ' + data.id; - - this.query(sql, function (err) { - callback(err); - }); -}; - /** * Must invoke callback(err, id) */ @@ -150,36 +141,8 @@ PG.prototype.escapeName = function (name) { return '"' + name + '"'; }; -PG.prototype.exists = function (model, id, callback) { - 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); - }); -}; - -PG.prototype.find = function find(model, id, callback) { - 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; - } else { - data = { items: [null] }; - } - callback(err, this.fromDatabase(model, data[0])); - }.bind(this)); -}; - -PG.prototype.destroy = function destroy(model, id, callback) { - var sql = 'DELETE FROM ' + this.tableEscaped(model) + ' WHERE "id" = ' + id; - this.query(sql, function (err) { - callback(err); - }); -}; - -// TODO: hook up where, order, limit and offset conditions PG.prototype.all = function all(model, filter, callback) { - this.query('SELECT * FROM ' + this.tableEscaped(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, []); } @@ -248,66 +211,6 @@ PG.prototype.toFilter = function (model, filter) { return out; }; -PG.prototype.destroyAll = function destroyAll(model, callback) { - this.query('DELETE FROM ' + this.tableEscaped(model) + '', function (err) { - if (err) { - return callback(err, []); - } - callback(err); - }.bind(this)); -}; - -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.tableEscaped(model) + '' + buildWhere(where), function (err, res) { - if (err) return callback(err); - callback(err, res && res[0] && res[0].cnt); - }); - - function buildWhere(conds) { - var cs = []; - Object.keys(conds || {}).forEach(function (key) { - if (conds[key] === null) { - cs.push(key + ' IS NULL'); - } else { - cs.push(key + ' = ' + self.toDatabase(props[key], conds[key])); - } - }); - return cs.length ? ' WHERE ' + cs.join(' AND ') : ''; - } -}; - -PG.prototype.updateAttributes = function updateAttrs(model, id, data, cb) { - data.id = id; - this.save(model, data, cb); -}; - -PG.prototype.disconnect = function disconnect() { - this.client.end(); -}; - -PG.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(); - } - } -}; - PG.prototype.autoupdate = function (cb) { var self = this; var wait = 0; @@ -380,14 +283,6 @@ PG.prototype.alterTable = function (model, actualFields, done) { } }; -PG.prototype.dropTable = function (model, cb) { - this.query('DROP TABLE IF EXISTS ' + this.tableEscaped(model) + '', cb); -}; - -PG.prototype.createTable = function (model, cb) { - this.query('CREATE TABLE ' + this.tableEscaped(model) + ' (\n ' + this.propertiesSQL(model) + '\n)', cb); -}; - PG.prototype.propertiesSQL = function (model) { var self = this; var sql = ['"id" SERIAL NOT NULL UNIQUE PRIMARY KEY'];