index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. 'use strict';
  2. // based on code from Brian White @mscdex mariasql library - https://github.com/mscdex/node-mariasql/blob/master/lib/Client.js#L272-L332
  3. // License: https://github.com/mscdex/node-mariasql/blob/master/LICENSE
  4. const RE_PARAM = /(?:\?)|(?::(\d+|(?:[a-zA-Z][a-zA-Z0-9_]*)))/g,
  5. DQUOTE = 34,
  6. SQUOTE = 39,
  7. BSLASH = 92;
  8. function parse(query) {
  9. let ppos = RE_PARAM.exec(query);
  10. let curpos = 0;
  11. let start = 0;
  12. let end;
  13. const parts = [];
  14. let inQuote = false;
  15. let escape = false;
  16. let qchr;
  17. const tokens = [];
  18. let qcnt = 0;
  19. let lastTokenEndPos = 0;
  20. let i;
  21. if (ppos) {
  22. do {
  23. for (i = curpos, end = ppos.index; i < end; ++i) {
  24. const chr = query.charCodeAt(i);
  25. if (chr === BSLASH) escape = !escape;
  26. else {
  27. if (escape) {
  28. escape = false;
  29. continue;
  30. }
  31. if (inQuote && chr === qchr) {
  32. if (query.charCodeAt(i + 1) === qchr) {
  33. // quote escaped via "" or ''
  34. ++i;
  35. continue;
  36. }
  37. inQuote = false;
  38. } else if (!inQuote && (chr === DQUOTE || chr === SQUOTE)) {
  39. inQuote = true;
  40. qchr = chr;
  41. }
  42. }
  43. }
  44. if (!inQuote) {
  45. parts.push(query.substring(start, end));
  46. tokens.push(ppos[0].length === 1 ? qcnt++ : ppos[1]);
  47. start = end + ppos[0].length;
  48. lastTokenEndPos = start;
  49. }
  50. curpos = end + ppos[0].length;
  51. } while ((ppos = RE_PARAM.exec(query)));
  52. if (tokens.length) {
  53. if (curpos < query.length) {
  54. parts.push(query.substring(lastTokenEndPos));
  55. }
  56. return [parts, tokens];
  57. }
  58. }
  59. return [query];
  60. }
  61. function createCompiler(config) {
  62. if (!config) config = {};
  63. if (!config.placeholder) {
  64. config.placeholder = '?';
  65. }
  66. let ncache = 100;
  67. let cache;
  68. if (typeof config.cache === 'number') {
  69. ncache = config.cache;
  70. }
  71. if (typeof config.cache === 'object') {
  72. cache = config.cache;
  73. }
  74. if (config.cache !== false && !cache) {
  75. cache = require('lru.min').createLRU({ max: ncache });
  76. }
  77. function toArrayParams(tree, params) {
  78. const arr = [];
  79. if (tree.length === 1) {
  80. return [tree[0], []];
  81. }
  82. if (typeof params === 'undefined')
  83. throw new Error(
  84. 'Named query contains placeholders, but parameters object is undefined'
  85. );
  86. const tokens = tree[1];
  87. for (let i = 0; i < tokens.length; ++i) {
  88. arr.push(params[tokens[i]]);
  89. }
  90. return [tree[0], arr];
  91. }
  92. function noTailingSemicolon(s) {
  93. if (s.slice(-1) === ':') {
  94. return s.slice(0, -1);
  95. }
  96. return s;
  97. }
  98. function join(tree) {
  99. if (tree.length === 1) {
  100. return tree;
  101. }
  102. let unnamed = noTailingSemicolon(tree[0][0]);
  103. for (let i = 1; i < tree[0].length; ++i) {
  104. if (tree[0][i - 1].slice(-1) === ':') {
  105. unnamed += config.placeholder;
  106. }
  107. unnamed += config.placeholder;
  108. unnamed += noTailingSemicolon(tree[0][i]);
  109. }
  110. const last = tree[0][tree[0].length - 1];
  111. if (tree[0].length === tree[1].length) {
  112. if (last.slice(-1) === ':') {
  113. unnamed += config.placeholder;
  114. }
  115. unnamed += config.placeholder;
  116. }
  117. return [unnamed, tree[1]];
  118. }
  119. function compile(query, paramsObj) {
  120. let tree;
  121. if (cache && (tree = cache.get(query))) {
  122. return toArrayParams(tree, paramsObj);
  123. }
  124. tree = join(parse(query));
  125. if (cache) {
  126. cache.set(query, tree);
  127. }
  128. return toArrayParams(tree, paramsObj);
  129. }
  130. compile.parse = parse;
  131. return compile;
  132. }
  133. // named :one :two to postgres-style numbered $1 $2 $3
  134. function toNumbered(q, params) {
  135. const tree = parse(q);
  136. const paramsArr = [];
  137. if (tree.length === 1) {
  138. return [tree[0], paramsArr];
  139. }
  140. const pIndexes = {};
  141. let pLastIndex = 0;
  142. let qs = '';
  143. let varIndex;
  144. const varNames = [];
  145. for (let i = 0; i < tree[0].length; ++i) {
  146. varIndex = pIndexes[tree[1][i]];
  147. if (!varIndex) {
  148. varIndex = ++pLastIndex;
  149. pIndexes[tree[1][i]] = varIndex;
  150. }
  151. if (tree[1][i]) {
  152. varNames[varIndex - 1] = tree[1][i];
  153. qs += `${tree[0][i]}$${varIndex}`;
  154. } else {
  155. qs += tree[0][i];
  156. }
  157. }
  158. return [qs, varNames.map((n) => params[n])];
  159. }
  160. module.exports = createCompiler;
  161. module.exports.toNumbered = toNumbered;