SqlString.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var SqlString = exports;
  2. SqlString.escapeId = function (val, forbidQualified) {
  3. if (forbidQualified) {
  4. return '`' + val.replace(/`/g, '``') + '`';
  5. }
  6. return '`' + val.replace(/`/g, '``').replace(/\./g, '`.`') + '`';
  7. };
  8. SqlString.escape = function(val, stringifyObjects, timeZone) {
  9. if (val === undefined || val === null) {
  10. return 'NULL';
  11. }
  12. switch (typeof val) {
  13. case 'boolean': return (val) ? 'true' : 'false';
  14. case 'number': return val+'';
  15. }
  16. if (val instanceof Date) {
  17. val = SqlString.dateToString(val, timeZone || "Z");
  18. }
  19. if (Buffer.isBuffer(val)) {
  20. return SqlString.bufferToString(val);
  21. }
  22. if (Array.isArray(val)) {
  23. return SqlString.arrayToList(val, timeZone);
  24. }
  25. if (typeof val === 'object') {
  26. if (stringifyObjects) {
  27. val = val.toString();
  28. } else {
  29. return SqlString.objectToValues(val, timeZone);
  30. }
  31. }
  32. val = val.replace(/[\0\n\r\b\t\\\'\"\x1a]/g, function(s) {
  33. switch(s) {
  34. case "\0": return "\\0";
  35. case "\n": return "\\n";
  36. case "\r": return "\\r";
  37. case "\b": return "\\b";
  38. case "\t": return "\\t";
  39. case "\x1a": return "\\Z";
  40. default: return "\\"+s;
  41. }
  42. });
  43. return "'"+val+"'";
  44. };
  45. SqlString.arrayToList = function(array, timeZone) {
  46. return array.map(function(v) {
  47. if (Array.isArray(v)) return '(' + SqlString.arrayToList(v) + ')';
  48. return SqlString.escape(v, true, timeZone);
  49. }).join(', ');
  50. };
  51. SqlString.format = function(sql, values, timeZone) {
  52. values = [].concat(values);
  53. return sql.replace(/\?/g, function(match) {
  54. if (!values.length) {
  55. return match;
  56. }
  57. return SqlString.escape(values.shift(), false, timeZone);
  58. });
  59. };
  60. SqlString.dateToString = function(date, timeZone) {
  61. var dt = new Date(date);
  62. if (timeZone != 'local') {
  63. var tz = convertTimezone(timeZone);
  64. dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000));
  65. if (tz !== false) {
  66. dt.setTime(dt.getTime() + (tz * 60000));
  67. }
  68. }
  69. var year = dt.getFullYear();
  70. var month = zeroPad(dt.getMonth() + 1);
  71. var day = zeroPad(dt.getDate());
  72. var hour = zeroPad(dt.getHours());
  73. var minute = zeroPad(dt.getMinutes());
  74. var second = zeroPad(dt.getSeconds());
  75. return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
  76. };
  77. SqlString.bufferToString = function(buffer) {
  78. var hex = '';
  79. try {
  80. hex = buffer.toString('hex');
  81. } catch (err) {
  82. // node v0.4.x does not support hex / throws unknown encoding error
  83. for (var i = 0; i < buffer.length; i++) {
  84. var byte = buffer[i];
  85. hex += zeroPad(byte.toString(16));
  86. }
  87. }
  88. return "X'" + hex+ "'";
  89. };
  90. SqlString.objectToValues = function(object, timeZone) {
  91. var values = [];
  92. for (var key in object) {
  93. var value = object[key];
  94. if(typeof value === 'function') {
  95. continue;
  96. }
  97. values.push(this.escapeId(key) + ' = ' + SqlString.escape(value, true, timeZone));
  98. }
  99. return values.join(', ');
  100. };
  101. function zeroPad(number) {
  102. return (number < 10) ? '0' + number : number;
  103. }
  104. function convertTimezone(tz) {
  105. if (tz == "Z") return 0;
  106. var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/);
  107. if (m) {
  108. return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60;
  109. }
  110. return false;
  111. }