knex/src/dialects/mssql/index.js

238 lines
6.7 KiB
JavaScript
Raw Normal View History

2015-12-08 11:37:31 -06:00
// MSSQL Client
// -------
import { assign, map, flatten, values } from 'lodash'
import inherits from 'inherits';
2016-03-02 16:52:32 +01:00
import Client from '../../client';
import Promise from 'bluebird';
import * as helpers from '../../helpers';
2015-12-08 11:37:31 -06:00
2016-09-13 08:15:58 -04:00
import Formatter from '../../formatter'
import Transaction from './transaction';
import QueryCompiler from './query/compiler';
import SchemaCompiler from './schema/compiler';
import TableCompiler from './schema/tablecompiler';
import ColumnCompiler from './schema/columncompiler';
2015-12-08 11:37:31 -06:00
const { isArray } = Array;
2015-12-08 11:37:31 -06:00
2016-05-26 22:27:55 +02:00
const SQL_INT4 = { MIN : -2147483648, MAX: 2147483647}
const SQL_BIGINT_SAFE = { MIN : -9007199254740991, MAX: 9007199254740991}
// Always initialize with the "QueryBuilder" and "QueryCompiler" objects, which
// extend the base 'lib/query/builder' and 'lib/query/compiler', respectively.
2015-12-08 11:37:31 -06:00
function Client_MSSQL(config) {
// #1235 mssql module wants 'server', not 'host'. This is to enforce the same
// options object across all dialects.
if(config && config.connection && config.connection.host) {
config.connection.server = config.connection.host;
}
2015-12-08 11:37:31 -06:00
Client.call(this, config);
}
inherits(Client_MSSQL, Client);
assign(Client_MSSQL.prototype, {
dialect: 'mssql',
driverName: 'mssql',
_driver() {
2015-12-08 11:37:31 -06:00
return require('mssql');
},
2016-09-13 08:15:58 -04:00
formatter() {
return new MSSQL_Formatter(this)
},
2016-09-12 18:45:35 -04:00
transaction() {
return new Transaction(this, ...arguments)
},
2016-09-13 08:15:58 -04:00
queryCompiler() {
return new QueryCompiler(this, ...arguments)
},
2016-09-13 08:15:58 -04:00
schemaCompiler() {
return new SchemaCompiler(this, ...arguments)
},
2015-12-08 11:37:31 -06:00
2016-09-13 08:15:58 -04:00
tableCompiler() {
return new TableCompiler(this, ...arguments)
},
2015-12-08 11:37:31 -06:00
2016-09-13 08:15:58 -04:00
columnCompiler() {
return new ColumnCompiler(this, ...arguments)
},
2015-12-08 11:37:31 -06:00
wrapIdentifier(value) {
return (value !== '*' ? `[${value.replace(/\[/g, '\[')}]` : '*')
2015-12-08 11:37:31 -06:00
},
// Get a raw connection, called by the `pool` whenever a new
// connection needs to be added to the pool.
acquireRawConnection() {
2016-09-13 18:12:23 -04:00
return new Promise((resolver, rejecter) => {
const connection = new this.driver.ConnectionPool(this.connectionSettings);
2016-09-13 18:12:23 -04:00
connection.connect((err) => {
if (err) {
return rejecter(err)
}
connection.on('error', (err) => {
connection.__knex__disposed = err
})
2015-12-08 11:37:31 -06:00
resolver(connection);
});
});
},
2016-09-13 18:12:23 -04:00
validateConnection(connection) {
return connection.connected === true
},
2015-12-08 11:37:31 -06:00
// Used to explicitly close a connection, called internally by the pool
// when a connection times out or the pool is shutdown.
2016-09-13 18:12:23 -04:00
destroyRawConnection(connection) {
connection.close()
2015-12-08 11:37:31 -06:00
},
2015-12-08 11:37:31 -06:00
// Position the bindings for the query.
positionBindings(sql) {
let questionCount = -1
2015-12-08 11:37:31 -06:00
return sql.replace(/\?/g, function() {
questionCount += 1
return `@p${questionCount}`
2015-12-08 11:37:31 -06:00
})
},
2015-12-08 11:37:31 -06:00
// Grab a connection, run the query via the MSSQL streaming interface,
// and pass that through to the stream we've sent back to the client.
_stream(connection, obj, stream, options) {
2015-12-08 11:37:31 -06:00
options = options || {}
if (!obj || typeof obj === 'string') obj = {sql: obj}
// convert ? params into positional bindings (@p1)
obj.sql = this.positionBindings(obj.sql);
2016-09-13 18:12:23 -04:00
return new Promise((resolver, rejecter) => {
stream.on('error', (err) => {
rejecter(err)
});
stream.on('end', resolver);
2016-09-16 16:04:11 -04:00
const { sql } = obj
if (!sql) return resolver()
const req = (connection.tx_ || connection).request();
//req.verbose = true;
req.multiple = true;
req.stream = true;
if (obj.bindings) {
for (let i = 0; i < obj.bindings.length; i++) {
2016-09-13 18:12:23 -04:00
this._setReqInput(req, i, obj.bindings[i])
}
}
req.pipe(stream)
req.query(sql)
2015-12-08 11:37:31 -06:00
})
},
// Runs the query on the specified connection, providing the bindings
// and any other necessary prep work.
_query(connection, obj) {
const client = this;
2015-12-08 11:37:31 -06:00
if (!obj || typeof obj === 'string') obj = {sql: obj}
// convert ? params into positional bindings (@p1)
obj.sql = this.positionBindings(obj.sql);
2016-09-13 18:12:23 -04:00
return new Promise((resolver, rejecter) => {
2016-09-16 16:04:11 -04:00
const { sql } = obj
2015-12-08 11:37:31 -06:00
if (!sql) return resolver()
const req = (connection.tx_ || connection).request();
// req.verbose = true;
req.multiple = true;
2015-12-08 11:37:31 -06:00
if (obj.bindings) {
for (let i = 0; i < obj.bindings.length; i++) {
client._setReqInput(req, i, obj.bindings[i])
2015-12-08 11:37:31 -06:00
}
}
2016-09-13 18:12:23 -04:00
req.query(sql, (err, recordset) => {
if (err) {
return rejecter(err)
}
obj.response = recordset.recordsets[0]
2015-12-08 11:37:31 -06:00
resolver(obj)
})
2015-12-08 11:37:31 -06:00
})
},
// sets a request input parameter. Detects bigints and decimals and sets type appropriately.
_setReqInput(req, i, binding) {
if (typeof binding == 'number') {
if (binding % 1 !== 0) {
req.input(`p${i}`, this.driver.Decimal(38, 10), binding)
} else if (binding < SQL_INT4.MIN || binding > SQL_INT4.MAX) {
if (binding < SQL_BIGINT_SAFE.MIN || binding > SQL_BIGINT_SAFE.MAX) {
throw new Error(`Bigint must be safe integer or must be passed as string, saw ${binding}`)
}
req.input(`p${i}`, this.driver.BigInt, binding)
} else {
req.input(`p${i}`, this.driver.Int, binding)
2016-05-24 22:21:57 +02:00
}
} else {
req.input(`p${i}`, binding)
}
},
2015-12-08 11:37:31 -06:00
// Process the response as returned from the query.
processResponse(obj, runner) {
2015-12-08 11:37:31 -06:00
if (obj == null) return;
let { response } = obj
const { method } = obj
if (obj.output) return obj.output.call(runner, response)
2015-12-08 11:37:31 -06:00
switch (method) {
case 'select':
case 'pluck':
case 'first':
response = helpers.skim(response)
2016-03-02 16:52:32 +01:00
if (method === 'pluck') return map(response, obj.pluck)
return method === 'first' ? response[0] : response
2015-12-08 11:37:31 -06:00
case 'insert':
case 'del':
case 'update':
case 'counter':
if (obj.returning) {
if (obj.returning === '@@rowcount') {
return response[0]['']
}
if (
(isArray(obj.returning) && obj.returning.length > 1) ||
obj.returning[0] === '*'
) {
return response;
}
// return an array with values if only one returning value was specified
2016-03-02 16:52:32 +01:00
return flatten(map(response, values));
}
return response;
2015-12-08 11:37:31 -06:00
default:
return response
}
}
2015-12-08 11:37:31 -06:00
})
2016-09-13 08:15:58 -04:00
class MSSQL_Formatter extends Formatter {
// Accepts a string or array of columns to wrap as appropriate.
columnizeWithPrefix(prefix, target) {
const columns = typeof target === 'string' ? [target] : target
let str = '', i = -1;
while (++i < columns.length) {
if (i > 0) str += ', '
str += prefix + this.wrap(columns[i])
}
return str
}
}
export default Client_MSSQL