123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- var Test = require('../../support/test-runner');
- describe('Query Generation ::', function() {
- describe('Subqueries', function() {
- describe('used as scalar values', function() {
- it('should generate a valid query when used inside a SELECT', function(done) {
- Test({
- query: {
- select: ['name', {
- select: ['username'],
- from: 'users',
- where: {
- or: [
- { status: 'active' },
- { name: 'John' }
- ]
- },
- as: 'username'
- }, 'age'],
- from: 'accounts'
- },
- outcomes: [
- {
- dialect: 'postgresql',
- sql: 'select "name", (select "username" from "users" where "status" = $1 or "name" = $2) as "username", "age" from "accounts"',
- bindings: ['active', 'John']
- },
- {
- dialect: 'mysql',
- sql: 'select `name`, (select `username` from `users` where `status` = ? or `name` = ?) as `username`, `age` from `accounts`',
- bindings: ['active', 'John']
- },
- {
- dialect: 'sqlite3',
- sql: 'select "name", (select "username" from "users" where "status" = ? or "name" = ?) as "username", "age" from "accounts"',
- bindings: ['active', 'John']
- },
- {
- dialect: 'oracle',
- sql: 'select "name", (select "username" from "users" where "status" = :1 or "name" = :2) "username", "age" from "accounts"',
- bindings: ['active', 'John']
- },
- {
- dialect: 'mariadb',
- sql: 'select `name`, (select `username` from `users` where `status` = ? or `name` = ?) as `username`, `age` from `accounts`',
- bindings: ['active', 'John']
- }
- ]
- }, done);
- });
- it('should generate a valid query when used as a value in a WHERE', function(done) {
- Test({
- query: {
- select: ['name', 'age'],
- from: 'accounts',
- where: {
- and: [
- {
- username: {
- select: ['username'],
- from: 'users',
- where: {
- color: 'accounts.color'
- }
- }
- }
- ]
- }
- },
- outcomes: [
- {
- dialect: 'postgresql',
- sql: 'select "name", "age" from "accounts" where "username" = (select "username" from "users" where "color" = $1)',
- bindings: ['accounts.color']
- },
- {
- dialect: 'mysql',
- sql: 'select `name`, `age` from `accounts` where `username` = (select `username` from `users` where `color` = ?)',
- bindings: ['accounts.color']
- },
- {
- dialect: 'sqlite3',
- sql: 'select "name", "age" from "accounts" where "username" = (select "username" from "users" where "color" = ?)',
- bindings: ['accounts.color']
- },
- {
- dialect: 'oracle',
- sql: 'select "name", "age" from "accounts" where "username" = (select "username" from "users" where "color" = :1)',
- bindings: ['accounts.color']
- },
- {
- dialect: 'mariadb',
- sql: 'select `name`, `age` from `accounts` where `username` = (select `username` from `users` where `color` = ?)',
- bindings: ['accounts.color']
- }
- ]
- }, done);
- });
- });
- });
- });
|