loopback-datasource-juggler/lib/sql.js

302 lines
8.1 KiB
JavaScript
Raw Normal View History

2012-03-10 08:40:00 +00:00
module.exports = BaseSQL;
2012-03-27 14:22:24 +00:00
/**
* Base SQL class
*/
function BaseSQL() {
}
2012-03-10 08:40:00 +00:00
2013-07-15 17:38:54 +00:00
BaseSQL.prototype.relational = true;
2012-03-10 10:32:14 +00:00
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]);
});
};
2013-05-24 05:40:33 +00:00
/**
* Look up the data source by model name
* @param model The model name
* @returns {DataSource} The data source
*/
2013-05-23 01:04:05 +00:00
BaseSQL.prototype.dataSource = function(model) {
var m = this._models[model];
if(!m) {
2013-05-31 00:23:31 +00:00
console.trace('Model not found: ' + model);
}
return m.model.schema;
2013-05-23 01:04:05 +00:00
}
2013-05-24 05:40:33 +00:00
/**
* Get the table name for a given model
* @param model The model name
* @returns {String} The table name
*/
2012-03-10 08:40:00 +00:00
BaseSQL.prototype.table = function (model) {
2013-05-23 17:46:01 +00:00
var name = this.dataSource(model).tableName(model);
var dbName = this.dbName;
if(typeof dbName === 'function') {
name = dbName(name);
}
return name;
2012-03-10 08:40:00 +00:00
};
2013-05-24 05:40:33 +00:00
/**
* Get the column name for given model property
* @param model The model name
* @param property The property name
* @returns {String} The column name
*/
BaseSQL.prototype.column = function (model, property) {
2013-05-23 17:46:01 +00:00
var name = this.dataSource(model).columnName(model, property);
var dbName = this.dbName;
if(typeof dbName === 'function') {
name = dbName(name);
}
return name;
2013-05-23 01:04:05 +00:00
};
2013-05-29 17:03:01 +00:00
/**
* Get the column name for given model property
* @param model The model name
* @param property The property name
* @returns {Object} The column metadata
*/
BaseSQL.prototype.columnMetadata = function (model, property) {
return this.dataSource(model).columnMetadata(model, property);
};
2013-05-24 05:40:33 +00:00
/**
* Get the corresponding property name for a given column name
* @param model The model name
* @param column The column name
* @returns {*}
*/
BaseSQL.prototype.propertyName = function (model, column) {
var props = this._models[model].properties;
for(var p in props) {
if(this.column(model, p) === column) {
return p;
}
}
return null;
};
2013-05-24 05:40:33 +00:00
/**
* Get the id property name
* @param model The model name
* @returns {String} The id property name
*/
2013-05-23 01:04:05 +00:00
BaseSQL.prototype.idName = function (model) {
return this.dataSource(model).idName(model);
};
2013-05-30 23:06:04 +00:00
/**
* Get the id property names
* @param model The model name
* @returns {[String]} The id property names
*/
BaseSQL.prototype.idNames = function (model) {
return this.dataSource(model).idNames(model);
};
2013-05-24 05:40:33 +00:00
/**
* Get the id column name
* @param model The model name
* @returns {String} The column name
*/
2013-05-23 01:04:05 +00:00
BaseSQL.prototype.idColumn = function (model) {
var name = this.dataSource(model).idColumnName(model);;
var dbName = this.dbName;
if(typeof dbName === 'function') {
name = dbName(name);
}
return name;
2013-05-23 01:04:05 +00:00
};
2013-05-24 05:40:33 +00:00
/**
* Get the escaped id column name
* @param model The model name
* @returns {String} the escaped id column name
*/
2013-05-23 01:04:05 +00:00
BaseSQL.prototype.idColumnEscaped = function (model) {
return this.escapeName(this.dataSource(model).idColumnName(model));
};
2013-05-24 05:40:33 +00:00
/**
* Get the id index (sequence number, starting from 1)
* @param model The model name
* @param prop The property name
* @returns {Number} The id index, undefined if the property is not part of the primary key
*/
2013-05-23 01:04:05 +00:00
BaseSQL.prototype.id = function (model, prop) {
var p = this._models[model].properties[prop];
if(!p) {
2013-05-31 00:23:31 +00:00
console.trace('Property not found: ' + model +'.' + prop);
}
return p.id;
};
2013-05-24 05:40:33 +00:00
/**
* Escape the name for the underlying database
* @param name The name
*/
2012-03-10 08:40:00 +00:00
BaseSQL.prototype.escapeName = function (name) {
throw new Error('escapeName method should be declared in adapter');
};
2013-05-24 05:40:33 +00:00
/**
* Get the escaped table name
* @param model The model name
* @returns {String} the escaped table name
*/
2012-03-10 08:40:00 +00:00
BaseSQL.prototype.tableEscaped = function (model) {
return this.escapeName(this.table(model));
};
2013-05-24 05:40:33 +00:00
/**
* Get the escaped column name for a given model property
* @param model The model name
* @param property The property name
* @returns {String} The escaped column name
*/
BaseSQL.prototype.columnEscaped = function (model, property) {
return this.escapeName(this.column(model, property));
};
2012-03-10 08:40:00 +00:00
BaseSQL.prototype.define = function (descr) {
if (!descr.settings) descr.settings = {};
this._models[descr.model.modelName] = descr;
};
BaseSQL.prototype.defineProperty = function (model, prop, params) {
this._models[model].properties[prop] = params;
};
BaseSQL.prototype.save = function (model, data, callback) {
2013-05-23 01:04:05 +00:00
var sql = 'UPDATE ' + this.tableEscaped(model) + ' SET '
+ this.toFields(model, data) + ' WHERE ' + this.idColumnEscaped(model) + ' = ' + Number(data.id);
2012-03-10 08:40:00 +00:00
this.query(sql, function (err) {
callback(err);
});
};
BaseSQL.prototype.exists = function (model, id, callback) {
var sql = 'SELECT 1 FROM ' +
2013-05-23 01:04:05 +00:00
this.tableEscaped(model) + ' WHERE ' + this.idColumnEscaped(model) + ' = ' + Number(id) + ' LIMIT 1';
2012-03-10 08:40:00 +00:00
this.query(sql, function (err, data) {
if (err) return callback(err);
callback(null, data.length === 1);
});
};
BaseSQL.prototype.find = function find(model, id, callback) {
var sql = 'SELECT * FROM ' +
2013-06-26 03:36:00 +00:00
this.tableEscaped(model) + ' WHERE ' + this.idColumnEscaped(model) + ' = ' + id + ' LIMIT 1';
2012-03-10 08:40:00 +00:00
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));
};
2013-06-26 03:36:00 +00:00
BaseSQL.prototype.delete = BaseSQL.prototype.destroy = function destroy(model, id, callback) {
2012-03-10 08:40:00 +00:00
var sql = 'DELETE FROM ' +
2013-06-26 03:36:00 +00:00
this.tableEscaped(model) + ' WHERE ' + this.idColumnEscaped(model) + ' = ' + id;
2012-03-10 08:40:00 +00:00
2012-03-10 10:32:14 +00:00
this.command(sql, function (err) {
2012-03-10 08:40:00 +00:00
callback(err);
});
};
2013-06-26 03:36:00 +00:00
BaseSQL.prototype.deleteAll = BaseSQL.prototype.destroyAll = function destroyAll(model, callback) {
2012-03-10 10:32:14 +00:00
this.command('DELETE FROM ' + this.tableEscaped(model), function (err) {
2012-03-10 08:40:00 +00:00
if (err) {
return callback(err, []);
}
callback(err);
}.bind(this));
};
BaseSQL.prototype.count = function count(model, callback, where) {
var self = this;
var props = this._models[model].properties;
2012-03-10 10:32:14 +00:00
this.queryOne('SELECT count(*) as cnt FROM ' +
2012-03-10 08:40:00 +00:00
this.tableEscaped(model) + ' ' + buildWhere(where), function (err, res) {
if (err) return callback(err);
2012-03-10 10:32:14 +00:00
callback(err, res && res.cnt);
2012-03-10 08:40:00 +00:00
});
function buildWhere(conds) {
var cs = [];
Object.keys(conds || {}).forEach(function (key) {
2013-05-23 01:04:05 +00:00
var keyEscaped = self.columnEscaped(key);
2012-03-10 08:40:00 +00:00
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 ') : '';
}
};
BaseSQL.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
data.id = id;
this.save(model, data, cb);
};
BaseSQL.prototype.disconnect = function disconnect() {
this.client.end();
};
BaseSQL.prototype.automigrate = function (cb) {
var self = this;
var wait = 0;
Object.keys(this._models).forEach(function (model) {
wait += 1;
self.dropTable(model, function () {
// console.log('drop', model);
2012-03-10 08:40:00 +00:00
self.createTable(model, function (err) {
// console.log('create', model);
2012-03-10 08:40:00 +00:00
if (err) console.log(err);
done();
});
});
});
if (wait === 0) cb();
2012-03-10 08:40:00 +00:00
function done() {
if (--wait === 0 && cb) {
cb();
}
}
};
BaseSQL.prototype.dropTable = function (model, cb) {
2012-03-10 10:32:14 +00:00
this.command('DROP TABLE IF EXISTS ' + this.tableEscaped(model), cb);
2012-03-10 08:40:00 +00:00
};
BaseSQL.prototype.createTable = function (model, cb) {
2012-03-10 10:32:14 +00:00
this.command('CREATE TABLE ' + this.tableEscaped(model) +
2012-03-10 08:40:00 +00:00
' (\n ' + this.propertiesSQL(model) + '\n)', cb);
};