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

Commit f6cb74ae by Felix Becker Committed by Mick Hansen

ES6 refactor of sql-string.js (#6080)

let, const,  arrow functions, exports
1 parent 7dc351f5
Showing with 23 additions and 20 deletions
'use strict'; 'use strict';
/* jshint -W110 */ /* jshint -W110 */
var dataTypes = require('./data-types') const dataTypes = require('./data-types');
, _ = require('lodash') const _ = require('lodash');
, SqlString = exports;
SqlString.escapeId = function(val, forbidQualified) { function escapeId(val, forbidQualified) {
if (forbidQualified) { if (forbidQualified) {
return '`' + val.replace(/`/g, '``') + '`'; return '`' + val.replace(/`/g, '``') + '`';
} }
return '`' + val.replace(/`/g, '``').replace(/\./g, '`.`') + '`'; return '`' + val.replace(/`/g, '``').replace(/\./g, '`.`') + '`';
}; }
exports.escapeId = escapeId;
SqlString.escape = function(val, timeZone, dialect, format) { function escape(val, timeZone, dialect, format) {
var prependN = false; let prependN = false;
if (val === undefined || val === null) { if (val === undefined || val === null) {
return 'NULL'; return 'NULL';
} }
...@@ -48,11 +48,11 @@ SqlString.escape = function(val, timeZone, dialect, format) { ...@@ -48,11 +48,11 @@ SqlString.escape = function(val, timeZone, dialect, format) {
} }
if (Array.isArray(val)) { if (Array.isArray(val)) {
var escape = _.partial(SqlString.escape, _, timeZone, dialect, format); const partialEscape = _.partial(escape, _, timeZone, dialect, format);
if (dialect === 'postgres' && !format) { if (dialect === 'postgres' && !format) {
return dataTypes.ARRAY.prototype.stringify(val, {escape: escape}); return dataTypes.ARRAY.prototype.stringify(val, {escape});
} }
return val.map(escape); return val.map(partialEscape);
} }
if (!val.replace) { if (!val.replace) {
...@@ -64,7 +64,7 @@ SqlString.escape = function(val, timeZone, dialect, format) { ...@@ -64,7 +64,7 @@ SqlString.escape = function(val, timeZone, dialect, format) {
// http://stackoverflow.com/q/603572/130598 // http://stackoverflow.com/q/603572/130598
val = val.replace(/'/g, "''"); val = val.replace(/'/g, "''");
} else { } else {
val = val.replace(/[\0\n\r\b\t\\\'\"\x1a]/g, function(s) { val = val.replace(/[\0\n\r\b\t\\\'\"\x1a]/g, s => {
switch (s) { switch (s) {
case '\0': return '\\0'; case '\0': return '\\0';
case '\n': return '\\n'; case '\n': return '\\n';
...@@ -77,30 +77,33 @@ SqlString.escape = function(val, timeZone, dialect, format) { ...@@ -77,30 +77,33 @@ SqlString.escape = function(val, timeZone, dialect, format) {
}); });
} }
return (prependN ? "N'" : "'") + val + "'"; return (prependN ? "N'" : "'") + val + "'";
}; }
exports.escape = escape;
SqlString.format = function(sql, values, timeZone, dialect) { function format(sql, values, timeZone, dialect) {
values = [].concat(values); values = [].concat(values);
return sql.replace(/\?/g, function(match) { return sql.replace(/\?/g, match => {
if (!values.length) { if (!values.length) {
return match; return match;
} }
return SqlString.escape(values.shift(), timeZone, dialect, true); return escape(values.shift(), timeZone, dialect, true);
}); });
}; }
exports.format = format;
SqlString.formatNamedParameters = function(sql, values, timeZone, dialect) { function formatNamedParameters(sql, values, timeZone, dialect) {
return sql.replace(/\:+(?!\d)(\w+)/g, function(value, key) { return sql.replace(/\:+(?!\d)(\w+)/g, (value, key) => {
if ('postgres' === dialect && '::' === value.slice(0, 2)) { if ('postgres' === dialect && '::' === value.slice(0, 2)) {
return value; return value;
} }
if (values[key] !== undefined) { if (values[key] !== undefined) {
return SqlString.escape(values[key], timeZone, dialect, true); return escape(values[key], timeZone, dialect, true);
} else { } else {
throw new Error('Named parameter "' + value + '" has no value in the given object.'); throw new Error('Named parameter "' + value + '" has no value in the given object.');
} }
}); });
}; }
exports.formatNamedParameters = formatNamedParameters;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!