knex/src/dialects/mssql/index.js

208 lines
6.1 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 Formatter from './formatter';
import Client from '../../client';
import Promise from '../../promise';
import * as helpers from '../../helpers';
2015-12-08 11:37:31 -06:00
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
// 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');
},
Transaction,
Formatter,
QueryCompiler,
SchemaCompiler,
2015-12-08 11:37:31 -06:00
TableCompiler,
2015-12-08 11:37:31 -06:00
ColumnCompiler,
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() {
const client = this;
const connection = new this.driver.Connection(this.connectionSettings);
2015-12-08 11:37:31 -06:00
return new Promise(function(resolver, rejecter) {
connection.connect(function(err) {
if (err) return rejecter(err);
connection.on('error', connectionErrorHandler.bind(null, client, connection));
connection.on('end', connectionErrorHandler.bind(null, client, connection));
resolver(connection);
});
});
},
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.
destroyRawConnection(connection, cb) {
2015-12-08 11:37:31 -06:00
connection.close(cb);
},
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) {
const client = this;
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);
2015-12-08 11:37:31 -06:00
return new Promise(function(resolver, rejecter) {
stream.on('error', rejecter);
stream.on('end', resolver);
let { sql } = obj
if (!sql) return resolver()
if (obj.options) ({ sql } = assign({sql}, obj.options))
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++) {
client._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);
return new Promise(function(resolver, rejecter) {
let { sql } = obj
2015-12-08 11:37:31 -06:00
if (!sql) return resolver()
if (obj.options) ({ sql } = assign({sql}, obj.options))
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
}
}
req.query(sql, function(err, recordset) {
if (err) return rejecter(err)
obj.response = recordset[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 sets type appropriately.
_setReqInput(req, i, binding) {
if (typeof binding == 'number' && (binding < -2147483648 || binding > 2147483647)) {
2016-05-24 22:21:57 +02:00
if (binding < -9007199254740991 || binding > 9007199254740991) {
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}`, 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
}
},
ping(resource, callback) {
resource.request().query('SELECT 1', callback);
2015-12-08 11:37:31 -06:00
}
2015-12-08 11:37:31 -06:00
})
// MSSQL Specific error handler
function connectionErrorHandler(client, connection, err) {
if (connection && err && err.fatal) {
if (connection.__knex__disposed) return;
connection.__knex__disposed = true;
client.pool.destroy(connection);
}
}
export default Client_MSSQL