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

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';
/* jshint -W110 */
var dataTypes = require('./data-types')
, _ = require('lodash')
, SqlString = exports;
const dataTypes = require('./data-types');
const _ = require('lodash');
SqlString.escapeId = function(val, forbidQualified) {
function escapeId(val, forbidQualified) {
if (forbidQualified) {
return '`' + val.replace(/`/g, '``') + '`';
}
return '`' + val.replace(/`/g, '``').replace(/\./g, '`.`') + '`';
};
}
exports.escapeId = escapeId;
SqlString.escape = function(val, timeZone, dialect, format) {
var prependN = false;
function escape(val, timeZone, dialect, format) {
let prependN = false;
if (val === undefined || val === null) {
return 'NULL';
}
......@@ -48,11 +48,11 @@ SqlString.escape = function(val, timeZone, dialect, format) {
}
if (Array.isArray(val)) {
var escape = _.partial(SqlString.escape, _, timeZone, dialect, format);
const partialEscape = _.partial(escape, _, timeZone, dialect, 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) {
......@@ -64,7 +64,7 @@ SqlString.escape = function(val, timeZone, dialect, format) {
// http://stackoverflow.com/q/603572/130598
val = val.replace(/'/g, "''");
} 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) {
case '\0': return '\\0';
case '\n': return '\\n';
......@@ -77,30 +77,33 @@ SqlString.escape = function(val, timeZone, dialect, format) {
});
}
return (prependN ? "N'" : "'") + val + "'";
};
}
exports.escape = escape;
SqlString.format = function(sql, values, timeZone, dialect) {
function format(sql, values, timeZone, dialect) {
values = [].concat(values);
return sql.replace(/\?/g, function(match) {
return sql.replace(/\?/g, match => {
if (!values.length) {
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) {
return sql.replace(/\:+(?!\d)(\w+)/g, function(value, key) {
function formatNamedParameters(sql, values, timeZone, dialect) {
return sql.replace(/\:+(?!\d)(\w+)/g, (value, key) => {
if ('postgres' === dialect && '::' === value.slice(0, 2)) {
return value;
}
if (values[key] !== undefined) {
return SqlString.escape(values[key], timeZone, dialect, true);
return escape(values[key], timeZone, dialect, true);
} else {
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!