2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// PostgreSQL
|
|
|
|
// -------
|
2016-05-18 20:22:50 +10:00
|
|
|
import { assign, map, extend } from 'lodash'
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import Client from '../../client';
|
2016-08-09 17:23:07 -04:00
|
|
|
import Promise from 'bluebird';
|
2016-05-17 01:01:34 +10:00
|
|
|
import * as utils from './utils';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import QueryCompiler from './query/compiler';
|
|
|
|
import ColumnCompiler from './schema/columncompiler';
|
|
|
|
import TableCompiler from './schema/tablecompiler';
|
|
|
|
import SchemaCompiler from './schema/compiler';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
function Client_PG(config) {
|
|
|
|
Client.apply(this, arguments)
|
|
|
|
if (config.returning) {
|
|
|
|
this.defaultReturning = config.returning;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.searchPath) {
|
|
|
|
this.searchPath = config.searchPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inherits(Client_PG, Client)
|
|
|
|
|
|
|
|
assign(Client_PG.prototype, {
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
QueryCompiler,
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
ColumnCompiler,
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
SchemaCompiler,
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
TableCompiler,
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
dialect: 'postgresql',
|
|
|
|
|
|
|
|
driverName: 'pg',
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
_driver() {
|
2016-03-02 17:07:05 +01:00
|
|
|
return require('pg')
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
wrapIdentifier(value) {
|
2016-03-02 17:07:05 +01:00
|
|
|
if (value === '*') return value;
|
2016-05-17 01:01:34 +10:00
|
|
|
const matched = value.match(/(.*?)(\[[0-9]\])/);
|
2016-03-02 17:07:05 +01:00
|
|
|
if (matched) return this.wrapIdentifier(matched[1]) + matched[2];
|
2016-05-17 01:01:34 +10:00
|
|
|
return `"${value.replace(/"/g, '""')}"`;
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Prep the bindings as needed by PostgreSQL.
|
2016-05-17 01:01:34 +10:00
|
|
|
prepBindings(bindings, tz) {
|
2016-03-02 17:07:05 +01:00
|
|
|
return map(bindings, (binding) => {
|
|
|
|
return utils.prepareValue(binding, tz, this.valueForUndefined)
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Get a raw connection, called by the `pool` whenever a new
|
|
|
|
// connection needs to be added to the pool.
|
2016-05-17 01:01:34 +10:00
|
|
|
acquireRawConnection() {
|
|
|
|
const client = this;
|
2016-03-02 17:07:05 +01:00
|
|
|
return new Promise(function(resolver, rejecter) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const connection = new client.driver.Client(client.connectionSettings);
|
2016-03-02 17:07:05 +01:00
|
|
|
connection.connect(function(err, connection) {
|
|
|
|
if (err) return rejecter(err);
|
|
|
|
connection.on('error', client.__endConnection.bind(client, connection));
|
|
|
|
connection.on('end', client.__endConnection.bind(client, connection));
|
|
|
|
if (!client.version) {
|
|
|
|
return client.checkVersion(connection).then(function(version) {
|
|
|
|
client.version = version;
|
|
|
|
resolver(connection);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
resolver(connection);
|
|
|
|
});
|
|
|
|
}).tap(function setSearchPath(connection) {
|
|
|
|
return client.setSchemaSearchPath(connection);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Used to explicitly close a connection, called internally by the pool
|
|
|
|
// when a connection times out or the pool is shutdown.
|
2016-05-17 01:01:34 +10:00
|
|
|
destroyRawConnection(connection, cb) {
|
2016-03-02 17:07:05 +01:00
|
|
|
connection.end()
|
|
|
|
cb()
|
|
|
|
},
|
|
|
|
|
|
|
|
// In PostgreSQL, we need to do a version check to do some feature
|
|
|
|
// checking on the database.
|
2016-05-17 01:01:34 +10:00
|
|
|
checkVersion(connection) {
|
2016-03-02 17:07:05 +01:00
|
|
|
return new Promise(function(resolver, rejecter) {
|
|
|
|
connection.query('select version();', function(err, resp) {
|
|
|
|
if (err) return rejecter(err);
|
2016-03-24 22:17:02 +08:00
|
|
|
resolver(/^PostgreSQL (.*?)( |$)/.exec(resp.rows[0].version)[1]);
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Position the bindings for the query. The escape sequence for question mark
|
|
|
|
// is \? (e.g. knex.raw("\\?") since javascript requires '\' to be escaped too...)
|
2016-05-17 01:01:34 +10:00
|
|
|
positionBindings(sql) {
|
|
|
|
let questionCount = 0;
|
2016-03-02 17:07:05 +01:00
|
|
|
return sql.replace(/(\\*)(\?)/g, function (match, escapes) {
|
|
|
|
if (escapes.length % 2) {
|
|
|
|
return '?';
|
|
|
|
} else {
|
|
|
|
questionCount++;
|
2016-05-17 01:01:34 +10:00
|
|
|
return `$${questionCount}`;
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
setSchemaSearchPath(connection, searchPath) {
|
|
|
|
const path = (searchPath || this.searchPath);
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
if (!path) return Promise.resolve(true);
|
|
|
|
|
|
|
|
return new Promise(function(resolver, rejecter) {
|
2016-05-17 01:01:34 +10:00
|
|
|
connection.query(`set search_path to ${path}`, function(err) {
|
2016-03-02 17:07:05 +01:00
|
|
|
if (err) return rejecter(err);
|
|
|
|
resolver(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
_stream(connection, obj, stream, options) {
|
|
|
|
const PGQueryStream = process.browser ? undefined : require('pg-query-stream');
|
|
|
|
const sql = obj.sql = this.positionBindings(obj.sql)
|
2016-03-02 17:07:05 +01:00
|
|
|
return new Promise(function(resolver, rejecter) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const queryStream = connection.query(new PGQueryStream(sql, obj.bindings, options));
|
2016-03-02 17:07:05 +01:00
|
|
|
queryStream.on('error', rejecter);
|
|
|
|
// 'error' is not propagated by .pipe, but it breaks the pipe
|
|
|
|
stream.on('error', rejecter);
|
|
|
|
// 'end' IS propagated by .pipe, by default
|
|
|
|
stream.on('end', resolver);
|
|
|
|
queryStream.pipe(stream);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Runs the query on the specified connection, providing the bindings
|
|
|
|
// and any other necessary prep work.
|
2016-05-17 01:01:34 +10:00
|
|
|
_query(connection, obj) {
|
|
|
|
let sql = obj.sql = this.positionBindings(obj.sql)
|
2016-03-02 17:07:05 +01:00
|
|
|
if (obj.options) sql = extend({text: sql}, obj.options);
|
|
|
|
return new Promise(function(resolver, rejecter) {
|
|
|
|
connection.query(sql, obj.bindings, function(err, response) {
|
|
|
|
if (err) return rejecter(err);
|
|
|
|
obj.response = response;
|
|
|
|
resolver(obj);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Ensures the response is returned in the same format as other clients.
|
2016-05-17 01:01:34 +10:00
|
|
|
processResponse(obj, runner) {
|
|
|
|
const resp = obj.response;
|
2016-03-02 17:07:05 +01:00
|
|
|
if (obj.output) return obj.output.call(runner, resp);
|
|
|
|
if (obj.method === 'raw') return resp;
|
2016-05-17 01:01:34 +10:00
|
|
|
const { returning } = obj;
|
2016-03-02 17:07:05 +01:00
|
|
|
if (resp.command === 'SELECT') {
|
|
|
|
if (obj.method === 'first') return resp.rows[0];
|
|
|
|
if (obj.method === 'pluck') return map(resp.rows, obj.pluck);
|
|
|
|
return resp.rows;
|
|
|
|
}
|
|
|
|
if (returning) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const returns = [];
|
|
|
|
for (let i = 0, l = resp.rows.length; i < l; i++) {
|
|
|
|
const row = resp.rows[i];
|
2016-03-02 17:07:05 +01:00
|
|
|
if (returning === '*' || Array.isArray(returning)) {
|
|
|
|
returns[i] = row;
|
|
|
|
} else {
|
|
|
|
returns[i] = row[returning];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returns;
|
|
|
|
}
|
|
|
|
if (resp.command === 'UPDATE' || resp.command === 'DELETE') {
|
|
|
|
return resp.rowCount;
|
|
|
|
}
|
|
|
|
return resp;
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
__endConnection(connection) {
|
2016-03-02 17:07:05 +01:00
|
|
|
if (!connection || connection.__knex__disposed) return;
|
|
|
|
if (this.pool) {
|
|
|
|
connection.__knex__disposed = true;
|
|
|
|
this.pool.destroy(connection);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
ping(resource, callback) {
|
2016-03-19 19:14:46 +01:00
|
|
|
resource.query('SELECT 1', [], callback);
|
|
|
|
}
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default Client_PG
|