sql-string.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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", "oracle"].includes(dialect)) {
  26. return +!!val;
  27. }
  28. return (!!val).toString();
  29. case "number":
  30. case "bigint":
  31. return val.toString();
  32. case "string":
  33. prependN = dialect === "mssql";
  34. break;
  35. }
  36. if (val instanceof Date) {
  37. val = dataTypes[dialect].DATE.prototype.stringify(val, { timezone: timeZone });
  38. }
  39. if (Buffer.isBuffer(val)) {
  40. if (dataTypes[dialect].BLOB) {
  41. return dataTypes[dialect].BLOB.prototype.stringify(val);
  42. }
  43. return dataTypes.BLOB.prototype.stringify(val);
  44. }
  45. if (Array.isArray(val)) {
  46. const partialEscape = (escVal) => escape(escVal, timeZone, dialect, format2);
  47. if (dialect === "postgres" && !format2) {
  48. return dataTypes.ARRAY.prototype.stringify(val, { escape: partialEscape });
  49. }
  50. return arrayToList(val, timeZone, dialect, format2);
  51. }
  52. if (!val.replace) {
  53. throw new Error(`Invalid value ${logger.inspect(val)}`);
  54. }
  55. if (["postgres", "sqlite", "mssql", "snowflake", "db2"].includes(dialect)) {
  56. val = val.replace(/'/g, "''");
  57. if (dialect === "postgres") {
  58. val = val.replace(/\0/g, "\\0");
  59. }
  60. } else if (dialect === "oracle" && typeof val === "string") {
  61. if (val.startsWith("TO_TIMESTAMP") || val.startsWith("TO_DATE")) {
  62. return val;
  63. }
  64. val = val.replace(/'/g, "''");
  65. } else {
  66. val = val.replace(/[\0\n\r\b\t\\'"\x1a]/g, (s) => {
  67. switch (s) {
  68. case "\0":
  69. return "\\0";
  70. case "\n":
  71. return "\\n";
  72. case "\r":
  73. return "\\r";
  74. case "\b":
  75. return "\\b";
  76. case " ":
  77. return "\\t";
  78. case "":
  79. return "\\Z";
  80. default:
  81. return `\\${s}`;
  82. }
  83. });
  84. }
  85. return `${(prependN ? "N'" : "'") + val}'`;
  86. }
  87. exports.escape = escape;
  88. function format(sql, values, timeZone, dialect) {
  89. values = [].concat(values);
  90. if (typeof sql !== "string") {
  91. throw new Error(`Invalid SQL string provided: ${sql}`);
  92. }
  93. return sql.replace(/\?/g, (match) => {
  94. if (!values.length) {
  95. return match;
  96. }
  97. return escape(values.shift(), timeZone, dialect, true);
  98. });
  99. }
  100. exports.format = format;
  101. function formatNamedParameters(sql, values, timeZone, dialect) {
  102. return sql.replace(/:+(?!\d)(\w+)/g, (value, key) => {
  103. if (dialect === "postgres" && value.slice(0, 2) === "::") {
  104. return value;
  105. }
  106. if (values[key] !== void 0) {
  107. return escape(values[key], timeZone, dialect, true);
  108. }
  109. throw new Error(`Named parameter "${value}" has no value in the given object.`);
  110. });
  111. }
  112. exports.formatNamedParameters = formatNamedParameters;
  113. //# sourceMappingURL=sql-string.js.map