2015-04-27 15:58:48 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Raw = require('../../lib/raw');
|
|
|
|
var Client = require('../../lib/client')
|
|
|
|
var test = require('tape')
|
2016-06-17 10:57:03 -07:00
|
|
|
var _ = require('lodash');
|
2015-04-27 15:58:48 -04:00
|
|
|
|
|
|
|
var client = new Client()
|
|
|
|
function raw(sql, bindings) {
|
|
|
|
return new Raw(client).set(sql, bindings)
|
|
|
|
}
|
|
|
|
|
|
|
|
test('allows for ?? to interpolate identifiers', function(t) {
|
|
|
|
t.plan(1)
|
|
|
|
t.equal(
|
2015-11-06 01:19:47 +02:00
|
|
|
raw('select * from ?? where id = ? and ?? = ??', ['table', 1, 'table.first', 'table.second']).toString(),
|
|
|
|
'select * from "table" where id = 1 and "table"."first" = "table"."second"'
|
2015-04-27 15:58:48 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('allows for object bindings', function(t) {
|
|
|
|
t.plan(1)
|
|
|
|
t.equal(
|
|
|
|
raw('select * from users where user_id = :userId and name = :name', {userId: 1, name: 'tim'}).toString(),
|
|
|
|
"select * from users where user_id = 1 and name = 'tim'"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('allows for :val: for interpolated identifiers', function(t) {
|
|
|
|
t.plan(1)
|
|
|
|
t.equal(
|
|
|
|
raw('select * from :table: where user_id = :userId and name = :name', {table: 'users', userId: 1, name: 'tim'}).toString(),
|
|
|
|
"select * from \"users\" where user_id = 1 and name = 'tim'"
|
|
|
|
)
|
|
|
|
})
|
2015-04-30 18:07:16 -04:00
|
|
|
|
2015-11-06 19:18:21 +02:00
|
|
|
test('allows use :val: in start of raw query', function(t) {
|
|
|
|
t.plan(1)
|
|
|
|
t.equal(
|
|
|
|
raw(':userIdCol: = :userId', {userIdCol: 'table', userId: 1}).toString(),
|
|
|
|
"\"table\" = 1"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('allows use :val in start of raw query', function(t) {
|
|
|
|
t.plan(1)
|
|
|
|
t.equal(
|
|
|
|
raw(':userId', {userId: 1}).toString(),
|
|
|
|
"1"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2015-11-06 01:19:47 +02:00
|
|
|
test('allows for :val: to be interpolated when identifiers with dots', function(t) {
|
|
|
|
t.plan(1)
|
|
|
|
t.equal(
|
|
|
|
raw('select * from "table" join "chair" on :tableCol: = :chairCol:', {tableCol: 'table.id', chairCol: 'chair.table_id'}).toString(),
|
|
|
|
'select * from "table" join "chair" on "table"."id" = "chair"."table_id"'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2015-04-30 18:07:16 -04:00
|
|
|
test('allows for options in raw queries, #605', function(t) {
|
|
|
|
t.plan(1)
|
|
|
|
var x = raw("select 'foo', 'bar';")
|
|
|
|
.options({ rowMode: "array" })
|
|
|
|
.toSQL()
|
|
|
|
|
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';",
|
|
|
|
options: {rowMode: "array"},
|
|
|
|
method: 'raw',
|
2016-03-15 17:51:10 +01:00
|
|
|
bindings: []
|
2015-04-30 18:07:16 -04:00
|
|
|
})
|
|
|
|
})
|
2015-05-06 10:48:05 -04:00
|
|
|
|
2015-06-01 09:14:45 -04:00
|
|
|
test('raw bindings are optional, #853', function(t) {
|
2016-06-17 10:57:03 -07:00
|
|
|
|
2015-06-01 07:30:38 -04:00
|
|
|
t.plan(2)
|
|
|
|
|
|
|
|
var sql = raw('select * from ? where id=?', [raw('foo'), 4]).toSQL()
|
|
|
|
|
|
|
|
t.equal(sql.sql, 'select * from foo where id=?')
|
|
|
|
|
|
|
|
t.deepEqual(sql.bindings, [4])
|
|
|
|
|
|
|
|
})
|
2016-03-15 21:33:39 +01:00
|
|
|
|