index.js 4.2 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. 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. function createCompiler(config) {
  63. if (!config)
  64. config = {};
  65. if (!config.placeholder) {
  66. config.placeholder = '?';
  67. }
  68. let ncache = 100;
  69. let cache;
  70. if (typeof config.cache === 'number') {
  71. ncache = config.cache;
  72. }
  73. if (typeof config.cache === 'object') {
  74. cache = config.cache;
  75. }
  76. if (config.cache !== false && !cache) {
  77. cache = new (require('lru-cache'))({ max: ncache });
  78. }
  79. function toArrayParams(tree, params) {
  80. const arr = [];
  81. if (tree.length == 1) {
  82. return [tree[0], []];
  83. }
  84. if (typeof params == 'undefined')
  85. throw new Error('Named query contains placeholders, but parameters object is undefined');
  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;