2013-09-11 23:36:55 -04:00
|
|
|
// ServerBase
|
|
|
|
// -------
|
|
|
|
var when = require('when');
|
2013-09-08 15:57:32 -04:00
|
|
|
var nodefn = require('when/node/function');
|
2013-09-11 23:36:55 -04:00
|
|
|
var sequence = require('when/sequence');
|
|
|
|
|
|
|
|
var _ = require('underscore');
|
2013-05-02 00:21:49 -04:00
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
var Pool = require('../pool').Pool;
|
2013-09-08 15:57:32 -04:00
|
|
|
var ClientBase = require('../base').ClientBase;
|
2013-05-02 00:21:49 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
var ServerBase = ClientBase.extend({
|
2013-07-17 05:03:37 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// Pass a config object into the constructor,
|
|
|
|
// which then initializes the pool and
|
|
|
|
constructor: function(config) {
|
|
|
|
this.connectionSettings = config.connection;
|
|
|
|
this.initPool(config.pool);
|
|
|
|
this.initialize(config);
|
|
|
|
_.bindAll(this, 'getRawConnection');
|
|
|
|
},
|
2013-05-02 00:21:49 -04:00
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
initialize: function() {},
|
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// Initialize a pool with the apporpriate configuration and
|
|
|
|
// bind the pool to the current client object.
|
2013-09-11 23:36:55 -04:00
|
|
|
initPool: function(poolConfig) {
|
|
|
|
this.pool = new Pool(_.defaults(poolConfig, _.result(this, 'poolDefaults')), this);
|
|
|
|
},
|
|
|
|
|
|
|
|
runQuery: function(connection, sql, bindings) {
|
|
|
|
return nodefn.call(connection.query.bind(connection), sql, bindings);
|
2013-09-08 15:57:32 -04:00
|
|
|
},
|
2013-05-04 14:36:41 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// Execute a query on the specified Builder or QueryBuilder
|
|
|
|
// interface. If a `connection` is specified, use it, otherwise
|
|
|
|
// acquire a connection, and then dispose of it when we're done.
|
|
|
|
query: function(builder) {
|
2013-09-11 23:36:55 -04:00
|
|
|
var conn, client = this;
|
|
|
|
var sql = builder.toSql(builder);
|
|
|
|
var bindings = builder.cleanBindings();
|
|
|
|
|
|
|
|
var chain = this.getConnection(builder).then(function(connection) {
|
|
|
|
|
|
|
|
if (client.isDebugging || builder.isDebugging) {
|
|
|
|
client.debug(sql, bindings, connection, builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
conn = connection;
|
|
|
|
if (_.isArray(sql)) {
|
|
|
|
return sequence(sql.map(function(query) {
|
|
|
|
return function() { return client.runQuery(connection, query, bindings); };
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return client.runQuery(connection, sql, bindings);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!builder.usingConnection) {
|
|
|
|
chain = chain.ensure(function() {
|
|
|
|
client.pool.release(conn);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return chain.then(builder.handleResponse);
|
2013-09-08 15:57:32 -04:00
|
|
|
},
|
2013-05-04 14:36:41 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// Debug a query.
|
2013-09-11 23:36:55 -04:00
|
|
|
debug: function(sql, bindings, connection, builder) {
|
|
|
|
console.log({sql: sql, bindings: bindings, __cid: connection.__cid});
|
2013-09-08 15:57:32 -04:00
|
|
|
},
|
2013-05-02 00:21:49 -04:00
|
|
|
|
2013-05-03 00:20:51 -04:00
|
|
|
// Retrieves a connection from the connection pool,
|
|
|
|
// returning a promise.
|
2013-09-08 15:57:32 -04:00
|
|
|
getConnection: function(builder) {
|
2013-09-11 23:36:55 -04:00
|
|
|
if (builder && builder.usingConnection) return when(builder.usingConnection);
|
2013-05-08 20:16:39 -04:00
|
|
|
return nodefn.call(this.pool.acquire);
|
2013-05-03 00:20:51 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Releases a connection from the connection pool,
|
|
|
|
// returning a promise.
|
|
|
|
releaseConnection: function(conn) {
|
2013-09-08 15:57:32 -04:00
|
|
|
return nodefn.call(this.pool.release, conn);
|
2013-05-03 00:20:51 -04:00
|
|
|
},
|
|
|
|
|
2013-05-02 00:21:49 -04:00
|
|
|
// Begins a transaction statement on the instance,
|
|
|
|
// resolving with the connection of the current transaction.
|
|
|
|
startTransaction: function() {
|
2013-09-08 15:57:32 -04:00
|
|
|
return this.getConnection()
|
|
|
|
.tap(function(connection) {
|
|
|
|
return nodefn.call(connection.query.bind(connection), 'begin;', []);
|
2013-05-03 00:20:51 -04:00
|
|
|
});
|
2013-05-02 00:21:49 -04:00
|
|
|
},
|
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// Finishes the transaction statement on the instance.
|
2013-09-05 16:36:49 -04:00
|
|
|
finishTransaction: function(type, transaction, msg) {
|
2013-09-08 15:57:32 -04:00
|
|
|
var client = this;
|
|
|
|
var dfd = transaction.dfd;
|
2013-09-11 23:36:55 -04:00
|
|
|
nodefn.call(transaction.connection.query.bind(transaction.connection), type + ';', []).then(function(resp) {
|
2013-07-04 13:56:47 -04:00
|
|
|
if (type === 'commit') dfd.resolve(msg || resp);
|
|
|
|
if (type === 'rollback') dfd.reject(msg || resp);
|
2013-08-29 09:21:05 +02:00
|
|
|
}, function (err) {
|
|
|
|
dfd.reject(err);
|
2013-05-08 20:16:39 -04:00
|
|
|
}).ensure(function() {
|
2013-09-11 23:36:55 -04:00
|
|
|
return client.releaseConnection(transaction.connection).tap(function() {
|
|
|
|
transaction.connection = null;
|
2013-07-18 00:28:17 -04:00
|
|
|
});
|
2013-05-02 00:21:49 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
exports.ServerBase = ServerBase;
|