Last active
August 29, 2015 13:58
-
-
Save adgsm/9976031 to your computer and use it in GitHub Desktop.
SqlString.js for node-mysql [allowing $or, $and usage within where claus]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var SqlString = exports; | |
| SqlString.escapeId = function (val, forbidQualified) { | |
| if (Array.isArray(val)) { | |
| return val.map(function(v) { | |
| return SqlString.escapeId(v, forbidQualified); | |
| }).join(', '); | |
| } | |
| if (forbidQualified) { | |
| return '`' + val.replace(/`/g, '``') + '`'; | |
| } | |
| return '`' + val.replace(/`/g, '``').replace(/\./g, '`.`') + '`'; | |
| }; | |
| SqlString.escape = function(val, stringifyObjects, timeZone , delimiter) { | |
| if (val === undefined || val === null) { | |
| return 'NULL'; | |
| } | |
| if( delimiter === undefined ) delimiter = ', '; | |
| switch (typeof val) { | |
| case 'boolean': return (val) ? 'true' : 'false'; | |
| case 'number': return val+''; | |
| } | |
| if (val instanceof Date) { | |
| val = SqlString.dateToString(val, timeZone || 'local'); | |
| } | |
| if (Buffer.isBuffer(val)) { | |
| return SqlString.bufferToString(val); | |
| } | |
| if (Array.isArray(val)) { | |
| return SqlString.arrayToList(val, timeZone , delimiter); | |
| } | |
| if (typeof val === 'object') { | |
| if (stringifyObjects) { | |
| val = val.toString(); | |
| } else { | |
| return SqlString.objectToValues(val, timeZone , delimiter); | |
| } | |
| } | |
| val = val.replace(/[\0\n\r\b\t\\\'\"\x1a]/g, function(s) { | |
| switch(s) { | |
| case "\0": return "\\0"; | |
| case "\n": return "\\n"; | |
| case "\r": return "\\r"; | |
| case "\b": return "\\b"; | |
| case "\t": return "\\t"; | |
| case "\x1a": return "\\Z"; | |
| default: return "\\"+s; | |
| } | |
| }); | |
| return "'"+val+"'"; | |
| }; | |
| SqlString.arrayToList = function(array, timeZone , delimiter) { | |
| return array.map(function(v) { | |
| if (Array.isArray(v)) return '(' + SqlString.arrayToList(v, timeZone , delimiter) + ')'; | |
| return SqlString.escape(v, ( typeof v === 'object' ) ? false : true, timeZone , delimiter); | |
| }).join( delimiter ); | |
| }; | |
| SqlString.format = function(sql, values, stringifyObjects, timeZone) { | |
| values = [].concat(values); | |
| return sql.replace(/\?\??/g, function(match) { | |
| if (!values.length) { | |
| return match; | |
| } | |
| if (match == "??") { | |
| return SqlString.escapeId(values.shift()); | |
| } | |
| return SqlString.escape(values.shift(), stringifyObjects, timeZone); | |
| }); | |
| }; | |
| SqlString.dateToString = function(date, timeZone) { | |
| var dt = new Date(date); | |
| if (timeZone != 'local') { | |
| var tz = convertTimezone(timeZone); | |
| dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000)); | |
| if (tz !== false) { | |
| dt.setTime(dt.getTime() + (tz * 60000)); | |
| } | |
| } | |
| var year = dt.getFullYear(); | |
| var month = zeroPad(dt.getMonth() + 1, 2); | |
| var day = zeroPad(dt.getDate(), 2); | |
| var hour = zeroPad(dt.getHours(), 2); | |
| var minute = zeroPad(dt.getMinutes(), 2); | |
| var second = zeroPad(dt.getSeconds(), 2); | |
| var millisecond = zeroPad(dt.getMilliseconds(), 3); | |
| return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + '.' + millisecond; | |
| }; | |
| SqlString.bufferToString = function(buffer) { | |
| var hex = ''; | |
| try { | |
| hex = buffer.toString('hex'); | |
| } catch (err) { | |
| // node v0.4.x does not support hex / throws unknown encoding error | |
| for (var i = 0; i < buffer.length; i++) { | |
| var byte = buffer[i]; | |
| hex += zeroPad(byte.toString(16)); | |
| } | |
| } | |
| return "X'" + hex+ "'"; | |
| }; | |
| SqlString.objectToValues = function(object, timeZone , delimiter) { | |
| var values = []; | |
| for (var key in object) { | |
| var value = object[key]; | |
| if(typeof value === 'function') { | |
| continue; | |
| } | |
| switch (key) { | |
| case '$or': | |
| values.push(' ( ' + SqlString.escape(value, false, timeZone , ' OR ' ) + ' ) '); | |
| break; | |
| case '$and': | |
| values.push(' ( ' + SqlString.escape(value, false, timeZone , ' AND ' ) + ' ) '); | |
| break; | |
| case '$like': | |
| values.push(' ( ' + SqlString.escape(value, false, timeZone , ' LIKE ' ) + ' ) '); | |
| break; | |
| case '$gt': | |
| values.push(' ( ' + SqlString.escape(value, false, timeZone , ' > ' ) + ' ) '); | |
| break; | |
| case '$gte': | |
| values.push(' ( ' + SqlString.escape(value, false, timeZone , ' >= ' ) + ' ) '); | |
| break; | |
| case '$lt': | |
| values.push(' ( ' + SqlString.escape(value, false, timeZone , ' < ' ) + ' ) '); | |
| break; | |
| case '$lte': | |
| values.push(' ( ' + SqlString.escape(value, false, timeZone , ' <= ' ) + ' ) '); | |
| break; | |
| default: | |
| values.push(this.escapeId(key) + ( ( [ ' LIKE ' , ' > ' , ' >= ' , ' < ' , ' <= ' ].indexOf( delimiter ) != -1 ) ? delimiter : ' = ' ) + SqlString.escape(value, ( typeof value === 'object' ) ? false : true, timeZone, delimiter)); | |
| } | |
| } | |
| return values.join( delimiter ); | |
| }; | |
| function zeroPad(number, length) { | |
| number = number.toString(); | |
| while (number.length < length) { | |
| number = '0' + number; | |
| } | |
| return number; | |
| } | |
| function convertTimezone(tz) { | |
| if (tz == "Z") return 0; | |
| var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/); | |
| if (m) { | |
| return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60; | |
| } | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment