2019-04-18 21:32:38 +00:00
|
|
|
// Copyright IBM Corp. 2015,2019. All Rights Reserved.
|
2016-05-06 04:50:59 +00:00
|
|
|
// Node module: loopback-connector
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2017-03-06 23:40:47 +00:00
|
|
|
'use strict';
|
2019-08-02 13:50:51 +00:00
|
|
|
const expect = require('chai').expect;
|
|
|
|
const SQLConnector = require('../lib/sql');
|
|
|
|
const ParameterizedSQL = SQLConnector.ParameterizedSQL;
|
|
|
|
const testConnector = require('./connectors/test-sql-connector');
|
2015-05-13 17:14:44 +00:00
|
|
|
|
2019-08-02 13:50:51 +00:00
|
|
|
const juggler = require('loopback-datasource-juggler');
|
|
|
|
const ds = new juggler.DataSource({
|
2015-05-13 17:14:44 +00:00
|
|
|
connector: testConnector,
|
2016-04-09 18:35:52 +00:00
|
|
|
debug: true,
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
2016-04-09 19:00:28 +00:00
|
|
|
/* eslint-disable one-var */
|
2019-08-02 13:50:51 +00:00
|
|
|
let connector;
|
|
|
|
let Customer;
|
2016-04-09 19:00:28 +00:00
|
|
|
/* eslint-enable one-var */
|
2015-05-13 17:14:44 +00:00
|
|
|
|
|
|
|
describe('sql connector', function() {
|
|
|
|
before(function() {
|
|
|
|
connector = ds.connector;
|
|
|
|
connector._tables = {};
|
|
|
|
connector._models = {};
|
2015-05-22 16:03:27 +00:00
|
|
|
Customer = ds.createModel('customer',
|
2015-05-13 17:14:44 +00:00
|
|
|
{
|
|
|
|
name: {
|
|
|
|
id: true,
|
|
|
|
type: String,
|
|
|
|
testdb: {
|
|
|
|
column: 'NAME',
|
|
|
|
dataType: 'VARCHAR',
|
2016-04-09 18:35:52 +00:00
|
|
|
dataLength: 32,
|
|
|
|
},
|
2019-04-05 08:56:19 +00:00
|
|
|
}, middleName: {
|
|
|
|
type: Boolean,
|
|
|
|
name: 'middle_name',
|
|
|
|
postgresql: {
|
|
|
|
column: 'MIDDLENAME',
|
|
|
|
},
|
|
|
|
}, lastName: {
|
|
|
|
type: Boolean,
|
|
|
|
name: 'last_name',
|
|
|
|
testdb: {
|
|
|
|
column: 'LASTNAME',
|
|
|
|
},
|
2015-05-13 17:14:44 +00:00
|
|
|
}, vip: {
|
2016-04-09 18:35:52 +00:00
|
|
|
type: Boolean,
|
|
|
|
testdb: {
|
|
|
|
column: 'VIP',
|
|
|
|
},
|
2019-04-05 08:56:19 +00:00
|
|
|
}, primaryAddress: {
|
|
|
|
type: String,
|
|
|
|
name: 'primary_address',
|
2016-04-09 18:35:52 +00:00
|
|
|
},
|
|
|
|
address: String,
|
2015-05-13 17:14:44 +00:00
|
|
|
},
|
2017-03-06 23:40:47 +00:00
|
|
|
{testdb: {table: 'CUSTOMER'}});
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should map table name', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const table = connector.table('customer');
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(table).to.eql('CUSTOMER');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should map column name', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const column = connector.column('customer', 'name');
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(column).to.eql('NAME');
|
|
|
|
});
|
|
|
|
|
2019-04-05 08:56:19 +00:00
|
|
|
it('should map column name from name attribute', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const column = connector.column('customer', 'primaryAddress');
|
2019-04-05 08:56:19 +00:00
|
|
|
expect(column).to.eql('primary_address');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('prefers database-specific column name over property name', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const column = connector.column('customer', 'lastName');
|
2019-04-05 08:56:19 +00:00
|
|
|
expect(column).to.eql('LASTNAME');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('prefers property name when database is different', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const column = connector.column('customer', 'middleName');
|
2019-04-05 08:56:19 +00:00
|
|
|
expect(column).to.eql('middle_name');
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:14:44 +00:00
|
|
|
it('should find column metadata', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const column = connector.columnMetadata('customer', 'name');
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(column).to.eql({
|
|
|
|
column: 'NAME',
|
|
|
|
dataType: 'VARCHAR',
|
2016-04-09 18:35:52 +00:00
|
|
|
dataLength: 32,
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should map property name', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const prop = connector.propertyName('customer', 'NAME');
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(prop).to.eql('name');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should map id column name', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const idCol = connector.idColumn('customer');
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(idCol).to.eql('NAME');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find escaped id column name', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const idCol = connector.idColumnEscaped('customer');
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(idCol).to.eql('`NAME`');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find escaped table name', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const table = connector.tableEscaped('customer');
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(table).to.eql('`CUSTOMER`');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find escaped column name', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const column = connector.columnEscaped('customer', 'vip');
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(column).to.eql('`VIP`');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should convert to escaped id column value', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const column = connector.idColumnValue('customer', 'John');
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(column).to.eql('John');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds where', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const where = connector.buildWhere('customer', {name: 'John'});
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(where.toJSON()).to.eql({
|
|
|
|
sql: 'WHERE `NAME`=?',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: ['John'],
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds where with null', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const where = connector.buildWhere('customer', {name: null});
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(where.toJSON()).to.eql({
|
|
|
|
sql: 'WHERE `NAME` IS NULL',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: [],
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds where with inq', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const where = connector.buildWhere('customer', {name: {inq: ['John', 'Mary']}});
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(where.toJSON()).to.eql({
|
|
|
|
sql: 'WHERE `NAME` IN (?,?)',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: ['John', 'Mary'],
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds where with or', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const where = connector.buildWhere('customer',
|
2017-03-06 23:40:47 +00:00
|
|
|
{or: [{name: 'John'}, {name: 'Mary'}]});
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(where.toJSON()).to.eql({
|
|
|
|
sql: 'WHERE (`NAME`=?) OR (`NAME`=?)',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: ['John', 'Mary'],
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds where with and', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const where = connector.buildWhere('customer',
|
2017-03-06 23:40:47 +00:00
|
|
|
{and: [{name: 'John'}, {vip: true}]});
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(where.toJSON()).to.eql({
|
|
|
|
sql: 'WHERE (`NAME`=?) AND (`VIP`=?)',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: ['John', true],
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-21 00:06:46 +00:00
|
|
|
it('builds where with a regexp string that does not have flags', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const where = connector.buildWhere('customer', {
|
2015-07-21 00:06:46 +00:00
|
|
|
name: {
|
2016-04-09 18:35:52 +00:00
|
|
|
regexp: '^J',
|
|
|
|
},
|
2015-07-21 00:06:46 +00:00
|
|
|
});
|
|
|
|
expect(where.toJSON()).to.eql({
|
|
|
|
sql: 'WHERE `NAME` REGEXP ?',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: ['^J'],
|
2015-07-21 00:06:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds where with a regexp string that has flags', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const where = connector.buildWhere('customer', {
|
2015-07-21 00:06:46 +00:00
|
|
|
name: {
|
2016-04-09 18:35:52 +00:00
|
|
|
regexp: '^J/i',
|
|
|
|
},
|
2015-07-21 00:06:46 +00:00
|
|
|
});
|
|
|
|
expect(where.toJSON()).to.eql({
|
|
|
|
sql: 'WHERE `NAME` REGEXP ?',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: ['^J/i'],
|
2015-07-21 00:06:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds where with a regexp literal that does not have flags', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const where = connector.buildWhere('customer', {
|
2015-07-21 00:06:46 +00:00
|
|
|
name: {
|
2016-04-09 18:35:52 +00:00
|
|
|
regexp: /^J/,
|
|
|
|
},
|
2015-07-21 00:06:46 +00:00
|
|
|
});
|
|
|
|
expect(where.toJSON()).to.eql({
|
|
|
|
sql: 'WHERE `NAME` REGEXP ?',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: [/^J/],
|
2015-07-21 00:06:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds where with a regexp literal that has flags', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const where = connector.buildWhere('customer', {
|
2015-07-21 00:06:46 +00:00
|
|
|
name: {
|
2016-04-09 18:35:52 +00:00
|
|
|
regexp: /^J/i,
|
|
|
|
},
|
2015-07-21 00:06:46 +00:00
|
|
|
});
|
|
|
|
expect(where.toJSON()).to.eql({
|
|
|
|
sql: 'WHERE `NAME` REGEXP ?',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: [/^J/i],
|
2015-07-21 00:06:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds where with a regexp object that does not have flags', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const where = connector.buildWhere('customer', {
|
2015-07-21 00:06:46 +00:00
|
|
|
name: {
|
2016-04-09 18:35:52 +00:00
|
|
|
regexp: new RegExp(/^J/),
|
|
|
|
},
|
2015-07-21 00:06:46 +00:00
|
|
|
});
|
|
|
|
expect(where.toJSON()).to.eql({
|
|
|
|
sql: 'WHERE `NAME` REGEXP ?',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: [/^J/],
|
2015-07-21 00:06:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds where with a regexp object that has flags', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const where = connector.buildWhere('customer', {
|
2015-07-21 00:06:46 +00:00
|
|
|
name: {
|
2016-04-09 18:35:52 +00:00
|
|
|
regexp: new RegExp(/^J/i),
|
|
|
|
},
|
2015-07-21 00:06:46 +00:00
|
|
|
});
|
|
|
|
expect(where.toJSON()).to.eql({
|
|
|
|
sql: 'WHERE `NAME` REGEXP ?',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: [new RegExp(/^J/i)],
|
2015-07-21 00:06:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-06-25 06:03:13 +00:00
|
|
|
it('builds where with nesting and/or', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const where = connector.buildWhere('customer',
|
2017-03-06 23:40:47 +00:00
|
|
|
{and: [{name: 'John'}, {or: [{vip: true}, {address: null}]}]});
|
2015-06-25 06:03:13 +00:00
|
|
|
expect(where.toJSON()).to.eql({
|
|
|
|
sql: 'WHERE (`NAME`=?) AND ((`VIP`=?) OR (`ADDRESS` IS NULL))',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: ['John', true],
|
2015-06-25 06:03:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-13 17:14:44 +00:00
|
|
|
it('builds order by with one field', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const orderBy = connector.buildOrderBy('customer', 'name');
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(orderBy).to.eql('ORDER BY `NAME`');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds order by with two fields', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const orderBy = connector.buildOrderBy('customer', ['name', 'vip']);
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(orderBy).to.eql('ORDER BY `NAME`,`VIP`');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds order by with two fields and dirs', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const orderBy = connector.buildOrderBy('customer', ['name ASC', 'vip DESC']);
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(orderBy).to.eql('ORDER BY `NAME` ASC,`VIP` DESC');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds fields for columns', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const fields = connector.buildFields('customer',
|
2017-03-06 23:40:47 +00:00
|
|
|
{name: 'John', vip: true, unknown: 'Random'});
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(fields.names).to.eql(['`NAME`', '`VIP`']);
|
|
|
|
expect(fields.columnValues[0].toJSON()).to.eql(
|
2019-08-02 13:50:51 +00:00
|
|
|
{sql: '?', params: ['John']}
|
|
|
|
);
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(fields.columnValues[1].toJSON()).to.eql(
|
2019-08-02 13:50:51 +00:00
|
|
|
{sql: '?', params: [true]}
|
|
|
|
);
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('builds fields for UPDATE without ids', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const fields = connector.buildFieldsForUpdate('customer',
|
2017-03-06 23:40:47 +00:00
|
|
|
{name: 'John', vip: true});
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(fields.toJSON()).to.eql({
|
|
|
|
sql: 'SET `VIP`=?',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: [true],
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds fields for UPDATE with ids', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const fields = connector.buildFieldsForUpdate('customer',
|
2017-03-06 23:40:47 +00:00
|
|
|
{name: 'John', vip: true}, false);
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(fields.toJSON()).to.eql({
|
|
|
|
sql: 'SET `NAME`=?,`VIP`=?',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: ['John', true],
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds column names for SELECT', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const cols = connector.buildColumnNames('customer');
|
2019-04-05 08:56:19 +00:00
|
|
|
expect(cols).to.eql('`NAME`,`middle_name`,`LASTNAME`,`VIP`,' +
|
|
|
|
'`primary_address`,`ADDRESS`');
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('builds column names with true fields filter for SELECT', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const cols = connector.buildColumnNames('customer', {fields: {name: true}});
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(cols).to.eql('`NAME`');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds column names with false fields filter for SELECT', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const cols = connector.buildColumnNames('customer', {
|
2019-04-05 08:56:19 +00:00
|
|
|
fields: {
|
|
|
|
name: false,
|
|
|
|
primaryAddress: false,
|
|
|
|
lastName: false,
|
|
|
|
middleName: false,
|
|
|
|
},
|
|
|
|
});
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(cols).to.eql('`VIP`,`ADDRESS`');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds column names with array fields filter for SELECT', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const cols = connector.buildColumnNames('customer', {fields: ['name']});
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(cols).to.eql('`NAME`');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds DELETE', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const sql = connector.buildDelete('customer', {name: 'John'});
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(sql.toJSON()).to.eql({
|
|
|
|
sql: 'DELETE FROM `CUSTOMER` WHERE `NAME`=$1',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: ['John'],
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds UPDATE', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const sql = connector.buildUpdate('customer', {name: 'John'}, {vip: false});
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(sql.toJSON()).to.eql({
|
|
|
|
sql: 'UPDATE `CUSTOMER` SET `VIP`=$1 WHERE `NAME`=$2',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: [false, 'John'],
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds SELECT', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const sql = connector.buildSelect('customer',
|
2017-03-06 23:40:47 +00:00
|
|
|
{order: 'name', limit: 5, where: {name: 'John'}});
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(sql.toJSON()).to.eql({
|
2019-04-05 08:56:19 +00:00
|
|
|
sql: 'SELECT `NAME`,`middle_name`,`LASTNAME`,`VIP`,`primary_address`,`ADDRESS`' +
|
|
|
|
' FROM `CUSTOMER`' +
|
2015-05-13 17:14:44 +00:00
|
|
|
' WHERE `NAME`=$1 ORDER BY `NAME` LIMIT 5',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: ['John'],
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('builds INSERT', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const sql = connector.buildInsert('customer', {name: 'John', vip: true});
|
2015-05-13 17:14:44 +00:00
|
|
|
expect(sql.toJSON()).to.eql({
|
|
|
|
sql: 'INSERT INTO `CUSTOMER`(`NAME`,`VIP`) VALUES($1,$2)',
|
2016-04-09 18:35:52 +00:00
|
|
|
params: ['John', true],
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('normalizes a SQL statement from string', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const sql = 'SELECT * FROM `CUSTOMER`';
|
|
|
|
const stmt = new ParameterizedSQL(sql);
|
2017-03-06 23:40:47 +00:00
|
|
|
expect(stmt.toJSON()).to.eql({sql: sql, params: []});
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('normalizes a SQL statement from object without params', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const sql = {sql: 'SELECT * FROM `CUSTOMER`'};
|
|
|
|
const stmt = new ParameterizedSQL(sql);
|
2017-03-06 23:40:47 +00:00
|
|
|
expect(stmt.toJSON()).to.eql({sql: sql.sql, params: []});
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('normalizes a SQL statement from object with params', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
const sql =
|
2017-03-06 23:40:47 +00:00
|
|
|
{sql: 'SELECT * FROM `CUSTOMER` WHERE `NAME`=?', params: ['John']};
|
2019-08-02 13:50:51 +00:00
|
|
|
const stmt = new ParameterizedSQL(sql);
|
2017-03-06 23:40:47 +00:00
|
|
|
expect(stmt.toJSON()).to.eql({sql: sql.sql, params: ['John']});
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if the statement is not a string or object', function() {
|
|
|
|
expect(function() {
|
2017-03-06 23:40:47 +00:00
|
|
|
/* jshint unused:false */
|
2019-08-02 13:50:51 +00:00
|
|
|
const stmt = new ParameterizedSQL(true);
|
2015-05-13 17:14:44 +00:00
|
|
|
}).to.throw('sql must be a string');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('concats SQL statements', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
let stmt1 = {sql: 'SELECT * from `CUSTOMER`'};
|
|
|
|
const where = {sql: 'WHERE `NAME`=?', params: ['John']};
|
2015-05-13 17:14:44 +00:00
|
|
|
stmt1 = ParameterizedSQL.append(stmt1, where);
|
|
|
|
expect(stmt1.toJSON()).to.eql(
|
2019-08-02 13:50:51 +00:00
|
|
|
{sql: 'SELECT * from `CUSTOMER` WHERE `NAME`=?', params: ['John']}
|
|
|
|
);
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('concats string SQL statements', function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
let stmt1 = 'SELECT * from `CUSTOMER`';
|
|
|
|
const where = {sql: 'WHERE `NAME`=?', params: ['John']};
|
2015-05-13 17:14:44 +00:00
|
|
|
stmt1 = ParameterizedSQL.append(stmt1, where);
|
|
|
|
expect(stmt1.toJSON()).to.eql(
|
2019-08-02 13:50:51 +00:00
|
|
|
{sql: 'SELECT * from `CUSTOMER` WHERE `NAME`=?', params: ['John']}
|
|
|
|
);
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if params does not match placeholders', function() {
|
|
|
|
expect(function() {
|
2019-08-02 13:50:51 +00:00
|
|
|
let stmt1 = 'SELECT * from `CUSTOMER`';
|
|
|
|
const where = {sql: 'WHERE `NAME`=?', params: ['John', 'Mary']};
|
2015-05-13 17:14:44 +00:00
|
|
|
stmt1 = ParameterizedSQL.append(stmt1, where);
|
|
|
|
}).to.throw('must match the number of params');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow execute(sql, callback)', function(done) {
|
|
|
|
connector.execute('SELECT * FROM `CUSTOMER`', done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow execute(sql, params, callback)', function(done) {
|
|
|
|
connector.execute('SELECT * FROM `CUSTOMER` WHERE `NAME`=$1',
|
|
|
|
['xyz'], done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow execute(sql, params, options, callback)', function(done) {
|
|
|
|
connector.execute('SELECT * FROM `CUSTOMER` WHERE `NAME`=$1',
|
2017-03-06 23:40:47 +00:00
|
|
|
['xyz'], {transaction: true}, done);
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if params is not an array for execute()', function() {
|
|
|
|
expect(function() {
|
|
|
|
connector.execute('SELECT * FROM `CUSTOMER`', 'xyz', function() {
|
|
|
|
});
|
|
|
|
}).to.throw('params must be an array');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if options is not an object for execute()', function() {
|
|
|
|
expect(function() {
|
|
|
|
connector.execute('SELECT * FROM `CUSTOMER`', [], 'xyz', function() {
|
|
|
|
});
|
|
|
|
}).to.throw('options must be an object');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if callback is not a function for execute()', function() {
|
|
|
|
expect(function() {
|
|
|
|
connector.execute('SELECT * FROM `CUSTOMER`', [], {}, 'xyz');
|
|
|
|
}).to.throw('callback must be a function');
|
|
|
|
});
|
2015-05-22 16:03:27 +00:00
|
|
|
|
|
|
|
it('should invoke hooks', function(done) {
|
2019-08-02 13:50:51 +00:00
|
|
|
const events = [];
|
2015-05-22 16:03:27 +00:00
|
|
|
connector.observe('before execute', function(ctx, next) {
|
|
|
|
expect(ctx.req.sql).be.a('string');
|
|
|
|
expect(ctx.req.params).be.a('array');
|
|
|
|
events.push('before execute');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
connector.observe('after execute', function(ctx, next) {
|
|
|
|
expect(ctx.res).be.an('array');
|
|
|
|
events.push('after execute');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
Customer.find(function(err, results) {
|
|
|
|
expect(events).to.eql(['before execute', 'after execute']);
|
|
|
|
done(err, results);
|
|
|
|
});
|
|
|
|
});
|
2015-05-13 17:14:44 +00:00
|
|
|
});
|