2015-04-24 10:10:34 -04:00
|
|
|
/*global describe, expect, it, testPromise, d*/
|
2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
const _ = require('lodash');
|
|
|
|
const assert = require('assert');
|
2018-06-27 16:42:50 -03:00
|
|
|
const Promise = testPromise;
|
|
|
|
const Runner = require('../../../lib/runner');
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
module.exports = function(knex) {
|
2013-09-12 13:30:47 -04:00
|
|
|
describe('Selects', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
it('runs with no conditions', function() {
|
|
|
|
return knex('accounts').select();
|
|
|
|
});
|
|
|
|
|
2014-06-04 16:24:29 -04:00
|
|
|
it('returns an array of a single column with `pluck`', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex
|
|
|
|
.pluck('id')
|
|
|
|
.orderBy('id')
|
|
|
|
.from('accounts')
|
2014-06-04 16:24:29 -04:00
|
|
|
.testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
2014-08-21 23:39:12 +02:00
|
|
|
'select `id` from `accounts` order by `id` asc',
|
2014-06-04 16:24:29 -04:00
|
|
|
[],
|
|
|
|
[1, 2, 3, 4, 5, 7]
|
|
|
|
);
|
|
|
|
tester(
|
2018-06-29 10:47:06 +03:00
|
|
|
'pg',
|
2014-08-21 23:39:12 +02:00
|
|
|
'select "id" from "accounts" order by "id" asc',
|
2014-06-04 16:24:29 -04:00
|
|
|
[],
|
|
|
|
['1', '2', '3', '4', '5', '7']
|
|
|
|
);
|
2018-02-03 08:33:02 -05:00
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
|
|
|
'select "id" from "accounts" order by "id" asc',
|
|
|
|
[],
|
|
|
|
['1', '2', '3', '4', '5', '6']
|
|
|
|
);
|
2014-06-04 16:24:29 -04:00
|
|
|
tester(
|
|
|
|
'sqlite3',
|
2017-05-28 22:48:18 +02:00
|
|
|
'select `id` from `accounts` order by `id` asc',
|
2014-06-04 16:24:29 -04:00
|
|
|
[],
|
|
|
|
[1, 2, 3, 4, 5, 6]
|
|
|
|
);
|
2014-08-21 23:39:12 +02:00
|
|
|
tester(
|
2018-07-08 14:10:51 +03:00
|
|
|
'oracledb',
|
2018-07-09 08:10:34 -04:00
|
|
|
'select "id" from "accounts" order by "id" asc',
|
2014-08-21 23:39:12 +02:00
|
|
|
[],
|
|
|
|
[1, 2, 3, 4, 5, 7]
|
|
|
|
);
|
2015-12-09 17:53:53 -06:00
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'select [id] from [accounts] order by [id] asc',
|
|
|
|
[],
|
|
|
|
['1', '2', '3', '4', '5', '7']
|
|
|
|
);
|
2014-06-04 16:24:29 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-09-13 09:56:53 -04:00
|
|
|
it('can pluck a qualified column name, #1619', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex
|
|
|
|
.pluck('accounts.id')
|
|
|
|
.from('accounts')
|
|
|
|
.orderBy('accounts.id')
|
2016-09-13 09:56:53 -04:00
|
|
|
.testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select `accounts`.`id` from `accounts` order by `accounts`.`id` asc',
|
|
|
|
[],
|
|
|
|
[1, 2, 3, 4, 5, 7]
|
|
|
|
);
|
|
|
|
tester(
|
2018-06-29 10:47:06 +03:00
|
|
|
'pg',
|
2016-09-13 09:56:53 -04:00
|
|
|
'select "accounts"."id" from "accounts" order by "accounts"."id" asc',
|
|
|
|
[],
|
|
|
|
['1', '2', '3', '4', '5', '7']
|
|
|
|
);
|
2018-02-03 08:33:02 -05:00
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
|
|
|
'select "accounts"."id" from "accounts" order by "accounts"."id" asc',
|
|
|
|
[],
|
|
|
|
['1', '2', '3', '4', '5', '6']
|
|
|
|
);
|
2016-09-13 09:56:53 -04:00
|
|
|
tester(
|
|
|
|
'sqlite3',
|
2017-05-28 22:48:18 +02:00
|
|
|
'select `accounts`.`id` from `accounts` order by `accounts`.`id` asc',
|
2016-09-13 09:56:53 -04:00
|
|
|
[],
|
|
|
|
[1, 2, 3, 4, 5, 6]
|
|
|
|
);
|
|
|
|
tester(
|
2018-07-08 14:10:51 +03:00
|
|
|
'oracledb',
|
|
|
|
'select "accounts"."id" from "accounts" order by "accounts"."id" asc',
|
2016-09-13 09:56:53 -04:00
|
|
|
[],
|
|
|
|
[1, 2, 3, 4, 5, 7]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'select [accounts].[id] from [accounts] order by [accounts].[id] asc',
|
|
|
|
[],
|
|
|
|
['1', '2', '3', '4', '5', '7']
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
it('starts selecting at offset', function() {
|
|
|
|
return knex
|
|
|
|
.pluck('id')
|
|
|
|
.orderBy('id')
|
|
|
|
.from('accounts')
|
|
|
|
.offset(2)
|
|
|
|
.testSql(function(tester) {
|
2014-08-28 13:43:09 +02:00
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select `id` from `accounts` order by `id` asc limit 18446744073709551615 offset ?',
|
|
|
|
[2],
|
|
|
|
[3, 4, 5, 7]
|
|
|
|
);
|
|
|
|
tester(
|
2018-06-29 10:47:06 +03:00
|
|
|
'pg',
|
2014-08-28 13:43:09 +02:00
|
|
|
'select "id" from "accounts" order by "id" asc offset ?',
|
|
|
|
[2],
|
|
|
|
['3', '4', '5', '7']
|
|
|
|
);
|
2018-02-03 08:33:02 -05:00
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
|
|
|
'select "id" from "accounts" order by "id" asc offset ?',
|
|
|
|
[2],
|
|
|
|
['3', '4', '5', '6']
|
|
|
|
);
|
2014-08-28 13:43:09 +02:00
|
|
|
tester(
|
|
|
|
'sqlite3',
|
2017-05-28 22:48:18 +02:00
|
|
|
'select `id` from `accounts` order by `id` asc limit ? offset ?',
|
2014-08-28 13:43:09 +02:00
|
|
|
[-1, 2],
|
|
|
|
[3, 4, 5, 6]
|
|
|
|
);
|
|
|
|
tester(
|
2018-07-08 14:10:51 +03:00
|
|
|
'oracledb',
|
2018-07-09 08:10:34 -04:00
|
|
|
'select * from (select row_.*, ROWNUM rownum_ from (select "id" from "accounts" order by "id" asc) row_ where rownum <= ?) where rownum_ > ?',
|
2014-08-28 13:43:09 +02:00
|
|
|
[10000000000002, 2],
|
|
|
|
[3, 4, 5, 7]
|
|
|
|
);
|
2015-12-09 17:53:53 -06:00
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'select [id] from [accounts] order by [id] asc offset ? rows',
|
|
|
|
[2],
|
|
|
|
['3', '4', '5', '7']
|
|
|
|
);
|
2014-08-28 13:43:09 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-06-04 16:24:29 -04:00
|
|
|
it('returns a single entry with first', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex
|
|
|
|
.first('id', 'first_name')
|
|
|
|
.orderBy('id')
|
|
|
|
.from('accounts')
|
2014-06-04 16:24:29 -04:00
|
|
|
.testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
2014-08-21 23:39:12 +02:00
|
|
|
'select `id`, `first_name` from `accounts` order by `id` asc limit ?',
|
2014-06-04 16:24:29 -04:00
|
|
|
[1],
|
|
|
|
{ id: 1, first_name: 'Test' }
|
|
|
|
);
|
|
|
|
tester(
|
2018-06-29 10:47:06 +03:00
|
|
|
'pg',
|
2014-08-21 23:39:12 +02:00
|
|
|
'select "id", "first_name" from "accounts" order by "id" asc limit ?',
|
2014-06-04 16:24:29 -04:00
|
|
|
[1],
|
|
|
|
{ id: '1', first_name: 'Test' }
|
|
|
|
);
|
2018-02-03 08:33:02 -05:00
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
|
|
|
'select "id", "first_name" from "accounts" order by "id" asc limit ?',
|
|
|
|
[1],
|
|
|
|
{ id: '1', first_name: 'Test' }
|
|
|
|
);
|
2014-06-04 16:24:29 -04:00
|
|
|
tester(
|
|
|
|
'sqlite3',
|
2017-05-28 22:48:18 +02:00
|
|
|
'select `id`, `first_name` from `accounts` order by `id` asc limit ?',
|
2014-08-21 23:39:12 +02:00
|
|
|
[1],
|
|
|
|
{ id: 1, first_name: 'Test' }
|
|
|
|
);
|
|
|
|
tester(
|
2018-07-08 14:10:51 +03:00
|
|
|
'oracledb',
|
2018-07-09 08:10:34 -04:00
|
|
|
'select * from (select "id", "first_name" from "accounts" order by "id" asc) where rownum <= ?',
|
2014-06-04 16:24:29 -04:00
|
|
|
[1],
|
|
|
|
{ id: 1, first_name: 'Test' }
|
|
|
|
);
|
2015-12-09 17:53:53 -06:00
|
|
|
tester(
|
|
|
|
'mssql',
|
2015-12-15 16:48:19 -06:00
|
|
|
'select top (?) [id], [first_name] from [accounts] order by [id] asc',
|
2015-12-09 17:53:53 -06:00
|
|
|
[1],
|
|
|
|
{ id: '1', first_name: 'Test' }
|
|
|
|
);
|
2014-06-04 16:24:29 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-05 19:48:12 -04:00
|
|
|
it('allows you to stream', function() {
|
2018-02-03 08:33:02 -05:00
|
|
|
let count = 0;
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.stream(function(rowStream) {
|
|
|
|
rowStream.on('data', function() {
|
|
|
|
count++;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(function() {
|
|
|
|
assert(count === 6, 'Six rows should have been streamed');
|
2014-05-06 16:45:16 -04:00
|
|
|
});
|
2014-05-05 19:48:12 -04:00
|
|
|
});
|
|
|
|
|
2014-06-25 02:42:22 -04:00
|
|
|
it('returns a stream if not passed a function', function(done) {
|
2018-02-03 08:33:02 -05:00
|
|
|
let count = 0;
|
|
|
|
const stream = knex('accounts').stream();
|
2014-06-25 02:42:22 -04:00
|
|
|
stream.on('data', function() {
|
|
|
|
count++;
|
|
|
|
if (count === 6) done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-05 10:47:22 -06:00
|
|
|
it('allows you to stream with mysql dialect options', function() {
|
2018-07-08 14:10:51 +03:00
|
|
|
if (!_.includes(['mysql', 'mysql2'], knex.client.driverName)) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return;
|
2017-11-05 10:47:22 -06:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
const rows = [];
|
2017-11-05 10:47:22 -06:00
|
|
|
return knex('accounts')
|
|
|
|
.options({
|
2018-07-09 08:10:34 -04:00
|
|
|
typeCast(field, next) {
|
|
|
|
let val;
|
2017-11-05 10:47:22 -06:00
|
|
|
if (field.type === 'VAR_STRING') {
|
2018-07-09 08:10:34 -04:00
|
|
|
val = field.string();
|
|
|
|
return val == null ? val : val.toUpperCase();
|
2017-11-05 10:47:22 -06:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
return next();
|
|
|
|
},
|
2017-11-05 10:47:22 -06:00
|
|
|
})
|
|
|
|
.stream(function(rowStream) {
|
|
|
|
rowStream.on('data', function(row) {
|
2018-07-09 08:10:34 -04:00
|
|
|
rows.push(row);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(function() {
|
|
|
|
expect(rows).to.have.lengthOf(6);
|
|
|
|
rows.forEach((row) => {
|
|
|
|
['first_name', 'last_name', 'email'].forEach((field) =>
|
|
|
|
expect(row[field]).to.equal(row[field].toUpperCase())
|
|
|
|
);
|
2017-11-05 10:47:22 -06:00
|
|
|
});
|
|
|
|
});
|
2018-07-09 08:10:34 -04:00
|
|
|
});
|
2017-11-05 10:47:22 -06:00
|
|
|
|
2017-10-03 17:39:10 +01:00
|
|
|
it('emits error on the stream, if not passed a function, and connecting fails', function() {
|
2018-06-27 16:42:50 -03:00
|
|
|
const expected = new Error();
|
|
|
|
const original = Runner.prototype.ensureConnection;
|
2017-10-03 17:39:10 +01:00
|
|
|
Runner.prototype.ensureConnection = function() {
|
|
|
|
return Promise.reject(expected);
|
|
|
|
};
|
|
|
|
|
2018-06-27 16:42:50 -03:00
|
|
|
const restore = () => {
|
2017-10-03 17:39:10 +01:00
|
|
|
Runner.prototype.ensureConnection = original;
|
|
|
|
};
|
|
|
|
|
2018-06-27 16:42:50 -03:00
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
const timeout = setTimeout(() => {
|
2017-10-03 17:39:10 +01:00
|
|
|
reject(new Error('Timeout'));
|
|
|
|
}, 5000);
|
|
|
|
|
2018-06-27 16:42:50 -03:00
|
|
|
const stream = knex('accounts').stream();
|
2017-10-03 17:39:10 +01:00
|
|
|
stream.on('error', function(actual) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
|
|
|
if (actual === expected) {
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
reject(new Error('Stream emitted unexpected error'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
promise.then(restore, restore);
|
|
|
|
return promise;
|
|
|
|
});
|
|
|
|
|
2018-06-27 16:42:50 -03:00
|
|
|
it('emits error on the stream, if not passed a function, and query fails', function(done) {
|
2018-07-09 08:10:34 -04:00
|
|
|
const stream = knex('accounts')
|
|
|
|
.select('invalid_field')
|
|
|
|
.stream();
|
2018-06-27 16:42:50 -03:00
|
|
|
stream.on('error', function(err) {
|
2018-07-09 08:10:34 -04:00
|
|
|
assert(err instanceof Error);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-06-27 16:42:50 -03:00
|
|
|
|
|
|
|
it('emits error if not passed a function and the query has wrong bindings', function(done) {
|
2018-07-09 08:10:34 -04:00
|
|
|
const stream = knex('accounts')
|
|
|
|
.whereRaw('id = ? and first_name = ?', ['2'])
|
|
|
|
.stream();
|
2018-06-27 16:42:50 -03:00
|
|
|
stream.on('error', function(err) {
|
2018-07-09 08:10:34 -04:00
|
|
|
assert(err instanceof Error);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2018-06-27 16:42:50 -03:00
|
|
|
|
2014-07-10 12:31:18 -04:00
|
|
|
it('properly escapes postgres queries on streaming', function() {
|
2018-02-03 08:33:02 -05:00
|
|
|
let count = 0;
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.where('id', 1)
|
|
|
|
.stream(function(rowStream) {
|
|
|
|
rowStream.on('data', function() {
|
|
|
|
count++;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(function() {
|
|
|
|
assert(count === 1, 'One row should have been streamed');
|
2014-07-10 12:31:18 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-09 17:23:07 -04:00
|
|
|
it('throws errors on the asCallback if uncaught in the last block', function(ok) {
|
2018-02-03 08:33:02 -05:00
|
|
|
const listeners = process.listeners('uncaughtException');
|
2013-09-11 23:36:55 -04:00
|
|
|
|
|
|
|
process.removeAllListeners('uncaughtException');
|
|
|
|
|
2014-04-16 02:50:19 -04:00
|
|
|
process.on('uncaughtException', function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
process.removeAllListeners('uncaughtException');
|
2018-02-03 08:33:02 -05:00
|
|
|
for (let i = 0, l = listeners.length; i < l; i++) {
|
2013-09-11 23:36:55 -04:00
|
|
|
process.on('uncaughtException', listeners[i]);
|
|
|
|
}
|
|
|
|
ok();
|
|
|
|
});
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
knex('accounts')
|
|
|
|
.select()
|
|
|
|
.asCallback(function() {
|
2018-10-15 22:29:53 -04:00
|
|
|
this.undefinedVar.test;
|
2018-07-09 08:10:34 -04:00
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('uses `orderBy`', function() {
|
|
|
|
return knex('accounts')
|
2014-08-21 23:39:12 +02:00
|
|
|
.pluck('id')
|
|
|
|
.orderBy('id', 'desc')
|
|
|
|
.testSql(function(tester) {
|
|
|
|
tester(
|
2018-07-08 14:10:51 +03:00
|
|
|
'oracledb',
|
2018-07-09 08:10:34 -04:00
|
|
|
'select "id" from "accounts" order by "id" desc',
|
2014-08-21 23:39:12 +02:00
|
|
|
[],
|
|
|
|
[7, 5, 4, 3, 2, 1]
|
|
|
|
);
|
2015-12-09 17:53:53 -06:00
|
|
|
tester(
|
|
|
|
'mssql',
|
2018-07-09 08:10:34 -04:00
|
|
|
'select [id] from [accounts] order by [id] desc',
|
2015-12-09 17:53:53 -06:00
|
|
|
[],
|
|
|
|
['7', '5', '4', '3', '2', '1']
|
|
|
|
);
|
2014-08-21 23:39:12 +02:00
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
describe('simple "where" cases', function() {
|
|
|
|
it('allows key, value', function() {
|
2013-12-27 14:44:21 -05:00
|
|
|
return knex('accounts')
|
|
|
|
.where('id', 1)
|
|
|
|
.select('first_name', 'last_name')
|
|
|
|
.testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select `first_name`, `last_name` from `accounts` where `id` = ?',
|
2014-08-21 23:39:12 +02:00
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
},
|
|
|
|
]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
|
|
|
tester(
|
2018-06-29 10:47:06 +03:00
|
|
|
'pg',
|
2013-12-27 14:44:21 -05:00
|
|
|
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
2014-08-21 23:39:12 +02:00
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
},
|
|
|
|
]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
2018-02-03 08:33:02 -05:00
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
|
|
|
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
},
|
|
|
|
]
|
2018-02-03 08:33:02 -05:00
|
|
|
);
|
2013-12-27 14:44:21 -05:00
|
|
|
tester(
|
|
|
|
'sqlite3',
|
2017-05-28 22:48:18 +02:00
|
|
|
'select `first_name`, `last_name` from `accounts` where `id` = ?',
|
2014-08-21 23:39:12 +02:00
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
},
|
|
|
|
]
|
2014-08-21 23:39:12 +02:00
|
|
|
);
|
|
|
|
tester(
|
2018-07-08 14:10:51 +03:00
|
|
|
'oracledb',
|
2014-08-21 23:39:12 +02:00
|
|
|
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
},
|
|
|
|
]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
2015-12-09 17:53:53 -06:00
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'select [first_name], [last_name] from [accounts] where [id] = ?',
|
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
},
|
|
|
|
]
|
2015-12-09 17:53:53 -06:00
|
|
|
);
|
2013-12-27 14:44:21 -05:00
|
|
|
});
|
2013-09-12 13:30:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('allows key, operator, value', function() {
|
2013-12-27 14:44:21 -05:00
|
|
|
return knex('accounts')
|
|
|
|
.where('id', 1)
|
|
|
|
.select('first_name', 'last_name')
|
|
|
|
.testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select `first_name`, `last_name` from `accounts` where `id` = ?',
|
2014-08-21 23:39:12 +02:00
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
},
|
|
|
|
]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
|
|
|
tester(
|
2018-06-29 10:47:06 +03:00
|
|
|
'pg',
|
2013-12-27 14:44:21 -05:00
|
|
|
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
2014-08-21 23:39:12 +02:00
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
},
|
|
|
|
]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
2018-02-03 08:33:02 -05:00
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
|
|
|
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
},
|
|
|
|
]
|
2018-02-03 08:33:02 -05:00
|
|
|
);
|
2013-12-27 14:44:21 -05:00
|
|
|
tester(
|
|
|
|
'sqlite3',
|
2017-05-28 22:48:18 +02:00
|
|
|
'select `first_name`, `last_name` from `accounts` where `id` = ?',
|
2014-08-21 23:39:12 +02:00
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
},
|
|
|
|
]
|
2014-08-21 23:39:12 +02:00
|
|
|
);
|
|
|
|
tester(
|
2018-07-08 14:10:51 +03:00
|
|
|
'oracledb',
|
2014-08-21 23:39:12 +02:00
|
|
|
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
},
|
|
|
|
]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
2015-12-09 17:53:53 -06:00
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'select [first_name], [last_name] from [accounts] where [id] = ?',
|
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
},
|
|
|
|
]
|
2015-12-09 17:53:53 -06:00
|
|
|
);
|
2013-12-27 14:44:21 -05:00
|
|
|
});
|
2013-09-12 13:30:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('allows selecting columns with an array', function() {
|
2013-12-27 14:44:21 -05:00
|
|
|
return knex('accounts')
|
|
|
|
.where('id', '>', 1)
|
|
|
|
.select(['email', 'logins'])
|
|
|
|
.testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select `email`, `logins` from `accounts` where `id` > ?',
|
|
|
|
[1]
|
|
|
|
);
|
|
|
|
tester(
|
2018-06-29 10:47:06 +03:00
|
|
|
'pg',
|
2013-12-27 14:44:21 -05:00
|
|
|
'select "email", "logins" from "accounts" where "id" > ?',
|
|
|
|
[1]
|
|
|
|
);
|
2018-02-03 08:33:02 -05:00
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
|
|
|
'select "email", "logins" from "accounts" where "id" > ?',
|
|
|
|
[1]
|
|
|
|
);
|
2013-12-27 14:44:21 -05:00
|
|
|
tester(
|
|
|
|
'sqlite3',
|
2017-05-28 22:48:18 +02:00
|
|
|
'select `email`, `logins` from `accounts` where `id` > ?',
|
2013-12-27 14:44:21 -05:00
|
|
|
[1]
|
|
|
|
);
|
2014-08-21 23:39:12 +02:00
|
|
|
tester(
|
2018-07-08 14:10:51 +03:00
|
|
|
'oracledb',
|
2014-08-21 23:39:12 +02:00
|
|
|
'select "email", "logins" from "accounts" where "id" > ?',
|
|
|
|
[1]
|
|
|
|
);
|
2015-12-09 17:53:53 -06:00
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'select [email], [logins] from [accounts] where [id] > ?',
|
|
|
|
[1]
|
|
|
|
);
|
2013-12-27 14:44:21 -05:00
|
|
|
});
|
2013-09-12 13:30:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('allows a hash of where attrs', function() {
|
2013-12-27 14:44:21 -05:00
|
|
|
return knex('accounts')
|
2018-07-09 08:10:34 -04:00
|
|
|
.where({ id: 1 })
|
2013-12-27 14:44:21 -05:00
|
|
|
.select('*')
|
|
|
|
.testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select * from `accounts` where `id` = ?',
|
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
email: 'test@example.com',
|
|
|
|
logins: 1,
|
|
|
|
balance: 0,
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
|
|
|
created_at: d,
|
|
|
|
updated_at: d,
|
|
|
|
phone: null,
|
|
|
|
},
|
|
|
|
]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
|
|
|
tester(
|
2018-06-29 10:47:06 +03:00
|
|
|
'pg',
|
2013-12-27 14:44:21 -05:00
|
|
|
'select * from "accounts" where "id" = ?',
|
2014-08-21 23:39:12 +02:00
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
id: '1',
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
email: 'test@example.com',
|
|
|
|
logins: 1,
|
|
|
|
balance: 0,
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
|
|
|
created_at: d,
|
|
|
|
updated_at: d,
|
|
|
|
phone: null,
|
|
|
|
},
|
|
|
|
]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
2018-02-03 08:33:02 -05:00
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
|
|
|
'select * from "accounts" where "id" = ?',
|
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
id: '1',
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
email: 'test@example.com',
|
|
|
|
logins: 1,
|
|
|
|
balance: 0,
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
|
|
|
created_at: d,
|
|
|
|
updated_at: d,
|
|
|
|
phone: null,
|
|
|
|
},
|
|
|
|
]
|
2018-02-03 08:33:02 -05:00
|
|
|
);
|
2013-12-27 14:44:21 -05:00
|
|
|
tester(
|
|
|
|
'sqlite3',
|
2017-05-28 22:48:18 +02:00
|
|
|
'select * from `accounts` where `id` = ?',
|
2014-08-21 23:39:12 +02:00
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
email: 'test@example.com',
|
|
|
|
logins: 1,
|
|
|
|
balance: 0,
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
|
|
|
created_at: d,
|
|
|
|
updated_at: d,
|
|
|
|
phone: null,
|
|
|
|
},
|
|
|
|
]
|
2014-08-21 23:39:12 +02:00
|
|
|
);
|
|
|
|
tester(
|
2018-07-08 14:10:51 +03:00
|
|
|
'oracledb',
|
2014-08-21 23:39:12 +02:00
|
|
|
'select * from "accounts" where "id" = ?',
|
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
email: 'test@example.com',
|
|
|
|
logins: 1,
|
|
|
|
balance: 0,
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
|
|
|
created_at: d,
|
|
|
|
updated_at: d,
|
|
|
|
phone: null,
|
|
|
|
},
|
|
|
|
]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
2015-12-09 17:53:53 -06:00
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'select * from [accounts] where [id] = ?',
|
|
|
|
[1],
|
2018-07-09 08:10:34 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
id: '1',
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User',
|
|
|
|
email: 'test@example.com',
|
|
|
|
logins: 1,
|
|
|
|
balance: 0,
|
|
|
|
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
|
|
|
created_at: d,
|
|
|
|
updated_at: d,
|
|
|
|
phone: null,
|
|
|
|
},
|
|
|
|
]
|
2015-12-09 17:53:53 -06:00
|
|
|
);
|
2013-12-27 14:44:21 -05:00
|
|
|
});
|
2013-09-12 13:30:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('allows where id: undefined or id: null as a where null clause', function() {
|
2013-12-27 14:44:21 -05:00
|
|
|
return knex('accounts')
|
2018-07-09 08:10:34 -04:00
|
|
|
.where({ id: null })
|
2013-12-27 14:44:21 -05:00
|
|
|
.select('first_name', 'email')
|
|
|
|
.testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select `first_name`, `email` from `accounts` where `id` is null',
|
2014-08-21 23:39:12 +02:00
|
|
|
[],
|
2013-12-27 14:44:21 -05:00
|
|
|
[]
|
|
|
|
);
|
|
|
|
tester(
|
2018-06-29 10:47:06 +03:00
|
|
|
'pg',
|
2013-12-27 14:44:21 -05:00
|
|
|
'select "first_name", "email" from "accounts" where "id" is null',
|
2014-08-21 23:39:12 +02:00
|
|
|
[],
|
2013-12-27 14:44:21 -05:00
|
|
|
[]
|
|
|
|
);
|
2018-02-03 08:33:02 -05:00
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
|
|
|
'select "first_name", "email" from "accounts" where "id" is null',
|
|
|
|
[],
|
|
|
|
[]
|
|
|
|
);
|
2013-12-27 14:44:21 -05:00
|
|
|
tester(
|
|
|
|
'sqlite3',
|
2017-05-28 22:48:18 +02:00
|
|
|
'select `first_name`, `email` from `accounts` where `id` is null',
|
2014-08-21 23:39:12 +02:00
|
|
|
[],
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
tester(
|
2018-07-08 14:10:51 +03:00
|
|
|
'oracledb',
|
2014-08-21 23:39:12 +02:00
|
|
|
'select "first_name", "email" from "accounts" where "id" is null',
|
|
|
|
[],
|
2013-12-27 14:44:21 -05:00
|
|
|
[]
|
|
|
|
);
|
2015-12-09 17:53:53 -06:00
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'select [first_name], [email] from [accounts] where [id] is null',
|
|
|
|
[],
|
|
|
|
[]
|
|
|
|
);
|
2013-12-27 14:44:21 -05:00
|
|
|
});
|
2013-09-12 13:30:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('allows where id = 0', function() {
|
2013-12-27 14:44:21 -05:00
|
|
|
return knex('accounts')
|
2018-07-09 08:10:34 -04:00
|
|
|
.where({ id: 0 })
|
2013-12-27 14:44:21 -05:00
|
|
|
.select()
|
|
|
|
.testSql(function(tester) {
|
2018-07-09 08:10:34 -04:00
|
|
|
tester('mysql', 'select * from `accounts` where `id` = ?', [0], []);
|
|
|
|
tester('pg', 'select * from "accounts" where "id" = ?', [0], []);
|
2018-02-03 08:33:02 -05:00
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
|
|
|
'select * from "accounts" where "id" = ?',
|
|
|
|
[0],
|
|
|
|
[]
|
|
|
|
);
|
2013-12-27 14:44:21 -05:00
|
|
|
tester(
|
|
|
|
'sqlite3',
|
2017-05-28 22:48:18 +02:00
|
|
|
'select * from `accounts` where `id` = ?',
|
2014-08-21 23:39:12 +02:00
|
|
|
[0],
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
tester(
|
2018-07-08 14:10:51 +03:00
|
|
|
'oracledb',
|
2014-08-21 23:39:12 +02:00
|
|
|
'select * from "accounts" where "id" = ?',
|
|
|
|
[0],
|
|
|
|
[]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
2018-07-09 08:10:34 -04:00
|
|
|
tester('mssql', 'select * from [accounts] where [id] = ?', [0], []);
|
2013-12-27 14:44:21 -05:00
|
|
|
});
|
2013-09-12 13:30:47 -04:00
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('has a "distinct" clause', function() {
|
2013-10-27 22:34:58 -04:00
|
|
|
return Promise.all([
|
2018-07-09 08:10:34 -04:00
|
|
|
knex('accounts')
|
|
|
|
.select()
|
|
|
|
.distinct('email')
|
|
|
|
.where('logins', 2)
|
|
|
|
.orderBy('email'),
|
|
|
|
knex('accounts')
|
|
|
|
.distinct('email')
|
|
|
|
.select()
|
|
|
|
.orderBy('email'),
|
2013-09-11 23:36:55 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does "orWhere" cases', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.where('id', 1)
|
|
|
|
.orWhere('id', '>', 2)
|
|
|
|
.select('first_name', 'last_name');
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does "andWhere" cases', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.select('first_name', 'last_name', 'about')
|
|
|
|
.where('id', 1)
|
|
|
|
.andWhere('email', 'test@example.com');
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('takes a function to wrap nested where statements', function() {
|
2013-10-27 22:34:58 -04:00
|
|
|
return Promise.all([
|
2018-07-09 08:10:34 -04:00
|
|
|
knex('accounts')
|
|
|
|
.where(function() {
|
|
|
|
this.where('id', 2);
|
|
|
|
this.orWhere('id', 3);
|
|
|
|
})
|
|
|
|
.select('*'),
|
2013-09-11 23:36:55 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles "where in" cases', function() {
|
2013-10-27 22:34:58 -04:00
|
|
|
return Promise.all([
|
2018-07-09 08:10:34 -04:00
|
|
|
knex('accounts')
|
|
|
|
.whereIn('id', [1, 2, 3])
|
|
|
|
.select(),
|
2013-09-11 23:36:55 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles "or where in" cases', function() {
|
|
|
|
return knex('accounts')
|
|
|
|
.where('email', 'test@example.com')
|
|
|
|
.orWhereIn('id', [2, 3, 4])
|
|
|
|
.select();
|
|
|
|
});
|
|
|
|
|
2014-03-25 18:56:07 +00:00
|
|
|
it('handles multi-column "where in" cases', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
if (
|
|
|
|
knex.client.driverName !== 'sqlite3' &&
|
|
|
|
knex.client.driverName !== 'mssql'
|
|
|
|
) {
|
2014-03-25 18:56:07 +00:00
|
|
|
return knex('composite_key_test')
|
|
|
|
.whereIn(['column_a', 'column_b'], [[1, 1], [1, 2]])
|
2018-02-03 08:33:02 -05:00
|
|
|
.orderBy('status', 'desc')
|
2014-04-16 02:50:19 -04:00
|
|
|
.select()
|
|
|
|
.testSql(function(tester) {
|
2018-07-09 08:10:34 -04:00
|
|
|
tester(
|
|
|
|
'mysql',
|
2018-02-22 07:51:09 +11:00
|
|
|
'select * from `composite_key_test` where (`column_a`, `column_b`) in ((?, ?), (?, ?)) order by `status` desc',
|
2018-07-09 08:10:34 -04:00
|
|
|
[1, 1, 1, 2],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
column_a: 1,
|
|
|
|
column_b: 1,
|
|
|
|
details: 'One, One, One',
|
|
|
|
status: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
column_a: 1,
|
|
|
|
column_b: 2,
|
|
|
|
details: 'One, Two, Zero',
|
|
|
|
status: 0,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'pg',
|
2018-06-29 10:47:06 +03:00
|
|
|
'select * from "composite_key_test" where ("column_a", "column_b") in ((?, ?), (?, ?)) order by "status" desc',
|
2018-07-09 08:10:34 -04:00
|
|
|
[1, 1, 1, 2],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
column_a: 1,
|
|
|
|
column_b: 1,
|
|
|
|
details: 'One, One, One',
|
|
|
|
status: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
column_a: 1,
|
|
|
|
column_b: 2,
|
|
|
|
details: 'One, Two, Zero',
|
|
|
|
status: 0,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
2018-04-04 18:43:39 -04:00
|
|
|
'select * from "composite_key_test" where ("column_a", "column_b") in ((?, ?), (?, ?)) order by "status" desc',
|
2018-07-09 08:10:34 -04:00
|
|
|
[1, 1, 1, 2],
|
2018-02-03 08:33:02 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
column_a: 1,
|
|
|
|
column_b: 1,
|
|
|
|
details: 'One, One, One',
|
2018-07-09 08:10:34 -04:00
|
|
|
status: 1,
|
2018-02-03 08:33:02 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
column_a: 1,
|
|
|
|
column_b: 2,
|
|
|
|
details: 'One, Two, Zero',
|
2018-07-09 08:10:34 -04:00
|
|
|
status: 0,
|
2018-02-03 08:33:02 -05:00
|
|
|
},
|
2018-07-09 08:10:34 -04:00
|
|
|
]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'oracledb',
|
2018-07-08 14:10:51 +03:00
|
|
|
'select * from "composite_key_test" where ("column_a", "column_b") in ((?, ?), (?, ?)) order by "status" desc',
|
2018-07-09 08:10:34 -04:00
|
|
|
[1, 1, 1, 2],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
column_a: 1,
|
|
|
|
column_b: 1,
|
|
|
|
details: 'One, One, One',
|
|
|
|
status: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
column_a: 1,
|
|
|
|
column_b: 2,
|
|
|
|
details: 'One, Two, Zero',
|
|
|
|
status: 0,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
2014-04-16 02:50:19 -04:00
|
|
|
});
|
2014-03-25 18:56:07 +00:00
|
|
|
}
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
2014-04-16 02:50:19 -04:00
|
|
|
it('handles multi-column "where in" cases with where', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
if (
|
|
|
|
knex.client.driverName !== 'sqlite3' &&
|
|
|
|
knex.client.driverName !== 'mssql'
|
|
|
|
) {
|
2014-03-25 18:56:07 +00:00
|
|
|
return knex('composite_key_test')
|
|
|
|
.where('status', 1)
|
|
|
|
.whereIn(['column_a', 'column_b'], [[1, 1], [1, 2]])
|
2014-04-16 02:50:19 -04:00
|
|
|
.select()
|
|
|
|
.testSql(function(tester) {
|
2018-07-09 08:10:34 -04:00
|
|
|
tester(
|
|
|
|
'mysql',
|
2018-02-22 07:51:09 +11:00
|
|
|
'select * from `composite_key_test` where `status` = ? and (`column_a`, `column_b`) in ((?, ?), (?, ?))',
|
2018-07-09 08:10:34 -04:00
|
|
|
[1, 1, 1, 1, 2],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
column_a: 1,
|
|
|
|
column_b: 1,
|
|
|
|
details: 'One, One, One',
|
|
|
|
status: 1,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'pg',
|
2018-06-29 10:47:06 +03:00
|
|
|
'select * from "composite_key_test" where "status" = ? and ("column_a", "column_b") in ((?, ?), (?, ?))',
|
2018-07-09 08:10:34 -04:00
|
|
|
[1, 1, 1, 1, 2],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
column_a: 1,
|
|
|
|
column_b: 1,
|
|
|
|
details: 'One, One, One',
|
|
|
|
status: 1,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'pg-redshift',
|
2018-04-04 18:43:39 -04:00
|
|
|
'select * from "composite_key_test" where "status" = ? and ("column_a", "column_b") in ((?, ?), (?, ?))',
|
2018-07-09 08:10:34 -04:00
|
|
|
[1, 1, 1, 1, 2],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
column_a: 1,
|
|
|
|
column_b: 1,
|
|
|
|
details: 'One, One, One',
|
|
|
|
status: 1,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'oracledb',
|
2018-07-08 14:10:51 +03:00
|
|
|
'select * from "composite_key_test" where "status" = ? and ("column_a", "column_b") in ((?, ?), (?, ?))',
|
2018-07-09 08:10:34 -04:00
|
|
|
[1, 1, 1, 1, 2],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
column_a: 1,
|
|
|
|
column_b: 1,
|
|
|
|
details: 'One, One, One',
|
|
|
|
status: 1,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
2014-04-16 02:50:19 -04:00
|
|
|
});
|
2014-03-25 18:56:07 +00:00
|
|
|
}
|
|
|
|
});
|
2013-09-11 23:36:55 -04:00
|
|
|
|
|
|
|
it('handles "where exists"', function() {
|
|
|
|
return knex('accounts')
|
2014-03-26 19:15:46 -04:00
|
|
|
.whereExists(function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
this.select('id')
|
|
|
|
.from('test_table_two')
|
|
|
|
.where({ id: 1 });
|
2013-09-11 23:36:55 -04:00
|
|
|
})
|
|
|
|
.select();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles "where between"', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.whereBetween('id', [1, 100])
|
|
|
|
.select();
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('handles "or where between"', function() {
|
|
|
|
return knex('accounts')
|
|
|
|
.whereBetween('id', [1, 100])
|
|
|
|
.orWhereBetween('id', [200, 300])
|
|
|
|
.select();
|
|
|
|
});
|
|
|
|
|
2014-04-21 08:24:23 -04:00
|
|
|
it('does where(raw)', function() {
|
2018-07-08 14:10:51 +03:00
|
|
|
if (knex.client.driverName === 'oracledb') {
|
2014-08-11 12:25:39 +02:00
|
|
|
// special case for oracle
|
|
|
|
return knex('accounts')
|
|
|
|
.whereExists(function() {
|
|
|
|
this.select(knex.raw(1))
|
|
|
|
.from('test_table_two')
|
2018-07-09 08:10:34 -04:00
|
|
|
.where(
|
|
|
|
knex.raw('"test_table_two"."account_id" = "accounts"."id"')
|
|
|
|
);
|
2014-08-11 12:25:39 +02:00
|
|
|
})
|
|
|
|
.select();
|
|
|
|
} else {
|
|
|
|
return knex('accounts')
|
|
|
|
.whereExists(function() {
|
|
|
|
this.select(knex.raw(1))
|
|
|
|
.from('test_table_two')
|
|
|
|
.where(knex.raw('test_table_two.account_id = accounts.id'));
|
|
|
|
})
|
|
|
|
.select();
|
|
|
|
}
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does sub-selects', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.whereIn('id', function() {
|
|
|
|
this.select('account_id')
|
|
|
|
.from('test_table_two')
|
|
|
|
.where('status', 1);
|
|
|
|
})
|
|
|
|
.select('first_name', 'last_name');
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
it('supports the <> operator', function() {
|
|
|
|
return knex('accounts')
|
|
|
|
.where('id', '<>', 2)
|
|
|
|
.select('email', 'logins');
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
it('Allows for knex.Raw passed to the `where` clause', function() {
|
2018-07-08 14:10:51 +03:00
|
|
|
if (knex.client.driverName === 'oracledb') {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.where(knex.raw('"id" = 2'))
|
|
|
|
.select('email', 'logins');
|
2014-08-11 12:25:39 +02:00
|
|
|
} else {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('accounts')
|
|
|
|
.where(knex.raw('id = 2'))
|
|
|
|
.select('email', 'logins');
|
2014-08-11 12:25:39 +02:00
|
|
|
}
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
2014-04-16 03:22:47 -04:00
|
|
|
it('Retains array bindings, #228', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
const raw = knex.raw(
|
|
|
|
'select * from table t where t.id = ANY( ?::int[] )',
|
|
|
|
[[1, 2, 3]]
|
|
|
|
);
|
|
|
|
const raw2 = knex.raw('select "stored_procedure"(?, ?, ?)', [
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
['a', 'b', 'c'],
|
|
|
|
]);
|
2018-02-03 08:33:02 -05:00
|
|
|
const expected1 = [[1, 2, 3]];
|
|
|
|
const expected2 = [1, 2, ['a', 'b', 'c']];
|
2016-03-15 17:51:10 +01:00
|
|
|
expect(raw.toSQL().bindings).to.eql(knex.client.prepBindings(expected1));
|
|
|
|
expect(raw2.toSQL().bindings).to.eql(knex.client.prepBindings(expected2));
|
|
|
|
//Also expect raw's bindings to not have been modified by calling .toSQL() (preserving original bindings)
|
|
|
|
expect(raw.bindings).to.eql(expected1);
|
|
|
|
expect(raw2.bindings).to.eql(expected2);
|
2014-04-02 21:28:56 -04:00
|
|
|
});
|
|
|
|
|
2014-06-04 15:55:41 -04:00
|
|
|
it('always returns the response object from raw', function() {
|
2018-07-08 14:10:51 +03:00
|
|
|
if (knex.client.driverName === 'pg') {
|
2014-06-04 15:55:41 -04:00
|
|
|
return knex.raw('select id from accounts').then(function(resp) {
|
|
|
|
assert(Array.isArray(resp.rows) === true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-13 01:17:32 -04:00
|
|
|
it('properly escapes identifiers, #737', function() {
|
2018-07-08 14:10:51 +03:00
|
|
|
if (knex.client.driverName === 'pg') {
|
2018-07-09 08:10:34 -04:00
|
|
|
const query = knex
|
|
|
|
.select('id","name')
|
|
|
|
.from('test')
|
|
|
|
.toSQL();
|
2015-03-13 01:17:32 -04:00
|
|
|
assert(query.sql === 'select "id"",""name" from "test"');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-02 12:31:16 +02:00
|
|
|
it('knex.ref() as column in .select()', function() {
|
|
|
|
return knex('accounts')
|
2018-07-09 08:10:34 -04:00
|
|
|
.select([knex.ref('accounts.id').as('userid')])
|
2018-05-02 12:31:16 +02:00
|
|
|
.where(knex.ref('accounts.id'), '1')
|
|
|
|
.first()
|
|
|
|
.then(function(row) {
|
|
|
|
expect(String(row.userid)).to.equal('1');
|
|
|
|
|
|
|
|
return true;
|
2018-07-09 08:10:34 -04:00
|
|
|
});
|
2018-05-02 12:31:16 +02:00
|
|
|
});
|
|
|
|
|
2018-06-29 10:47:06 +03:00
|
|
|
it('select for update locks selected row', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
if (knex.client.driverName === 'sqlite3') {
|
2018-06-29 10:47:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('test_default_table')
|
|
|
|
.insert({ string: 'making sure there is a row to lock' })
|
2018-06-29 10:47:06 +03:00
|
|
|
.then(() => {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex
|
|
|
|
.transaction((trx) => {
|
|
|
|
// select all from test table and lock
|
|
|
|
return trx('test_default_table')
|
|
|
|
.forUpdate()
|
|
|
|
.then((res) => {
|
|
|
|
// try to select stuff from table in other connection should just hang...
|
|
|
|
return knex('test_default_table')
|
|
|
|
.forUpdate()
|
|
|
|
.timeout(100);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
expect('Second query should have timed out').to.be.false;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
expect(err.message).to.be.contain(
|
|
|
|
'Defined query timeout of 100ms exceeded when running query'
|
|
|
|
);
|
2018-06-29 10:47:06 +03:00
|
|
|
});
|
2018-07-09 08:10:34 -04:00
|
|
|
});
|
|
|
|
});
|
2018-06-29 10:47:06 +03:00
|
|
|
|
2018-10-04 15:39:24 +02:00
|
|
|
it('select for update locks only some tables, #2834', function() {
|
|
|
|
if (knex.client.driverName !== 'pg') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return knex('test_default_table')
|
|
|
|
.insert({ string: 'making sure there is a row to lock', tinyint: 1 })
|
|
|
|
.then(() => {
|
|
|
|
return knex('test_default_table2')
|
|
|
|
.insert({
|
|
|
|
string: 'making sure there is a row to lock',
|
|
|
|
tinyint: 1,
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return knex
|
|
|
|
.transaction((trx) => {
|
|
|
|
// select all from two test tables and lock only one table
|
|
|
|
return trx('test_default_table')
|
|
|
|
.innerJoin(
|
|
|
|
'test_default_table2',
|
|
|
|
'test_default_table.tinyint',
|
|
|
|
'test_default_table2.tinyint'
|
|
|
|
)
|
|
|
|
.forUpdate('test_default_table')
|
|
|
|
.then((res) => {
|
|
|
|
// try to select stuff from unlocked table should not hang...
|
|
|
|
return knex('test_default_table2')
|
|
|
|
.forUpdate()
|
|
|
|
.timeout(150);
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
// try to select stuff from table in other connection should just hang...
|
|
|
|
return knex('test_default_table')
|
|
|
|
.forUpdate()
|
|
|
|
.timeout(100);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
expect('Second query should have timed out').to.be.false;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
expect(err.message).to.be.contain(
|
|
|
|
'Defined query timeout of 100ms exceeded when running query'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-06-29 10:47:06 +03:00
|
|
|
it('select for share prevents updating in other transaction', function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
if (
|
|
|
|
knex.client.driverName === 'sqlite3' ||
|
|
|
|
knex.client.driverName === 'oracledb'
|
|
|
|
) {
|
2018-06-29 10:47:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex('test_default_table')
|
|
|
|
.insert({ string: 'making sure there is a row to lock' })
|
2018-06-29 10:47:06 +03:00
|
|
|
.then(() => {
|
2018-07-09 08:10:34 -04:00
|
|
|
return knex
|
|
|
|
.transaction((trx) => {
|
|
|
|
// select all from test table and lock
|
|
|
|
return trx('test_default_table')
|
|
|
|
.forShare()
|
|
|
|
.then((res) => {
|
|
|
|
// try to update row that was selected for share should just hang...
|
|
|
|
return knex.transaction((trx2) => {
|
|
|
|
return trx2('test_default_table')
|
|
|
|
.update({ string: 'foo' })
|
|
|
|
.timeout(100);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
expect('Second query should have timed out').to.be.false;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
// mssql fails because it tires to rollback at the same time when update query is running
|
|
|
|
// hopefully for share really works though...
|
|
|
|
if (knex.client.driverName == 'mssql') {
|
|
|
|
expect(err.message).to.be.contain(
|
|
|
|
"Can't rollback transaction. There is a request in progress"
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
expect(err.message).to.be.contain(
|
|
|
|
'Defined query timeout of 100ms exceeded when running query'
|
|
|
|
);
|
|
|
|
}
|
2018-06-29 10:47:06 +03:00
|
|
|
});
|
2018-07-09 08:10:34 -04:00
|
|
|
});
|
2018-06-29 10:47:06 +03:00
|
|
|
});
|
2018-07-09 08:10:34 -04:00
|
|
|
});
|
2014-08-21 23:39:12 +02:00
|
|
|
};
|