2016-08-09 17:23:07 -04:00
|
|
|
import Promise from 'bluebird';
|
2016-05-17 01:01:34 +10:00
|
|
|
import * as helpers from './helpers';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import Raw from './raw';
|
|
|
|
import Runner from './runner';
|
|
|
|
import Formatter from './formatter';
|
|
|
|
import Transaction from './transaction';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import QueryBuilder from './query/builder';
|
|
|
|
import QueryCompiler from './query/compiler';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import SchemaBuilder from './schema/builder';
|
|
|
|
import SchemaCompiler from './schema/compiler';
|
|
|
|
import TableBuilder from './schema/tablebuilder';
|
|
|
|
import TableCompiler from './schema/tablecompiler';
|
|
|
|
import ColumnBuilder from './schema/columnbuilder';
|
|
|
|
import ColumnCompiler from './schema/columncompiler';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-09-13 18:12:23 -04:00
|
|
|
import { Pool } from 'generic-pool';
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import { EventEmitter } from 'events';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-09-12 18:01:47 -04:00
|
|
|
import { makeEscape } from './query/string'
|
2016-05-18 20:22:50 +10:00
|
|
|
import { assign, uniqueId, cloneDeep } from 'lodash'
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-18 19:59:24 +10:00
|
|
|
const debug = require('debug')('knex:client')
|
|
|
|
const debugQuery = require('debug')('knex:query')
|
2016-09-16 16:04:11 -04:00
|
|
|
const debugBindings = require('debug')('knex:bindings')
|
2016-09-13 18:12:23 -04:00
|
|
|
const debugPool = require('debug')('knex:pool')
|
|
|
|
|
|
|
|
let id = 0
|
|
|
|
function clientId() {
|
|
|
|
return `client${id++}`
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// The base client provides the general structure
|
|
|
|
// for a dialect specific client object.
|
|
|
|
function Client(config = {}) {
|
|
|
|
this.config = config
|
|
|
|
this.connectionSettings = cloneDeep(config.connection || {})
|
|
|
|
if (this.driverName && config.connection) {
|
|
|
|
this.initializeDriver()
|
|
|
|
if (!config.pool || (config.pool && config.pool.max !== 0)) {
|
2016-09-13 18:12:23 -04:00
|
|
|
this.__cid = clientId()
|
2016-03-02 17:07:05 +01:00
|
|
|
this.initializePool(config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.valueForUndefined = this.raw('DEFAULT');
|
|
|
|
if (config.useNullAsDefault) {
|
|
|
|
this.valueForUndefined = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inherits(Client, EventEmitter)
|
|
|
|
|
|
|
|
assign(Client.prototype, {
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
formatter() {
|
2016-09-12 18:26:49 -04:00
|
|
|
return new Formatter(this)
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
queryBuilder() {
|
2016-09-12 18:45:35 -04:00
|
|
|
return new QueryBuilder(this)
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
queryCompiler(builder) {
|
2016-09-12 18:45:35 -04:00
|
|
|
return new QueryCompiler(this, builder)
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
schemaBuilder() {
|
2016-09-12 18:45:35 -04:00
|
|
|
return new SchemaBuilder(this)
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
schemaCompiler(builder) {
|
2016-09-12 18:45:35 -04:00
|
|
|
return new SchemaCompiler(this, builder)
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
tableBuilder(type, tableName, fn) {
|
2016-09-12 18:45:35 -04:00
|
|
|
return new TableBuilder(this, type, tableName, fn)
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
tableCompiler(tableBuilder) {
|
2016-09-12 18:45:35 -04:00
|
|
|
return new TableCompiler(this, tableBuilder)
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
columnBuilder(tableBuilder, type, args) {
|
2016-09-12 18:45:35 -04:00
|
|
|
return new ColumnBuilder(this, tableBuilder, type, args)
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
columnCompiler(tableBuilder, columnBuilder) {
|
2016-09-12 18:45:35 -04:00
|
|
|
return new ColumnCompiler(this, tableBuilder, columnBuilder)
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
runner(connection) {
|
2016-09-12 18:45:35 -04:00
|
|
|
return new Runner(this, connection)
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
transaction(container, config, outerTx) {
|
2016-09-12 18:45:35 -04:00
|
|
|
return new Transaction(this, container, config, outerTx)
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
raw() {
|
2016-09-12 18:45:35 -04:00
|
|
|
return new Raw(this).set(...arguments)
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-09-12 18:01:47 -04:00
|
|
|
_formatQuery(sql, bindings, timeZone) {
|
|
|
|
bindings = bindings == null ? [] : [].concat(bindings);
|
|
|
|
let index = 0;
|
|
|
|
return sql.replace(/\\?\?/g, (match) => {
|
|
|
|
if (match === '\\?') {
|
|
|
|
return '?'
|
|
|
|
}
|
|
|
|
if (index === bindings.length) {
|
|
|
|
return match
|
|
|
|
}
|
|
|
|
const value = bindings[index++];
|
|
|
|
return this._escapeBinding(value, {timeZone})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
_escapeBinding: makeEscape({
|
|
|
|
escapeString(str) {
|
|
|
|
return `'${str.replace(/'/g, "''")}'`
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
query(connection, obj) {
|
2016-03-02 17:07:05 +01:00
|
|
|
if (typeof obj === 'string') obj = {sql: obj}
|
2016-09-12 18:01:47 -04:00
|
|
|
obj.bindings = this.prepBindings(obj.bindings)
|
2016-03-02 17:07:05 +01:00
|
|
|
debugQuery(obj.sql)
|
2016-09-12 18:01:47 -04:00
|
|
|
this.emit('query', assign({__knexUid: connection.__knexUid}, obj))
|
2016-09-16 16:04:11 -04:00
|
|
|
debugBindings(obj.bindings)
|
2016-09-12 18:01:47 -04:00
|
|
|
return this._query(connection, obj).catch((err) => {
|
|
|
|
err.message = this._formatQuery(obj.sql, obj.bindings) + ' - ' + err.message
|
2016-03-24 18:59:37 +01:00
|
|
|
this.emit('query-error', err, assign({__knexUid: connection.__knexUid}, obj))
|
2016-03-02 17:07:05 +01:00
|
|
|
throw err
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
stream(connection, obj, stream, options) {
|
2016-03-02 17:07:05 +01:00
|
|
|
if (typeof obj === 'string') obj = {sql: obj}
|
|
|
|
this.emit('query', assign({__knexUid: connection.__knexUid}, obj))
|
|
|
|
debugQuery(obj.sql)
|
2016-09-12 18:01:47 -04:00
|
|
|
obj.bindings = this.prepBindings(obj.bindings)
|
2016-09-16 16:04:11 -04:00
|
|
|
debugBindings(obj.bindings)
|
2016-09-12 18:01:47 -04:00
|
|
|
return this._stream(connection, obj, stream, options)
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
prepBindings(bindings) {
|
2016-05-11 21:01:56 +03:00
|
|
|
return bindings;
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
wrapIdentifier(value) {
|
|
|
|
return (value !== '*' ? `"${value.replace(/"/g, '""')}"` : '*')
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
initializeDriver() {
|
2016-03-02 17:07:05 +01:00
|
|
|
try {
|
|
|
|
this.driver = this._driver()
|
|
|
|
} catch (e) {
|
2016-05-17 01:01:34 +10:00
|
|
|
helpers.exit(`Knex: run\n$ npm install ${this.driverName} --save\n${e.stack}`)
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
poolDefaults(poolConfig) {
|
2016-09-13 18:12:23 -04:00
|
|
|
const name = this.dialect + ':' + this.driverName + ':' + this.__cid
|
2016-03-02 17:07:05 +01:00
|
|
|
return {
|
|
|
|
min: 2,
|
|
|
|
max: 10,
|
2016-09-13 18:12:23 -04:00
|
|
|
name: name,
|
|
|
|
log(str, level) {
|
|
|
|
if (level === 'info') {
|
|
|
|
debugPool(level.toUpperCase() + ' pool ' + name + ' - ' + str)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
create: (callback) => {
|
|
|
|
this.acquireRawConnection()
|
2016-03-02 17:07:05 +01:00
|
|
|
.tap(function(connection) {
|
|
|
|
connection.__knexUid = uniqueId('__knexUid')
|
|
|
|
if (poolConfig.afterCreate) {
|
|
|
|
return Promise.promisify(poolConfig.afterCreate)(connection)
|
|
|
|
}
|
|
|
|
})
|
2016-03-15 20:51:27 +01:00
|
|
|
.asCallback(callback)
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
2016-09-13 18:12:23 -04:00
|
|
|
destroy: (connection) => {
|
2016-03-02 17:07:05 +01:00
|
|
|
if (poolConfig.beforeDestroy) {
|
2016-09-13 18:12:23 -04:00
|
|
|
helpers.warn(`
|
|
|
|
beforeDestroy is deprecated, please open an issue if you use this
|
|
|
|
to discuss alternative apis
|
|
|
|
`)
|
|
|
|
poolConfig.beforeDestroy(connection, function() {})
|
|
|
|
}
|
|
|
|
if (connection !== void 0) {
|
|
|
|
this.destroyRawConnection(connection)
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2016-03-19 19:14:46 +01:00
|
|
|
},
|
2016-09-13 18:12:23 -04:00
|
|
|
validate: (connection) => {
|
|
|
|
if (connection.__knex__disposed) {
|
|
|
|
helpers.warn(`Connection Error: ${connection.__knex__disposed}`)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return this.validateConnection(connection)
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-09-13 18:12:23 -04:00
|
|
|
initializePool(config) {
|
|
|
|
if (this.pool) {
|
|
|
|
helpers.warn('The pool has already been initialized')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.pool = new Pool(assign(this.poolDefaults(config.pool || {}), config.pool))
|
|
|
|
},
|
|
|
|
|
|
|
|
validateConnection(connection) {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
// Acquire a connection from the pool.
|
2016-05-17 01:01:34 +10:00
|
|
|
acquireConnection() {
|
2016-10-09 14:00:55 -04:00
|
|
|
return new Promise((resolver, rejecter) => {
|
2016-09-13 18:12:23 -04:00
|
|
|
if (!this.pool) {
|
|
|
|
return rejecter(new Error('Unable to acquire a connection'))
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
2016-10-09 14:00:55 -04:00
|
|
|
let wasRejected = false
|
|
|
|
const t = setTimeout(() => {
|
|
|
|
wasRejected = true
|
|
|
|
rejecter(new Promise.TimeoutError(
|
|
|
|
'Knex: Timeout acquiring a connection. The pool is probably full. ' +
|
|
|
|
'Are you missing a .transacting(trx) call?'
|
|
|
|
))
|
|
|
|
}, this.config.acquireConnectionTimeout || 60000)
|
|
|
|
this.pool.acquire((err, connection) => {
|
|
|
|
if (err) {
|
|
|
|
return rejecter(err)
|
|
|
|
}
|
|
|
|
clearTimeout(t)
|
|
|
|
if (wasRejected) {
|
|
|
|
this.pool.release(connection)
|
|
|
|
} else {
|
|
|
|
debug('acquired connection from pool: %s', connection.__knexUid)
|
|
|
|
resolver(connection)
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
// Releases a connection back to the connection pool,
|
|
|
|
// returning a promise resolved when the connection is released.
|
2016-05-17 01:01:34 +10:00
|
|
|
releaseConnection(connection) {
|
2016-09-13 18:12:23 -04:00
|
|
|
return new Promise((resolver) => {
|
2016-03-02 17:07:05 +01:00
|
|
|
debug('releasing connection to pool: %s', connection.__knexUid)
|
2016-09-13 18:12:23 -04:00
|
|
|
this.pool.release(connection)
|
2016-03-02 17:07:05 +01:00
|
|
|
resolver()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
// Destroy the current connection pool for the client.
|
2016-05-17 01:01:34 +10:00
|
|
|
destroy(callback) {
|
2016-09-13 18:12:23 -04:00
|
|
|
const promise = new Promise((resolver) => {
|
|
|
|
if (!this.pool) {
|
|
|
|
return resolver()
|
|
|
|
}
|
|
|
|
this.pool.drain(() => {
|
|
|
|
this.pool.destroyAllNow(() => {
|
|
|
|
this.pool = undefined
|
|
|
|
resolver()
|
|
|
|
})
|
2016-03-02 17:07:05 +01:00
|
|
|
})
|
|
|
|
})
|
2016-09-13 18:12:23 -04:00
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
// Allow either a callback or promise interface for destruction.
|
|
|
|
if (typeof callback === 'function') {
|
2016-03-15 20:51:27 +01:00
|
|
|
promise.asCallback(callback)
|
2016-03-02 17:07:05 +01:00
|
|
|
} else {
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Return the database being used by this client.
|
2016-05-17 01:01:34 +10:00
|
|
|
database() {
|
2016-03-02 17:07:05 +01:00
|
|
|
return this.connectionSettings.database
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
toString() {
|
2016-03-02 17:07:05 +01:00
|
|
|
return '[object KnexClient]'
|
2016-05-26 11:06:33 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
canCancelQuery: false,
|
2016-05-27 14:47:12 -07:00
|
|
|
|
|
|
|
assertCanCancelQuery() {
|
|
|
|
if (!this.canCancelQuery) {
|
|
|
|
throw new Error("Query cancelling not supported for this dialect");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-05-26 11:06:33 -07:00
|
|
|
cancelQuery() {
|
|
|
|
throw new Error("Query cancelling not supported for this dialect")
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default Client
|