2015-04-27 15:58:48 -04:00
|
|
|
'use strict';
|
|
|
|
|
2019-07-10 22:48:43 +01:00
|
|
|
const Raw = require('../../lib/raw');
|
2021-01-02 19:56:29 +05:30
|
|
|
const MysqlClient = require('../../lib/dialects/mysql/index');
|
|
|
|
const MssqlClient = require('../../lib/dialects/mssql/index');
|
|
|
|
const PostgreClient = require('../../lib/dialects/postgres/index');
|
|
|
|
const SqliteClient = require('../../lib/dialects/sqlite3/index');
|
2018-10-15 22:29:53 -04:00
|
|
|
const test = require('tape');
|
|
|
|
const _ = require('lodash');
|
2015-04-27 15:58:48 -04:00
|
|
|
|
2021-01-02 19:56:29 +05:30
|
|
|
const clientMysql = new MysqlClient({ client: 'mysql' });
|
|
|
|
const clientPostgre = new PostgreClient({ client: 'pg' });
|
|
|
|
const clientSqlite = new SqliteClient({ client: 'sqlite3' });
|
|
|
|
const clientMssql = new MssqlClient({ client: 'mssql' });
|
|
|
|
|
|
|
|
function raw(sql, bindings, client = clientMysql) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new Raw(client).set(sql, bindings);
|
2015-04-27 15:58:48 -04:00
|
|
|
}
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('allows for ?? to interpolate identifiers', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2015-04-27 15:58:48 -04:00
|
|
|
t.equal(
|
2018-07-09 08:10:34 -04:00
|
|
|
raw('select * from ?? where id = ? and ?? = ??', [
|
|
|
|
'table',
|
|
|
|
1,
|
|
|
|
'table.first',
|
|
|
|
'table.second',
|
|
|
|
]).toString(),
|
2021-01-02 19:56:29 +05:30
|
|
|
'select * from `table` where id = 1 and `table`.`first` = `table`.`second`'
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
|
|
|
});
|
2015-04-27 15:58:48 -04:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('allows for object bindings', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2015-04-27 15:58:48 -04:00
|
|
|
t.equal(
|
2018-07-09 08:10:34 -04:00
|
|
|
raw('select * from users where user_id = :userId and name = :name', {
|
|
|
|
userId: 1,
|
|
|
|
name: 'tim',
|
|
|
|
}).toString(),
|
2015-04-27 15:58:48 -04:00
|
|
|
"select * from users where user_id = 1 and name = 'tim'"
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
|
|
|
});
|
2015-04-27 15:58:48 -04:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('allows for :val: for interpolated identifiers', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2015-04-27 15:58:48 -04:00
|
|
|
t.equal(
|
2018-07-09 08:10:34 -04:00
|
|
|
raw('select * from :table: where user_id = :userId and name = :name', {
|
|
|
|
table: 'users',
|
|
|
|
userId: 1,
|
|
|
|
name: 'tim',
|
|
|
|
}).toString(),
|
2021-01-02 19:56:29 +05:30
|
|
|
"select * from `users` where user_id = 1 and name = 'tim'"
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
|
|
|
});
|
2015-04-30 18:07:16 -04:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('allows use :val: in start of raw query', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2015-11-06 19:18:21 +02:00
|
|
|
t.equal(
|
2018-07-09 08:10:34 -04:00
|
|
|
raw(':userIdCol: = :userId', { userIdCol: 'table', userId: 1 }).toString(),
|
2021-01-02 19:56:29 +05:30
|
|
|
'`table` = 1'
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
|
|
|
});
|
2015-11-06 19:18:21 +02:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('allows use :val in start of raw query', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
|
|
|
t.equal(raw(':userId', { userId: 1 }).toString(), '1');
|
|
|
|
});
|
2015-11-06 19:18:21 +02:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('allows for :val: to be interpolated when identifiers with dots', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2015-11-06 01:19:47 +02:00
|
|
|
t.equal(
|
2018-07-09 08:10:34 -04:00
|
|
|
raw('select * from "table" join "chair" on :tableCol: = :chairCol:', {
|
|
|
|
tableCol: 'table.id',
|
|
|
|
chairCol: 'chair.table_id',
|
|
|
|
}).toString(),
|
2021-01-02 19:56:29 +05:30
|
|
|
'select * from "table" join "chair" on `table`.`id` = `chair`.`table_id`'
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
|
|
|
});
|
2015-11-06 01:19:47 +02:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('allows for options in raw queries, #605', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2020-04-19 00:40:23 +02:00
|
|
|
const x = raw("select 'foo', 'bar';").options({ rowMode: 'array' }).toSQL();
|
2015-04-30 18:07:16 -04:00
|
|
|
|
2016-06-17 11:04:35 -07:00
|
|
|
t.deepEqual(_.pick(x, ['sql', 'options', 'method', 'bindings']), {
|
2015-04-30 18:07:16 -04:00
|
|
|
sql: "select 'foo', 'bar';",
|
2018-07-09 08:10:34 -04:00
|
|
|
options: { rowMode: 'array' },
|
2015-04-30 18:07:16 -04:00
|
|
|
method: 'raw',
|
2018-07-09 08:10:34 -04:00
|
|
|
bindings: [],
|
|
|
|
});
|
|
|
|
});
|
2015-05-06 10:48:05 -04:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('raw bindings are optional, #853', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(2);
|
2016-06-17 10:57:03 -07:00
|
|
|
|
2018-10-15 22:29:53 -04:00
|
|
|
const sql = raw('select * from ? where id=?', [raw('foo'), 4]).toSQL();
|
2015-06-01 07:30:38 -04:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
t.equal(sql.sql, 'select * from foo where id=?');
|
2016-03-15 21:33:39 +01:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
t.deepEqual(sql.bindings, [4]);
|
|
|
|
});
|
2021-01-02 19:56:29 +05:30
|
|
|
|
|
|
|
test('Allows retrieval of raw query through toNative (MySQL)', function (t) {
|
|
|
|
t.plan(2);
|
|
|
|
t.deepEqual(
|
|
|
|
raw('count(*) as user_count, status', undefined, clientMysql)
|
|
|
|
.toSQL()
|
|
|
|
.toNative(),
|
|
|
|
{
|
|
|
|
sql: 'count(*) as user_count, status',
|
|
|
|
bindings: [],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
t.deepEqual(
|
|
|
|
raw('select * from users where id = ?', [1], clientMysql)
|
|
|
|
.toSQL()
|
|
|
|
.toNative(),
|
|
|
|
{
|
|
|
|
sql: 'select * from users where id = ?',
|
|
|
|
bindings: [1],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Allows retrieval of raw query through toNative (PostgreSQL)', function (t) {
|
|
|
|
t.plan(2);
|
|
|
|
t.deepEqual(
|
|
|
|
raw('count(*) as user_count, status', undefined, clientPostgre)
|
|
|
|
.toSQL()
|
|
|
|
.toNative(),
|
|
|
|
{
|
|
|
|
sql: 'count(*) as user_count, status',
|
|
|
|
bindings: [],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
t.deepEqual(
|
|
|
|
raw('select * from users where id = ?', [1], clientPostgre)
|
|
|
|
.toSQL()
|
|
|
|
.toNative(),
|
|
|
|
{
|
|
|
|
sql: 'select * from users where id = $1',
|
|
|
|
bindings: [1],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Allows retrieval of raw query through toNative (SQLite)', function (t) {
|
|
|
|
t.plan(2);
|
|
|
|
t.deepEqual(
|
|
|
|
raw('count(*) as user_count, status', undefined, clientSqlite)
|
|
|
|
.toSQL()
|
|
|
|
.toNative(),
|
|
|
|
{
|
|
|
|
sql: 'count(*) as user_count, status',
|
|
|
|
bindings: [],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
t.deepEqual(
|
|
|
|
raw('select * from users where id = ?', [1], clientSqlite)
|
|
|
|
.toSQL()
|
|
|
|
.toNative(),
|
|
|
|
{
|
|
|
|
sql: 'select * from users where id = ?',
|
|
|
|
bindings: [1],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Allows retrieval of raw query through toNative (mssql)', function (t) {
|
|
|
|
t.plan(2);
|
|
|
|
t.deepEqual(
|
|
|
|
raw('count(*) as user_count, status', undefined, clientMssql)
|
|
|
|
.toSQL()
|
|
|
|
.toNative(),
|
|
|
|
{
|
|
|
|
sql: 'count(*) as user_count, status',
|
|
|
|
bindings: [],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
t.deepEqual(
|
|
|
|
raw('select * from users where id = ?', [1], clientMssql)
|
|
|
|
.toSQL()
|
|
|
|
.toNative(),
|
|
|
|
{
|
|
|
|
sql: 'select * from users where id = @p0',
|
|
|
|
bindings: [1],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|