-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
329 lines (315 loc) · 9.43 KB
/
index.js
File metadata and controls
329 lines (315 loc) · 9.43 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
(function () {
"use strict";
var _ = require('lodash');
var debugModule;
try {
debugModule = require('debug');
} catch( e ) {
// debug module not found, ignore and use a no-op
debugModule = function(name) { return function() {}; };
}
var debug = {
main: debugModule('pg-spice'),
parsed: debugModule('pg-spice:parsed'),
params: debugModule('pg-spice:params'),
sql: debugModule('pg-spice:sql')
};
var defaults = {
enableParseCache: true,
allowMultipleParamTypes: false,
trimDebugSql: process.env.PG_SPICE_TRIM_DEBUG_SQL === 'true'
};
var globals = {
isPatched: false,
options: defaults
};
var parseCache = {};
var PARAMETER_SEPARATORS = ['"', '\'', ':', '&', ',', ';', '(', ')', '|', '=', '+', '-', '*', '%', '/', '\\', '<', '>', '^'];
var SKIPS = [{start: "'", stop: "'"},
{start: "\"", stop: "\""},
{start: "--", stop: "\n"},
{start: "/*", stop: "*/"}];
var START_SKIP = ["'", "\"", "--", "/*"];
var STOP_SKIP = ["'", "\"", "\n", "*/"];
function skipCommentsAndQuotes(sql, position) {
var i, j, m, n, match, offset, endMatch, endPos;
for(i=0;i<START_SKIP.length;i++) {
if( sql[position] == START_SKIP[i][0] ) {
match = true;
for(j=1;j<START_SKIP[i].length;j++) {
if( sql[position+j] != START_SKIP[i][j] ) {
match = false;
break;
}
}
if( match ) {
offset = START_SKIP[i].length;
for(m=position+offset;m<sql.length;m++) {
if( sql[m] == STOP_SKIP[i][0] ) {
endMatch = true;
endPos = m;
for(n=1;n<STOP_SKIP[i].length;n++) {
if( (m+n) >= sql.length ) {
// multi char skip not properly closed
return sql.length;
}
if( sql[m+n] != STOP_SKIP[i][n] ) {
endMatch = false;
break;
}
endPos = m + n;
}
if( endMatch ) {
return endPos + 1;
}
}
}
// comment or qutoe not closed properly
return sql.length;
}
}
}
return position;
}
function isParamSeparator(c) {
if( /\s/.test(c) ) {
return true;
}
for(var i=0;i<PARAMETER_SEPARATORS.length;i++) {
if( PARAMETER_SEPARATORS[i] == c ) {
return true;
}
}
return false;
}
function parseSql(stmt) {
var ret;
if( globals.options.enableParseCache ) {
ret = parseCache[stmt];
if( ret ) {
return ret;
}
}
ret = _parseSql(stmt);
if( globals.options.enableParseCache ) {
parseCache[stmt] = ret;
}
return ret;
}
/**
* Parses sql for named parameters
*
* Returns a list of named parameters found in the sql
* sql - the parsed sql with parameters replaced with $X placeholders
* originalSql - the original sql passed in as a parameter
* params[] - list of parsed parameters
*
* Each entry of params[] has the following properties:
* name - the name of the parameter
* indexes[] - the list of indexes, one-orgin, of the parameter
*/
function _parseSql(stmt) {
var params = [];
var i = 0, j;
if( !_.isString(stmt) ) {
throw new Error("stmt argument must be a string, given: " + typeof(stmt));
}
while( i < stmt.length ) {
var skipToPosition = i;
while( i < stmt.length ) {
skipToPosition = skipCommentsAndQuotes(stmt, i);
if( i == skipToPosition ) {
break;
} else {
i = skipToPosition;
}
}
if( i>= stmt.length ) {
break;
}
var c = stmt[i];
if( c == ':' || c == '&' || c == '$' ) {
j = i + 1;
if( j < stmt.length && c == ':' && stmt[j] == ':' ) {
// Postgres-style "::" cast (skip)
i = i + 2;
continue;
}
if( j < stmt.length && c == ':' && stmt[j] == '{' ) {
// :{foobar} style parameter
while( j < stmt.length && '}' != stmt[j] ) {
j++;
if( ':' == stmt[j] || '{' == stmt[j] ) {
throw new Error("Parameter name contains invalid character '" +
stmt[j] +
"' at position " + i + " in statement " + stmt);
}
}
if( j >= stmt.length ) {
throw new Error("Non-terminated named parameter declaration" +
" at position " + i + " in statement " + stmt);
}
if( (j-i) > 3 ) {
params.push({
name: stmt.substring(i+2, j),
start: i,
end: j+1,
type: ':{}'
});
}
j++;
} else {
// :foobar or $foobar style parameter
while( j < stmt.length && !isParamSeparator(stmt[j]) ) {
j++;
}
if( (j-i) > 1 ) {
params.push({
name: stmt.substring(i+1, j),
start: i,
end: j,
type: stmt[i]
});
}
}
i = j - 1;
} else {
if( c == '\\' ) {
j = i + 1;
if( j < stmt.length && stmt[j] == ':' ) {
// escaped ":" (skip)
i = i + 2;
continue;
}
}
if( c == '?' ) {
// unamed param?
}
if( c == '$' ) {
// unamed param?
}
}
i++;
}
var ret = {
"sql": stmt,
"originalSql": stmt,
"params": [],
"numParams": params.length,
"numDistinctParams": 0
};
var param;
var namedParam;
var paramTypes = {};
var namedParams = {};
for(i=0;i<params.length;i++) {
param = params[i];
paramTypes[param.type] = (paramTypes[param.type] || 0) + 1;
if( /^[0-9]+$/.test(param.name) ) {
throw new Error("You cannot mix named and numbered parameters." +
" Check parameter '" + param.name + "' at position " + param.start +
" in statement: " + stmt);
}
namedParam = namedParams[param.name];
if( !namedParam ) {
// increment before we use it so it's 1-origin
ret.numDistinctParams++;
namedParam = {
// :foo
"name": param.name,
// the $N order it appears in the sql
"index": ret.numDistinctParams,
// all the $N spots replaced that it appears
"indexes": []
};
namedParams[param.name] = namedParam;
// Add them in order of appearance
ret.params.push(namedParam);
}
namedParam.indexes.push(i+1);
}
if( !globals.options.allowMultipleParamTypes ) {
if( _.size(_.keys(paramTypes)) > 1 ) {
throw new Error("You cannot mix multiple types of named parameters in statement: " + stmt);
}
}
// Loop backwards so the start/end stay accurrate:
for(i=params.length-1;i>=0;i--) {
param = params[i];
namedParam = namedParams[param.name];
// "SELECT :foo FROM bar" ==> "SELECT " + $1 + " FROM bar"
ret.sql = ret.sql.substring(0,param.start) +
"$" + namedParam.index +
ret.sql.substring(param.end);
}
return ret;
}
/**
* Converts parameter values from object to an array. Parameter value
* indexes come from the parsedSql object and a given parameter may
* appear in multiple positions.
*/
function convertParamValues(parsedSql, values) {
var ret = [];
_.each(parsedSql.params, function(param) {
if( !_.has(values, param.name) ) {
throw new Error("No value found for parameter: " + param.name);
}
ret.push(values[param.name]);
});
return ret;
}
function filterDebugSQL(sql) {
if( globals.options.trimDebugSql ) {
return sql.replace(/\s+/g, " ");
}
return sql;
}
function patch(pg, options) {
if( globals.isPatched ) {
debug.main('Already patched, skipping');
return;
}
globals.isPatched = true;
globals.options = _.defaults(options || {}, defaults);
debug.main('Patching pg module with options:', globals.options);
// Add named parameter support to Client.query:
var origQuery = pg.Client.prototype.query;
pg.Client.prototype.query = function(config, values, callback) {
var sql;
if( _.isString(config) ) {
sql = config;
} else if( _.isObject(config) && _.has(config, 'text') ) {
sql = config.text;
}
if( sql ) {
debug.sql(filterDebugSQL(sql));
}
if( arguments.length === 3 && !_.isArray(values) && _.isObject(values) ) {
try {
if( !sql ) {
throw new Error("First parameter of query() must be a string or config object with a name property");
}
var parsedSql = parseSql(sql);
debug.main("parsed sql:", parsedSql);
var params = convertParamValues(parsedSql, values);
debug.main("parsed params:", params);
return origQuery.apply(this, [parsedSql.sql, params, callback]);
} catch( err ) {
if( callback ) {
return callback(err);
} else {
return;
}
}
}
return origQuery.apply(this, arguments);
};
}
module.exports = {
"patch": patch,
// For testing:
"parseSql": parseSql,
"convertParamValues": convertParamValues
};
})();