123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- var Test = require('../../support/test-runner');
- describe('Query Generation ::', function() {
- describe('JOINS ::', function() {
- it('should generate a basic join query', function(done) {
- Test({
- query: {
- select: ['users.id', 'contacts.phone'],
- from: 'users',
- join: [
- {
- from: 'contacts',
- on: {
- users: 'id',
- contacts: 'user_id'
- }
- }
- ]
- },
- outcomes: [
- {
- dialect: 'postgresql',
- sql: 'select "users"."id", "contacts"."phone" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id"',
- bindings: []
- },
- {
- dialect: 'mysql',
- sql: 'select `users`.`id`, `contacts`.`phone` from `users` inner join `contacts` on `users`.`id` = `contacts`.`user_id`',
- bindings: []
- },
- {
- dialect: 'sqlite3',
- sql: 'select "users"."id", "contacts"."phone" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id"',
- bindings: []
- },
- {
- dialect: 'oracle',
- sql: 'select "users"."id", "contacts"."phone" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id"',
- bindings: []
- },
- {
- dialect: 'mariadb',
- sql: 'select `users`.`id`, `contacts`.`phone` from `users` inner join `contacts` on `users`.`id` = `contacts`.`user_id`',
- bindings: []
- }
- ]
- }, done);
- });
- it('should be able to contain multiple joins', function(done) {
- Test({
- query: {
- select: ['users.id', 'contacts.phone', 'carriers.name'],
- from: 'users',
- join: [
- {
- from: 'contacts',
- on: {
- users: 'id',
- contacts: 'user_id'
- }
- },
- {
- from: 'carriers',
- on: {
- users: 'id',
- carriers: 'user_id'
- }
- }
- ]
- },
- outcomes: [
- {
- dialect: 'postgresql',
- sql: 'select "users"."id", "contacts"."phone", "carriers"."name" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id" inner join "carriers" on "users"."id" = "carriers"."user_id"',
- bindings: []
- },
- {
- dialect: 'mysql',
- sql: 'select `users`.`id`, `contacts`.`phone`, `carriers`.`name` from `users` inner join `contacts` on `users`.`id` = `contacts`.`user_id` inner join `carriers` on `users`.`id` = `carriers`.`user_id`',
- bindings: []
- },
- {
- dialect: 'sqlite3',
- sql: 'select "users"."id", "contacts"."phone", "carriers"."name" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id" inner join "carriers" on "users"."id" = "carriers"."user_id"',
- bindings: []
- },
- {
- dialect: 'oracle',
- sql: 'select "users"."id", "contacts"."phone", "carriers"."name" from "users" inner join "contacts" on "users"."id" = "contacts"."user_id" inner join "carriers" on "users"."id" = "carriers"."user_id"',
- bindings: []
- },
- {
- dialect: 'mariadb',
- sql: 'select `users`.`id`, `contacts`.`phone`, `carriers`.`name` from `users` inner join `contacts` on `users`.`id` = `contacts`.`user_id` inner join `carriers` on `users`.`id` = `carriers`.`user_id`',
- bindings: []
- }
- ]
- }, done);
- });
- it('should be able to group joins', function(done) {
- Test({
- query: {
- select: ['*'],
- from: 'users',
- join: [
- {
- from: 'accounts',
- on: {
- or: [
- {
- accounts: 'id',
- users: 'account_id'
- },
- {
- accounts: 'owner_id',
- users: 'id'
- }
- ]
- }
- }
- ]
- },
- outcomes: [
- {
- dialect: 'postgresql',
- sql: 'select * from "users" inner join "accounts" on "accounts"."id" = "users"."account_id" or "accounts"."owner_id" = "users"."id"',
- bindings: []
- },
- {
- dialect: 'mysql',
- sql: 'select * from `users` inner join `accounts` on `accounts`.`id` = `users`.`account_id` or `accounts`.`owner_id` = `users`.`id`',
- bindings: []
- },
- {
- dialect: 'sqlite3',
- sql: 'select * from "users" inner join "accounts" on "accounts"."id" = "users"."account_id" or "accounts"."owner_id" = "users"."id"',
- bindings: []
- },
- {
- dialect: 'oracle',
- sql: 'select * from "users" inner join "accounts" on "accounts"."id" = "users"."account_id" or "accounts"."owner_id" = "users"."id"',
- bindings: []
- },
- {
- dialect: 'mariadb',
- sql: 'select * from `users` inner join `accounts` on `accounts`.`id` = `users`.`account_id` or `accounts`.`owner_id` = `users`.`id`',
- bindings: []
- }
- ]
- }, done);
- });
- });
- });
|