pg_query 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,41 +0,0 @@
1
- /* Polyfills to avoid building unnecessary objects from the PostgreSQL source */
2
-
3
- #include "postgres.h"
4
-
5
- /* src/backend/postmaster/postmaster.c */
6
- bool ClientAuthInProgress = false;
7
- bool redirection_done = false;
8
-
9
- /* src/backend/postmaster/syslogger.c */
10
- bool am_syslogger = false;
11
-
12
- /* src/backend/tcop/postgres.c */
13
- #include "tcop/dest.h"
14
- const char *debug_query_string;
15
- CommandDest whereToSendOutput = DestDebug;
16
-
17
- /* src/backend/utils/misc/guc.c */
18
- char *application_name;
19
- int client_min_messages = NOTICE;
20
- int log_min_error_statement = ERROR;
21
- int log_min_messages = WARNING;
22
- int trace_recovery_messages = LOG;
23
-
24
- /* src/backend/storage/lmgr/proc.c */
25
- #include "storage/proc.h"
26
- PGPROC *MyProc = NULL;
27
-
28
- /* src/backend/storage/ipc/ipc.c */
29
- bool proc_exit_inprogress = false;
30
-
31
- /* src/backend/tcop/postgres.c */
32
- #include "miscadmin.h"
33
- void check_stack_depth(void) { /* Do nothing */ }
34
-
35
- /* src/backends/commands/define.c */
36
- #include "commands/defrem.h"
37
- #include "nodes/makefuncs.h"
38
- DefElem * defWithOids(bool value)
39
- {
40
- return makeDefElem("oids", (Node *) makeInteger(value));
41
- }
@@ -1,15 +0,0 @@
1
- #include "pg_query.h"
2
-
3
- const char* progname = "pg_query";
4
-
5
- void Init_pg_query(void)
6
- {
7
- VALUE cPgQuery;
8
-
9
- MemoryContextInit();
10
-
11
- cPgQuery = rb_const_get(rb_cObject, rb_intern("PgQuery"));
12
-
13
- rb_define_singleton_method(cPgQuery, "_raw_parse", pg_query_raw_parse, 1);
14
- rb_define_singleton_method(cPgQuery, "normalize", pg_query_normalize, 1);
15
- }
@@ -1,18 +0,0 @@
1
- #ifndef PG_QUERY_H
2
- #define PG_QUERY_H
3
-
4
- #include "postgres.h"
5
- #include "utils/memutils.h"
6
-
7
- #include <ruby.h>
8
-
9
- #define STDERR_BUFFER_LEN 4096
10
- //#define DEBUG
11
-
12
- VALUE new_parse_error(ErrorData* error);
13
-
14
- void Init_pg_query(void);
15
- VALUE pg_query_normalize(VALUE self, VALUE input);
16
- VALUE pg_query_raw_parse(VALUE self, VALUE input);
17
-
18
- #endif
@@ -1,363 +0,0 @@
1
- #include "pg_query.h"
2
-
3
- #include "parser/parser.h"
4
- #include "parser/scanner.h"
5
- #include "parser/scansup.h"
6
- #include "mb/pg_wchar.h"
7
- #include "nodes/nodeFuncs.h"
8
-
9
- /*
10
- * Struct for tracking locations/lengths of constants during normalization
11
- */
12
- typedef struct pgssLocationLen
13
- {
14
- int location; /* start offset in query text */
15
- int length; /* length in bytes, or -1 to ignore */
16
- } pgssLocationLen;
17
-
18
- /*
19
- * Working state for constant tree walker
20
- */
21
- typedef struct pgssConstLocations
22
- {
23
- /* Array of locations of constants that should be removed */
24
- pgssLocationLen *clocations;
25
-
26
- /* Allocated length of clocations array */
27
- int clocations_buf_size;
28
-
29
- /* Current number of valid entries in clocations array */
30
- int clocations_count;
31
- } pgssConstLocations;
32
-
33
- /*
34
- * comp_location: comparator for qsorting pgssLocationLen structs by location
35
- */
36
- static int
37
- comp_location(const void *a, const void *b)
38
- {
39
- int l = ((const pgssLocationLen *) a)->location;
40
- int r = ((const pgssLocationLen *) b)->location;
41
-
42
- if (l < r)
43
- return -1;
44
- else if (l > r)
45
- return +1;
46
- else
47
- return 0;
48
- }
49
-
50
- /*
51
- * Given a valid SQL string and an array of constant-location records,
52
- * fill in the textual lengths of those constants.
53
- *
54
- * The constants may use any allowed constant syntax, such as float literals,
55
- * bit-strings, single-quoted strings and dollar-quoted strings. This is
56
- * accomplished by using the public API for the core scanner.
57
- *
58
- * It is the caller's job to ensure that the string is a valid SQL statement
59
- * with constants at the indicated locations. Since in practice the string
60
- * has already been parsed, and the locations that the caller provides will
61
- * have originated from within the authoritative parser, this should not be
62
- * a problem.
63
- *
64
- * Duplicate constant pointers are possible, and will have their lengths
65
- * marked as '-1', so that they are later ignored. (Actually, we assume the
66
- * lengths were initialized as -1 to start with, and don't change them here.)
67
- *
68
- * N.B. There is an assumption that a '-' character at a Const location begins
69
- * a negative numeric constant. This precludes there ever being another
70
- * reason for a constant to start with a '-'.
71
- */
72
- static void
73
- fill_in_constant_lengths(pgssConstLocations *jstate, const char *query)
74
- {
75
- pgssLocationLen *locs;
76
- core_yyscan_t yyscanner;
77
- core_yy_extra_type yyextra;
78
- core_YYSTYPE yylval;
79
- YYLTYPE yylloc;
80
- int last_loc = -1;
81
- int i;
82
-
83
- /*
84
- * Sort the records by location so that we can process them in order while
85
- * scanning the query text.
86
- */
87
- if (jstate->clocations_count > 1)
88
- qsort(jstate->clocations, jstate->clocations_count,
89
- sizeof(pgssLocationLen), comp_location);
90
- locs = jstate->clocations;
91
-
92
- /* initialize the flex scanner --- should match raw_parser() */
93
- yyscanner = scanner_init(query,
94
- &yyextra,
95
- ScanKeywords,
96
- NumScanKeywords);
97
-
98
- /* Search for each constant, in sequence */
99
- for (i = 0; i < jstate->clocations_count; i++)
100
- {
101
- int loc = locs[i].location;
102
- int tok;
103
-
104
- Assert(loc >= 0);
105
-
106
- if (loc <= last_loc)
107
- continue; /* Duplicate constant, ignore */
108
-
109
- /* Lex tokens until we find the desired constant */
110
- for (;;)
111
- {
112
- tok = core_yylex(&yylval, &yylloc, yyscanner);
113
-
114
- /* We should not hit end-of-string, but if we do, behave sanely */
115
- if (tok == 0)
116
- break; /* out of inner for-loop */
117
-
118
- /*
119
- * We should find the token position exactly, but if we somehow
120
- * run past it, work with that.
121
- */
122
- if (yylloc >= loc)
123
- {
124
- if (query[loc] == '-')
125
- {
126
- /*
127
- * It's a negative value - this is the one and only case
128
- * where we replace more than a single token.
129
- *
130
- * Do not compensate for the core system's special-case
131
- * adjustment of location to that of the leading '-'
132
- * operator in the event of a negative constant. It is
133
- * also useful for our purposes to start from the minus
134
- * symbol. In this way, queries like "select * from foo
135
- * where bar = 1" and "select * from foo where bar = -2"
136
- * will have identical normalized query strings.
137
- */
138
- tok = core_yylex(&yylval, &yylloc, yyscanner);
139
- if (tok == 0)
140
- break; /* out of inner for-loop */
141
- }
142
-
143
- /*
144
- * We now rely on the assumption that flex has placed a zero
145
- * byte after the text of the current token in scanbuf.
146
- */
147
- locs[i].length = (int) strlen(yyextra.scanbuf + loc);
148
-
149
- /* Quoted string with Unicode escapes
150
- *
151
- * The lexer consumes trailing whitespace in order to find UESCAPE, but if there
152
- * is no UESCAPE it has still consumed it - don't include it in constant length.
153
- */
154
- if (locs[i].length > 4 && /* U&'' */
155
- (yyextra.scanbuf[loc] == 'u' || yyextra.scanbuf[loc] == 'U') &&
156
- yyextra.scanbuf[loc + 1] == '&' && yyextra.scanbuf[loc + 2] == '\'')
157
- {
158
- int j = locs[i].length - 1; /* Skip the \0 */
159
- for (; j >= 0 && scanner_isspace(yyextra.scanbuf[loc + j]); j--) {}
160
- locs[i].length = j + 1; /* Count the \0 */
161
- }
162
-
163
- break; /* out of inner for-loop */
164
- }
165
- }
166
-
167
- /* If we hit end-of-string, give up, leaving remaining lengths -1 */
168
- if (tok == 0)
169
- break;
170
-
171
- last_loc = loc;
172
- }
173
-
174
- scanner_finish(yyscanner);
175
- }
176
-
177
- /*
178
- * Generate a normalized version of the query string that will be used to
179
- * represent all similar queries.
180
- *
181
- * Note that the normalized representation may well vary depending on
182
- * just which "equivalent" query is used to create the hashtable entry.
183
- * We assume this is OK.
184
- *
185
- * *query_len_p contains the input string length, and is updated with
186
- * the result string length (which cannot be longer) on exit.
187
- *
188
- * Returns a palloc'd string.
189
- */
190
- static char *
191
- generate_normalized_query(pgssConstLocations *jstate, const char *query,
192
- int *query_len_p, int encoding)
193
- {
194
- char *norm_query;
195
- int query_len = *query_len_p;
196
- int i,
197
- len_to_wrt, /* Length (in bytes) to write */
198
- quer_loc = 0, /* Source query byte location */
199
- n_quer_loc = 0, /* Normalized query byte location */
200
- last_off = 0, /* Offset from start for previous tok */
201
- last_tok_len = 0; /* Length (in bytes) of that tok */
202
-
203
- /*
204
- * Get constants' lengths (core system only gives us locations). Note
205
- * this also ensures the items are sorted by location.
206
- */
207
- fill_in_constant_lengths(jstate, query);
208
-
209
- /* Allocate result buffer */
210
- norm_query = palloc(query_len + 1);
211
-
212
- for (i = 0; i < jstate->clocations_count; i++)
213
- {
214
- int off, /* Offset from start for cur tok */
215
- tok_len; /* Length (in bytes) of that tok */
216
-
217
- off = jstate->clocations[i].location;
218
- tok_len = jstate->clocations[i].length;
219
-
220
- if (tok_len < 0)
221
- continue; /* ignore any duplicates */
222
-
223
- /* Copy next chunk (what precedes the next constant) */
224
- len_to_wrt = off - last_off;
225
- len_to_wrt -= last_tok_len;
226
-
227
- Assert(len_to_wrt >= 0);
228
- memcpy(norm_query + n_quer_loc, query + quer_loc, len_to_wrt);
229
- n_quer_loc += len_to_wrt;
230
-
231
- /* And insert a '?' in place of the constant token */
232
- norm_query[n_quer_loc++] = '?';
233
-
234
- quer_loc = off + tok_len;
235
- last_off = off;
236
- last_tok_len = tok_len;
237
- }
238
-
239
- /*
240
- * We've copied up until the last ignorable constant. Copy over the
241
- * remaining bytes of the original query string.
242
- */
243
- len_to_wrt = query_len - quer_loc;
244
-
245
- Assert(len_to_wrt >= 0);
246
- memcpy(norm_query + n_quer_loc, query + quer_loc, len_to_wrt);
247
- n_quer_loc += len_to_wrt;
248
-
249
- Assert(n_quer_loc <= query_len);
250
- norm_query[n_quer_loc] = '\0';
251
-
252
- *query_len_p = n_quer_loc;
253
- return norm_query;
254
- }
255
-
256
- static bool const_record_walker(Node *node, pgssConstLocations *jstate)
257
- {
258
- bool result;
259
-
260
- if (node == NULL) return false;
261
-
262
- if (IsA(node, A_Const) && ((A_Const *) node)->location >= 0)
263
- {
264
- /* enlarge array if needed */
265
- if (jstate->clocations_count >= jstate->clocations_buf_size)
266
- {
267
- jstate->clocations_buf_size *= 2;
268
- jstate->clocations = (pgssLocationLen *)
269
- repalloc(jstate->clocations,
270
- jstate->clocations_buf_size *
271
- sizeof(pgssLocationLen));
272
- }
273
- jstate->clocations[jstate->clocations_count].location = ((A_Const *) node)->location;
274
- /* initialize lengths to -1 to simplify fill_in_constant_lengths */
275
- jstate->clocations[jstate->clocations_count].length = -1;
276
- jstate->clocations_count++;
277
- }
278
- else if (IsA(node, VariableSetStmt))
279
- {
280
- return const_record_walker((Node *) ((VariableSetStmt *) node)->args, jstate);
281
- }
282
- else if (IsA(node, CopyStmt))
283
- {
284
- return const_record_walker((Node *) ((CopyStmt *) node)->query, jstate);
285
- }
286
- else if (IsA(node, ExplainStmt))
287
- {
288
- return const_record_walker((Node *) ((ExplainStmt *) node)->query, jstate);
289
- }
290
-
291
- PG_TRY();
292
- {
293
- result = raw_expression_tree_walker(node, const_record_walker, (void*) jstate);
294
- }
295
- PG_CATCH();
296
- {
297
- FlushErrorState();
298
- result = false;
299
- }
300
- PG_END_TRY();
301
-
302
- return result;
303
- }
304
-
305
- VALUE pg_query_normalize(VALUE self, VALUE input)
306
- {
307
- Check_Type(input, T_STRING);
308
-
309
- MemoryContext ctx = NULL;
310
- VALUE result = Qnil;
311
- VALUE error = Qnil;
312
-
313
- ctx = AllocSetContextCreate(TopMemoryContext,
314
- "pg_query_normalize",
315
- ALLOCSET_DEFAULT_MINSIZE,
316
- ALLOCSET_DEFAULT_INITSIZE,
317
- ALLOCSET_DEFAULT_MAXSIZE);
318
- MemoryContextSwitchTo(ctx);
319
-
320
- PG_TRY();
321
- {
322
- List *tree;
323
- char *str;
324
- pgssConstLocations jstate;
325
- int query_len;
326
-
327
- /* Parse query */
328
- str = StringValueCStr(input);
329
- tree = raw_parser(str);
330
-
331
- /* Set up workspace for constant recording */
332
- jstate.clocations_buf_size = 32;
333
- jstate.clocations = (pgssLocationLen *)
334
- palloc(jstate.clocations_buf_size * sizeof(pgssLocationLen));
335
- jstate.clocations_count = 0;
336
-
337
- /* Walk tree and record const locations */
338
- const_record_walker((Node *) tree, &jstate);
339
-
340
- /* Normalize query */
341
- query_len = (int) strlen(str);
342
- str = generate_normalized_query(&jstate, str, &query_len, PG_UTF8);
343
-
344
- result = rb_str_new2(str);
345
-
346
- pfree(str);
347
- }
348
- PG_CATCH();
349
- {
350
- ErrorData* error_data = CopyErrorData();
351
- error = new_parse_error(error_data);
352
- FlushErrorState();
353
- }
354
- PG_END_TRY();
355
-
356
- MemoryContextSwitchTo(TopMemoryContext);
357
- MemoryContextDelete(ctx);
358
-
359
- // If we got an error, throw it
360
- if (!NIL_P(error)) rb_exc_raise(error);
361
-
362
- return result;
363
- }
@@ -1,108 +0,0 @@
1
- #include "pg_query.h"
2
-
3
- #include "parser/parser.h"
4
- #include "parser/scanner.h"
5
- #include "parser/scansup.h"
6
-
7
- #include <unistd.h>
8
- #include <fcntl.h>
9
-
10
- VALUE new_parse_error(ErrorData* error)
11
- {
12
- VALUE cPgQuery, cParseError;
13
- VALUE args[4];
14
-
15
- cPgQuery = rb_const_get(rb_cObject, rb_intern("PgQuery"));
16
- cParseError = rb_const_get_at(cPgQuery, rb_intern("ParseError"));
17
-
18
- // exception message
19
- args[0] = rb_str_new2(error->message);
20
- // source of exception (e.g. parse.l)
21
- args[1] = rb_str_new2(error->filename);
22
- // source of exception (e.g. 104)
23
- args[2] = INT2NUM(error->lineno);
24
- // char in query at which exception occurred
25
- args[3] = INT2NUM(error->cursorpos);
26
-
27
- return rb_class_new_instance(4, args, cParseError);
28
- }
29
-
30
- VALUE pg_query_raw_parse(VALUE self, VALUE input)
31
- {
32
- Check_Type(input, T_STRING);
33
-
34
- MemoryContext ctx = NULL;
35
- VALUE result = Qnil;
36
- VALUE error = Qnil;
37
- char stderr_buffer[STDERR_BUFFER_LEN + 1] = {0};
38
- #ifndef DEBUG
39
- int stderr_global;
40
- int stderr_pipe[2];
41
- #endif
42
-
43
- ctx = AllocSetContextCreate(TopMemoryContext,
44
- "pg_query_raw_parse",
45
- ALLOCSET_DEFAULT_MINSIZE,
46
- ALLOCSET_DEFAULT_INITSIZE,
47
- ALLOCSET_DEFAULT_MAXSIZE);
48
- MemoryContextSwitchTo(ctx);
49
-
50
- #ifndef DEBUG
51
- // Setup pipe for stderr redirection
52
- if (pipe(stderr_pipe) != 0)
53
- rb_raise(rb_eIOError, "Failed to open pipe, too many open file descriptors");
54
-
55
- fcntl(stderr_pipe[0], F_SETFL, fcntl(stderr_pipe[0], F_GETFL) | O_NONBLOCK);
56
-
57
- // Redirect stderr to the pipe
58
- stderr_global = dup(STDERR_FILENO);
59
- dup2(stderr_pipe[1], STDERR_FILENO);
60
- close(stderr_pipe[1]);
61
- #endif
62
-
63
- // Parse it!
64
- PG_TRY();
65
- {
66
- List *tree;
67
- char *str;
68
-
69
- str = StringValueCStr(input);
70
- tree = raw_parser(str);
71
-
72
- str = nodeToJSONString(tree);
73
-
74
- #ifndef DEBUG
75
- // Save stderr for result
76
- read(stderr_pipe[0], stderr_buffer, STDERR_BUFFER_LEN);
77
- #endif
78
-
79
- result = rb_ary_new();
80
- rb_ary_push(result, rb_str_new2(str));
81
- rb_ary_push(result, rb_str_new2(stderr_buffer));
82
-
83
- pfree(str);
84
- }
85
- PG_CATCH();
86
- {
87
- ErrorData* error_data = CopyErrorData();
88
- error = new_parse_error(error_data);
89
- FlushErrorState();
90
- }
91
- PG_END_TRY();
92
-
93
- #ifndef DEBUG
94
- // Restore stderr, close pipe
95
- dup2(stderr_global, STDERR_FILENO);
96
- close(stderr_pipe[0]);
97
- close(stderr_global);
98
- #endif
99
-
100
- // Return to previous PostgreSQL memory context
101
- MemoryContextSwitchTo(TopMemoryContext);
102
- MemoryContextDelete(ctx);
103
-
104
- // If we got an error, throw it
105
- if (!NIL_P(error)) rb_exc_raise(error);
106
-
107
- return result;
108
- }