SqlString.js 3.4 KB

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