2014-09-01 17:18:45 +02:00
/*global describe, it, d*/
'use strict' ;
2013-09-12 01:00:44 -04:00
module . exports = function ( knex ) {
2013-09-12 13:30:47 -04:00
describe ( 'Joins' , function ( ) {
2013-09-12 01:00:44 -04:00
it ( 'uses inner join by default' , function ( ) {
return knex ( 'accounts' )
. join ( 'test_table_two' , 'accounts.id' , '=' , 'test_table_two.account_id' )
2013-12-27 14:44:21 -05:00
. select ( 'accounts.*' , 'test_table_two.details' )
2014-08-21 23:39:12 +02:00
. orderBy ( 'accounts.id' )
2013-12-27 14:44:21 -05:00
. testSql ( function ( tester ) {
tester (
'mysql' ,
2014-08-21 23:39:12 +02:00
'select `accounts`.*, `test_table_two`.`details` from `accounts` inner join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id` order by `accounts`.`id` asc' , [ ] , [ {
2013-12-27 14:44:21 -05:00
id : 1 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : 2 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test2@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : 3 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test3@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : ''
} ]
) ;
tester (
'postgresql' ,
2014-08-21 23:39:12 +02:00
'select "accounts".*, "test_table_two"."details" from "accounts" inner join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" order by "accounts"."id" asc' , [ ] , [ {
2013-12-27 14:44:21 -05:00
id : '1' ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : '2' ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test2@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : '3' ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test3@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : ''
} ]
) ;
tester (
'sqlite3' ,
2014-08-21 23:39:12 +02:00
'select "accounts".*, "test_table_two"."details" from "accounts" inner join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" order by "accounts"."id" asc' , [ ] , [ {
2013-12-27 14:44:21 -05:00
id : 1 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : 2 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test2@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : 3 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test3@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : ''
} ]
) ;
2014-08-21 23:39:12 +02:00
tester (
'oracle' ,
"select \"accounts\".*, \"test_table_two\".\"details\" from \"accounts\" inner join \"test_table_two\" on \"accounts\".\"id\" = \"test_table_two\".\"account_id\" order by \"accounts\".\"id\" asc" , [ ] , [ {
id : 1 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : 2 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test2@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : 3 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test3@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : null // Oracle implicitly converted '' to NULL
} ]
) ;
2013-12-27 14:44:21 -05:00
} ) ;
2014-08-21 23:39:12 +02:00
2013-09-12 01:00:44 -04:00
} ) ;
2013-12-27 14:44:21 -05:00
it ( 'has a leftJoin method parameter to specify the join type' , function ( ) {
2013-09-12 01:00:44 -04:00
return knex ( 'accounts' )
2013-12-27 14:44:21 -05:00
. leftJoin ( 'test_table_two' , 'accounts.id' , '=' , 'test_table_two.account_id' )
. select ( 'accounts.*' , 'test_table_two.details' )
2014-08-21 23:39:12 +02:00
. orderBy ( 'accounts.id' )
2013-12-27 14:44:21 -05:00
. testSql ( function ( tester ) {
tester (
'mysql' ,
2014-08-21 23:39:12 +02:00
'select `accounts`.*, `test_table_two`.`details` from `accounts` left join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id` order by `accounts`.`id` asc' , [ ] , [ {
2013-12-27 14:44:21 -05:00
id : 1 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : 2 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test2@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : 3 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test3@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : ''
} , {
id : 4 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test4@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : null
} , {
id : 5 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test5@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : null
} , {
id : 7 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test6@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : null
} ]
) ;
tester (
'postgresql' ,
2014-08-21 23:39:12 +02:00
'select "accounts".*, "test_table_two"."details" from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" order by "accounts"."id" asc' , [ ] , [ {
2013-12-27 14:44:21 -05:00
id : '1' ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : '2' ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test2@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : '3' ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test3@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : ''
} , {
2014-08-21 23:39:12 +02:00
id : '4' ,
2013-12-27 14:44:21 -05:00
first _name : 'Test' ,
last _name : 'User' ,
2014-08-21 23:39:12 +02:00
email : 'test4@example.com' ,
2013-12-27 14:44:21 -05:00
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : null
} , {
2014-08-21 23:39:12 +02:00
id : '5' ,
2013-12-27 14:44:21 -05:00
first _name : 'Test' ,
last _name : 'User' ,
2014-08-21 23:39:12 +02:00
email : 'test5@example.com' ,
2013-12-27 14:44:21 -05:00
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : null
} , {
id : '7' ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test6@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : null
} ]
) ;
tester (
'sqlite3' ,
2014-08-21 23:39:12 +02:00
'select "accounts".*, "test_table_two"."details" from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" order by "accounts"."id" asc' , [ ] , [ {
2013-12-27 14:44:21 -05:00
id : 1 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : 2 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test2@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : 3 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test3@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : ''
} , {
id : 4 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test4@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : null
} , {
id : 5 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test5@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : null
} , {
id : 6 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test6@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : null
} ]
) ;
2014-08-21 23:39:12 +02:00
tester (
'oracle' ,
"select \"accounts\".*, \"test_table_two\".\"details\" from \"accounts\" left join \"test_table_two\" on \"accounts\".\"id\" = \"test_table_two\".\"account_id\" order by \"accounts\".\"id\" asc" , [ ] , [ {
id : 1 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : 2 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test2@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
} , {
id : 3 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test3@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : null // Oracle implicitly converted '' to NULL
} , {
id : 4 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test4@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : null
} , {
id : 5 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test5@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : null
} , {
id : 7 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test6@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
details : null
} ]
) ;
2013-12-27 14:44:21 -05:00
} ) ;
2013-09-12 01:00:44 -04:00
} ) ;
it ( 'accepts a callback as the second argument for advanced joins' , function ( ) {
2013-12-27 14:44:21 -05:00
return knex ( 'accounts' ) . leftJoin ( 'test_table_two' , function ( join ) {
2013-09-12 01:00:44 -04:00
join . on ( 'accounts.id' , '=' , 'test_table_two.account_id' ) ;
join . orOn ( 'accounts.email' , '=' , 'test_table_two.details' ) ;
2013-12-27 14:44:21 -05:00
} )
. select ( )
2014-08-21 23:39:12 +02:00
. orderBy ( 'accounts.id' )
2013-12-27 14:44:21 -05:00
. testSql ( function ( tester ) {
tester (
'mysql' ,
2014-08-21 23:39:12 +02:00
'select * from `accounts` left join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id` or `accounts`.`email` = `test_table_two`.`details` order by `accounts`.`id` asc' , [ ] , [ {
2013-12-27 14:44:21 -05:00
id : 1 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : 1 ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.' ,
status : 0 ,
json _data : null
} , {
id : 2 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test2@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : 2 ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.' ,
status : 1 ,
json _data : null
} , {
id : 3 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test3@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : 3 ,
details : '' ,
status : 1 ,
json _data : null
} , {
id : null ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test4@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : null ,
details : null ,
status : null ,
json _data : null
} , {
id : null ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test5@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : null ,
details : null ,
status : null ,
json _data : null
} , {
id : null ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test6@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : null ,
details : null ,
status : null ,
json _data : null
} ]
) ;
tester (
'postgresql' ,
2014-08-21 23:39:12 +02:00
'select * from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" or "accounts"."email" = "test_table_two"."details" order by "accounts"."id" asc' , [ ] , [ {
2013-12-27 14:44:21 -05:00
id : 1 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : 1 ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.' ,
status : 0 ,
json _data : null
} , {
id : 2 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test2@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : 2 ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.' ,
status : 1 ,
json _data : null
} , {
id : 3 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test3@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : 3 ,
details : '' ,
status : 1 ,
json _data : null
} , {
id : null ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test4@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : null ,
details : null ,
status : null ,
json _data : null
} , {
id : null ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test5@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : null ,
details : null ,
status : null ,
json _data : null
} , {
id : null ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test6@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : null ,
details : null ,
status : null ,
json _data : null
} ]
) ;
tester (
'sqlite3' ,
2014-08-21 23:39:12 +02:00
'select * from "accounts" left join "test_table_two" on "accounts"."id" = "test_table_two"."account_id" or "accounts"."email" = "test_table_two"."details" order by "accounts"."id" asc' , [ ] , [ {
2013-12-27 14:44:21 -05:00
id : 1 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : 1 ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.' ,
status : 0 ,
json _data : null
} , {
id : 2 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test2@example.com' ,
logins : 1 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : 2 ,
details : 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.' ,
status : 1 ,
json _data : null
} , {
id : 3 ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test3@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : 3 ,
details : '' ,
status : 1 ,
json _data : null
} , {
id : null ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test4@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : null ,
details : null ,
status : null ,
json _data : null
} , {
id : null ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test5@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : null ,
details : null ,
status : null ,
json _data : null
} , {
id : null ,
first _name : 'Test' ,
last _name : 'User' ,
email : 'test6@example.com' ,
logins : 2 ,
about : 'Lorem ipsum Dolore labore incididunt enim.' ,
created _at : d ,
updated _at : d ,
phone : null ,
account _id : null ,
details : null ,
status : null ,
json _data : null
} ]
) ;
} ) ;
2013-09-12 01:00:44 -04:00
} ) ;
it ( 'supports join aliases' , function ( ) {
//Expected output: all pairs of account emails, excluding pairs where the emails are the same.
return knex ( 'accounts' )
2013-12-27 14:44:21 -05:00
. join ( 'accounts as a2' , 'a2.email' , '<>' , 'accounts.email' )
. select ( [ 'accounts.email as e1' , 'a2.email as e2' ] )
. where ( 'a2.email' , 'test2@example.com' )
2014-06-03 02:35:55 -04:00
. orderBy ( 'e1' )
2013-12-27 14:44:21 -05:00
. limit ( 5 )
. testSql ( function ( tester ) {
tester (
'mysql' ,
2014-06-03 02:35:55 -04:00
'select `accounts`.`email` as `e1`, `a2`.`email` as `e2` from `accounts` inner join `accounts` as `a2` on `a2`.`email` <> `accounts`.`email` where `a2`.`email` = ? order by `e1` asc limit ?' ,
2014-06-03 02:23:20 -04:00
[ 'test2@example.com' , 5 ] ,
[ {
2013-12-27 14:44:21 -05:00
e1 : 'test3@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test4@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test5@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test6@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test@example.com' ,
e2 : 'test2@example.com'
} ]
) ;
tester (
'postgresql' ,
2014-06-03 02:35:55 -04:00
'select "accounts"."email" as "e1", "a2"."email" as "e2" from "accounts" inner join "accounts" as "a2" on "a2"."email" <> "accounts"."email" where "a2"."email" = ? order by "e1" asc limit ?' ,
2013-12-27 14:44:21 -05:00
[ 'test2@example.com' , 5 ] ,
[ {
e1 : 'test3@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test4@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test5@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test6@example.com' ,
e2 : 'test2@example.com'
2014-06-03 02:35:55 -04:00
} , {
e1 : 'test@example.com' ,
e2 : 'test2@example.com'
2013-12-27 14:44:21 -05:00
} ]
) ;
tester (
'sqlite3' ,
2014-07-26 09:06:48 +02:00
'select "accounts"."email" as "e1", "a2"."email" as "e2" from "accounts" inner join "accounts" as "a2" on "a2"."email" <> "accounts"."email" where "a2"."email" = ? order by "e1" asc limit ?' ,
2013-12-27 14:44:21 -05:00
[ 'test2@example.com' , 5 ] ,
[ {
e1 : 'test3@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test4@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test5@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test6@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test@example.com' ,
e2 : 'test2@example.com'
} ]
) ;
2014-08-21 23:39:12 +02:00
tester (
'oracle' ,
"select * from (select \"accounts\".\"email\" \"e1\", \"a2\".\"email\" \"e2\" from \"accounts\" inner join \"accounts\" \"a2\" on \"a2\".\"email\" <> \"accounts\".\"email\" where \"a2\".\"email\" = ? order by \"e1\" asc) where rownum <= ?" ,
[ 'test2@example.com' , 5 ] ,
[ {
e1 : 'test3@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test4@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test5@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test6@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test@example.com' ,
e2 : 'test2@example.com'
} ]
) ;
2013-12-27 14:44:21 -05:00
} ) ;
2013-09-12 01:00:44 -04:00
} ) ;
it ( 'supports join aliases with advanced joins' , function ( ) {
//Expected output: all pairs of account emails, excluding pairs where the emails are the same.
//But also include the case where the emails are the same, for account 2.
return knex ( 'accounts' )
2013-12-27 14:44:21 -05:00
. join ( 'accounts as a2' , function ( ) {
this . on ( 'accounts.email' , '<>' , 'a2.email' ) . orOn ( 'accounts.id' , '=' , 2 ) ;
} )
. where ( 'a2.email' , 'test2@example.com' )
. select ( [ 'accounts.email as e1' , 'a2.email as e2' ] )
. limit ( 5 )
2014-08-21 23:39:12 +02:00
. orderBy ( 'e1' )
2013-12-27 14:44:21 -05:00
. testSql ( function ( tester ) {
tester (
'mysql' ,
2014-08-21 23:39:12 +02:00
'select `accounts`.`email` as `e1`, `a2`.`email` as `e2` from `accounts` inner join `accounts` as `a2` on `accounts`.`email` <> `a2`.`email` or `accounts`.`id` = 2 where `a2`.`email` = ? order by `e1` asc limit ?' ,
2013-12-27 14:44:21 -05:00
[ 'test2@example.com' , 5 ] ,
[ {
e1 : 'test2@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test3@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test4@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test5@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test6@example.com' ,
e2 : 'test2@example.com'
} ]
) ;
tester (
'postgresql' ,
2014-08-21 23:39:12 +02:00
'select "accounts"."email" as "e1", "a2"."email" as "e2" from "accounts" inner join "accounts" as "a2" on "accounts"."email" <> "a2"."email" or "accounts"."id" = 2 where "a2"."email" = ? order by "e1" asc limit ?' ,
2013-12-27 14:44:21 -05:00
[ 'test2@example.com' , 5 ] ,
[ {
2014-08-21 23:39:12 +02:00
e1 : 'test2@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test3@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test4@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test5@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test6@example.com' ,
e2 : 'test2@example.com'
2013-12-27 14:44:21 -05:00
} ]
) ;
tester (
'sqlite3' ,
2014-08-21 23:39:12 +02:00
'select "accounts"."email" as "e1", "a2"."email" as "e2" from "accounts" inner join "accounts" as "a2" on "accounts"."email" <> "a2"."email" or "accounts"."id" = 2 where "a2"."email" = ? order by "e1" asc limit ?' ,
2013-12-27 14:44:21 -05:00
[ 'test2@example.com' , 5 ] ,
[ {
e1 : 'test2@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test3@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test4@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test5@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test6@example.com' ,
e2 : 'test2@example.com'
2014-08-21 23:39:12 +02:00
} ]
) ;
tester (
'oracle' ,
"select * from (select \"accounts\".\"email\" \"e1\", \"a2\".\"email\" \"e2\" from \"accounts\" inner join \"accounts\" \"a2\" on \"accounts\".\"email\" <> \"a2\".\"email\" or \"accounts\".\"id\" = 2 where \"a2\".\"email\" = ? order by \"e1\" asc) where rownum <= ?" ,
[ 'test2@example.com' , 5 ] ,
[ {
e1 : 'test2@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test3@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test4@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test5@example.com' ,
e2 : 'test2@example.com'
} , {
e1 : 'test6@example.com' ,
e2 : 'test2@example.com'
2013-12-27 14:44:21 -05:00
} ]
) ;
} ) ;
2013-09-12 01:00:44 -04:00
} ) ;
2014-09-29 10:05:53 +02:00
it ( 'supports cross join without arguments' , function ( ) {
return knex . select ( 'account_id' ) . from ( 'accounts' ) . crossJoin ( 'test_table_two' ) . orderBy ( 'account_id' ) . testSql ( function ( tester ) {
tester (
'mysql' ,
'select `account_id` from `accounts` cross join `test_table_two` order by `account_id` asc' ,
[ ] ,
function ( res ) {
return res . length === 30 ;
}
) ;
tester (
'postgresql' ,
'select "account_id" from "accounts" cross join "test_table_two" order by "account_id" asc' ,
[ ] ,
function ( res ) {
return res . length === 30 ;
}
) ;
tester (
'oracle' ,
'select "account_id" from "accounts" cross join "test_table_two" order by "account_id" asc' ,
[ ] ,
function ( res ) {
return res . length === 30 ;
}
) ;
tester (
'sqlite3' ,
'select "account_id" from "accounts" cross join "test_table_two" order by "account_id" asc' ,
[ ] ,
function ( res ) {
return res . length === 30 ;
}
) ;
} ) ;
} ) ;
2013-09-13 11:40:51 -04:00
it ( 'supports joins with overlapping column names' , function ( ) {
2014-08-11 12:25:39 +02:00
if ( knex . client . dialect === 'oracle' ) {
console . warn ( "Overlapping column names not supported with oracle" ) ;
2014-08-11 14:11:09 +02:00
return ;
}
return knex ( 'accounts as a1' )
. leftJoin ( 'accounts as a2' , function ( ) {
this . on ( 'a1.email' , '<>' , 'a2.email' ) ;
} )
. select ( [ 'a1.email' , 'a2.email' ] )
. where ( knex . raw ( 'a1.id = 1' ) )
. options ( {
nestTables : true ,
rowMode : 'array'
} )
. limit ( 2 )
. testSql ( function ( tester ) {
tester (
'mysql' ,
'select `a1`.`email`, `a2`.`email` from `accounts` as `a1` left join `accounts` as `a2` on `a1`.`email` <> `a2`.`email` where a1.id = 1 limit ?' ,
[ 2 ] ,
[ {
a1 : {
email : 'test@example.com'
} ,
a2 : {
2013-12-27 14:44:21 -05:00
email : 'test2@example.com'
2014-08-11 14:11:09 +02:00
}
} , {
a1 : {
email : 'test@example.com'
} ,
a2 : {
2013-12-27 14:44:21 -05:00
email : 'test3@example.com'
2014-08-11 14:11:09 +02:00
}
} ]
) ;
tester (
'postgres' ,
'select "a1"."email", "a2"."email" from "accounts" as "a1" left join "accounts" as "a2" on "a1"."email" <> "a2"."email" where a1.id = 1 limit ?' ,
[ 2 ] ,
[ {
0 : 'test@example.com' ,
1 : 'test2@example.com'
} , {
0 : 'test@example.com' ,
1 : 'test3@example.com'
} ]
) ;
tester (
'sqlite3' ,
'select "a1"."email", "a2"."email" from "accounts" as "a1" left join "accounts" as "a2" on "a1"."email" <> "a2"."email" where a1.id = 1 limit ?' ,
[ 2 ] ,
[ {
email : 'test2@example.com'
} , {
email : 'test3@example.com'
} ]
) ;
2013-12-27 14:44:21 -05:00
} ) ;
2013-09-13 11:40:51 -04:00
} ) ;
2013-09-12 01:00:44 -04:00
} ) ;
2014-08-11 14:11:09 +02:00
} ;