282 lines
7.9 KiB
JavaScript
Raw Normal View History

2016-03-02 17:07:05 +01:00
// PostgreSQL
// -------
import { assign, map, extend, isArray, isString, includes } from 'lodash'
import inherits from 'inherits';
import Client from '../../client';
import Promise from 'bluebird';
2016-03-02 17:07:05 +01:00
import QueryCompiler from './query/compiler';
import ColumnCompiler from './schema/columncompiler';
import TableCompiler from './schema/tablecompiler';
import SchemaCompiler from './schema/compiler';
import {makeEscape} from '../../query/string'
2016-03-02 17:07:05 +01:00
function Client_PG(config) {
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;
}
if (config.version) {
this.version = config.version;
}
2016-03-02 17:07:05 +01: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() {
return new QueryCompiler(this, ...arguments)
},
2016-03-02 17:07:05 +01:00
2016-09-13 08:15:58 -04:00
columnCompiler() {
return new ColumnCompiler(this, ...arguments)
},
2016-03-02 17:07:05 +01:00
2016-09-13 08:15:58 -04:00
schemaCompiler() {
return new SchemaCompiler(this, ...arguments)
},
2016-03-02 17:07:05 +01:00
2016-09-13 08:15:58 -04:00
tableCompiler() {
return new TableCompiler(this, ...arguments)
},
2016-03-02 17:07:05 +01:00
dialect: 'postgresql',
driverName: 'pg',
_driver() {
2016-03-02 17:07:05 +01:00
return require('pg')
},
_escapeBinding: makeEscape({
escapeArray(val, esc) {
return esc(arrayString(val, esc))
},
escapeString(str) {
let hasBackslash = false
let escaped = '\''
for (let i = 0; i < str.length; i++) {
const c = str[i]
if (c === '\'') {
escaped += c + c
} else if (c === '\\') {
escaped += c + c
hasBackslash = true
} else {
escaped += c
}
}
escaped += '\''
if (hasBackslash === true) {
escaped = 'E' + escaped
}
return escaped
},
escapeObject(val, prepareValue, timezone, seen = []) {
if (val && typeof val.toPostgres === 'function') {
seen = seen || [];
if (seen.indexOf(val) !== -1) {
throw new Error(`circular reference detected while preparing "${val}" for query`);
}
seen.push(val);
return prepareValue(val.toPostgres(prepareValue), seen);
}
return JSON.stringify(val);
}
}),
wrapIdentifierImpl(value) {
2016-03-02 17:07:05 +01:00
if (value === '*') return value;
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.
acquireRawConnection() {
const client = this;
2016-03-02 17:07:05 +01:00
return new Promise(function(resolver, rejecter) {
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) => {
connection.__knex__disposed = err
})
connection.on('end', (err) => {
connection.__knex__disposed = err || 'Connection ended unexpectedly';
})
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) {
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.
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);
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...)
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++;
return `$${questionCount}`;
2016-03-02 17:07:05 +01:00
}
});
},
setSchemaSearchPath(connection, searchPath) {
let path = (searchPath || this.searchPath);
2016-03-02 17:07:05 +01:00
if (!path) return Promise.resolve(true);
if(!isArray(path) && !isString(path)) {
throw new TypeError(`knex: Expected searchPath to be Array/String, got: ${typeof path}`);
}
if(isString(path)) {
if(includes(path, ',')) {
const parts = path.split(',');
const arraySyntax = `[${map(parts, (searchPath) => `'${searchPath}'`).join(', ')}]`;
this.logger.warn(
`Detected comma in searchPath "${path}".`
+
`If you are trying to specify multiple schemas, use Array syntax: ${arraySyntax}`);
}
path = [path];
}
path = map(path, (schemaName) => `"${schemaName}"`).join(',');
2016-03-02 17:07:05 +01:00
return new Promise(function(resolver, rejecter) {
connection.query(`set search_path to ${path}`, function(err) {
2016-03-02 17:07:05 +01:00
if (err) return rejecter(err);
resolver(true);
});
});
},
_stream(connection, obj, stream, options) {
const PGQueryStream = process.browser ? undefined : require('pg-query-stream');
const sql = obj.sql;
2016-03-02 17:07:05 +01:00
return new Promise(function(resolver, rejecter) {
const queryStream = connection.query(new PGQueryStream(sql, obj.bindings, options));
queryStream.on('error', function(error) {
rejecter(error);
stream.emit('error', error);
});
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.
_query(connection, obj) {
let sql = 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.
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;
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) {
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;
}
2016-03-02 17:07:05 +01:00
})
function arrayString(arr, esc) {
let result = '{'
for (let i = 0 ; i < arr.length; i++) {
if (i > 0) result += ','
const val = arr[i]
if (val === null || typeof val === 'undefined') {
result += 'NULL'
} else if (Array.isArray(val)) {
result += arrayString(val, esc)
} else if (typeof val === 'number') {
result += val
} else {
result += JSON.stringify(typeof val === 'string' ? val : esc(val))
}
}
return result + '}'
}
export default Client_PG