subquery.scalar.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var Test = require('../../support/test-runner');
  2. describe('Query Generation ::', function() {
  3. describe('Subqueries', function() {
  4. describe('used as scalar values', function() {
  5. it('should generate a valid query when used inside a SELECT', function(done) {
  6. Test({
  7. query: {
  8. select: ['name', {
  9. select: ['username'],
  10. from: 'users',
  11. where: {
  12. or: [
  13. { status: 'active' },
  14. { name: 'John' }
  15. ]
  16. },
  17. as: 'username'
  18. }, 'age'],
  19. from: 'accounts'
  20. },
  21. outcomes: [
  22. {
  23. dialect: 'postgresql',
  24. sql: 'select "name", (select "username" from "users" where "status" = $1 or "name" = $2) as "username", "age" from "accounts"',
  25. bindings: ['active', 'John']
  26. },
  27. {
  28. dialect: 'mysql',
  29. sql: 'select `name`, (select `username` from `users` where `status` = ? or `name` = ?) as `username`, `age` from `accounts`',
  30. bindings: ['active', 'John']
  31. },
  32. {
  33. dialect: 'sqlite3',
  34. sql: 'select "name", (select "username" from "users" where "status" = ? or "name" = ?) as "username", "age" from "accounts"',
  35. bindings: ['active', 'John']
  36. },
  37. {
  38. dialect: 'oracle',
  39. sql: 'select "name", (select "username" from "users" where "status" = :1 or "name" = :2) "username", "age" from "accounts"',
  40. bindings: ['active', 'John']
  41. },
  42. {
  43. dialect: 'mariadb',
  44. sql: 'select `name`, (select `username` from `users` where `status` = ? or `name` = ?) as `username`, `age` from `accounts`',
  45. bindings: ['active', 'John']
  46. }
  47. ]
  48. }, done);
  49. });
  50. it('should generate a valid query when used as a value in a WHERE', function(done) {
  51. Test({
  52. query: {
  53. select: ['name', 'age'],
  54. from: 'accounts',
  55. where: {
  56. and: [
  57. {
  58. username: {
  59. select: ['username'],
  60. from: 'users',
  61. where: {
  62. color: 'accounts.color'
  63. }
  64. }
  65. }
  66. ]
  67. }
  68. },
  69. outcomes: [
  70. {
  71. dialect: 'postgresql',
  72. sql: 'select "name", "age" from "accounts" where "username" = (select "username" from "users" where "color" = $1)',
  73. bindings: ['accounts.color']
  74. },
  75. {
  76. dialect: 'mysql',
  77. sql: 'select `name`, `age` from `accounts` where `username` = (select `username` from `users` where `color` = ?)',
  78. bindings: ['accounts.color']
  79. },
  80. {
  81. dialect: 'sqlite3',
  82. sql: 'select "name", "age" from "accounts" where "username" = (select "username" from "users" where "color" = ?)',
  83. bindings: ['accounts.color']
  84. },
  85. {
  86. dialect: 'oracle',
  87. sql: 'select "name", "age" from "accounts" where "username" = (select "username" from "users" where "color" = :1)',
  88. bindings: ['accounts.color']
  89. },
  90. {
  91. dialect: 'mariadb',
  92. sql: 'select `name`, `age` from `accounts` where `username` = (select `username` from `users` where `color` = ?)',
  93. bindings: ['accounts.color']
  94. }
  95. ]
  96. }, done);
  97. });
  98. });
  99. });
  100. });