/
index.js
197 lines (169 loc) · 5.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// PostgreSQL
// -------
var _ = require('lodash')
var inherits = require('inherits')
var Client = require('../../client')
var Promise = require('../../promise')
var utils = require('./utils')
var assign = require('lodash/object/assign')
var QueryCompiler = require('./query/compiler')
var ColumnCompiler = require('./schema/columncompiler')
var TableCompiler = require('./schema/tablecompiler')
var SchemaCompiler = require('./schema/compiler')
var PGQueryStream;
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, {
QueryCompiler: QueryCompiler,
ColumnCompiler: ColumnCompiler,
SchemaCompiler: SchemaCompiler,
TableCompiler: TableCompiler,
dialect: 'postgresql',
driverName: 'pg',
_driver: function() {
return require('pg')
},
wrapIdentifier: function(value) {
if (value === '*') return value;
var matched = value.match(/(.*?)(\[[0-9]\])/);
if (matched) return this.wrapIdentifier(matched[1]) + matched[2];
return '"' + value.replace(/"/g, '""') + '"';
},
// Prep the bindings as needed by PostgreSQL.
prepBindings: function(bindings, tz) {
return _.map(bindings, function(binding) {
return utils.prepareValue(binding, tz, this.valueForUndefined)
}, this);
},
// 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 client.driver.Client(client.connectionSettings);
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.
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]);
});
});
},
// 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: function(sql) {
var questionCount = 0;
return sql.replace(/(\\*)(\?)/g, function (match, escapes) {
if (escapes.length % 2) {
return '?';
} else {
questionCount++;
return '$' + questionCount;
}
});
},
setSchemaSearchPath: function(connection, searchPath) {
var path = (searchPath || this.searchPath);
if (!path) return Promise.resolve(true);
return new Promise(function(resolver, rejecter) {
connection.query('set search_path to ' + path, function(err) {
if (err) return rejecter(err);
resolver(true);
});
});
},
_stream: function(connection, obj, stream, options) {
PGQueryStream = process.browser ? undefined : require('pg-query-stream');
var sql = obj.sql = this.positionBindings(obj.sql)
return new Promise(function(resolver, rejecter) {
var queryStream = connection.query(new PGQueryStream(sql, obj.bindings, options));
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.
_query: function(connection, obj) {
var sql = obj.sql = this.positionBindings(obj.sql)
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: function(obj, runner) {
var resp = obj.response;
if (obj.output) return obj.output.call(runner, resp);
if (obj.method === 'raw') return resp;
var returning = obj.returning;
if (resp.command === 'SELECT') {
if (obj.method === 'first') return resp.rows[0];
if (obj.method === 'pluck') return _.pluck(resp.rows, obj.pluck);
return resp.rows;
}
if (returning) {
var returns = [];
for (var i = 0, l = resp.rows.length; i < l; i++) {
var row = resp.rows[i];
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;
},
__endConnection: function(connection) {
if (!connection || connection.__knex__disposed) return;
if (this.pool) {
connection.__knex__disposed = true;
this.pool.destroy(connection);
}
},
})
module.exports = Client_PG