2015-04-24 10:10:34 -04:00
|
|
|
/*global describe, expect, it, testPromise, d*/
|
2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2017-11-05 10:47:22 -06:00
|
|
|
var _ = require('lodash')
|
2015-04-24 10:10:34 -04:00
|
|
|
var assert = require('assert')
|
2013-10-27 22:34:58 -04:00
|
|
|
var Promise = testPromise;
|
2017-10-03 17:39:10 +01:00
|
|
|
var 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() {
|
2014-08-21 23:39:12 +02: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(
|
|
|
|
'postgresql',
|
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(
|
|
|
|
'oracle',
|
|
|
|
"select \"id\" from \"accounts\" order by \"id\" asc",
|
|
|
|
[],
|
|
|
|
[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() {
|
|
|
|
return knex.pluck('accounts.id').from('accounts').orderBy('accounts.id')
|
|
|
|
.testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select `accounts`.`id` from `accounts` order by `accounts`.`id` asc',
|
|
|
|
[],
|
|
|
|
[1, 2, 3, 4, 5, 7]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'postgresql',
|
|
|
|
'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(
|
|
|
|
'oracle',
|
|
|
|
'select "accounts"."id" from "accounts" order by "accounts"."id" asc',
|
|
|
|
[],
|
|
|
|
[1, 2, 3, 4, 5, 7]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'select [accounts].[id] from [accounts] order by [accounts].[id] asc',
|
|
|
|
[],
|
|
|
|
['1', '2', '3', '4', '5', '7']
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-08-28 13:43:09 +02:00
|
|
|
it('starts selecting at offset', function () {
|
|
|
|
return knex.pluck('id').orderBy('id').from('accounts').offset(2)
|
|
|
|
.testSql(function (tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select `id` from `accounts` order by `id` asc limit 18446744073709551615 offset ?',
|
|
|
|
[2],
|
|
|
|
[3, 4, 5, 7]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'postgresql',
|
|
|
|
'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(
|
|
|
|
'oracle',
|
|
|
|
"select * from (select row_.*, ROWNUM rownum_ from (select \"id\" from \"accounts\" order by \"id\" asc) row_ where rownum <= ?) where rownum_ > ?",
|
|
|
|
[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() {
|
2014-08-21 23:39:12 +02: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(
|
|
|
|
'postgresql',
|
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(
|
|
|
|
'oracle',
|
|
|
|
"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;
|
2014-05-05 19:48:12 -04:00
|
|
|
return knex('accounts').stream(function(rowStream) {
|
2014-09-02 22:56:51 +02:00
|
|
|
rowStream.on('data', function() {
|
2014-05-06 16:45:16 -04:00
|
|
|
count++;
|
|
|
|
});
|
2014-05-05 19:48:12 -04:00
|
|
|
}).then(function() {
|
2014-05-06 16:45:16 -04:00
|
|
|
assert(count === 6, 'Six rows should have been streamed');
|
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() {
|
|
|
|
if (!_.includes(['mysql', 'mysql2'], knex.client.dialect)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const rows = []
|
|
|
|
return knex('accounts')
|
|
|
|
.options({
|
|
|
|
typeCast (field, next) {
|
|
|
|
var val
|
|
|
|
if (field.type === 'VAR_STRING') {
|
|
|
|
val = field.string()
|
|
|
|
return val == null ? val : val.toUpperCase()
|
|
|
|
}
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.stream(function(rowStream) {
|
|
|
|
rowStream.on('data', function(row) {
|
|
|
|
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-10-03 17:39:10 +01:00
|
|
|
it('emits error on the stream, if not passed a function, and connecting fails', function() {
|
|
|
|
var expected = new Error();
|
|
|
|
var original = Runner.prototype.ensureConnection;
|
|
|
|
Runner.prototype.ensureConnection = function() {
|
|
|
|
return Promise.reject(expected);
|
|
|
|
};
|
|
|
|
|
|
|
|
var restore = () => {
|
|
|
|
Runner.prototype.ensureConnection = original;
|
|
|
|
};
|
|
|
|
|
|
|
|
var promise = new Promise((resolve, reject) => {
|
|
|
|
var timeout = setTimeout(() => {
|
|
|
|
reject(new Error('Timeout'));
|
|
|
|
}, 5000);
|
|
|
|
|
|
|
|
var stream = knex('accounts').stream();
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
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;
|
2014-07-10 12:31:18 -04:00
|
|
|
return knex('accounts').where('id', 1).stream(function(rowStream) {
|
2014-09-02 22:56:51 +02:00
|
|
|
rowStream.on('data', function() {
|
2014-07-10 12:31:18 -04:00
|
|
|
count++;
|
|
|
|
});
|
|
|
|
}).then(function() {
|
|
|
|
assert(count === 1, 'One row should have been streamed');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-09 17:23:07 -04:00
|
|
|
it('throws errors on the asCallback if uncaught in the last block', function(ok) {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2016-08-09 17:23:07 -04:00
|
|
|
knex('accounts').select().asCallback(function() {
|
2014-09-02 22:25:02 +02:00
|
|
|
console.log(this.undefinedVar.test);
|
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(
|
|
|
|
'oracle',
|
|
|
|
"select \"id\" from \"accounts\" order by \"id\" desc",
|
|
|
|
[],
|
|
|
|
[7, 5, 4, 3, 2, 1]
|
|
|
|
);
|
2015-12-09 17:53:53 -06:00
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
"select [id] from [accounts] order by [id] desc",
|
|
|
|
[],
|
|
|
|
['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() {
|
2013-09-11 23:36:55 -04:00
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
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],
|
|
|
|
[{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User'
|
|
|
|
}]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'postgresql',
|
|
|
|
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
2014-08-21 23:39:12 +02:00
|
|
|
[1],
|
|
|
|
[{
|
|
|
|
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],
|
|
|
|
[{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User'
|
|
|
|
}]
|
|
|
|
);
|
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],
|
|
|
|
[{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User'
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'oracle',
|
|
|
|
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
|
|
|
[1],
|
|
|
|
[{
|
|
|
|
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],
|
|
|
|
[{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User'
|
|
|
|
}]
|
|
|
|
);
|
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],
|
|
|
|
[{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User'
|
|
|
|
}]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'postgresql',
|
|
|
|
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
2014-08-21 23:39:12 +02:00
|
|
|
[1],
|
|
|
|
[{
|
|
|
|
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],
|
|
|
|
[{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User'
|
|
|
|
}]
|
|
|
|
);
|
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],
|
|
|
|
[{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User'
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'oracle',
|
|
|
|
'select "first_name", "last_name" from "accounts" where "id" = ?',
|
|
|
|
[1],
|
|
|
|
[{
|
|
|
|
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],
|
|
|
|
[{
|
|
|
|
first_name: 'Test',
|
|
|
|
last_name: 'User'
|
|
|
|
}]
|
|
|
|
);
|
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(
|
|
|
|
'postgresql',
|
|
|
|
'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(
|
|
|
|
'oracle',
|
|
|
|
'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')
|
|
|
|
.where({'id': 1})
|
|
|
|
.select('*')
|
|
|
|
.testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select * from `accounts` where `id` = ?',
|
|
|
|
[1],
|
|
|
|
[{
|
|
|
|
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
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'postgresql',
|
|
|
|
'select * from "accounts" where "id" = ?',
|
2014-08-21 23:39:12 +02:00
|
|
|
[1],
|
|
|
|
[{
|
|
|
|
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
|
|
|
|
}]
|
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],
|
|
|
|
[{
|
|
|
|
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
|
|
|
|
}]
|
|
|
|
);
|
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],
|
|
|
|
[{
|
|
|
|
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
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'oracle',
|
|
|
|
'select * from "accounts" where "id" = ?',
|
|
|
|
[1],
|
|
|
|
[{
|
|
|
|
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
|
|
|
|
}]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
2015-12-09 17:53:53 -06:00
|
|
|
tester(
|
|
|
|
'mssql',
|
|
|
|
'select * from [accounts] where [id] = ?',
|
|
|
|
[1],
|
|
|
|
[{
|
|
|
|
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
|
|
|
|
}]
|
|
|
|
);
|
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')
|
|
|
|
.where({'id': null})
|
|
|
|
.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(
|
|
|
|
'postgresql',
|
|
|
|
'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(
|
|
|
|
'oracle',
|
|
|
|
'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')
|
|
|
|
.where({'id': 0})
|
|
|
|
.select()
|
|
|
|
.testSql(function(tester) {
|
|
|
|
tester(
|
|
|
|
'mysql',
|
|
|
|
'select * from `accounts` where `id` = ?',
|
2014-08-21 23:39:12 +02:00
|
|
|
[0],
|
|
|
|
[]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
|
|
|
tester(
|
|
|
|
'postgresql',
|
|
|
|
'select * from "accounts" where "id" = ?',
|
2014-08-21 23:39:12 +02:00
|
|
|
[0],
|
|
|
|
[]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
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(
|
|
|
|
'oracle',
|
|
|
|
'select * from "accounts" where "id" = ?',
|
|
|
|
[0],
|
|
|
|
[]
|
2013-12-27 14:44:21 -05:00
|
|
|
);
|
2015-12-09 17:53:53 -06: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([
|
2013-09-11 23:36:55 -04:00
|
|
|
knex('accounts').select().distinct('email').where('logins', 2).orderBy('email'),
|
|
|
|
knex('accounts').distinct('email').select().orderBy('email')
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does "orWhere" cases', function() {
|
2014-04-16 02:50:19 -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() {
|
2014-04-16 02:50:19 -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([
|
2013-09-11 23:36:55 -04:00
|
|
|
knex('accounts').where(function() {
|
|
|
|
this.where('id', 2);
|
|
|
|
this.orWhere('id', 3);
|
|
|
|
}).select('*')
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles "where in" cases', function() {
|
2013-10-27 22:34:58 -04:00
|
|
|
return Promise.all([
|
2013-09-11 23:36:55 -04:00
|
|
|
knex('accounts').whereIn('id', [1, 2, 3]).select()
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
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() {
|
2015-12-09 17:53:53 -06:00
|
|
|
if (knex.client.dialect !== 'sqlite3' && knex.client.dialect !== '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) {
|
|
|
|
tester('mysql',
|
2018-02-03 08:33:02 -05:00
|
|
|
'select * from `composite_key_test` where (`column_a`, `column_b`) in ((?, ?),(?, ?)) order by `status` desc',
|
2014-04-16 02:50:19 -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
|
2018-02-03 08:33:02 -05:00
|
|
|
}]);
|
2014-04-16 02:50:19 -04:00
|
|
|
tester('postgresql',
|
2018-02-03 08:33:02 -05:00
|
|
|
'select * from "composite_key_test" where ("column_a", "column_b") in ((?, ?),(?, ?)) order by "status" desc',
|
2014-04-16 02:50:19 -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
|
|
|
|
}]);
|
2018-02-03 08:33:02 -05:00
|
|
|
tester('pg-redshift',
|
|
|
|
'select * from "composite_key_test" where ("column_a", "column_b") in ((?, ?),(?, ?)) order by "status" desc',
|
|
|
|
[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-08-21 23:39:12 +02:00
|
|
|
tester('oracle',
|
2018-02-03 08:33:02 -05:00
|
|
|
'select * from "composite_key_test" where ("column_a","column_b") in ((?, ?),(?, ?)) order by "status" desc',
|
2014-08-21 23:39:12 +02: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() {
|
2015-12-09 17:53:53 -06:00
|
|
|
if (knex.client.dialect !== 'sqlite3' && knex.client.dialect !== '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) {
|
|
|
|
tester('mysql',
|
2015-04-22 10:34:14 -04:00
|
|
|
'select * from `composite_key_test` where `status` = ? and (`column_a`, `column_b`) in ((?, ?),(?, ?))',
|
2014-04-16 02:50:19 -04:00
|
|
|
[1,1,1,1,2],
|
|
|
|
[{
|
|
|
|
column_a: 1,
|
|
|
|
column_b: 1,
|
|
|
|
details: 'One, One, One',
|
|
|
|
status: 1
|
|
|
|
}]);
|
|
|
|
tester('postgresql',
|
2015-04-22 10:34:14 -04:00
|
|
|
'select * from "composite_key_test" where "status" = ? and ("column_a", "column_b") in ((?, ?),(?, ?))',
|
2014-04-16 02:50:19 -04:00
|
|
|
[1,1,1,1,2],
|
|
|
|
[{
|
|
|
|
column_a: 1,
|
|
|
|
column_b: 1,
|
|
|
|
details: 'One, One, One',
|
|
|
|
status: 1
|
|
|
|
}]);
|
2018-02-03 08:33:02 -05:00
|
|
|
tester('pg-redshift',
|
|
|
|
'select * from "composite_key_test" where "status" = ? and ("column_a", "column_b") in ((?, ?),(?, ?))',
|
|
|
|
[1,1,1,1,2],
|
|
|
|
[{
|
|
|
|
column_a: 1,
|
|
|
|
column_b: 1,
|
|
|
|
details: 'One, One, One',
|
|
|
|
status: 1
|
|
|
|
}]);
|
2014-08-21 23:39:12 +02:00
|
|
|
tester('oracle',
|
2015-04-22 10:34:14 -04:00
|
|
|
'select * from "composite_key_test" where "status" = ? and ("column_a", "column_b") in ((?, ?),(?, ?))',
|
2014-08-21 23:39:12 +02: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() {
|
2013-09-11 23:36:55 -04:00
|
|
|
this.select('id').from('test_table_two').where({id: 1});
|
|
|
|
})
|
|
|
|
.select();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles "where between"', function() {
|
|
|
|
return knex('accounts').whereBetween('id', [1, 100]).select();
|
|
|
|
});
|
|
|
|
|
|
|
|
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() {
|
2014-08-11 12:25:39 +02:00
|
|
|
if (knex.client.dialect === 'oracle') {
|
|
|
|
// special case for oracle
|
|
|
|
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();
|
|
|
|
} 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() {
|
|
|
|
return knex('accounts').whereIn('id', function() {
|
|
|
|
this.select('account_id').from('test_table_two').where('status', 1);
|
|
|
|
}).select('first_name', 'last_name');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("supports the <> operator", function() {
|
|
|
|
return knex('accounts').where('id', '<>', 2).select('email', 'logins');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Allows for knex.Raw passed to the `where` clause", function() {
|
2014-08-11 12:25:39 +02:00
|
|
|
if (knex.client.dialect === 'oracle') {
|
|
|
|
return knex('accounts').where(knex.raw('"id" = 2')).select('email', 'logins');
|
|
|
|
} else {
|
|
|
|
return knex('accounts').where(knex.raw('id = 2')).select('email', 'logins');
|
|
|
|
}
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
2014-04-16 03:22:47 -04:00
|
|
|
it('Retains array bindings, #228', function() {
|
2018-02-03 08:33:02 -05: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']]);
|
|
|
|
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() {
|
2015-03-24 17:48:14 -07:00
|
|
|
if (knex.client.dialect === 'postgresql') {
|
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() {
|
|
|
|
if (knex.client.dialect === 'postgresql') {
|
2018-02-03 08:33:02 -05: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"');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
});
|
|
|
|
|
2014-08-21 23:39:12 +02:00
|
|
|
};
|