2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
var Promise = require('./promise')
|
|
|
|
var helpers = require('./helpers')
|
|
|
|
|
|
|
|
var Raw = require('./raw')
|
|
|
|
var Runner = require('./runner')
|
|
|
|
var Formatter = require('./formatter')
|
|
|
|
var Transaction = require('./transaction')
|
|
|
|
|
|
|
|
var QueryBuilder = require('./query/builder')
|
|
|
|
var QueryCompiler = require('./query/compiler')
|
|
|
|
|
|
|
|
var SchemaBuilder = require('./schema/builder')
|
|
|
|
var SchemaCompiler = require('./schema/compiler')
|
|
|
|
var TableBuilder = require('./schema/tablebuilder')
|
|
|
|
var TableCompiler = require('./schema/tablecompiler')
|
|
|
|
var ColumnBuilder = require('./schema/columnbuilder')
|
|
|
|
var ColumnCompiler = require('./schema/columncompiler')
|
|
|
|
|
|
|
|
var Pool2 = require('pool2')
|
|
|
|
var inherits = require('inherits')
|
|
|
|
var EventEmitter = require('events').EventEmitter
|
2015-04-28 19:34:11 -04:00
|
|
|
var SqlString = require('./query/string')
|
2015-04-19 16:31:52 -04:00
|
|
|
|
|
|
|
var assign = require('lodash/object/assign')
|
|
|
|
var uniqueId = require('lodash/utility/uniqueId')
|
|
|
|
var cloneDeep = require('lodash/lang/cloneDeep')
|
2015-04-22 10:34:14 -04:00
|
|
|
var debug = require('debug')('knex:client')
|
2015-04-24 10:10:34 -04:00
|
|
|
var debugQuery = require('debug')('knex:query')
|
2014-04-08 16:25:57 -04:00
|
|
|
|
2014-04-16 10:41:16 -04:00
|
|
|
// The base client provides the general structure
|
2015-04-19 16:31:52 -04:00
|
|
|
// for a dialect specific client object.
|
2014-05-28 22:29:34 -04:00
|
|
|
function Client(config) {
|
2015-04-22 16:19:59 -04:00
|
|
|
config = config || {}
|
2015-04-22 10:34:14 -04:00
|
|
|
this.config = config
|
2015-04-19 16:31:52 -04:00
|
|
|
if (this.driverName && config.connection) {
|
|
|
|
this.initializeDriver()
|
|
|
|
this.initializePool(config)
|
2015-04-17 15:00:08 -04:00
|
|
|
}
|
2014-04-08 16:25:57 -04:00
|
|
|
}
|
2015-04-19 16:31:52 -04:00
|
|
|
inherits(Client, EventEmitter)
|
|
|
|
|
|
|
|
assign(Client.prototype, {
|
|
|
|
|
|
|
|
Formatter: Formatter,
|
|
|
|
|
|
|
|
formatter: function() {
|
|
|
|
return new this.Formatter(this)
|
|
|
|
},
|
|
|
|
|
|
|
|
QueryBuilder: QueryBuilder,
|
|
|
|
|
|
|
|
queryBuilder: function() {
|
|
|
|
return new this.QueryBuilder(this)
|
|
|
|
},
|
|
|
|
|
|
|
|
QueryCompiler: QueryCompiler,
|
|
|
|
|
|
|
|
queryCompiler: function(builder) {
|
|
|
|
return new this.QueryCompiler(this, builder)
|
|
|
|
},
|
|
|
|
|
|
|
|
SchemaBuilder: SchemaBuilder,
|
|
|
|
|
|
|
|
schemaBuilder: function() {
|
|
|
|
return new this.SchemaBuilder(this)
|
|
|
|
},
|
|
|
|
|
|
|
|
SchemaCompiler: SchemaCompiler,
|
|
|
|
|
|
|
|
schemaCompiler: function(builder) {
|
|
|
|
return new this.SchemaCompiler(this, builder)
|
|
|
|
},
|
|
|
|
|
|
|
|
TableBuilder: TableBuilder,
|
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
tableBuilder: function(type, tableName, fn) {
|
|
|
|
return new this.TableBuilder(this, type, tableName, fn)
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
TableCompiler: TableCompiler,
|
|
|
|
|
|
|
|
tableCompiler: function(tableBuilder) {
|
|
|
|
return new this.TableCompiler(this, tableBuilder)
|
|
|
|
},
|
|
|
|
|
|
|
|
ColumnBuilder: ColumnBuilder,
|
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
columnBuilder: function(tableBuilder, type, args) {
|
|
|
|
return new this.ColumnBuilder(this, tableBuilder, type, args)
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
ColumnCompiler: ColumnCompiler,
|
|
|
|
|
|
|
|
columnCompiler: function(tableBuilder, columnBuilder) {
|
|
|
|
return new this.ColumnCompiler(this, tableBuilder, columnBuilder)
|
|
|
|
},
|
|
|
|
|
|
|
|
Runner: Runner,
|
|
|
|
|
|
|
|
runner: function(connection) {
|
|
|
|
return new this.Runner(this, connection)
|
|
|
|
},
|
|
|
|
|
|
|
|
Transaction: Transaction,
|
|
|
|
|
2015-04-23 14:51:33 -04:00
|
|
|
transaction: function(container, config, outerTx) {
|
|
|
|
return new this.Transaction(this, container, config, outerTx)
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
Raw: Raw,
|
|
|
|
|
2015-04-22 15:39:29 -04:00
|
|
|
raw: function() {
|
2015-04-19 16:31:52 -04:00
|
|
|
var raw = new this.Raw(this)
|
|
|
|
return raw.set.apply(raw, arguments)
|
|
|
|
},
|
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
query: function(connection, obj) {
|
|
|
|
if (typeof obj === 'string') obj = {sql: obj}
|
|
|
|
this.emit('query', assign({__knexUid: connection.__knexUid}, obj))
|
2015-04-24 10:10:34 -04:00
|
|
|
debugQuery(obj.sql)
|
|
|
|
return this._query.call(this, connection, obj).catch(function(err) {
|
2015-04-28 19:34:11 -04:00
|
|
|
err.message = SqlString.format(obj.sql, obj.bindings) + ' - ' + err.message
|
2015-04-24 10:10:34 -04:00
|
|
|
throw err
|
|
|
|
})
|
2015-04-22 10:34:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
stream: function(connection, obj, stream, options) {
|
|
|
|
if (typeof obj === 'string') obj = {sql: obj}
|
|
|
|
this.emit('query', assign({__knexUid: connection.__knexUid}, obj))
|
2015-04-24 10:10:34 -04:00
|
|
|
debugQuery(obj.sql)
|
2015-04-22 10:34:14 -04:00
|
|
|
return this._stream.call(this, connection, obj, stream, options)
|
|
|
|
},
|
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
wrapIdentifier: function(value) {
|
|
|
|
return (value !== '*' ? '"' + value.replace(/"/g, '""') + '"' : '*')
|
|
|
|
},
|
|
|
|
|
|
|
|
initializeDriver: function() {
|
2015-04-22 10:34:14 -04:00
|
|
|
try {
|
2015-04-27 15:58:48 -04:00
|
|
|
this.driver = require(this.driverName)
|
2015-04-22 10:34:14 -04:00
|
|
|
} catch (e) {
|
|
|
|
helpers.exit('Knex: run\n$ npm install ' + this.driverName + ' --save')
|
|
|
|
}
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
Pool: Pool2,
|
|
|
|
|
|
|
|
initializePool: function(config) {
|
|
|
|
this.connectionSettings = cloneDeep(config.connection)
|
|
|
|
if (this.pool) this.destroy()
|
2015-04-22 11:45:12 -04:00
|
|
|
this.pool = new this.Pool(assign(this.poolDefaults(config.pool || {}), config.pool))
|
2015-04-19 16:31:52 -04:00
|
|
|
this.pool.on('error', function(err) {
|
|
|
|
helpers.error('Pool2 - ' + err)
|
|
|
|
})
|
|
|
|
this.pool.on('warn', function(msg) {
|
|
|
|
helpers.warn('Pool2 - ' + msg)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
poolDefaults: function(poolConfig) {
|
|
|
|
var dispose, client = this
|
|
|
|
if (poolConfig.destroy) {
|
2015-04-22 15:39:29 -04:00
|
|
|
helpers.deprecate('config.pool.destroy', 'config.pool.dispose')
|
2015-04-19 16:31:52 -04:00
|
|
|
dispose = poolConfig.destroy
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
min: 2,
|
|
|
|
max: 10,
|
|
|
|
acquire: function(callback) {
|
|
|
|
client.acquireRawConnection()
|
|
|
|
.tap(function(connection) {
|
2015-04-22 10:34:14 -04:00
|
|
|
connection.__knexUid = uniqueId('__knexUid')
|
2015-04-19 16:31:52 -04:00
|
|
|
if (poolConfig.afterCreate) {
|
|
|
|
return Promise.promisify(poolConfig.afterCreate)(connection)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.nodeify(callback)
|
|
|
|
},
|
|
|
|
dispose: function(connection, callback) {
|
|
|
|
if (poolConfig.beforeDestroy) {
|
|
|
|
poolConfig.beforeDestroy(connection, function() {
|
|
|
|
if (connection !== undefined) {
|
|
|
|
client.destroyRawConnection(connection, callback)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else if (connection !== void 0) {
|
|
|
|
client.destroyRawConnection(connection, callback)
|
|
|
|
}
|
2015-04-17 15:00:08 -04:00
|
|
|
}
|
|
|
|
}
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Acquire a connection from the pool.
|
|
|
|
acquireConnection: function() {
|
|
|
|
var client = this
|
|
|
|
return new Promise(function(resolver, rejecter) {
|
|
|
|
if (!client.pool) {
|
|
|
|
return rejecter(new Error('There is no pool defined on the current client'))
|
|
|
|
}
|
|
|
|
client.pool.acquire(function(err, connection) {
|
|
|
|
if (err) return rejecter(err)
|
2015-04-22 10:34:14 -04:00
|
|
|
debug('acquiring connection from pool: %s', connection.__knexUid)
|
2015-04-19 16:31:52 -04:00
|
|
|
resolver(connection)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2015-04-22 10:34:14 -04:00
|
|
|
// Releases a connection back to the connection pool,
|
2015-04-19 16:31:52 -04:00
|
|
|
// returning a promise resolved when the connection is released.
|
|
|
|
releaseConnection: function(connection) {
|
|
|
|
var pool = this.pool
|
2015-04-22 15:39:29 -04:00
|
|
|
return new Promise(function(resolver) {
|
2015-04-22 10:34:14 -04:00
|
|
|
debug('releasing connection to pool: %s', connection.__knexUid)
|
|
|
|
pool.release(connection)
|
2015-04-19 16:31:52 -04:00
|
|
|
resolver()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
// Destroy the current connection pool for the client.
|
|
|
|
destroy: function(callback) {
|
|
|
|
var client = this
|
2015-04-22 15:39:29 -04:00
|
|
|
var promise = new Promise(function(resolver) {
|
2015-04-19 16:31:52 -04:00
|
|
|
if (!client.pool) return resolver()
|
|
|
|
client.pool.end(function() {
|
|
|
|
client.pool = undefined
|
|
|
|
resolver()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
// Allow either a callback or promise interface for destruction.
|
|
|
|
if (typeof callback === 'function') {
|
|
|
|
promise.nodeify(callback)
|
|
|
|
} else {
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
},
|
2015-04-17 15:00:08 -04:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
// Return the database being used by this client.
|
|
|
|
database: function() {
|
|
|
|
return this.databaseName || this.connectionSettings.database
|
2015-04-22 10:34:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
toString: function() {
|
|
|
|
return '[object KnexClient]'
|
2014-07-14 13:48:17 -04:00
|
|
|
}
|
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
})
|
2014-04-21 23:08:59 -04:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
module.exports = Client
|