2016-03-02 17:07:05 +01:00
|
|
|
// PostgreSQL
|
|
|
|
// -------
|
2018-07-09 08:10:34 -04:00
|
|
|
import { assign, map, extend, isArray, isString, includes } 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-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';
|
2018-07-09 08:10:34 -04:00
|
|
|
import { makeEscape } from '../../query/string';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
function Client_PG(config) {
|
2017-03-27 00:21:15 +03:00
|
|
|
Client.apply(this, arguments);
|
2016-03-02 17:07:05 +01:00
|
|
|
if (config.returning) {
|
|
|
|
this.defaultReturning = config.returning;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.searchPath) {
|
|
|
|
this.searchPath = config.searchPath;
|
|
|
|
}
|
|
|
|
}
|
2017-03-27 00:21:15 +03:00
|
|
|
inherits(Client_PG, Client);
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
assign(Client_PG.prototype, {
|
2016-09-13 08:15:58 -04:00
|
|
|
queryCompiler() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new QueryCompiler(this, ...arguments);
|
2016-09-13 08:15:58 -04:00
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-09-13 08:15:58 -04:00
|
|
|
columnCompiler() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new ColumnCompiler(this, ...arguments);
|
2016-09-13 08:15:58 -04:00
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-09-13 08:15:58 -04:00
|
|
|
schemaCompiler() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new SchemaCompiler(this, ...arguments);
|
2016-09-13 08:15:58 -04:00
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-09-13 08:15:58 -04:00
|
|
|
tableCompiler() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return new TableCompiler(this, ...arguments);
|
2016-09-13 08:15:58 -04:00
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
dialect: 'postgresql',
|
|
|
|
|
|
|
|
driverName: 'pg',
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
_driver() {
|
2018-07-09 08:10:34 -04:00
|
|
|
return require('pg');
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
2016-09-12 18:01:47 -04:00
|
|
|
_escapeBinding: makeEscape({
|
|
|
|
escapeArray(val, esc) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return esc(arrayString(val, esc));
|
2016-09-12 18:01:47 -04:00
|
|
|
},
|
|
|
|
escapeString(str) {
|
2018-07-09 08:10:34 -04:00
|
|
|
let hasBackslash = false;
|
|
|
|
let escaped = "'";
|
2016-09-12 18:01:47 -04:00
|
|
|
for (let i = 0; i < str.length; i++) {
|
2018-07-09 08:10:34 -04:00
|
|
|
const c = str[i];
|
|
|
|
if (c === "'") {
|
|
|
|
escaped += c + c;
|
2016-09-12 18:01:47 -04:00
|
|
|
} else if (c === '\\') {
|
2018-07-09 08:10:34 -04:00
|
|
|
escaped += c + c;
|
|
|
|
hasBackslash = true;
|
2016-09-12 18:01:47 -04:00
|
|
|
} else {
|
2018-07-09 08:10:34 -04:00
|
|
|
escaped += c;
|
2016-09-12 18:01:47 -04:00
|
|
|
}
|
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
escaped += "'";
|
2016-09-12 18:01:47 -04:00
|
|
|
if (hasBackslash === true) {
|
2018-07-09 08:10:34 -04:00
|
|
|
escaped = 'E' + escaped;
|
2016-09-12 18:01:47 -04:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
return escaped;
|
2016-09-12 18:01:47 -04:00
|
|
|
},
|
2017-04-12 12:57:27 +02:00
|
|
|
escapeObject(val, prepareValue, timezone, seen = []) {
|
2016-09-12 18:01:47 -04:00
|
|
|
if (val && typeof val.toPostgres === 'function') {
|
|
|
|
seen = seen || [];
|
|
|
|
if (seen.indexOf(val) !== -1) {
|
2018-07-09 08:10:34 -04:00
|
|
|
throw new Error(
|
|
|
|
`circular reference detected while preparing "${val}" for query`
|
|
|
|
);
|
2016-09-12 18:01:47 -04:00
|
|
|
}
|
|
|
|
seen.push(val);
|
|
|
|
return prepareValue(val.toPostgres(prepareValue), seen);
|
|
|
|
}
|
|
|
|
return JSON.stringify(val);
|
2018-07-09 08:10:34 -04:00
|
|
|
},
|
2016-09-12 18:01:47 -04:00
|
|
|
}),
|
|
|
|
|
2017-09-08 14:58:59 +03:00
|
|
|
wrapIdentifierImpl(value) {
|
2016-03-02 17:07:05 +01:00
|
|
|
if (value === '*') return value;
|
2018-01-03 00:05:48 +02:00
|
|
|
|
|
|
|
let arrayAccessor = '';
|
|
|
|
const arrayAccessorMatch = value.match(/(.*?)(\[[0-9]+\])/);
|
|
|
|
|
|
|
|
if (arrayAccessorMatch) {
|
|
|
|
value = arrayAccessorMatch[1];
|
|
|
|
arrayAccessor = arrayAccessorMatch[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
return `"${value.replace(/"/g, '""')}"${arrayAccessor}`;
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// 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) {
|
2016-09-13 18:12:23 -04:00
|
|
|
if (err) {
|
|
|
|
return rejecter(err);
|
|
|
|
}
|
|
|
|
connection.on('error', (err) => {
|
2018-07-09 08:10:34 -04:00
|
|
|
connection.__knex__disposed = err;
|
|
|
|
});
|
2017-06-01 16:41:35 -03:00
|
|
|
connection.on('end', (err) => {
|
|
|
|
connection.__knex__disposed = err || 'Connection ended unexpectedly';
|
2018-07-09 08:10:34 -04:00
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
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-09-13 18:12:23 -04:00
|
|
|
destroyRawConnection(connection) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return Promise.fromCallback(connection.end.bind(connection));
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// 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;
|
2018-07-09 08:10:34 -04:00
|
|
|
return sql.replace(/(\\*)(\?)/g, function(match, escapes) {
|
2016-03-02 17:07:05 +01:00
|
|
|
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) {
|
2018-07-09 08:10:34 -04:00
|
|
|
let path = searchPath || this.searchPath;
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
if (!path) return Promise.resolve(true);
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
if (!isArray(path) && !isString(path)) {
|
|
|
|
throw new TypeError(
|
|
|
|
`knex: Expected searchPath to be Array/String, got: ${typeof path}`
|
|
|
|
);
|
2017-11-18 17:09:05 +01:00
|
|
|
}
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
if (isString(path)) {
|
|
|
|
if (includes(path, ',')) {
|
2017-11-18 17:09:05 +01:00
|
|
|
const parts = path.split(',');
|
2018-07-09 08:10:34 -04:00
|
|
|
const arraySyntax = `[${map(
|
|
|
|
parts,
|
|
|
|
(searchPath) => `'${searchPath}'`
|
|
|
|
).join(', ')}]`;
|
2018-05-29 17:42:03 +02:00
|
|
|
this.logger.warn(
|
2018-07-09 08:10:34 -04:00
|
|
|
`Detected comma in searchPath "${path}".` +
|
|
|
|
`If you are trying to specify multiple schemas, use Array syntax: ${arraySyntax}`
|
|
|
|
);
|
2017-11-18 17:09:05 +01:00
|
|
|
}
|
|
|
|
path = [path];
|
|
|
|
}
|
|
|
|
|
|
|
|
path = map(path, (schemaName) => `"${schemaName}"`).join(',');
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
return new Promise(function(resolver, rejecter) {
|
2017-11-18 17:09:05 +01: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) {
|
2018-07-09 08:10:34 -04:00
|
|
|
const PGQueryStream = process.browser
|
|
|
|
? undefined
|
|
|
|
: require('pg-query-stream');
|
2017-09-27 13:12:40 +03:00
|
|
|
const sql = obj.sql;
|
2018-06-27 16:42:50 -03:00
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
return new Promise(function(resolver, rejecter) {
|
2018-07-09 08:10:34 -04:00
|
|
|
const queryStream = connection.query(
|
|
|
|
new PGQueryStream(sql, obj.bindings, options)
|
|
|
|
);
|
2018-06-27 16:42:50 -03:00
|
|
|
|
|
|
|
queryStream.on('error', function(error) {
|
2017-02-24 10:55:13 -06:00
|
|
|
rejecter(error);
|
2018-06-27 16:42:50 -03:00
|
|
|
stream.emit('error', error);
|
2017-02-24 10:55:13 -06:00
|
|
|
});
|
2018-07-09 08:10:34 -04:00
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
// '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) {
|
2017-09-27 13:12:40 +03:00
|
|
|
let sql = obj.sql;
|
2018-07-09 08:10:34 -04:00
|
|
|
if (obj.options) sql = extend({ text: sql }, obj.options);
|
2016-03-02 17:07:05 +01:00
|
|
|
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 {
|
2018-06-05 15:31:09 +03:00
|
|
|
// Pluck the only column in the row.
|
|
|
|
returns[i] = row[Object.keys(row)[0]];
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return returns;
|
|
|
|
}
|
|
|
|
if (resp.command === 'UPDATE' || resp.command === 'DELETE') {
|
|
|
|
return resp.rowCount;
|
|
|
|
}
|
|
|
|
return resp;
|
2018-07-09 08:10:34 -04:00
|
|
|
},
|
|
|
|
});
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-10-12 00:43:30 -04:00
|
|
|
function arrayString(arr, esc) {
|
2018-07-09 08:10:34 -04:00
|
|
|
let result = '{';
|
|
|
|
for (let i = 0; i < arr.length; i++) {
|
|
|
|
if (i > 0) result += ',';
|
|
|
|
const val = arr[i];
|
2016-10-12 00:43:30 -04:00
|
|
|
if (val === null || typeof val === 'undefined') {
|
2018-07-09 08:10:34 -04:00
|
|
|
result += 'NULL';
|
2016-10-12 00:43:30 -04:00
|
|
|
} else if (Array.isArray(val)) {
|
2018-07-09 08:10:34 -04:00
|
|
|
result += arrayString(val, esc);
|
2016-10-12 00:43:30 -04:00
|
|
|
} else if (typeof val === 'number') {
|
2018-07-09 08:10:34 -04:00
|
|
|
result += val;
|
2016-10-12 00:43:30 -04:00
|
|
|
} else {
|
2018-07-09 08:10:34 -04:00
|
|
|
result += JSON.stringify(typeof val === 'string' ? val : esc(val));
|
2016-10-12 00:43:30 -04:00
|
|
|
}
|
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
return result + '}';
|
2016-10-12 00:43:30 -04:00
|
|
|
}
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
export default Client_PG;
|