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

Commit 378d4ba8 by User4martin

moved code from sqlString to abstract/query / refactor bind replacement

fix spacing
1 parent 1e709899
'use strict'; 'use strict';
var Utils = require('../../utils') var Utils = require('../../utils')
, SqlString = require('../../sql-string')
, Dot = require('dottie') , Dot = require('dottie')
, QueryTypes = require('../../query-types'); , QueryTypes = require('../../query-types');
...@@ -382,11 +383,86 @@ var groupJoinData = function(rows, includeOptions, options) { ...@@ -382,11 +383,86 @@ var groupJoinData = function(rows, includeOptions, options) {
/** /**
* rewrite query with parameters * rewrite query with parameters
*
* Examples:
*
* query.formatBindParameters('select $1 as foo', ['fooval']);
*
* query.formatBindParameters('select $foo as foo', { foo: 'fooval' });
*
* Options
* skipUnescape: bool, skip unescaping $$
* skipValueReplace: bool, do not replace (but do unescape $$). Check correct syntax and if all values are available
*/ */
AbstractQuery.prototype.formatBindParameters = function(sql, parameters, dialect) { AbstractQuery.prototype.formatBindParameters = function(sql, values, dialect, replacementFunc, options) {
sql = Utils.formatBindParameters(sql, parameters, dialect); if (!values) {
return [sql, []];
}
options = options || {};
if (typeof replacementFunc !== 'function') {
options = replacementFunc || {};
replacementFunc = undefined;
}
if (!replacementFunc) {
if (options.skipValueReplace) {
replacementFunc = function(match, key, values, timeZone, dialect, options) {
if (values[key] !== undefined) {
return match;
}
return undefined;
};
} else {
replacementFunc = function(match, key, values, timeZone, dialect, options) {
if (values[key] !== undefined) {
return SqlString.escape(values[key], false, timeZone, dialect);
}
return undefined;
};
}
} else {
if (options.skipValueReplace) {
var origReplacementFunc = replacementFunc;
replacementFunc = function(match, key, values, timeZone, dialect, options) {
if (origReplacementFunc(match, key, values, timeZone, dialect, options) !== undefined) {
return match;
}
return undefined;
};
}
}
var timeZone = null;
if (Array.isArray(values)) {
sql = sql.replace(/\$(\$|(?:\d+))/g, function(match, key) {
if ('$' === key) {
return options.skipUnescape ? match : key;
}
key = key - 1;
var replVal = replacementFunc(match, key, values, timeZone, dialect, options);
if (replVal === undefined) {
throw new Error('Named bind parameter "' + match + '" has no value in the given object.');
}
return replVal;
});
} else {
sql = sql.replace(/\$(\$|(?!\d)(?:\w+))/g, function(match, key) {
if ('$' === key) {
return options.skipUnescape ? match : key;
}
var replVal = replacementFunc(match, key, values, timeZone, dialect, options);
if (replVal === undefined) {
throw new Error('Named bind parameter "' + match + '" has no value in the given object.');
}
return replVal;
});
}
return [sql, []]; return [sql, []];
}; };
/** /**
* Execute the passed sql query. * Execute the passed sql query.
* *
......
...@@ -90,30 +90,15 @@ Query.prototype.parseDialectSpecificFields = parseDialectSpecificFields; ...@@ -90,30 +90,15 @@ Query.prototype.parseDialectSpecificFields = parseDialectSpecificFields;
/** /**
* rewrite query with parameters * rewrite query with parameters
*/ */
Query.prototype.formatBindParameters = function(sql, values, timeZone, dialect) { Query.prototype.formatBindParameters = function(sql, values, dialect) {
var bindParam = []; var bindParam = [];
if (Array.isArray(values)) { if (Array.isArray(values)) {
bindParam = values; bindParam = values;
sql = sql.replace(/\$(\$|(?:\d+))/g, function(value, key) { sql = this.parent.formatBindParameters(sql, values, dialect, { skipValueReplace: true })[0];
if ('$' === key) {
return key;
}
key = key - 1;
if (values[key] !== undefined) {
return value;
} else {
throw new Error('Named bind parameter "' + value + '" has no value in the given object.');
}
});
} else { } else {
var i = 0; var i = 0;
var seen = {}; var seen = {};
sql = sql.replace(/\$(\$|(?!\d)(?:\w+))/g, function(value, key) { var replacementFunc = function(match, key, values, timeZone, dialect, options) {
if ('$' === key) {
return key;
}
if (seen[key] !== undefined) { if (seen[key] !== undefined) {
return seen[key]; return seen[key];
} }
...@@ -122,10 +107,10 @@ Query.prototype.formatBindParameters = function(sql, values, timeZone, dialect) ...@@ -122,10 +107,10 @@ Query.prototype.formatBindParameters = function(sql, values, timeZone, dialect)
bindParam.push(values[key]); bindParam.push(values[key]);
seen[key] = '$'+i; seen[key] = '$'+i;
return '$'+i; return '$'+i;
} else {
throw new Error('Named bind parameter "' + value + '" has no value in the given object.');
} }
}); return undefined;
};
sql = this.parent.formatBindParameters(sql, values, dialect, replacementFunc)[0];
} }
return [sql, bindParam]; return [sql, bindParam];
}; };
......
...@@ -27,25 +27,14 @@ Query.prototype.getInsertIdField = function() { ...@@ -27,25 +27,14 @@ Query.prototype.getInsertIdField = function() {
/** /**
* rewrite query with parameters * rewrite query with parameters
*/ */
Query.prototype.formatBindParameters = function(sql, values, timeZone, dialect) { Query.prototype.formatBindParameters = function(sql, values, dialect) {
var bindParam = []; var bindParam = [];
if (Array.isArray(values)) { if (Array.isArray(values)) {
bindParam = {}; bindParam = {};
values.forEach(function(v, i) { values.forEach(function(v, i) {
bindParam['$'+(i+1)] = v; bindParam['$'+(i+1)] = v;
}); });
sql = sql.replace(/\$(\$|(?:\d+))/g, function(value, key) { sql = this.parent.formatBindParameters(sql, values, dialect, { skipValueReplace: true })[0];
if ('$' === key) {
return key;
}
key = key - 1;
if (values[key] !== undefined) {
return value;
} else {
throw new Error('Named bind parameter "' + value + '" has no value in the given object.');
}
});
} else { } else {
bindParam = {}; bindParam = {};
if (typeof values === 'object') { if (typeof values === 'object') {
...@@ -53,17 +42,7 @@ Query.prototype.formatBindParameters = function(sql, values, timeZone, dialect) ...@@ -53,17 +42,7 @@ Query.prototype.formatBindParameters = function(sql, values, timeZone, dialect)
bindParam['$'+k] = values[k]; bindParam['$'+k] = values[k];
}); });
} }
sql = sql.replace(/\$(\$|(?!\d)(?:\w+))/g, function(value, key) { sql = this.parent.formatBindParameters(sql, values, dialect, { skipValueReplace: true })[0];
if ('$' === key) {
return key;
}
if (values[key] !== undefined) {
return value;
} else {
throw new Error('Named bind parameter "' + value + '" has no value in the given object.');
}
});
} }
return [sql, bindParam]; return [sql, bindParam];
}; };
...@@ -76,7 +55,7 @@ Query.prototype.run = function(sql, parameters) { ...@@ -76,7 +55,7 @@ Query.prototype.run = function(sql, parameters) {
var method = self.getDatabaseMethod(); var method = self.getDatabaseMethod();
if (method === 'exec') { if (method === 'exec') {
// exec does not support bind parameter // exec does not support bind parameter
sql = Utils.formatBindParameters(sql, self.options.bind, self.options.dialect, { noUnescape: true }); sql = this.parent.formatBindParameters(sql, self.options.bind, self.options.dialect, { skipUnescape: true })[0];
this.sql = sql; this.sql = sql;
} }
......
...@@ -712,7 +712,7 @@ Sequelize.prototype.query = function(sql, options) { ...@@ -712,7 +712,7 @@ Sequelize.prototype.query = function(sql, options) {
throw new Error('Both `sql.bind` and `options.bind` cannot be set at the same time'); throw new Error('Both `sql.bind` and `options.bind` cannot be set at the same time');
} }
options.bind= sql.bind; options.bind = sql.bind;
} }
if (sql.query !== undefined) { if (sql.query !== undefined) {
......
...@@ -161,36 +161,6 @@ SqlString.formatNamedParameters = function(sql, values, timeZone, dialect) { ...@@ -161,36 +161,6 @@ SqlString.formatNamedParameters = function(sql, values, timeZone, dialect) {
}); });
}; };
SqlString.formatBindParameters = function(sql, values, timeZone, dialect, options) {
options = options || {};
if (Array.isArray(values)) {
return sql.replace(/\$(\$|(?:\d+))/g, function(value, key) {
if ('$' === key) {
return options.noUnescape ? value : key;
}
key = key - 1;
if (values[key] !== undefined) {
return SqlString.escape(values[key], false, timeZone, dialect);
} else {
throw new Error('Named bind parameter "' + value + '" has no value in the given object.');
}
});
} else {
return sql.replace(/\$(\$|(?!\d)(?:\w+))/g, function(value, key) {
if ('$' === key) {
return options.noUnescape ? value : key;
}
if (values[key] !== undefined) {
return SqlString.escape(values[key], false, timeZone, dialect);
} else {
throw new Error('Named bind parameter "' + value + '" has no value in the given object.');
}
});
}
};
SqlString.dateToString = function(date, timeZone, dialect) { SqlString.dateToString = function(date, timeZone, dialect) {
if (moment.tz.zone(timeZone)) { if (moment.tz.zone(timeZone)) {
date = moment(date).tz(timeZone); date = moment(date).tz(timeZone);
......
...@@ -63,13 +63,6 @@ var Utils = module.exports = { ...@@ -63,13 +63,6 @@ var Utils = module.exports = {
var timeZone = null; var timeZone = null;
return SqlString.formatNamedParameters(sql, parameters, timeZone, dialect); return SqlString.formatNamedParameters(sql, parameters, timeZone, dialect);
}, },
formatBindParameters: function(sql, parameters, dialect, options) {
if (parameters) {
var timeZone = null;
return SqlString.formatBindParameters(sql, parameters, timeZone, dialect, options);
}
return sql;
},
cloneDeep: function(obj, fn) { cloneDeep: function(obj, fn) {
return _.cloneDeep(obj, function (elem) { return _.cloneDeep(obj, function (elem) {
// Do not try to customize cloning of plain objects and strings // Do not try to customize cloning of plain objects and strings
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!