sql-string.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. const dataTypes = require("./data-types");
  3. const { logger } = require("./utils/logger");
  4. function arrayToList(array, timeZone, dialect, format2) {
  5. return array.reduce((sql, val, i) => {
  6. if (i !== 0) {
  7. sql += ", ";
  8. }
  9. if (Array.isArray(val)) {
  10. sql += `(${arrayToList(val, timeZone, dialect, format2)})`;
  11. } else {
  12. sql += escape(val, timeZone, dialect, format2);
  13. }
  14. return sql;
  15. }, "");
  16. }
  17. exports.arrayToList = arrayToList;
  18. function escape(val, timeZone, dialect, format2) {
  19. let prependN = false;
  20. if (val === void 0 || val === null) {
  21. return "NULL";
  22. }
  23. switch (typeof val) {
  24. case "boolean":
  25. if (["sqlite", "mssql"].includes(dialect)) {
  26. return +!!val;
  27. }
  28. return (!!val).toString();
  29. case "number":
  30. return val.toString();
  31. case "string":
  32. prependN = dialect === "mssql";
  33. break;
  34. }
  35. if (val instanceof Date) {
  36. val = dataTypes[dialect].DATE.prototype.stringify(val, { timezone: timeZone });
  37. }
  38. if (Buffer.isBuffer(val)) {
  39. if (dataTypes[dialect].BLOB) {
  40. return dataTypes[dialect].BLOB.prototype.stringify(val);
  41. }
  42. return dataTypes.BLOB.prototype.stringify(val);
  43. }
  44. if (Array.isArray(val)) {
  45. const partialEscape = (escVal) => escape(escVal, timeZone, dialect, format2);
  46. if (dialect === "postgres" && !format2) {
  47. return dataTypes.ARRAY.prototype.stringify(val, { escape: partialEscape });
  48. }
  49. return arrayToList(val, timeZone, dialect, format2);
  50. }
  51. if (!val.replace) {
  52. throw new Error(`Invalid value ${logger.inspect(val)}`);
  53. }
  54. if (["postgres", "sqlite", "mssql", "snowflake", "db2"].includes(dialect)) {
  55. val = val.replace(/'/g, "''");
  56. if (dialect === "postgres") {
  57. val = val.replace(/\0/g, "\\0");
  58. }
  59. } else {
  60. val = val.replace(/[\0\n\r\b\t\\'"\x1a]/g, (s) => {
  61. switch (s) {
  62. case "\0":
  63. return "\\0";
  64. case "\n":
  65. return "\\n";
  66. case "\r":
  67. return "\\r";
  68. case "\b":
  69. return "\\b";
  70. case " ":
  71. return "\\t";
  72. case "":
  73. return "\\Z";
  74. default:
  75. return `\\${s}`;
  76. }
  77. });
  78. }
  79. return `${(prependN ? "N'" : "'") + val}'`;
  80. }
  81. exports.escape = escape;
  82. function format(sql, values, timeZone, dialect) {
  83. values = [].concat(values);
  84. if (typeof sql !== "string") {
  85. throw new Error(`Invalid SQL string provided: ${sql}`);
  86. }
  87. return sql.replace(/\?/g, (match) => {
  88. if (!values.length) {
  89. return match;
  90. }
  91. return escape(values.shift(), timeZone, dialect, true);
  92. });
  93. }
  94. exports.format = format;
  95. function formatNamedParameters(sql, values, timeZone, dialect) {
  96. return sql.replace(/:+(?!\d)(\w+)/g, (value, key) => {
  97. if (dialect === "postgres" && value.slice(0, 2) === "::") {
  98. return value;
  99. }
  100. if (values[key] !== void 0) {
  101. return escape(values[key], timeZone, dialect, true);
  102. }
  103. throw new Error(`Named parameter "${value}" has no value in the given object.`);
  104. });
  105. }
  106. exports.formatNamedParameters = formatNamedParameters;
  107. //# sourceMappingURL=sql-string.js.map