DRY sql adapters

This commit is contained in:
Anatoliy Chakkaev 2012-03-10 12:39:39 +04:00
parent 13dce54a14
commit 7ae592ec9a
2 changed files with 27 additions and 236 deletions

View File

@ -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'];

View File

@ -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'];