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
|
|
|
|
|
|
|
|
var assign = require('lodash/object/assign')
|
|
|
|
var uniqueId = require('lodash/utility/uniqueId')
|
|
|
|
var cloneDeep = require('lodash/lang/cloneDeep')
|
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-19 16:31:52 -04:00
|
|
|
this.config = config
|
|
|
|
this.transacting = false
|
|
|
|
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,
|
|
|
|
|
|
|
|
tableBuilder: function() {
|
|
|
|
return new this.TableBuilder(this)
|
|
|
|
},
|
|
|
|
|
|
|
|
TableCompiler: TableCompiler,
|
|
|
|
|
|
|
|
tableCompiler: function(tableBuilder) {
|
|
|
|
return new this.TableCompiler(this, tableBuilder)
|
|
|
|
},
|
|
|
|
|
|
|
|
ColumnBuilder: ColumnBuilder,
|
|
|
|
|
|
|
|
columnBuilder: function() {
|
|
|
|
return new this.ColumnBuilder(this)
|
|
|
|
},
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
|
|
|
transaction: function(container) {
|
|
|
|
return new this.Transaction(this).run(container)
|
|
|
|
},
|
|
|
|
|
|
|
|
Raw: Raw,
|
|
|
|
|
|
|
|
raw: function(sql, bindings) {
|
|
|
|
var raw = new this.Raw(this)
|
|
|
|
return raw.set.apply(raw, arguments)
|
|
|
|
},
|
|
|
|
|
|
|
|
wrapIdentifier: function(value) {
|
|
|
|
return (value !== '*' ? '"' + value.replace(/"/g, '""') + '"' : '*')
|
|
|
|
},
|
|
|
|
|
|
|
|
initializeDriver: function() {
|
|
|
|
this.driver = require(this.driverName)
|
|
|
|
},
|
|
|
|
|
|
|
|
Pool: Pool2,
|
|
|
|
|
|
|
|
initializePool: function(config) {
|
|
|
|
this.connectionSettings = cloneDeep(config.connection)
|
|
|
|
if (this.pool) this.destroy()
|
|
|
|
this.pool = new this.Pool(_.extend(this.poolDefaults(config.pool), config.pool))
|
|
|
|
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) {
|
|
|
|
deprecate('config.pool.destroy', 'config.pool.dispose')
|
|
|
|
dispose = poolConfig.destroy
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
min: 2,
|
|
|
|
max: 10,
|
|
|
|
acquire: function(callback) {
|
|
|
|
client.acquireRawConnection()
|
|
|
|
.tap(function(connection) {
|
|
|
|
connection.__knexUid = _.uniqueId('__knexUid')
|
|
|
|
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)
|
|
|
|
resolver(connection)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
// Releases a connection from the connection pool,
|
|
|
|
// returning a promise resolved when the connection is released.
|
|
|
|
releaseConnection: function(connection) {
|
|
|
|
var pool = this.pool
|
|
|
|
return new Promise(function(resolver, rejecter) {
|
|
|
|
pool.release(connection)
|
|
|
|
resolver()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
// Destroy the current connection pool for the client.
|
|
|
|
destroy: function(callback) {
|
|
|
|
var client = this
|
|
|
|
var promise = new Promise(function(resolver, rejecter) {
|
|
|
|
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
|
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
|