analyzer.subquery.test.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. var Analyzer = require('../../../index').query.analyzer;
  2. var tokenize = require('../../support/tokenize');
  3. var assert = require('assert');
  4. describe('Analyzer ::', function() {
  5. describe('Subqueries', function() {
  6. describe('used as a predicate', function() {
  7. it('should generate a valid group for an IN subquery', function() {
  8. var tokens = tokenize({
  9. select: ['*'],
  10. where: {
  11. and: [
  12. {
  13. id: {
  14. in: {
  15. select: ['id'],
  16. from: 'users',
  17. where: {
  18. or: [
  19. { status: 'active' },
  20. { name: 'John' }
  21. ]
  22. }
  23. }
  24. }
  25. }
  26. ]
  27. },
  28. from: 'accounts'
  29. });
  30. var result = Analyzer(tokens);
  31. assert.deepEqual(result, [
  32. [
  33. { type: 'IDENTIFIER', value: 'SELECT' },
  34. { type: 'VALUE', value: '*' }
  35. ],
  36. [
  37. { type: 'IDENTIFIER', value: 'WHERE' },
  38. { type: 'CONDITION', value: 'AND' },
  39. [
  40. { type: 'KEY', value: 'id' },
  41. { type: 'CONDITION', value: 'IN' },
  42. { type: 'SUBQUERY', value: null },
  43. [
  44. [
  45. { type: 'IDENTIFIER', value: 'SELECT' },
  46. { type: 'VALUE', value: 'id' }
  47. ],
  48. [
  49. { type: 'IDENTIFIER', value: 'FROM' },
  50. { type: 'VALUE', value: 'users' }
  51. ],
  52. [
  53. { type: 'IDENTIFIER', value: 'WHERE' },
  54. [
  55. { type: 'KEY', value: 'status' },
  56. { type: 'VALUE', value: 'active' }
  57. ],
  58. [
  59. { type: 'KEY', value: 'name' },
  60. { type: 'VALUE', value: 'John' }
  61. ]
  62. ]
  63. ]
  64. ]
  65. ],
  66. [
  67. { type: 'IDENTIFIER', value: 'FROM' },
  68. { type: 'VALUE', value: 'accounts' }
  69. ]
  70. ]);
  71. });
  72. it('should generate a valid group for a NOT IN subquery', function() {
  73. var tokens = tokenize({
  74. select: ['*'],
  75. from: 'accounts',
  76. where: {
  77. and: [
  78. {
  79. id: {
  80. nin: {
  81. select: ['id'],
  82. from: 'users',
  83. where: {
  84. or: [
  85. { status: 'active' },
  86. { name: 'John' }
  87. ]
  88. }
  89. }
  90. }
  91. }
  92. ]
  93. }
  94. });
  95. var result = Analyzer(tokens);
  96. assert.deepEqual(result, [
  97. [
  98. { type: 'IDENTIFIER', value: 'SELECT' },
  99. { type: 'VALUE', value: '*' }
  100. ],
  101. [
  102. { type: 'IDENTIFIER', value: 'FROM' },
  103. { type: 'VALUE', value: 'accounts' }
  104. ],
  105. [
  106. { type: 'IDENTIFIER', value: 'WHERE' },
  107. { type: 'CONDITION', value: 'AND' },
  108. [
  109. { type: 'KEY', value: 'id' },
  110. { type: 'CONDITION', value: 'NOTIN' },
  111. { type: 'SUBQUERY', value: null },
  112. [
  113. [
  114. { type: 'IDENTIFIER', value: 'SELECT' },
  115. { type: 'VALUE', value: 'id' }
  116. ],
  117. [
  118. { type: 'IDENTIFIER', value: 'FROM' },
  119. { type: 'VALUE', value: 'users' }
  120. ],
  121. [
  122. { type: 'IDENTIFIER', value: 'WHERE' },
  123. [
  124. { type: 'KEY', value: 'status' },
  125. { type: 'VALUE', value: 'active' }
  126. ],
  127. [
  128. { type: 'KEY', value: 'name' },
  129. { type: 'VALUE', value: 'John' }
  130. ]
  131. ]
  132. ]
  133. ]
  134. ]
  135. ]);
  136. });
  137. });
  138. describe('used as scalar values', function() {
  139. it('should generate a valid group when used inside a SELECT', function() {
  140. var tokens = tokenize({
  141. select: ['name', {
  142. select: ['username'],
  143. from: 'users',
  144. where: {
  145. or: [
  146. { status: 'active' },
  147. { name: 'John' }
  148. ]
  149. },
  150. as: 'username'
  151. }, 'age'],
  152. from: 'accounts'
  153. });
  154. var result = Analyzer(tokens);
  155. assert.deepEqual(result, [
  156. [
  157. { type: 'IDENTIFIER', value: 'SELECT' },
  158. { type: 'VALUE', value: 'name' }
  159. ],
  160. [
  161. { type: 'IDENTIFIER', value: 'SELECT' },
  162. { type: 'SUBQUERY', value: null },
  163. [
  164. [
  165. { type: 'IDENTIFIER', value: 'SELECT' },
  166. { type: 'VALUE', value: 'username' }
  167. ],
  168. [
  169. { type: 'IDENTIFIER', value: 'FROM' },
  170. { type: 'VALUE', value: 'users' }
  171. ],
  172. [
  173. { type: 'IDENTIFIER', value: 'WHERE' },
  174. [
  175. { type: 'KEY', value: 'status' },
  176. { type: 'VALUE', value: 'active' }
  177. ],
  178. [
  179. { type: 'KEY', value: 'name' },
  180. { type: 'VALUE', value: 'John' }
  181. ]
  182. ],
  183. [
  184. { type: 'IDENTIFIER', value: 'AS' },
  185. { type: 'VALUE', value: 'username' }
  186. ]
  187. ]
  188. ],
  189. [
  190. { type: 'IDENTIFIER', value: 'SELECT' },
  191. { type: 'VALUE', value: 'age' }
  192. ],
  193. [
  194. { type: 'IDENTIFIER', value: 'FROM' },
  195. { type: 'VALUE', value: 'accounts' }
  196. ]
  197. ]);
  198. });
  199. it('should generate a valid group when used as a value in a WHERE', function() {
  200. var tokens = tokenize({
  201. select: ['name', 'age'],
  202. from: 'accounts',
  203. where: {
  204. and: [
  205. {
  206. username: {
  207. select: ['username'],
  208. from: 'users',
  209. where: {
  210. color: 'accounts.color'
  211. }
  212. }
  213. }
  214. ]
  215. }
  216. });
  217. var result = Analyzer(tokens);
  218. assert.deepEqual(result, [
  219. [
  220. { type: 'IDENTIFIER', value: 'SELECT' },
  221. { type: 'VALUE', value: 'name' }
  222. ],
  223. [
  224. { type: 'IDENTIFIER', value: 'SELECT' },
  225. { type: 'VALUE', value: 'age' }
  226. ],
  227. [
  228. { type: 'IDENTIFIER', value: 'FROM' },
  229. { type: 'VALUE', value: 'accounts' }
  230. ],
  231. [
  232. { type: 'IDENTIFIER', value: 'WHERE' },
  233. { type: 'CONDITION', value: 'AND' },
  234. [
  235. { type: 'KEY', value: 'username' },
  236. { type: 'SUBQUERY', value: null },
  237. [
  238. [
  239. { type: 'IDENTIFIER', value: 'SELECT' },
  240. { type: 'VALUE', value: 'username' }
  241. ],
  242. [
  243. { type: 'IDENTIFIER', value: 'FROM' },
  244. { type: 'VALUE', value: 'users' }
  245. ],
  246. [
  247. { type: 'IDENTIFIER', value: 'WHERE' },
  248. { type: 'KEY', value: 'color' },
  249. { type: 'VALUE', value: 'accounts.color' }
  250. ]
  251. ]
  252. ]
  253. ]
  254. ]);
  255. });
  256. });
  257. describe('used as table sub query', function() {
  258. it('should generate a valid group when used as a value in a FROM with an AS alias', function() {
  259. var tokens = tokenize({
  260. select: ['name', 'age'],
  261. from: {
  262. select: ['age'],
  263. from: 'users',
  264. where: {
  265. and: [
  266. {
  267. age: 21
  268. }
  269. ]
  270. },
  271. as: 'userage'
  272. }
  273. });
  274. var result = Analyzer(tokens);
  275. assert.deepEqual(result, [
  276. [
  277. { type: 'IDENTIFIER', value: 'SELECT' },
  278. { type: 'VALUE', value: 'name' }
  279. ],
  280. [
  281. { type: 'IDENTIFIER', value: 'SELECT' },
  282. { type: 'VALUE', value: 'age' }
  283. ],
  284. [
  285. { type: 'IDENTIFIER', value: 'FROM' },
  286. { type: 'SUBQUERY', value: null },
  287. [
  288. [
  289. { type: 'IDENTIFIER', value: 'SELECT' },
  290. { type: 'VALUE', value: 'age' }
  291. ],
  292. [
  293. { type: 'IDENTIFIER', value: 'FROM' },
  294. { type: 'VALUE', value: 'users' }
  295. ],
  296. [
  297. { type: 'IDENTIFIER', value: 'WHERE' },
  298. { type: 'CONDITION', value: 'AND' },
  299. [
  300. { type: 'KEY', value: 'age' },
  301. { type: 'VALUE', value: 21 }
  302. ]
  303. ],
  304. [
  305. { type: 'IDENTIFIER', value: 'AS' },
  306. { type: 'VALUE', value: 'userage' }
  307. ]
  308. ]
  309. ]
  310. ]);
  311. });
  312. });
  313. });
  314. });