index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. let chr = query.charCodeAt(i);
  25. if (chr === BSLASH)
  26. escape = !escape;
  27. else {
  28. if (escape) {
  29. escape = false;
  30. continue;
  31. }
  32. if (inQuote && chr === qchr) {
  33. if (query.charCodeAt(i + 1) === qchr) {
  34. // quote escaped via "" or ''
  35. ++i;
  36. continue;
  37. }
  38. inQuote = false;
  39. } else if (chr === DQUOTE || chr === SQUOTE) {
  40. inQuote = true;
  41. qchr = chr;
  42. }
  43. }
  44. }
  45. if (!inQuote) {
  46. parts.push(query.substring(start, end));
  47. tokens.push(ppos[0].length === 1 ? qcnt++ : ppos[1]);
  48. start = end + ppos[0].length;
  49. lastTokenEndPos = start;
  50. }
  51. curpos = end + ppos[0].length;
  52. } while (ppos = RE_PARAM.exec(query));
  53. if (tokens.length) {
  54. if (curpos < query.length) {
  55. parts.push(query.substring(lastTokenEndPos));
  56. }
  57. return [parts, tokens];
  58. }
  59. }
  60. return [query];
  61. };
  62. const EMPTY_LRU_FN = (key, value) => {};
  63. function createCompiler(config) {
  64. if (!config)
  65. config = {};
  66. if (!config.placeholder) {
  67. config.placeholder = '?';
  68. }
  69. let ncache = 100;
  70. let cache;
  71. if (typeof config.cache === 'number') {
  72. ncache = config.cache;
  73. }
  74. if (typeof config.cache === 'object') {
  75. cache = config.cache;
  76. }
  77. if (config.cache !== false && !cache) {
  78. cache = require('lru-cache')({ max: ncache, dispose: EMPTY_LRU_FN });
  79. }
  80. function toArrayParams(tree, params) {
  81. const arr = [];
  82. if (tree.length == 1) {
  83. return [tree[0], []];
  84. }
  85. if (typeof params == 'undefined')
  86. throw new Error('Named query contains placeholders, but parameters object is undefined');
  87. const tokens = tree[1];
  88. for (let i=0; i < tokens.length; ++i) {
  89. arr.push(params[tokens[i]]);
  90. }
  91. return [tree[0], arr];
  92. }
  93. function noTailingSemicolon(s) {
  94. if (s.slice(-1) == ':') {
  95. return s.slice(0, -1);
  96. }
  97. return s;
  98. }
  99. function join(tree) {
  100. if (tree.length == 1) {
  101. return tree;
  102. }
  103. let unnamed = noTailingSemicolon(tree[0][0]);
  104. for (let i=1; i < tree[0].length; ++i) {
  105. if (tree[0][i-1].slice(-1) == ':') {
  106. unnamed += config.placeholder;
  107. }
  108. unnamed += config.placeholder;
  109. unnamed += noTailingSemicolon(tree[0][i]);
  110. }
  111. const last = tree[0][tree[0].length -1];
  112. if (tree[0].length == tree[1].length) {
  113. if (last.slice(-1) == ':') {
  114. unnamed += config.placeholder;
  115. }
  116. unnamed += config.placeholder;
  117. }
  118. return [unnamed, tree[1]];
  119. }
  120. function compile(query, paramsObj) {
  121. let tree;
  122. if (cache && (tree = cache.get(query))) {
  123. return toArrayParams(tree, paramsObj)
  124. }
  125. tree = join(parse(query));
  126. if(cache) {
  127. cache.set(query, tree);
  128. }
  129. return toArrayParams(tree, paramsObj);
  130. }
  131. compile.parse = parse;
  132. return compile;
  133. }
  134. // named :one :two to postgres-style numbered $1 $2 $3
  135. function toNumbered(q, params) {
  136. const tree = parse(q);
  137. const paramsArr = [];
  138. if (tree.length == 1) {
  139. return [tree[0], paramsArr];
  140. }
  141. const pIndexes = {};
  142. let pLastIndex = 0;
  143. let qs = '';
  144. let varIndex;
  145. const varNames = [];
  146. for (let i=0; i < tree[0].length; ++i) {
  147. varIndex = pIndexes[tree[1][i]];
  148. if (!varIndex) {
  149. varIndex = ++pLastIndex;
  150. pIndexes[tree[1][i]] = varIndex;
  151. }
  152. if (tree[1][i]) {
  153. varNames[varIndex - 1] = tree[1][i];
  154. qs += tree[0][i] + '$' + varIndex;
  155. } else {
  156. qs += tree[0][i];
  157. }
  158. }
  159. return [qs, varNames.map(n => params[n])];
  160. }
  161. module.exports = createCompiler;
  162. module.exports.toNumbered = toNumbered;