select.test.coffee 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. ###
  2. Copyright (c) 2014 Ramesh Nair (hiddentao.com)
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. ###
  22. squel = require "../dist/squel-basic"
  23. {_, testCreator, assert, expect, should} = require './testbase'
  24. test = testCreator()
  25. test['SELECT builder'] =
  26. beforeEach: ->
  27. @func = squel.select
  28. @inst = @func()
  29. 'instanceof QueryBuilder': ->
  30. assert.instanceOf @inst, squel.cls.QueryBuilder
  31. 'constructor':
  32. 'override options': ->
  33. @inst = squel.select
  34. usingValuePlaceholders: true
  35. dummy: true
  36. expectedOptions = _.extend {}, squel.cls.DefaultQueryBuilderOptions,
  37. usingValuePlaceholders: true
  38. dummy: true
  39. for block in @inst.blocks
  40. if (block instanceof squel.cls.FromTableBlock)
  41. assert.same _.extend({}, expectedOptions, { prefix: 'FROM'}), block.options
  42. else if (block instanceof squel.cls.WhereBlock)
  43. assert.same _.extend({}, expectedOptions, { verb: 'WHERE'}), block.options
  44. else if (block instanceof squel.cls.HavingBlock)
  45. assert.same _.extend({}, expectedOptions, { verb: 'HAVING'}), block.options
  46. else
  47. assert.same expectedOptions, block.options
  48. 'override blocks': ->
  49. block = new squel.cls.StringBlock('SELECT')
  50. @inst = @func {}, [block]
  51. assert.same [block], @inst.blocks
  52. 'build query':
  53. 'no need to call from() first': ->
  54. @inst.toString()
  55. '>> function(1)':
  56. beforeEach: -> @inst.function('1')
  57. toString: ->
  58. assert.same @inst.toString(), 'SELECT 1'
  59. toParam: ->
  60. assert.same @inst.toParam(), { text: 'SELECT 1', values: [] }
  61. '>> function(MAX(?,?), 3, 5)':
  62. beforeEach: -> @inst.function('MAX(?, ?)', 3, 5)
  63. toString: ->
  64. assert.same @inst.toString(), 'SELECT MAX(3, 5)'
  65. toParam: ->
  66. assert.same @inst.toParam(), { text: 'SELECT MAX(?, ?)', values: [3, 5] }
  67. '>> from(table).from(table2, alias2)':
  68. beforeEach: -> @inst.from('table').from('table2', 'alias2')
  69. toString: ->
  70. assert.same @inst.toString(), 'SELECT * FROM table, table2 `alias2`'
  71. '>> field(squel.select().field("MAX(score)").FROM("scores"), fa1)':
  72. beforeEach: -> @inst.field(squel.select().field("MAX(score)").from("scores"), 'fa1')
  73. toString: ->
  74. assert.same @inst.toString(), 'SELECT (SELECT MAX(score) FROM scores) AS "fa1" FROM table, table2 `alias2`'
  75. '>> field(squel.case().when(score > ?, 1).then(1), fa1)':
  76. beforeEach: -> @inst.field(squel.case().when("score > ?", 1).then(1), 'fa1')
  77. toString: ->
  78. assert.same @inst.toString(), 'SELECT CASE WHEN (score > 1) THEN 1 ELSE NULL END AS "fa1" FROM table, table2 `alias2`'
  79. toParam: ->
  80. assert.same @inst.toParam(), { text: 'SELECT CASE WHEN (score > ?) THEN 1 ELSE NULL END AS "fa1" FROM table, table2 `alias2`', values: [1] }
  81. '>> field( squel.str(SUM(?), squel.case().when(score > ?, 1).then(1) ), fa1)':
  82. beforeEach: -> @inst.field( squel.str('SUM(?)', squel.case().when("score > ?", 1).then(1)), 'fa1')
  83. toString: ->
  84. assert.same @inst.toString(), 'SELECT (SUM((CASE WHEN (score > 1) THEN 1 ELSE NULL END))) AS "fa1" FROM table, table2 `alias2`'
  85. toParam: ->
  86. assert.same @inst.toParam(), { text: 'SELECT (SUM(CASE WHEN (score > ?) THEN 1 ELSE NULL END)) AS "fa1" FROM table, table2 `alias2`', values: [1] }
  87. '>> field(field1, fa1) >> field(field2)':
  88. beforeEach: -> @inst.field('field1', 'fa1').field('field2')
  89. toString: ->
  90. assert.same @inst.toString(), 'SELECT field1 AS "fa1", field2 FROM table, table2 `alias2`'
  91. '>> distinct()':
  92. beforeEach: -> @inst.distinct()
  93. toString: ->
  94. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2`'
  95. '>> group(field) >> group(field2)':
  96. beforeEach: -> @inst.group('field').group('field2')
  97. toString: ->
  98. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` GROUP BY field, field2'
  99. '>> where(a = ?, squel.select().field("MAX(score)").from("scores"))':
  100. beforeEach: ->
  101. @subQuery = squel.select().field("MAX(score)").from("scores")
  102. @inst.where('a = ?', @subQuery)
  103. toString: ->
  104. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` WHERE (a = (SELECT MAX(score) FROM scores)) GROUP BY field, field2'
  105. toParam: ->
  106. assert.same @inst.toParam(), {
  107. text: 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` WHERE (a = (SELECT MAX(score) FROM scores)) GROUP BY field, field2'
  108. values: []
  109. }
  110. '>> where(squel.expr().and(a = ?, 1).and( expr().or(b = ?, 2).or(c = ?, 3) ))':
  111. beforeEach: -> @inst.where(squel.expr().and("a = ?", 1).and(squel.expr().or("b = ?", 2).or("c = ?", 3)))
  112. toString: ->
  113. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` WHERE (a = 1 AND (b = 2 OR c = 3)) GROUP BY field, field2'
  114. toParam: ->
  115. assert.same @inst.toParam(), {
  116. text: 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` WHERE (a = ? AND (b = ? OR c = ?)) GROUP BY field, field2'
  117. values: [1, 2, 3]
  118. }
  119. '>> where(squel.expr().and(a = ?, QueryBuilder).and( expr().or(b = ?, 2).or(c = ?, 3) ))':
  120. beforeEach: ->
  121. subQuery = squel.select().field('field1').from('table1').where('field2 = ?', 10)
  122. @inst.where(squel.expr().and("a = ?", subQuery).and(squel.expr().or("b = ?", 2).or("c = ?", 3)))
  123. toString: ->
  124. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` WHERE (a = (SELECT field1 FROM table1 WHERE (field2 = 10)) AND (b = 2 OR c = 3)) GROUP BY field, field2'
  125. toParam: ->
  126. assert.same @inst.toParam(), {
  127. text: 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` WHERE (a = (SELECT field1 FROM table1 WHERE (field2 = ?)) AND (b = ? OR c = ?)) GROUP BY field, field2'
  128. values: [10, 2, 3]
  129. }
  130. '>> having(squel.expr().and(a = ?, QueryBuilder).and( expr().or(b = ?, 2).or(c = ?, 3) ))':
  131. beforeEach: ->
  132. subQuery = squel.select().field('field1').from('table1').having('field2 = ?', 10)
  133. @inst.having(squel.expr().and("a = ?", subQuery).and(squel.expr().or("b = ?", 2).or("c = ?", 3)))
  134. toString: ->
  135. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` GROUP BY field, field2 HAVING (a = (SELECT field1 FROM table1 HAVING (field2 = 10)) AND (b = 2 OR c = 3))'
  136. toParam: ->
  137. assert.same @inst.toParam(), {
  138. text: 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` GROUP BY field, field2 HAVING (a = (SELECT field1 FROM table1 HAVING (field2 = ?)) AND (b = ? OR c = ?))'
  139. values: [10, 2, 3]
  140. }
  141. '>> where(a = ?, null)':
  142. beforeEach: -> @inst.where('a = ?', null)
  143. toString: ->
  144. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` WHERE (a = NULL) GROUP BY field, field2'
  145. toParam: ->
  146. assert.same @inst.toParam(), {
  147. text: 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` WHERE (a = ?) GROUP BY field, field2'
  148. values: [null]
  149. }
  150. '>> where(a = ?, 1)':
  151. beforeEach: -> @inst.where('a = ?', 1)
  152. toString: ->
  153. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` WHERE (a = 1) GROUP BY field, field2'
  154. toParam: ->
  155. assert.same @inst.toParam(), {
  156. text: 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` WHERE (a = ?) GROUP BY field, field2'
  157. values: [1]
  158. }
  159. '>> join(other_table)':
  160. beforeEach: -> @inst.join('other_table')
  161. toString: ->
  162. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` INNER JOIN other_table WHERE (a = 1) GROUP BY field, field2'
  163. '>> order(a)':
  164. beforeEach: -> @inst.order('a')
  165. toString: ->
  166. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` INNER JOIN other_table WHERE (a = 1) GROUP BY field, field2 ORDER BY a ASC'
  167. '>> order(a, null)':
  168. beforeEach: -> @inst.order('a', null)
  169. toString: ->
  170. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` INNER JOIN other_table WHERE (a = 1) GROUP BY field, field2 ORDER BY a'
  171. '>> order(a, true)':
  172. beforeEach: -> @inst.order('a', true)
  173. toString: ->
  174. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` INNER JOIN other_table WHERE (a = 1) GROUP BY field, field2 ORDER BY a ASC'
  175. '>> limit(2)':
  176. beforeEach: -> @inst.limit(2)
  177. toString: ->
  178. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` INNER JOIN other_table WHERE (a = 1) GROUP BY field, field2 ORDER BY a ASC LIMIT 2'
  179. '>> offset(3)':
  180. beforeEach: -> @inst.offset(3)
  181. toString: ->
  182. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` INNER JOIN other_table WHERE (a = 1) GROUP BY field, field2 ORDER BY a ASC LIMIT 2 OFFSET 3'
  183. '>> order(DIST(?,?), true, 2, 3)':
  184. beforeEach: -> @inst.order('DIST(?, ?)', true, 2, false)
  185. toString: ->
  186. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` INNER JOIN other_table WHERE (a = 1) GROUP BY field, field2 ORDER BY DIST(2, FALSE) ASC'
  187. toParam: ->
  188. assert.same @inst.toParam(), {
  189. text: 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` INNER JOIN other_table WHERE (a = ?) GROUP BY field, field2 ORDER BY DIST(?, ?) ASC'
  190. values: [1, 2, false]
  191. }
  192. '>> order(a)':
  193. beforeEach: -> @inst.order('a')
  194. toString: ->
  195. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` INNER JOIN other_table WHERE (a = 1) GROUP BY field, field2 ORDER BY a ASC'
  196. '>> order(b, null)':
  197. beforeEach: -> @inst.order('b', null)
  198. toString: ->
  199. assert.same @inst.toString(), 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` INNER JOIN other_table WHERE (a = 1) GROUP BY field, field2 ORDER BY b'
  200. '>> join(other_table, condition = expr())':
  201. beforeEach: ->
  202. subQuery = squel.select().field('abc').from('table1').where('adf = ?', 'today1')
  203. subQuery2 = squel.select().field('xyz').from('table2').where('adf = ?', 'today2')
  204. expr = squel.expr().and('field1 = ?', subQuery)
  205. @inst.join('other_table', null, expr)
  206. @inst.where('def IN ?', subQuery2)
  207. toString: ->
  208. assert.same @inst.toString(), "SELECT DISTINCT field1 AS \"fa1\", field2 FROM table, table2 `alias2` INNER JOIN other_table ON (field1 = (SELECT abc FROM table1 WHERE (adf = 'today1'))) WHERE (a = 1) AND (def IN (SELECT xyz FROM table2 WHERE (adf = 'today2'))) GROUP BY field, field2"
  209. toParam: ->
  210. assert.same @inst.toParam(), { text: 'SELECT DISTINCT field1 AS "fa1", field2 FROM table, table2 `alias2` INNER JOIN other_table ON (field1 = (SELECT abc FROM table1 WHERE (adf = ?))) WHERE (a = ?) AND (def IN (SELECT xyz FROM table2 WHERE (adf = ?))) GROUP BY field, field2', values: ["today1",1,"today2"] }
  211. 'nested queries':
  212. 'basic': ->
  213. inner1 = squel.select().from('students')
  214. inner2 = squel.select().from('scores')
  215. @inst.from(inner1).from(inner2, 'scores')
  216. assert.same @inst.toString(), "SELECT * FROM (SELECT * FROM students), (SELECT * FROM scores) `scores`"
  217. 'deep nesting': ->
  218. inner1 = squel.select().from('students')
  219. inner2 = squel.select().from(inner1)
  220. @inst.from(inner2)
  221. assert.same @inst.toString(), "SELECT * FROM (SELECT * FROM (SELECT * FROM students))"
  222. 'nesting in JOINs': ->
  223. inner1 = squel.select().from('students')
  224. inner2 = squel.select().from(inner1)
  225. @inst.from('schools').join(inner2, 'meh', 'meh.ID = ID')
  226. assert.same @inst.toString(), "SELECT * FROM schools INNER JOIN (SELECT * FROM (SELECT * FROM students)) `meh` ON (meh.ID = ID)"
  227. 'nesting in JOINs with params': ->
  228. inner1 = squel.select().from('students').where('age = ?', 6)
  229. inner2 = squel.select().from(inner1)
  230. @inst.from('schools').where('school_type = ?', 'junior').join(inner2, 'meh', 'meh.ID = ID')
  231. assert.same @inst.toString(), "SELECT * FROM schools INNER JOIN (SELECT * FROM (SELECT * FROM students WHERE (age = 6))) `meh` ON (meh.ID = ID) WHERE (school_type = 'junior')"
  232. assert.same @inst.toParam(), { "text": "SELECT * FROM schools INNER JOIN (SELECT * FROM (SELECT * FROM students WHERE (age = ?))) `meh` ON (meh.ID = ID) WHERE (school_type = ?)", "values": [6,'junior'] }
  233. assert.same @inst.toParam({ "numberedParameters": true}), { "text": "SELECT * FROM schools INNER JOIN (SELECT * FROM (SELECT * FROM students WHERE (age = $1))) `meh` ON (meh.ID = ID) WHERE (school_type = $2)", "values": [6,'junior'] }
  234. 'Complex table name, e.g. LATERAL (#230)':
  235. beforeEach: ->
  236. @inst = squel.select().from('foo').from(squel.str('LATERAL(?)', squel.select().from('bar').where('bar.id = ?', 2)), 'ss')
  237. 'toString': ->
  238. assert.same @inst.toString(), 'SELECT * FROM foo, (LATERAL((SELECT * FROM bar WHERE (bar.id = 2)))) `ss`',
  239. 'toParam': ->
  240. assert.same @inst.toParam(), {
  241. text: 'SELECT * FROM foo, (LATERAL((SELECT * FROM bar WHERE (bar.id = ?)))) `ss`'
  242. values: [2]
  243. }
  244. 'cloning':
  245. 'basic': ->
  246. newinst = @inst.from('students').limit(10).clone()
  247. newinst.limit(20)
  248. assert.same 'SELECT * FROM students LIMIT 10', @inst.toString()
  249. assert.same 'SELECT * FROM students LIMIT 20', newinst.toString()
  250. 'with expressions (ticket #120)': ->
  251. expr = squel.expr().and('a = 1')
  252. newinst = @inst.from('table').left_join('table_2', 't', expr)
  253. .clone()
  254. .where('c = 1')
  255. expr.and('b = 2')
  256. assert.same 'SELECT * FROM table LEFT JOIN table_2 `t` ON (a = 1 AND b = 2)', @inst.toString()
  257. assert.same 'SELECT * FROM table LEFT JOIN table_2 `t` ON (a = 1) WHERE (c = 1)', newinst.toString()
  258. 'with sub-queries (ticket #120)': ->
  259. newinst = @inst.from(squel.select().from('students')).limit(30)
  260. .clone()
  261. .where('c = 1')
  262. .limit(35)
  263. assert.same 'SELECT * FROM (SELECT * FROM students) LIMIT 30', @inst.toString()
  264. assert.same 'SELECT * FROM (SELECT * FROM students) WHERE (c = 1) LIMIT 35', newinst.toString()
  265. 'with complex expressions': ->
  266. expr = squel.expr().and(
  267. squel.expr().or('b = 2').or(
  268. squel.expr().and('c = 3').and('d = 4')
  269. )
  270. ).and('a = 1')
  271. newinst = @inst.from('table').left_join('table_2', 't', expr)
  272. .clone()
  273. .where('c = 1')
  274. expr.and('e = 5')
  275. assert.same @inst.toString(), 'SELECT * FROM table LEFT JOIN table_2 `t` ON ((b = 2 OR (c = 3 AND d = 4)) AND a = 1 AND e = 5)'
  276. assert.same newinst.toString(), 'SELECT * FROM table LEFT JOIN table_2 `t` ON ((b = 2 OR (c = 3 AND d = 4)) AND a = 1) WHERE (c = 1)'
  277. 'can specify block separator': ->
  278. assert.same( squel.select({separator: '\n'})
  279. .field('thing')
  280. .from('table')
  281. .toString(), """
  282. SELECT
  283. thing
  284. FROM table
  285. """
  286. )
  287. '#242 - auto-quote table names':
  288. beforeEach: ->
  289. @inst = squel
  290. .select({ autoQuoteTableNames: true })
  291. .field('name')
  292. .where('age > ?', 15)
  293. 'using string':
  294. beforeEach: ->
  295. @inst.from('students', 's')
  296. toString: ->
  297. assert.same @inst.toString(), """
  298. SELECT name FROM `students` `s` WHERE (age > 15)
  299. """
  300. toParam: ->
  301. assert.same @inst.toParam(), {
  302. "text": "SELECT name FROM `students` `s` WHERE (age > ?)"
  303. "values": [15]
  304. }
  305. 'using query builder':
  306. beforeEach: ->
  307. @inst.from(squel.select().from('students'), 's')
  308. toString: ->
  309. assert.same @inst.toString(), """
  310. SELECT name FROM (SELECT * FROM students) `s` WHERE (age > 15)
  311. """
  312. toParam: ->
  313. assert.same @inst.toParam(), {
  314. "text": "SELECT name FROM (SELECT * FROM students) `s` WHERE (age > ?)"
  315. "values": [15]
  316. }
  317. 'UNION JOINs':
  318. 'Two Queries NO Params':
  319. beforeEach: ->
  320. @qry1 = squel.select().field('name').from('students').where('age > 15')
  321. @qry2 = squel.select().field('name').from('students').where('age < 6')
  322. @qry1.union(@qry2)
  323. toString: ->
  324. assert.same @qry1.toString(), """
  325. SELECT name FROM students WHERE (age > 15) UNION (SELECT name FROM students WHERE (age < 6))
  326. """
  327. toParam: ->
  328. assert.same @qry1.toParam(), {
  329. "text": "SELECT name FROM students WHERE (age > 15) UNION (SELECT name FROM students WHERE (age < 6))"
  330. "values": [
  331. ]
  332. }
  333. 'Two Queries with Params':
  334. beforeEach: ->
  335. @qry1 = squel.select().field('name').from('students').where('age > ?', 15)
  336. @qry2 = squel.select().field('name').from('students').where('age < ?', 6)
  337. @qry1.union(@qry2)
  338. toString: ->
  339. assert.same @qry1.toString(), """
  340. SELECT name FROM students WHERE (age > 15) UNION (SELECT name FROM students WHERE (age < 6))
  341. """
  342. toParam: ->
  343. assert.same @qry1.toParam(), {
  344. "text": "SELECT name FROM students WHERE (age > ?) UNION (SELECT name FROM students WHERE (age < ?))"
  345. "values": [
  346. 15
  347. 6
  348. ]
  349. }
  350. 'Three Queries':
  351. beforeEach: ->
  352. @qry1 = squel.select().field('name').from('students').where('age > ?', 15)
  353. @qry2 = squel.select().field('name').from('students').where('age < 6')
  354. @qry3 = squel.select().field('name').from('students').where('age = ?', 8)
  355. @qry1.union(@qry2)
  356. @qry1.union(@qry3)
  357. toParam: ->
  358. assert.same @qry1.toParam(), {
  359. "text": "SELECT name FROM students WHERE (age > ?) UNION (SELECT name FROM students WHERE (age < 6)) UNION (SELECT name FROM students WHERE (age = ?))"
  360. "values": [
  361. 15
  362. 8
  363. ]
  364. }
  365. 'toParam(2)': ->
  366. assert.same @qry1.toParam({ "numberedParameters": true, "numberedParametersStartAt": 2}), {
  367. "text": "SELECT name FROM students WHERE (age > $2) UNION (SELECT name FROM students WHERE (age < 6)) UNION (SELECT name FROM students WHERE (age = $3))"
  368. "values": [
  369. 15
  370. 8
  371. ]
  372. }
  373. 'Multi-Parameter Query':
  374. beforeEach: ->
  375. @qry1 = squel.select().field('name').from('students').where('age > ?', 15)
  376. @qry2 = squel.select().field('name').from('students').where('age < ?', 6)
  377. @qry3 = squel.select().field('name').from('students').where('age = ?', 8)
  378. @qry4 = squel.select().field('name').from('students').where('age IN [?, ?]', 2, 10)
  379. @qry1.union(@qry2)
  380. @qry1.union(@qry3)
  381. @qry4.union_all(@qry1)
  382. toString: ->
  383. assert.same @qry4.toString(), """
  384. SELECT name FROM students WHERE (age IN [2, 10]) UNION ALL (SELECT name FROM students WHERE (age > 15) UNION (SELECT name FROM students WHERE (age < 6)) UNION (SELECT name FROM students WHERE (age = 8)))
  385. """
  386. toParam: ->
  387. assert.same @qry4.toParam({ "numberedParameters": true}), {
  388. "text": "SELECT name FROM students WHERE (age IN [$1, $2]) UNION ALL (SELECT name FROM students WHERE (age > $3) UNION (SELECT name FROM students WHERE (age < $4)) UNION (SELECT name FROM students WHERE (age = $5)))"
  389. "values": [
  390. 2
  391. 10
  392. 15
  393. 6
  394. 8
  395. ]
  396. }
  397. module?.exports[require('path').basename(__filename)] = test