knex/clients/server/postgres.js

67 lines
2.3 KiB
JavaScript
Raw Normal View History

2013-09-13 13:50:41 -04:00
// PostgreSQL
// -------
// Other dependencies, including the `pg` library,
// which needs to be added as a dependency to the project
// using this database.
2013-11-25 02:00:12 -05:00
var _ = require('lodash');
var pg = require('pg');
// All other local project modules needed in this scope.
var ServerBase = require('./base').ServerBase;
var Helpers = require('../../lib/helpers').Helpers;
var Promise = require('../../lib/promise').Promise;
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 Promise.promisify(connection.query, 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 connection = new pg.Client(this.connectionSettings);
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
},
// 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];
});
}
});