analyzer.unionAll.test.js 8.7 KB

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