2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
2013-09-12 01:00:44 -04:00
|
|
|
|
2019-03-03 13:57:40 -06:00
|
|
|
const expect = require('chai').expect;
|
2021-03-08 07:16:07 -05:00
|
|
|
const { isMssql, isOracle, isPgBased, isSQLite } = require('../../util/db-helpers');
|
2019-03-03 13:57:40 -06:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
module.exports = function (knex) {
|
|
|
|
describe('unions', function () {
|
|
|
|
it('handles unions with a callback', function () {
|
2013-09-12 01:00:44 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.select('*')
|
|
|
|
.where('id', '=', 1)
|
2020-04-19 00:40:23 +02:00
|
|
|
.union(function () {
|
|
|
|
this.select('*').from('accounts').where('id', 2);
|
2013-09-12 01:00:44 -04:00
|
|
|
});
|
|
|
|
});
|
2018-08-03 15:11:38 -04:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('handles unions with an array of callbacks', function () {
|
2018-08-03 15:11:38 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.select('*')
|
|
|
|
.where('id', '=', 1)
|
|
|
|
.union([
|
2020-04-19 00:40:23 +02:00
|
|
|
function () {
|
|
|
|
this.select('*').from('accounts').where('id', 2);
|
2018-08-03 15:11:38 -04:00
|
|
|
},
|
2020-04-19 00:40:23 +02:00
|
|
|
function () {
|
|
|
|
this.select('*').from('accounts').where('id', 3);
|
2018-08-03 15:11:38 -04:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('handles unions with a list of callbacks', function () {
|
2018-08-03 15:11:38 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.select('*')
|
|
|
|
.where('id', '=', 1)
|
|
|
|
.union(
|
2020-04-19 00:40:23 +02:00
|
|
|
function () {
|
|
|
|
this.select('*').from('accounts').where('id', 2);
|
2018-08-03 15:11:38 -04:00
|
|
|
},
|
2020-04-19 00:40:23 +02:00
|
|
|
function () {
|
|
|
|
this.select('*').from('accounts').where('id', 3);
|
2018-08-03 15:11:38 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('handles unions with an array of builders', function () {
|
2018-08-03 15:11:38 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.select('*')
|
|
|
|
.where('id', '=', 1)
|
|
|
|
.union([
|
2020-04-19 00:40:23 +02:00
|
|
|
knex.select('*').from('accounts').where('id', 2),
|
|
|
|
knex.select('*').from('accounts').where('id', 3),
|
2018-08-03 15:11:38 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('handles unions with a list of builders', function () {
|
2018-08-03 15:11:38 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.select('*')
|
|
|
|
.where('id', '=', 1)
|
|
|
|
.union(
|
2020-04-19 00:40:23 +02:00
|
|
|
knex.select('*').from('accounts').where('id', 2),
|
|
|
|
knex.select('*').from('accounts').where('id', 3)
|
2018-08-03 15:11:38 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('handles unions with a raw query', function () {
|
2018-08-03 15:11:38 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.select('*')
|
|
|
|
.where('id', '=', 1)
|
2018-11-23 19:58:23 +08:00
|
|
|
.union(
|
|
|
|
knex.raw('select * from ?? where ?? = ?', ['accounts', 'id', 2])
|
|
|
|
);
|
2018-08-03 15:11:38 -04:00
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('handles unions with an array raw queries', function () {
|
2018-08-03 15:11:38 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.select('*')
|
|
|
|
.where('id', '=', 1)
|
|
|
|
.union([
|
2018-11-23 19:58:23 +08:00
|
|
|
knex.raw('select * from ?? where ?? = ?', ['accounts', 'id', 2]),
|
|
|
|
knex.raw('select * from ?? where ?? = ?', ['accounts', 'id', 3]),
|
2018-08-03 15:11:38 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('handles unions with a list of raw queries', function () {
|
2018-08-03 15:11:38 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.select('*')
|
|
|
|
.where('id', '=', 1)
|
|
|
|
.union(
|
2018-11-23 19:58:23 +08:00
|
|
|
knex.raw('select * from ?? where ?? = ?', ['accounts', 'id', 2]),
|
|
|
|
knex.raw('select * from ?? where ?? = ?', ['accounts', 'id', 3])
|
2018-08-03 15:11:38 -04:00
|
|
|
);
|
|
|
|
});
|
2013-09-12 01:00:44 -04:00
|
|
|
});
|
2019-03-03 13:57:40 -06:00
|
|
|
|
2021-03-08 07:16:07 -05:00
|
|
|
if (isPgBased(knex) || isMssql(knex) || isOracle(knex) || isSQLite(knex)) {
|
2020-04-19 00:40:23 +02:00
|
|
|
describe('intersects', function () {
|
|
|
|
before(function () {
|
|
|
|
return knex.schema.createTable('intersect_test', function (t) {
|
2019-03-03 13:57:40 -06:00
|
|
|
t.integer('id');
|
|
|
|
t.integer('test_col_1');
|
|
|
|
t.integer('test_col_2');
|
|
|
|
t.integer('test_col_3');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
beforeEach(function () {
|
2019-03-03 13:57:40 -06:00
|
|
|
return knex('intersect_test').insert([
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
test_col_1: 1,
|
|
|
|
test_col_2: 2,
|
|
|
|
test_col_3: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 2,
|
|
|
|
test_col_1: 2,
|
|
|
|
test_col_2: 3,
|
|
|
|
test_col_3: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 3,
|
|
|
|
test_col_1: 2,
|
|
|
|
test_col_2: 3,
|
|
|
|
test_col_3: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 4,
|
|
|
|
test_col_1: 1,
|
|
|
|
test_col_2: 2,
|
|
|
|
test_col_3: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 5,
|
|
|
|
test_col_1: 1,
|
|
|
|
test_col_2: 2,
|
|
|
|
test_col_3: 1,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
after(function () {
|
2019-03-03 13:57:40 -06:00
|
|
|
return knex.schema.dropTable('intersect_test');
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('handles intersects with a callback', function () {
|
2019-03-03 13:57:40 -06:00
|
|
|
return knex('intersect_test')
|
|
|
|
.select('*')
|
|
|
|
.where('test_col_1', '=', 1)
|
2020-04-19 00:40:23 +02:00
|
|
|
.intersect(function () {
|
|
|
|
this.select('*').from('intersect_test').where('test_col_2', 2);
|
2019-03-03 13:57:40 -06:00
|
|
|
})
|
2020-04-19 00:40:23 +02:00
|
|
|
.then(function (result) {
|
2019-03-03 13:57:40 -06:00
|
|
|
expect(result.length).to.equal(3);
|
|
|
|
expect(result.map((r) => r.id)).to.have.members([1, 4, 5]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('handles intersects with an array of callbacks', function () {
|
2019-03-03 13:57:40 -06:00
|
|
|
return knex('intersect_test')
|
|
|
|
.select('*')
|
|
|
|
.where('test_col_1', '=', 1)
|
|
|
|
.intersect([
|
2020-04-19 00:40:23 +02:00
|
|
|
function () {
|
|
|
|
this.select('*').from('intersect_test').where('test_col_2', 2);
|
2019-03-03 13:57:40 -06:00
|
|
|
},
|
2020-04-19 00:40:23 +02:00
|
|
|
function () {
|
|
|
|
this.select('*').from('intersect_test').where('test_col_3', 1);
|
2019-03-03 13:57:40 -06:00
|
|
|
},
|
|
|
|
])
|
2020-04-19 00:40:23 +02:00
|
|
|
.then(function (result) {
|
2019-03-03 13:57:40 -06:00
|
|
|
expect(result.length).to.equal(2);
|
|
|
|
expect(result.map((r) => r.id)).to.have.members([1, 5]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('handles intersects with a list of callbacks', function () {
|
2019-03-03 13:57:40 -06:00
|
|
|
return knex('intersect_test')
|
|
|
|
.select('*')
|
|
|
|
.where('test_col_1', '=', 1)
|
|
|
|
.intersect(
|
2020-04-19 00:40:23 +02:00
|
|
|
function () {
|
|
|
|
this.select('*').from('intersect_test').where('test_col_2', 2);
|
2019-03-03 13:57:40 -06:00
|
|
|
},
|
2020-04-19 00:40:23 +02:00
|
|
|
function () {
|
|
|
|
this.select('*').from('intersect_test').where('test_col_3', 1);
|
2019-03-03 13:57:40 -06:00
|
|
|
}
|
|
|
|
)
|
2020-04-19 00:40:23 +02:00
|
|
|
.then(function (result) {
|
2019-03-03 13:57:40 -06:00
|
|
|
expect(result.length).to.equal(2);
|
|
|
|
expect(result.map((r) => r.id)).to.have.members([1, 5]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('handles intersects with an array of builders', function () {
|
2019-03-03 13:57:40 -06:00
|
|
|
return knex('intersect_test')
|
|
|
|
.select('*')
|
|
|
|
.where('test_col_1', '=', 1)
|
|
|
|
.intersect([
|
2020-04-19 00:40:23 +02:00
|
|
|
knex.select('*').from('intersect_test').where('test_col_2', 2),
|
|
|
|
knex.select('*').from('intersect_test').where('test_col_3', 1),
|
2019-03-03 13:57:40 -06:00
|
|
|
])
|
2020-04-19 00:40:23 +02:00
|
|
|
.then(function (result) {
|
2019-03-03 13:57:40 -06:00
|
|
|
expect(result.length).to.equal(2);
|
|
|
|
expect(result.map((r) => r.id)).to.have.members([1, 5]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('handles intersects with a list of builders', function () {
|
2019-03-03 13:57:40 -06:00
|
|
|
return knex('intersect_test')
|
|
|
|
.select('*')
|
|
|
|
.where('test_col_1', '=', 1)
|
|
|
|
.intersect(
|
2020-04-19 00:40:23 +02:00
|
|
|
knex.select('*').from('intersect_test').where('test_col_2', 2),
|
|
|
|
knex.select('*').from('intersect_test').where('test_col_3', 1)
|
2019-03-03 13:57:40 -06:00
|
|
|
)
|
2020-04-19 00:40:23 +02:00
|
|
|
.then(function (result) {
|
2019-03-03 13:57:40 -06:00
|
|
|
expect(result.length).to.equal(2);
|
|
|
|
expect(result.map((r) => r.id)).to.have.members([1, 5]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('handles intersects with a raw query', function () {
|
2019-03-03 13:57:40 -06:00
|
|
|
return knex('intersect_test')
|
|
|
|
.select('*')
|
|
|
|
.where('test_col_1', '=', 2)
|
|
|
|
.intersect(
|
|
|
|
knex.raw('select * from ?? where ?? = ?', [
|
|
|
|
'intersect_test',
|
|
|
|
'test_col_2',
|
|
|
|
3,
|
|
|
|
])
|
|
|
|
)
|
2020-04-19 00:40:23 +02:00
|
|
|
.then(function (result) {
|
2019-03-03 13:57:40 -06:00
|
|
|
expect(result.length).to.equal(2);
|
|
|
|
expect(result.map((r) => r.id)).to.have.members([2, 3]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('handles intersects with an array raw queries', function () {
|
2019-03-03 13:57:40 -06:00
|
|
|
return knex('intersect_test')
|
|
|
|
.select('*')
|
|
|
|
.where('test_col_1', '=', 1)
|
|
|
|
.intersect([
|
|
|
|
knex.raw('select * from ?? where ?? = ?', [
|
|
|
|
'intersect_test',
|
|
|
|
'test_col_2',
|
|
|
|
2,
|
|
|
|
]),
|
|
|
|
knex.raw('select * from ?? where ?? = ?', [
|
|
|
|
'intersect_test',
|
|
|
|
'test_col_3',
|
|
|
|
1,
|
|
|
|
]),
|
|
|
|
])
|
2020-04-19 00:40:23 +02:00
|
|
|
.then(function (result) {
|
2019-03-03 13:57:40 -06:00
|
|
|
expect(result.length).to.equal(2);
|
|
|
|
expect(result.map((r) => r.id)).to.have.members([1, 5]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
it('handles intersects with a list of raw queries', function () {
|
2019-03-03 13:57:40 -06:00
|
|
|
return knex('intersect_test')
|
|
|
|
.select('*')
|
|
|
|
.where('test_col_1', '=', 1)
|
|
|
|
.intersect(
|
|
|
|
knex.raw('select * from ?? where ?? = ?', [
|
|
|
|
'intersect_test',
|
|
|
|
'test_col_2',
|
|
|
|
2,
|
|
|
|
]),
|
|
|
|
knex.raw('select * from ?? where ?? = ?', [
|
|
|
|
'intersect_test',
|
|
|
|
'test_col_3',
|
|
|
|
1,
|
|
|
|
])
|
|
|
|
)
|
2020-04-19 00:40:23 +02:00
|
|
|
.then(function (result) {
|
2019-03-03 13:57:40 -06:00
|
|
|
expect(result.length).to.equal(2);
|
|
|
|
expect(result.map((r) => r.id)).to.have.members([1, 5]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-09-01 17:18:45 +02:00
|
|
|
};
|