2013-09-13 13:50:41 -04:00
|
|
|
// PostgreSQL
|
2013-09-08 15:57:32 -04:00
|
|
|
// -------
|
2013-09-03 22:01:31 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// Other dependencies, including the `pg` library,
|
|
|
|
// which needs to be added as a dependency to the project
|
|
|
|
// using this database.
|
|
|
|
var _ = require('underscore');
|
|
|
|
var pg = require('pg');
|
2013-07-17 06:16:41 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// All other local project modules needed in this scope.
|
|
|
|
var ServerBase = require('./base').ServerBase;
|
|
|
|
var Helpers = require('../../lib/helpers').Helpers;
|
2013-10-27 22:34:58 -04:00
|
|
|
var Promise = require('../../lib/promise').Promise;
|
2013-04-28 20:58:38 -04:00
|
|
|
|
2013-09-17 06:54:26 -04:00
|
|
|
var grammar = require('./postgres/grammar').grammar;
|
|
|
|
var schemaGrammar = require('./postgres/schemagrammar').schemaGrammar;
|
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
// Constructor for the PostgreSQL Client
|
|
|
|
exports.Client = ServerBase.extend({
|
2013-07-17 06:16:41 -04:00
|
|
|
|
2013-09-08 15:57:32 -04:00
|
|
|
dialect: 'postgresql',
|
2013-05-04 12:23:55 -04:00
|
|
|
|
2013-09-17 06:54:26 -04:00
|
|
|
// Attach the appropriate grammar definitions onto the current client.
|
|
|
|
attachGrammars: function() {
|
|
|
|
this.grammar = grammar;
|
|
|
|
this.schemaGrammar = schemaGrammar;
|
|
|
|
},
|
|
|
|
|
2013-09-13 17:20:40 -04:00
|
|
|
// Runs the query on the specified connection, providing the bindings
|
|
|
|
// and any other necessary prep work.
|
2013-09-13 11:40:51 -04:00
|
|
|
runQuery: function(connection, sql, bindings, builder) {
|
2013-09-13 13:27:00 -04:00
|
|
|
if (!connection) throw new Error('No database connection exists for the query');
|
2013-09-13 12:59:12 -04:00
|
|
|
var questionCount = 0;
|
|
|
|
sql = sql.replace(/\?/g, function() {
|
|
|
|
questionCount++;
|
|
|
|
return '$' + questionCount;
|
|
|
|
});
|
2013-09-13 11:40:51 -04:00
|
|
|
if (builder && builder.flags.options) sql = _.extend({text: sql}, builder.flags.options);
|
2013-10-27 22:34:58 -04:00
|
|
|
return Promise.promisify(connection.query, connection)(sql, bindings);
|
2013-05-04 12:23:55 -04:00
|
|
|
},
|
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
// Get a raw connection, called by the `pool` whenever a new
|
|
|
|
// connection needs to be added to the pool.
|
2013-06-09 16:57:31 -04:00
|
|
|
getRawConnection: function(callback) {
|
2013-07-17 09:53:00 -04:00
|
|
|
var connection = new pg.Client(this.connectionSettings);
|
2013-10-27 22:34:58 -04:00
|
|
|
return Promise.promisify(connection.connect, connection)().bind(this).tap(function() {
|
|
|
|
if (!this.version) return this.checkVersion(connection);
|
|
|
|
}).bind().yield(connection);
|
2013-07-17 09:53:00 -04:00
|
|
|
},
|
|
|
|
|
2013-09-13 16:50:51 -04:00
|
|
|
// Used to explicitly close a connection, called internally by the pool
|
|
|
|
// when a connection times out or the pool is shutdown.
|
|
|
|
destroyRawConnection: function(connection) {
|
|
|
|
connection.end();
|
|
|
|
},
|
|
|
|
|
2013-09-11 23:36:55 -04:00
|
|
|
// In PostgreSQL, we need to do a version check to do some feature
|
|
|
|
// checking on the database.
|
|
|
|
checkVersion: function(connection) {
|
2013-07-17 09:53:00 -04:00
|
|
|
var instance = this;
|
2013-09-11 23:36:55 -04:00
|
|
|
this.runQuery(connection, 'select version();').then(function(resp) {
|
2013-07-17 09:53:00 -04:00
|
|
|
instance.version = /^PostgreSQL (.*?) /.exec(resp.rows[0].version)[1];
|
2013-06-09 16:57:31 -04:00
|
|
|
});
|
2013-05-02 00:21:49 -04:00
|
|
|
}
|
2013-05-04 12:23:55 -04:00
|
|
|
|
2013-05-02 00:21:49 -04:00
|
|
|
});
|