mirror of
https://github.com/knex/knex.git
synced 2025-07-04 07:26:21 +00:00
49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
// PostgreSQL Formatter
|
|
// -------
|
|
module.exports = function(client) {
|
|
|
|
var Formatter = require('../../formatter');
|
|
var inherits = require('inherits');
|
|
|
|
// The "formatter" is used to ensure all output is properly
|
|
// escaped & parameterized.
|
|
function Formatter_PG() {
|
|
this.client = client;
|
|
this.paramCount = 0;
|
|
Formatter.apply(this, arguments);
|
|
}
|
|
inherits(Formatter_PG, Formatter);
|
|
|
|
Formatter_PG.prototype.operators = [
|
|
'=', '<', '>', '<=', '>=', '<>', '!=',
|
|
'like', 'not like', 'between', 'ilike',
|
|
'&', '|', '#', '<<', '>>', '&&', '^', '@>', '<@', '||'
|
|
];
|
|
|
|
// Wraps a value (column, tableName) with the correct ticks.
|
|
Formatter_PG.prototype.wrapValue = function(value) {
|
|
if (value === '*') return value;
|
|
var matched = value.match(/(.*?)(\[[0-9]\])/);
|
|
if (matched) return this.wrapValue(matched[1]) + matched[2];
|
|
return '"' + value + '"';
|
|
};
|
|
|
|
// Memoize the calls to "wrap" for a little extra perf.
|
|
var wrapperMemo = (function(){
|
|
var memo = Object.create(null);
|
|
return function(key) {
|
|
if (memo[key] === void 0) {
|
|
memo[key] = this._wrapString(key);
|
|
}
|
|
return memo[key];
|
|
};
|
|
}());
|
|
|
|
Formatter_PG.prototype._wrap = wrapperMemo;
|
|
|
|
// Assign the formatter to the the client.
|
|
client.Formatter = Formatter_PG;
|
|
|
|
}; |