sql.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. var __create = Object.create;
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __getProtoOf = Object.getPrototypeOf;
  6. var __hasOwnProp = Object.prototype.hasOwnProperty;
  7. var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
  8. var __export = (target, all) => {
  9. __markAsModule(target);
  10. for (var name in all)
  11. __defProp(target, name, { get: all[name], enumerable: true });
  12. };
  13. var __reExport = (target, module2, desc) => {
  14. if (module2 && typeof module2 === "object" || typeof module2 === "function") {
  15. for (let key of __getOwnPropNames(module2))
  16. if (!__hasOwnProp.call(target, key) && key !== "default")
  17. __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
  18. }
  19. return target;
  20. };
  21. var __toModule = (module2) => {
  22. return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
  23. };
  24. __export(exports, {
  25. injectReplacements: () => injectReplacements
  26. });
  27. var import_isPlainObject = __toModule(require("lodash/isPlainObject"));
  28. var import_sql_string = __toModule(require("../sql-string"));
  29. function injectReplacements(sqlString, dialect, replacements) {
  30. var _a, _b, _c, _d;
  31. if (replacements == null) {
  32. return sqlString;
  33. }
  34. if (!Array.isArray(replacements) && !(0, import_isPlainObject.default)(replacements)) {
  35. throw new TypeError(`"replacements" must be an array or a plain object, but received ${JSON.stringify(replacements)} instead.`);
  36. }
  37. const isNamedReplacements = (0, import_isPlainObject.default)(replacements);
  38. const isPositionalReplacements = Array.isArray(replacements);
  39. let lastConsumedPositionalReplacementIndex = -1;
  40. let output = "";
  41. let currentDollarStringTagName = null;
  42. let isString = false;
  43. let isColumn = false;
  44. let previousSliceEnd = 0;
  45. let isSingleLineComment = false;
  46. let isCommentBlock = false;
  47. let stringIsBackslashEscapable = false;
  48. for (let i = 0; i < sqlString.length; i++) {
  49. const char = sqlString[i];
  50. if (isColumn) {
  51. if (char === dialect.TICK_CHAR_RIGHT) {
  52. isColumn = false;
  53. }
  54. continue;
  55. }
  56. if (isString) {
  57. if (char === "'" && (!stringIsBackslashEscapable || !isBackslashEscaped(sqlString, i - 1))) {
  58. isString = false;
  59. stringIsBackslashEscapable = false;
  60. }
  61. continue;
  62. }
  63. if (currentDollarStringTagName !== null) {
  64. if (char !== "$") {
  65. continue;
  66. }
  67. const remainingString = sqlString.slice(i, sqlString.length);
  68. const dollarStringStartMatch = remainingString.match(/^\$(?<name>[a-z_][0-9a-z_]*)?(\$)/i);
  69. const tagName = ((_a = dollarStringStartMatch == null ? void 0 : dollarStringStartMatch.groups) == null ? void 0 : _a.name) || "";
  70. if (currentDollarStringTagName === tagName) {
  71. currentDollarStringTagName = null;
  72. }
  73. continue;
  74. }
  75. if (isSingleLineComment) {
  76. if (char === "\n") {
  77. isSingleLineComment = false;
  78. }
  79. continue;
  80. }
  81. if (isCommentBlock) {
  82. if (char === "*" && sqlString[i + 1] === "/") {
  83. isCommentBlock = false;
  84. }
  85. continue;
  86. }
  87. if (char === dialect.TICK_CHAR_LEFT) {
  88. isColumn = true;
  89. continue;
  90. }
  91. if (char === "'") {
  92. isString = true;
  93. stringIsBackslashEscapable = dialect.canBackslashEscape() || dialect.supports.escapeStringConstants && (sqlString[i - 1] === "E" || sqlString[i - 1] === "e") && canPrecedeNewToken(sqlString[i - 2]);
  94. continue;
  95. }
  96. if (char === "-" && sqlString.slice(i, i + 3) === "-- ") {
  97. isSingleLineComment = true;
  98. continue;
  99. }
  100. if (char === "/" && sqlString.slice(i, i + 2) === "/*") {
  101. isCommentBlock = true;
  102. continue;
  103. }
  104. if (char === "$") {
  105. const previousChar = sqlString[i - 1];
  106. if (/[0-9a-z_]/i.test(previousChar)) {
  107. continue;
  108. }
  109. const remainingString = sqlString.slice(i, sqlString.length);
  110. const dollarStringStartMatch = remainingString.match(/^\$(?<name>[a-z_][0-9a-z_]*)?(\$)/i);
  111. if (dollarStringStartMatch) {
  112. currentDollarStringTagName = (_c = (_b = dollarStringStartMatch.groups) == null ? void 0 : _b.name) != null ? _c : "";
  113. i += dollarStringStartMatch[0].length - 1;
  114. continue;
  115. }
  116. continue;
  117. }
  118. if (isNamedReplacements && char === ":") {
  119. const previousChar = sqlString[i - 1];
  120. if (!canPrecedeNewToken(previousChar) && previousChar !== "[") {
  121. continue;
  122. }
  123. const remainingString = sqlString.slice(i, sqlString.length);
  124. const match = remainingString.match(/^:(?<name>[a-z_][0-9a-z_]*)(?:\)|,|$|\s|::|;|])/i);
  125. const replacementName = (_d = match == null ? void 0 : match.groups) == null ? void 0 : _d.name;
  126. if (!replacementName) {
  127. continue;
  128. }
  129. const replacementValue = replacements[replacementName];
  130. if (!Object.prototype.hasOwnProperty.call(replacements, replacementName) || replacementValue === void 0) {
  131. throw new Error(`Named replacement ":${replacementName}" has no entry in the replacement map.`);
  132. }
  133. const escapedReplacement = (0, import_sql_string.escape)(replacementValue, void 0, dialect.name, true);
  134. output += sqlString.slice(previousSliceEnd, i);
  135. previousSliceEnd = i + replacementName.length + 1;
  136. output += escapedReplacement;
  137. continue;
  138. }
  139. if (isPositionalReplacements && char === "?") {
  140. const previousChar = sqlString[i - 1];
  141. if (!canPrecedeNewToken(previousChar) && previousChar !== "[") {
  142. continue;
  143. }
  144. const nextChar = sqlString[i + 1];
  145. if (nextChar === "|" || nextChar === "&") {
  146. continue;
  147. }
  148. const replacementIndex = ++lastConsumedPositionalReplacementIndex;
  149. const replacementValue = replacements[lastConsumedPositionalReplacementIndex];
  150. if (replacementValue === void 0) {
  151. throw new Error(`Positional replacement (?) ${replacementIndex} has no entry in the replacement map (replacements[${replacementIndex}] is undefined).`);
  152. }
  153. const escapedReplacement = (0, import_sql_string.escape)(replacementValue, void 0, dialect.name, true);
  154. output += sqlString.slice(previousSliceEnd, i);
  155. previousSliceEnd = i + 1;
  156. output += escapedReplacement;
  157. }
  158. }
  159. if (isString) {
  160. throw new Error(`The following SQL query includes an unterminated string literal:
  161. ${sqlString}`);
  162. }
  163. output += sqlString.slice(previousSliceEnd, sqlString.length);
  164. return output;
  165. }
  166. function canPrecedeNewToken(char) {
  167. return char === void 0 || /[\s(>,=]/.test(char);
  168. }
  169. function isBackslashEscaped(string, pos) {
  170. let escaped = false;
  171. for (let i = pos; i >= 0; i--) {
  172. const char = string[i];
  173. if (char !== "\\") {
  174. break;
  175. }
  176. escaped = !escaped;
  177. }
  178. return escaped;
  179. }
  180. //# sourceMappingURL=sql.js.map