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

Commit 948ea4d1 by Jan Scheurer

match codestyle in sql-string.js

1 parent 0f3b6f26
Showing with 59 additions and 57 deletions
...@@ -2,20 +2,20 @@ var moment = require("moment") ...@@ -2,20 +2,20 @@ var moment = require("moment")
, isArrayBufferView , isArrayBufferView
, SqlString = exports; , SqlString = exports;
if(typeof(ArrayBufferView) === 'function') { if (typeof ArrayBufferView === 'function') {
isArrayBufferView = function(object) { return object && (object instanceof ArrayBufferView); }; isArrayBufferView = function(object) { return object && (object instanceof ArrayBufferView) }
} else { } else {
var arrayBufferViews = [ var arrayBufferViews = [
Int8Array, Uint8Array, Int16Array, Uint16Array, Int8Array, Uint8Array, Int16Array, Uint16Array,
Int32Array, Uint32Array, Float32Array, Float64Array Int32Array, Uint32Array, Float32Array, Float64Array
]; ]
isArrayBufferView = function(object) { isArrayBufferView = function(object) {
for(var i=0;i<8;i++) { for (var i=0; i<8; i++) {
if(object instanceof arrayBufferViews[i]) { if (object instanceof arrayBufferViews[i]) {
return true; return true
} }
} }
return false; return false
}; };
} }
...@@ -56,28 +56,28 @@ SqlString.escape = function(val, stringifyObjects, timeZone, dialect, field) { ...@@ -56,28 +56,28 @@ SqlString.escape = function(val, stringifyObjects, timeZone, dialect, field) {
} }
if (val instanceof Date) { if (val instanceof Date) {
val = SqlString.dateToString(val, timeZone || "Z", dialect); val = SqlString.dateToString(val, timeZone || "Z", dialect)
} }
if (Buffer.isBuffer(val)) { if (Buffer.isBuffer(val)) {
return SqlString.bufferToString(val, dialect); return SqlString.bufferToString(val, dialect)
} }
if (Array.isArray(val) || isArrayBufferView(val)) { if (Array.isArray(val) || isArrayBufferView(val)) {
return SqlString.arrayToList(val, timeZone, dialect, field); return SqlString.arrayToList(val, timeZone, dialect, field)
} }
if (typeof val === 'object') { if (typeof val === 'object') {
if (stringifyObjects) { if (stringifyObjects) {
val = val.toString(); val = val.toString()
} else { } else {
return SqlString.objectToValues(val, timeZone); return SqlString.objectToValues(val, timeZone)
} }
} }
if (dialect === 'postgres' || dialect === 'sqlite') { if (dialect === 'postgres' || dialect === 'sqlite') {
// http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS // http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS
// 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, function(s) {
switch(s) { switch(s) {
...@@ -91,40 +91,41 @@ SqlString.escape = function(val, stringifyObjects, timeZone, dialect, field) { ...@@ -91,40 +91,41 @@ SqlString.escape = function(val, stringifyObjects, timeZone, dialect, field) {
} }
}); });
} }
return "'"+val+"'"; return "'"+val+"'"
}; };
SqlString.arrayToList = function(array, timeZone, dialect, field) { SqlString.arrayToList = function(array, timeZone, dialect, field) {
if (dialect === 'postgres') { if (dialect === 'postgres') {
if (array.map) { if (array.map) {
var valstr = array.map(function(v) { var 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 {
var valstr = ""; var valstr = ""
for (var i = 0; i < array.length; i++) { for (var 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)
} }
var ret = 'ARRAY[' + valstr + ']'; var ret = 'ARRAY[' + valstr + ']'
if (!!field && !!field.type) { if (!!field && !!field.type) {
ret += '::' + field.type.replace(/\(\d+\)/g, ''); ret += '::' + field.type.replace(/\(\d+\)/g, '')
} }
return ret; return ret
} else { } else {
if (array.map) { if (array.map) {
return array.map(function(v) { return array.map(function(v) {
if (Array.isArray(v)) if (Array.isArray(v)) {
return '(' + SqlString.arrayToList(v, timeZone, dialect) + ')'; return '(' + SqlString.arrayToList(v, timeZone, dialect) + ')'
return SqlString.escape(v, true, timeZone, dialect); }
}).join(', '); return SqlString.escape(v, true, timeZone, dialect)
}).join(', ')
} else { } else {
var valstr = ""; var valstr = ""
for (var i = 0; i < array.length; i++) { for (var 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)
} }
} }
}; };
...@@ -134,87 +135,88 @@ SqlString.format = function(sql, values, timeZone, dialect) { ...@@ -134,87 +135,88 @@ SqlString.format = function(sql, values, timeZone, dialect) {
return sql.replace(/\?/g, function(match) { return sql.replace(/\?/g, function(match) {
if (!values.length) { if (!values.length) {
return match; return match
} }
return SqlString.escape(values.shift(), false, timeZone, dialect); return SqlString.escape(values.shift(), false, timeZone, dialect)
}); })
}; };
SqlString.formatNamedParameters = function(sql, values, timeZone, dialect) { SqlString.formatNamedParameters = function(sql, values, timeZone, dialect) {
return sql.replace(/\:(\w+)/g, function (value, key) { return sql.replace(/\:(\w+)/g, function (value, key) {
if (values.hasOwnProperty(key)) { if (values.hasOwnProperty(key)) {
return SqlString.escape(values[key], false, timeZone, dialect); return SqlString.escape(values[key], false, timeZone, dialect)
} } 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.');
} }
}); });
}; };
SqlString.dateToString = function(date, timeZone, dialect) { SqlString.dateToString = function(date, timeZone, dialect) {
var dt = new Date(date); var dt = new Date(date)
// TODO: Ideally all dialects would work a bit more like this // TODO: Ideally all dialects would work a bit more like this
if (dialect === "postgres") { if (dialect === "postgres") {
return moment(dt).zone('+00:00').format("YYYY-MM-DD HH:mm:ss.SSS Z"); return moment(dt).zone('+00:00').format("YYYY-MM-DD HH:mm:ss.SSS Z")
} }
if (timeZone !== 'local') { if (timeZone !== 'local') {
var tz = convertTimezone(timeZone); var tz = convertTimezone(timeZone)
dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000)); dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000))
if (tz !== false) { if (tz !== false) {
dt.setTime(dt.getTime() + (tz * 60000)); dt.setTime(dt.getTime() + (tz * 60000))
} }
} }
return moment(dt).format("YYYY-MM-DD HH:mm:ss"); return moment(dt).format("YYYY-MM-DD HH:mm:ss")
}; };
SqlString.bufferToString = function(buffer, dialect) { SqlString.bufferToString = function(buffer, dialect) {
var hex = ''; var hex = ''
try { try {
hex = buffer.toString('hex'); hex = buffer.toString('hex')
} catch (err) { } catch (err) {
// node v0.4.x does not support hex / throws unknown encoding error // node v0.4.x does not support hex / throws unknown encoding error
for (var i = 0; i < buffer.length; i++) { for (var i = 0; i < buffer.length; i++) {
var byte = buffer[i]; var byte = buffer[i]
hex += zeroPad(byte.toString(16)); hex += zeroPad(byte.toString(16))
} }
} }
if (dialect === 'postgres') { if (dialect === 'postgres') {
// bytea hex format http://www.postgresql.org/docs/current/static/datatype-binary.html // bytea hex format http://www.postgresql.org/docs/current/static/datatype-binary.html
return "E'\\\\x" + hex+ "'"; return "E'\\\\x" + hex+ "'"
} }
return "X'" + hex+ "'"; return "X'" + hex+ "'"
}; };
SqlString.objectToValues = function(object, timeZone) { SqlString.objectToValues = function(object, timeZone) {
var values = []; var values = []
for (var key in object) { for (var key in object) {
var value = object[key]; var value = object[key]
if(typeof value === 'function') { if(typeof value === 'function') {
continue; continue;
} }
values.push(this.escapeId(key) + ' = ' + SqlString.escape(value, true, timeZone)); values.push(this.escapeId(key) + ' = ' + SqlString.escape(value, true, timeZone))
} }
return values.join(', '); return values.join(', ')
}; };
function zeroPad(number) { function zeroPad(number) {
return (number < 10) ? '0' + number : number; return (number < 10) ? '0' + number : number
} }
function convertTimezone(tz) { function convertTimezone(tz) {
if (tz == "Z") return 0; if (tz == "Z") {
return 0
}
var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/); var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/)
if (m) { if (m) {
return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60; return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60
} }
return false; return false
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!