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

Commit 3f510d83 by Jan Aagaard Meier

Removed eval from abstract query

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