不要怂,就是干,撸起袖子干!

Commit 3f510d83 by Jan Aagaard Meier

Removed eval from abstract query

1 parent 7468014b
...@@ -21,9 +21,7 @@ ...@@ -21,9 +21,7 @@
"sub":true, "sub":true,
/* questionable */ /* questionable */
"shadow":true,
"loopfunc":true, "loopfunc":true,
"evil":true,
"predef": [ "predef": [
"alert", "alert",
......
...@@ -26,6 +26,9 @@ module.exports = (function() { ...@@ -26,6 +26,9 @@ module.exports = (function() {
HasManySingleLinked.prototype.injectSetter = function(oldAssociations, newAssociations, defaultAttributes) { HasManySingleLinked.prototype.injectSetter = function(oldAssociations, newAssociations, defaultAttributes) {
var self = this var self = this
, primaryKeys
, primaryKey
, updateWhere
, associationKeys = Object.keys((oldAssociations[0] || newAssociations[0] || {Model: {primaryKeys: {}}}).Model.primaryKeys || {}) , associationKeys = Object.keys((oldAssociations[0] || newAssociations[0] || {Model: {primaryKeys: {}}}).Model.primaryKeys || {})
, associationKey = (associationKeys.length === 1) ? associationKeys[0] : 'id' , associationKey = (associationKeys.length === 1) ? associationKeys[0] : 'id'
, options = {} , options = {}
...@@ -56,9 +59,9 @@ module.exports = (function() { ...@@ -56,9 +59,9 @@ module.exports = (function() {
update = {}; update = {};
update[self.__factory.identifier] = null; update[self.__factory.identifier] = null;
var primaryKeys = Object.keys(this.__factory.target.primaryKeys) primaryKeys = Object.keys(this.__factory.target.primaryKeys);
, primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id' primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id';
, updateWhere = {}; updateWhere = {};
updateWhere[primaryKey] = obsoleteIds; updateWhere[primaryKey] = obsoleteIds;
promises.push(this.__factory.target.update( promises.push(this.__factory.target.update(
...@@ -71,11 +74,11 @@ module.exports = (function() { ...@@ -71,11 +74,11 @@ module.exports = (function() {
if (unassociatedObjects.length > 0) { if (unassociatedObjects.length > 0) {
// For the self.instance // For the self.instance
var pkeys = Object.keys(self.instance.Model.primaryKeys) var pkeys = Object.keys(self.instance.Model.primaryKeys)
, pkey = pkeys.length === 1 ? pkeys[0] : 'id' , pkey = pkeys.length === 1 ? pkeys[0] : 'id';
// For chainer
, primaryKeys = Object.keys(this.__factory.target.primaryKeys) primaryKeys = Object.keys(this.__factory.target.primaryKeys);
, primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id' primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id';
, updateWhere = {}; updateWhere = {};
// set the new associations // set the new associations
var unassociatedIds = unassociatedObjects.map(function(associatedObject) { var unassociatedIds = unassociatedObjects.map(function(associatedObject) {
......
...@@ -289,6 +289,8 @@ module.exports = (function() { ...@@ -289,6 +289,8 @@ module.exports = (function() {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull); attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull);
var query var query
, key
, value
, values = []; , values = [];
query = 'UPDATE <%= table %> SET <%= values %> WHERE <%= where %>'; query = 'UPDATE <%= table %> SET <%= values %> WHERE <%= where %>';
...@@ -296,14 +298,14 @@ module.exports = (function() { ...@@ -296,14 +298,14 @@ module.exports = (function() {
query += ' RETURNING *'; query += ' RETURNING *';
} }
for (var key in attrValueHash) { for (key in attrValueHash) {
var value = attrValueHash[key]; value = attrValueHash[key];
values.push(this.quoteIdentifier(key) + '=' + this.quoteIdentifier(key) + ' + ' + this.escape(value)); values.push(this.quoteIdentifier(key) + '=' + this.quoteIdentifier(key) + ' + ' + this.escape(value));
} }
options = options || {}; options = options || {};
for (var key in options) { for (key in options) {
var value = options[key]; value = options[key];
values.push(this.quoteIdentifier(key) + '=' + this.escape(value)); values.push(this.quoteIdentifier(key) + '=' + this.escape(value));
} }
......
...@@ -65,15 +65,15 @@ module.exports = (function() { ...@@ -65,15 +65,15 @@ module.exports = (function() {
AbstractQuery.prototype.formatResults = function(data) { AbstractQuery.prototype.formatResults = function(data) {
var result = this.callee; var result = this.callee;
if (isInsertQuery.call(this, data)) { if (this.isInsertQuery(data)) {
handleInsertQuery.call(this, data); this.handleInsertQuery(data);
} }
if (isSelectQuery.call(this)) { if (this.isSelectQuery()) {
result = handleSelectQuery.call(this, data); result = this.handleSelectQuery(data);
} else if (isShowTableQuery.call(this)) { } else if (this.isShowTableQuery()) {
result = handleShowTableQuery.call(this, data); result = this.handleShowTableQuery(data);
} else if (isShowOrDescribeQuery.call(this)) { } else if (this.isShowOrDescribeQuery()) {
result = data; result = data;
if (this.sql.toLowerCase().indexOf('describe') === 0) { if (this.sql.toLowerCase().indexOf('describe') === 0) {
...@@ -97,9 +97,9 @@ module.exports = (function() { ...@@ -97,9 +97,9 @@ module.exports = (function() {
return row.name; return row.name;
}); });
} }
} else if (isCallQuery.call(this)) { } else if (this.isCallQuery()) {
result = data[0]; result = data[0];
} else if (isBulkUpdateQuery.call(this) || isBulkDeleteQuery.call(this)) { } else if (this.isBulkUpdateQuery() || this.isBulkDeleteQuery()) {
result = data.affectedRows; result = data.affectedRows;
} }
...@@ -107,17 +107,6 @@ module.exports = (function() { ...@@ -107,17 +107,6 @@ module.exports = (function() {
}; };
/** /**
* This function is a wrapper for private methods.
*
* @param {String} fctName The name of the private method.
*
*/
AbstractQuery.prototype.send = function(fctName/*, arg1, arg2, arg3, ...*/) {
var args = Array.prototype.slice.call(arguments).slice(1);
return eval(fctName).apply(this, args);
};
/**
* Get the attributes of an insert query, which contains the just inserted id. * Get the attributes of an insert query, which contains the just inserted id.
* *
* @return {String} The field name. * @return {String} The field name.
...@@ -126,10 +115,6 @@ module.exports = (function() { ...@@ -126,10 +115,6 @@ module.exports = (function() {
return 'insertId'; return 'insertId';
}; };
/////////////
// private //
/////////////
/** /**
* Iterate over all known tables and search their names inside the sql query. * Iterate over all known tables and search their names inside the sql query.
* This method will also check association aliases ('as' option). * This method will also check association aliases ('as' option).
...@@ -137,7 +122,7 @@ module.exports = (function() { ...@@ -137,7 +122,7 @@ module.exports = (function() {
* @param {String} attribute An attribute of a SQL query. (?) * @param {String} attribute An attribute of a SQL query. (?)
* @return {String} The found tableName / alias. * @return {String} The found tableName / alias.
*/ */
var findTableNameInAttribute = function(attribute) { AbstractQuery.prototype.findTableNameInAttribute = function(attribute) {
if (!this.options.include) { if (!this.options.include) {
return null; return null;
} }
...@@ -158,7 +143,7 @@ module.exports = (function() { ...@@ -158,7 +143,7 @@ module.exports = (function() {
} }
}; };
var isInsertQuery = function(results, metaData) { AbstractQuery.prototype.isInsertQuery = function(results, metaData) {
var result = true; var result = true;
// is insert query if sql contains insert into // is insert query if sql contains insert into
...@@ -173,7 +158,7 @@ module.exports = (function() { ...@@ -173,7 +158,7 @@ module.exports = (function() {
return result; return result;
}; };
var handleInsertQuery = function(results, metaData) { AbstractQuery.prototype.handleInsertQuery = function(results, metaData) {
if (this.callee) { if (this.callee) {
// add the inserted row id to the instance // add the inserted row id to the instance
var autoIncrementField = this.callee.Model.autoIncrementField var autoIncrementField = this.callee.Model.autoIncrementField
...@@ -186,33 +171,33 @@ module.exports = (function() { ...@@ -186,33 +171,33 @@ module.exports = (function() {
} }
}; };
var isShowTableQuery = function() { AbstractQuery.prototype.isShowTableQuery = function() {
return (this.sql.toLowerCase().indexOf('show tables') === 0); return (this.sql.toLowerCase().indexOf('show tables') === 0);
}; };
var handleShowTableQuery = function(results) { AbstractQuery.prototype.handleShowTableQuery = function(results) {
return Utils._.flatten(results.map(function(resultSet) { return Utils._.flatten(results.map(function(resultSet) {
return Utils._.values(resultSet); return Utils._.values(resultSet);
})); }));
}; };
var isSelectQuery = function() { AbstractQuery.prototype.isSelectQuery = function() {
return this.options.type === QueryTypes.SELECT; return this.options.type === QueryTypes.SELECT;
}; };
var isBulkUpdateQuery = function() { AbstractQuery.prototype.isBulkUpdateQuery = function() {
return this.options.type === QueryTypes.BULKUPDATE; return this.options.type === QueryTypes.BULKUPDATE;
}; };
var isBulkDeleteQuery = function() { AbstractQuery.prototype.isBulkDeleteQuery = function() {
return this.options.type === QueryTypes.BULKDELETE; return this.options.type === QueryTypes.BULKDELETE;
}; };
var isUpdateQuery = function() { AbstractQuery.prototype.isUpdateQuery = function() {
return (this.sql.toLowerCase().indexOf('update') === 0); return (this.sql.toLowerCase().indexOf('update') === 0);
}; };
var handleSelectQuery = function(results) { AbstractQuery.prototype.handleSelectQuery = function(results) {
var result = null; var result = null;
// Raw queries // Raw queries
...@@ -284,7 +269,7 @@ module.exports = (function() { ...@@ -284,7 +269,7 @@ module.exports = (function() {
return result; return result;
}; };
var isShowOrDescribeQuery = function() { AbstractQuery.prototype.isShowOrDescribeQuery = function() {
var result = false; var result = false;
result = result || (this.sql.toLowerCase().indexOf('show') === 0); result = result || (this.sql.toLowerCase().indexOf('show') === 0);
...@@ -293,7 +278,7 @@ module.exports = (function() { ...@@ -293,7 +278,7 @@ module.exports = (function() {
return result; return result;
}; };
var isCallQuery = function() { AbstractQuery.prototype.isCallQuery = function() {
var result = false; var result = false;
result = result || (this.sql.toLowerCase().indexOf('call') === 0); result = result || (this.sql.toLowerCase().indexOf('call') === 0);
......
...@@ -36,24 +36,25 @@ module.exports = (function() { ...@@ -36,24 +36,25 @@ module.exports = (function() {
for (var attr in attributes) { for (var attr in attributes) {
if (attributes.hasOwnProperty(attr)) { if (attributes.hasOwnProperty(attr)) {
var dataType = this.mysqlDataTypeMapping(tableName, attr, attributes[attr]); var dataType = this.mysqlDataTypeMapping(tableName, attr, attributes[attr])
, match;
if (Utils._.includes(dataType, 'PRIMARY KEY')) { if (Utils._.includes(dataType, 'PRIMARY KEY')) {
primaryKeys.push(attr); primaryKeys.push(attr);
if (Utils._.includes(dataType, 'REFERENCES')) { if (Utils._.includes(dataType, 'REFERENCES')) {
// MySQL doesn't support inline REFERENCES declarations: move to the end // MySQL doesn't support inline REFERENCES declarations: move to the end
var m = dataType.match(/^(.+) (REFERENCES.*)$/); match = dataType.match(/^(.+) (REFERENCES.*)$/);
attrStr.push(this.quoteIdentifier(attr) + ' ' + m[1].replace(/PRIMARY KEY/, '')); attrStr.push(this.quoteIdentifier(attr) + ' ' + match[1].replace(/PRIMARY KEY/, ''));
foreignKeys[attr] = m[2]; foreignKeys[attr] = match[2];
} else { } else {
attrStr.push(this.quoteIdentifier(attr) + ' ' + dataType.replace(/PRIMARY KEY/, '')); attrStr.push(this.quoteIdentifier(attr) + ' ' + dataType.replace(/PRIMARY KEY/, ''));
} }
} else if (Utils._.includes(dataType, 'REFERENCES')) { } else if (Utils._.includes(dataType, 'REFERENCES')) {
// MySQL doesn't support inline REFERENCES declarations: move to the end // MySQL doesn't support inline REFERENCES declarations: move to the end
var m = dataType.match(/^(.+) (REFERENCES.*)$/); match = dataType.match(/^(.+) (REFERENCES.*)$/);
attrStr.push(this.quoteIdentifier(attr) + ' ' + m[1]); attrStr.push(this.quoteIdentifier(attr) + ' ' + match[1]);
foreignKeys[attr] = m[2]; foreignKeys[attr] = match[2];
} else { } else {
attrStr.push(this.quoteIdentifier(attr) + ' ' + dataType); attrStr.push(this.quoteIdentifier(attr) + ' ' + dataType);
} }
......
...@@ -304,7 +304,7 @@ module.exports = (function() { ...@@ -304,7 +304,7 @@ module.exports = (function() {
}, },
deleteQuery: function(tableName, where, options, model) { deleteQuery: function(tableName, where, options, model) {
options = options ||  {}; options = options || {};
tableName = Utils.removeTicks(this.quoteTable(tableName), '"'); tableName = Utils.removeTicks(this.quoteTable(tableName), '"');
......
...@@ -91,7 +91,7 @@ module.exports = (function() { ...@@ -91,7 +91,7 @@ module.exports = (function() {
return results; return results;
} }
if (self.send('isSelectQuery')) { if (self.isSelectQuery()) {
if (self.sql.toLowerCase().indexOf('select c.column_name') === 0) { if (self.sql.toLowerCase().indexOf('select c.column_name') === 0) {
result = {}; result = {};
...@@ -146,9 +146,9 @@ module.exports = (function() { ...@@ -146,9 +146,9 @@ module.exports = (function() {
}); });
} }
return self.send('handleSelectQuery', rows); return self.handleSelectQuery(rows);
} }
} else if (self.send('isShowOrDescribeQuery')) { } else if (self.isShowOrDescribeQuery()) {
return results; return results;
} else if (QueryTypes.BULKUPDATE === self.options.type) { } else if (QueryTypes.BULKUPDATE === self.options.type) {
if (!self.options.returning) { if (!self.options.returning) {
...@@ -161,10 +161,10 @@ module.exports = (function() { ...@@ -161,10 +161,10 @@ module.exports = (function() {
}); });
} }
return self.send('handleSelectQuery', rows); return self.handleSelectQuery(rows);
} else if (QueryTypes.BULKDELETE === self.options.type) { } else if (QueryTypes.BULKDELETE === self.options.type) {
return result.rowCount; return result.rowCount;
} else if (self.send('isInsertQuery') || self.send('isUpdateQuery')) { } else if (self.isInsertQuery() || self.isUpdateQuery()) {
if (!!self.callee && self.callee.dataValues) { if (!!self.callee && self.callee.dataValues) {
if (!!self.callee.Model && !!self.callee.Model._hasHstoreAttributes) { if (!!self.callee.Model && !!self.callee.Model._hasHstoreAttributes) {
parseHstoreFields(self.callee.Model, rows[0]); parseHstoreFields(self.callee.Model, rows[0]);
......
...@@ -45,7 +45,7 @@ module.exports = (function() { ...@@ -45,7 +45,7 @@ module.exports = (function() {
return resolve(); return resolve();
} else { } else {
resolve(new Utils.Promise(function(resolve, reject) { resolve(new Utils.Promise(function(resolve, reject) {
self.database[getDatabaseMethod.call(self)](self.sql, function(err, results) { self.database[self.getDatabaseMethod()](self.sql, function(err, results) {
// allow clients to listen to sql to do their own logging or whatnot // allow clients to listen to sql to do their own logging or whatnot
promise.emit('sql', self.sql, self.options.uuid); promise.emit('sql', self.sql, self.options.uuid);
...@@ -59,13 +59,13 @@ module.exports = (function() { ...@@ -59,13 +59,13 @@ module.exports = (function() {
var result = self.callee; var result = self.callee;
// add the inserted row id to the instance // add the inserted row id to the instance
if (self.send('isInsertQuery', results, metaData)) { if (self.isInsertQuery(results, metaData)) {
self.send('handleInsertQuery', results, metaData); self.handleInsertQuery(results, metaData);
} }
if (self.sql.indexOf('sqlite_master') !== -1) { if (self.sql.indexOf('sqlite_master') !== -1) {
result = results.map(function(resultSet) { return resultSet.name; }); result = results.map(function(resultSet) { return resultSet.name; });
} else if (self.send('isSelectQuery')) { } else if (self.isSelectQuery()) {
if (!self.options.raw) { if (!self.options.raw) {
results = results.map(function(result) { results = results.map(function(result) {
for (var name in result) { for (var name in result) {
...@@ -93,8 +93,8 @@ module.exports = (function() { ...@@ -93,8 +93,8 @@ module.exports = (function() {
}); });
} }
result = self.send('handleSelectQuery', results); result = self.handleSelectQuery(results);
} else if (self.send('isShowOrDescribeQuery')) { } else if (self.isShowOrDescribeQuery()) {
result = results; result = results;
} else if (self.sql.indexOf('PRAGMA INDEX_LIST') !== -1) { } else if (self.sql.indexOf('PRAGMA INDEX_LIST') !== -1) {
// this is the sqlite way of getting the indexes of a table // this is the sqlite way of getting the indexes of a table
...@@ -144,7 +144,7 @@ module.exports = (function() { ...@@ -144,7 +144,7 @@ module.exports = (function() {
} }
}; };
if ((getDatabaseMethod.call(self) === 'all')) { if ((self.getDatabaseMethod() === 'all')) {
var tableNames = []; var tableNames = [];
if (self.options && self.options.tableNames) { if (self.options && self.options.tableNames) {
tableNames = self.options.tableNames; tableNames = self.options.tableNames;
...@@ -178,9 +178,8 @@ module.exports = (function() { ...@@ -178,9 +178,8 @@ module.exports = (function() {
}); });
}; };
//private Query.prototype.getDatabaseMethod = function() {
var getDatabaseMethod = function() { if (this.isInsertQuery() || this.isUpdateQuery() || (this.sql.toLowerCase().indexOf('CREATE TEMPORARY TABLE'.toLowerCase()) !== -1) || this.options.type === QueryTypes.BULKDELETE) {
if (this.send('isInsertQuery') || this.send('isUpdateQuery') || (this.sql.toLowerCase().indexOf('CREATE TEMPORARY TABLE'.toLowerCase()) !== -1) || this.options.type === QueryTypes.BULKDELETE) {
return 'run'; return 'run';
} else { } else {
return 'all'; return 'all';
......
...@@ -173,17 +173,17 @@ module.exports = (function() { ...@@ -173,17 +173,17 @@ module.exports = (function() {
if (this._hasCustomGetters) { if (this._hasCustomGetters) {
var values = {} var values = {}
, key; , _key;
for (key in this._customGetters) { for (_key in this._customGetters) {
if (this._customGetters.hasOwnProperty(key)) { if (this._customGetters.hasOwnProperty(_key)) {
values[key] = this.get(key); values[_key] = this.get(_key);
} }
} }
for (key in this.dataValues) { for (_key in this.dataValues) {
if (!values.hasOwnProperty(key) && this.dataValues.hasOwnProperty(key)) { if (!values.hasOwnProperty(_key) && this.dataValues.hasOwnProperty(_key)) {
values[key] = this.dataValues[key]; values[_key] = this.dataValues[_key];
} }
} }
return values; return values;
...@@ -524,7 +524,7 @@ module.exports = (function() { ...@@ -524,7 +524,7 @@ module.exports = (function() {
var identifier = self.primaryKeyValues; var identifier = self.primaryKeyValues;
if (identifier) { if (identifier) {
for (var attrName in identifier) { for (attrName in identifier) {
// Field name mapping // Field name mapping
if (self.Model.rawAttributes[attrName].field) { if (self.Model.rawAttributes[attrName].field) {
identifier[self.Model.rawAttributes[attrName].field] = identifier[attrName]; identifier[self.Model.rawAttributes[attrName].field] = identifier[attrName];
......
...@@ -716,7 +716,8 @@ module.exports = (function() { ...@@ -716,7 +716,8 @@ module.exports = (function() {
return Promise.resolve(null); return Promise.resolve(null);
} }
var primaryKeys = this.primaryKeys var where
, primaryKeys = this.primaryKeys
, keys = Object.keys(primaryKeys) , keys = Object.keys(primaryKeys)
, keysLength = keys.length , keysLength = keys.length
, tableNames = { }; , tableNames = { };
...@@ -733,7 +734,7 @@ module.exports = (function() { ...@@ -733,7 +734,7 @@ module.exports = (function() {
options.where.id = oldOption; options.where.id = oldOption;
} }
} else if (Utils._.size(primaryKeys) && Utils.argsArePrimaryKeys(arguments, primaryKeys)) { } else if (Utils._.size(primaryKeys) && Utils.argsArePrimaryKeys(arguments, primaryKeys)) {
var where = {}; where = {};
Utils._.each(arguments, function(arg, i) { Utils._.each(arguments, function(arg, i) {
var key = keys[i]; var key = keys[i];
...@@ -764,7 +765,7 @@ module.exports = (function() { ...@@ -764,7 +765,7 @@ module.exports = (function() {
// whereCollection is used for non-primary key updates // whereCollection is used for non-primary key updates
this.options.whereCollection = options.where || null; this.options.whereCollection = options.where || null;
} else if (typeof options === 'string') { } else if (typeof options === 'string') {
var where = {}; where = {};
if (this.primaryKeyCount === 1) { if (this.primaryKeyCount === 1) {
where[primaryKeys[keys[0]]] = options; where[primaryKeys[keys[0]]] = options;
......
...@@ -97,14 +97,15 @@ SqlString.escape = function(val, stringifyObjects, timeZone, dialect, field) { ...@@ -97,14 +97,15 @@ SqlString.escape = function(val, stringifyObjects, timeZone, dialect, field) {
}; };
SqlString.arrayToList = function(array, timeZone, dialect, field) { SqlString.arrayToList = function(array, timeZone, dialect, field) {
var valstr, i;
if (dialect === 'postgres') { if (dialect === 'postgres') {
var valstr = ''; valstr = '';
if (array.map) { if (array.map) {
valstr = array.map(function(v) { valstr = array.map(function(v) {
return SqlString.escape(v, true, timeZone, dialect, field); return SqlString.escape(v, true, timeZone, dialect, field);
}).join(','); }).join(',');
} else { } else {
for (var i = 0; i < array.length; i++) { for (i = 0; i < array.length; i++) {
valstr += SqlString.escape(array[i], true, timeZone, dialect, field) + ','; valstr += SqlString.escape(array[i], true, timeZone, dialect, field) + ',';
} }
valstr = valstr.slice(0, -1); valstr = valstr.slice(0, -1);
...@@ -123,8 +124,8 @@ SqlString.arrayToList = function(array, timeZone, dialect, field) { ...@@ -123,8 +124,8 @@ SqlString.arrayToList = function(array, timeZone, dialect, field) {
return SqlString.escape(v, true, timeZone, dialect); return SqlString.escape(v, true, timeZone, dialect);
}).join(', '); }).join(', ');
} else { } else {
var valstr = ''; valstr = '';
for (var i = 0; i < array.length; i++) { for (i = 0; i < array.length; i++) {
valstr += SqlString.escape(array[i], true, timeZone, dialect) + ', '; valstr += SqlString.escape(array[i], true, timeZone, dialect) + ', ';
} }
return valstr.slice(0, -2); return valstr.slice(0, -2);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!