2014-09-01 17:18:45 +02:00
|
|
|
'use strict';
|
|
|
|
|
2014-04-08 16:25:57 -04:00
|
|
|
// PostgreSQL
|
|
|
|
// -------
|
2015-04-19 16:31:52 -04:00
|
|
|
var _ = require('lodash')
|
|
|
|
var inherits = require('inherits')
|
|
|
|
var Runner = require('./runner')
|
|
|
|
var Client = require('../../client')
|
|
|
|
var Promise = require('../../promise')
|
|
|
|
var assign = require('lodash/object/assign')
|
|
|
|
var QueryCompiler = require('./query/compiler')
|
|
|
|
var utils = require('./utils')
|
2014-04-08 16:25:57 -04:00
|
|
|
|
|
|
|
function Client_PG(config) {
|
2015-04-19 16:31:52 -04:00
|
|
|
Client.apply(this, arguments)
|
|
|
|
if (config.returning) {
|
|
|
|
this.defaultReturning = config.returning;
|
2015-02-03 09:07:52 -05:00
|
|
|
}
|
|
|
|
}
|
2015-04-19 16:31:52 -04:00
|
|
|
inherits(Client_PG, Client)
|
|
|
|
|
|
|
|
assign(Client_PG.prototype, {
|
|
|
|
|
|
|
|
Runner: Runner,
|
|
|
|
|
|
|
|
QueryCompiler: QueryCompiler,
|
2015-02-03 09:07:52 -05:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
dialect: 'postgresql',
|
|
|
|
|
|
|
|
wrapValue: function(value) {
|
|
|
|
if (value === '*') return value;
|
|
|
|
var matched = value.match(/(.*?)(\[[0-9]\])/);
|
|
|
|
if (matched) return this.wrapValue(matched[1]) + matched[2];
|
|
|
|
return '"' + value.replace(/"/g, '""') + '"';
|
|
|
|
},
|
|
|
|
|
|
|
|
// Lazy load the pg dependency, since we might just be using
|
|
|
|
// the client to generate SQL strings.
|
|
|
|
initDriver: function() {
|
|
|
|
pg = pg || (function() {
|
|
|
|
try {
|
|
|
|
return require('pg');
|
|
|
|
} catch (e) {
|
|
|
|
return require('pg.js');
|
2014-04-16 01:23:50 -04:00
|
|
|
}
|
2015-04-19 16:31:52 -04:00
|
|
|
})();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Prep the bindings as needed by PostgreSQL.
|
|
|
|
prepBindings: function(bindings, tz) {
|
|
|
|
return _.map(bindings, utils.prepareValue);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get a raw connection, called by the `pool` whenever a new
|
|
|
|
// connection needs to be added to the pool.
|
|
|
|
acquireRawConnection: function() {
|
|
|
|
var client = this;
|
|
|
|
return new Promise(function(resolver, rejecter) {
|
|
|
|
var connection = new pg.Client(client.connectionSettings);
|
|
|
|
client.databaseName = connection.database;
|
|
|
|
connection.connect(function(err, connection) {
|
|
|
|
if (err) return rejecter(err);
|
|
|
|
connection.on('error', this.endConnection.bind(this, connection));
|
|
|
|
connection.on('end', this.endConnection.bind(this, connection));
|
|
|
|
if (!client.version) {
|
|
|
|
return client.checkVersion(connection).then(function(version) {
|
|
|
|
client.version = version;
|
|
|
|
resolver(connection);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
resolver(connection);
|
|
|
|
});
|
2014-04-16 01:23:50 -04:00
|
|
|
});
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
2014-04-08 16:25:57 -04:00
|
|
|
|
2015-04-19 16:31:52 -04:00
|
|
|
endConnection: function(connection) {
|
|
|
|
if (!connection || connection.__knex__disposed) return;
|
|
|
|
if (this.pool) {
|
|
|
|
connection.__knex__disposed = true;
|
|
|
|
this.pool.destroy(connection);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Used to explicitly close a connection, called internally by the pool
|
|
|
|
// when a connection times out or the pool is shutdown.
|
|
|
|
destroyRawConnection: function(connection, cb) {
|
|
|
|
connection.end();
|
|
|
|
cb()
|
|
|
|
},
|
|
|
|
|
|
|
|
// In PostgreSQL, we need to do a version check to do some feature
|
|
|
|
// checking on the database.
|
|
|
|
checkVersion: function(connection) {
|
|
|
|
return new Promise(function(resolver, rejecter) {
|
|
|
|
connection.query('select version();', function(err, resp) {
|
|
|
|
if (err) return rejecter(err);
|
|
|
|
resolver(/^PostgreSQL (.*?) /.exec(resp.rows[0].version)[1]);
|
|
|
|
});
|
2014-04-16 01:23:50 -04:00
|
|
|
});
|
2015-04-19 16:31:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Position the bindings for the query.
|
|
|
|
positionBindings: function(sql) {
|
|
|
|
var questionCount = 0;
|
|
|
|
return sql.replace(/\?/g, function() {
|
|
|
|
questionCount++;
|
|
|
|
return '$' + questionCount;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2014-07-14 13:51:43 -04:00
|
|
|
|
2014-07-21 12:00:12 -06:00
|
|
|
module.exports = Client_PG;
|