knex/clients/server/postgres.js

71 lines
2.4 KiB
JavaScript
Raw Normal View History

2013-09-13 13:50:41 -04:00
// PostgreSQL
// -------
// All of the "when.js" promise components needed in this module.
var when = require('when');
var nodefn = require('when/node/function');
2013-03-12 11:52:08 -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');
// All other local project modules needed in this scope.
var ServerBase = require('./base').ServerBase;
var Helpers = require('../../lib/helpers').Helpers;
2013-04-28 20:58:38 -04:00
var grammar = require('./postgres/grammar').grammar;
var schemaGrammar = require('./postgres/schemagrammar').schemaGrammar;
// Constructor for the PostgreSQL Client
exports.Client = ServerBase.extend({
dialect: 'postgresql',
// 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.
runQuery: function(connection, sql, bindings, builder) {
if (!connection) throw new Error('No database connection exists for the query');
var questionCount = 0;
sql = sql.replace(/\?/g, function() {
questionCount++;
return '$' + questionCount;
});
if (builder && builder.flags.options) sql = _.extend({text: sql}, builder.flags.options);
return nodefn.call(connection.query.bind(connection), sql, bindings);
},
// Get a raw connection, called by the `pool` whenever a new
// connection needs to be added to the pool.
getRawConnection: function(callback) {
2013-07-17 09:53:00 -04:00
var instance = this;
var connection = new pg.Client(this.connectionSettings);
return nodefn.call(connection.connect.bind(connection)).tap(function() {
if (!instance.version) return instance.checkVersion(connection);
}).yield(connection);
2013-07-17 09:53:00 -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();
},
// 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;
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];
});
}
});