analyzer.join.test.js 10.0 KB

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