-
Notifications
You must be signed in to change notification settings - Fork 6
/
plphp_spi.c
541 lines (453 loc) · 13.4 KB
/
plphp_spi.c
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/**********************************************************************
* plphp_spi.c - SPI-related functions for PL/php.
*
* This software is copyright (c) Command Prompt Inc.
*
* The author hereby grants permission to use, copy, modify,
* distribute, and license this software and its documentation for any
* purpose, provided that existing copyright notices are retained in
* all copies and that this notice is included verbatim in any
* distributions. No written agreement, license, or royalty fee is
* required for any of the authorized uses. Modifications to this
* software may be copyrighted by their author and need not follow the
* licensing terms described here, provided that the new terms are
* clearly indicated on the first page of each file where they apply.
*
* IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY PARTY
* FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
* DERIVATIVES THEREOF, EVEN IF THE AUTHOR HAVE BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* THE AUTHOR AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
* NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
* AND THE AUTHOR AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
* MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* IDENTIFICATION
* $Id$
*********************************************************************
*/
#include "postgres.h"
#include "plphp_spi.h"
#include "plphp_io.h"
/* PHP stuff */
#include "php.h"
/* PostgreSQL stuff */
#include "access/xact.h"
#include "miscadmin.h"
#undef DEBUG_PLPHP_MEMORY
#ifdef DEBUG_PLPHP_MEMORY
#define REPORT_PHP_MEMUSAGE(where) \
elog(NOTICE, "PHP mem usage: «%s»: %u", where, AG(allocated_memory));
#else
#define REPORT_PHP_MEMUSAGE(a)
#endif
/* resource type Id for SPIresult */
int SPIres_rtype;
/* SPI function table */
zend_function_entry spi_functions[] =
{
ZEND_FE(spi_exec, NULL)
ZEND_FE(spi_fetch_row, NULL)
ZEND_FE(spi_processed, NULL)
ZEND_FE(spi_status, NULL)
ZEND_FE(spi_rewind, NULL)
ZEND_FE(pg_raise, NULL)
ZEND_FE(return_next, NULL)
{NULL, NULL, NULL}
};
/* SRF support: */
FunctionCallInfo current_fcinfo = NULL;
TupleDesc current_tupledesc = NULL;
AttInMetadata *current_attinmeta = NULL;
MemoryContext current_memcxt = NULL;
Tuplestorestate *current_tuplestore = NULL;
/* A symbol table to save for return_next for the RETURNS TABLE case */
HashTable *saved_symbol_table;
static zval *get_table_arguments(AttInMetadata *attinmeta);
/*
* spi_exec
* PL/php equivalent to SPI_exec().
*
* This function creates and return a PHP resource which describes the result
* of a user-specified query. If the query returns tuples, it's possible to
* retrieve them by using spi_fetch_row.
*
* Receives one or two arguments. The mandatory first argument is the query
* text. The optional second argument is the tuple limit.
*
* Note that just like PL/Perl, we start a subtransaction before invoking the
* SPI call, and automatically roll it back if the call fails.
*/
ZEND_FUNCTION(spi_exec)
{
char *query;
int query_len;
long status;
long limit;
php_SPIresult *SPIres;
int spi_id;
MemoryContext oldcontext = CurrentMemoryContext;
ResourceOwner oldowner = CurrentResourceOwner;
REPORT_PHP_MEMUSAGE("spi_exec called");
if ((ZEND_NUM_ARGS() > 2) || (ZEND_NUM_ARGS() < 1))
WRONG_PARAM_COUNT;
/* Parse arguments */
if (ZEND_NUM_ARGS() == 2)
{
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl",
&query, &query_len, &limit) == FAILURE)
{
zend_error(E_WARNING, "Can not parse parameters in %s",
get_active_function_name(TSRMLS_C));
RETURN_FALSE;
}
}
else if (ZEND_NUM_ARGS() == 1)
{
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
&query, &query_len) == FAILURE)
{
zend_error(E_WARNING, "Can not parse parameters in %s",
get_active_function_name(TSRMLS_C));
RETURN_FALSE;
}
limit = 0;
}
else
{
zend_error(E_WARNING, "Incorrect number of parameters to %s",
get_active_function_name(TSRMLS_C));
RETURN_FALSE;
}
BeginInternalSubTransaction(NULL);
MemoryContextSwitchTo(oldcontext);
/* Call SPI */
PG_TRY();
{
status = SPI_exec(query, limit);
ReleaseCurrentSubTransaction();
MemoryContextSwitchTo(oldcontext);
CurrentResourceOwner = oldowner;
/*
* AtEOSubXact_SPI() should not have popped any SPI context, but just
* in case it did, make sure we remain connected.
*/
SPI_restore_connection();
}
PG_CATCH();
{
ErrorData *edata;
/* Save error info */
MemoryContextSwitchTo(oldcontext);
edata = CopyErrorData();
FlushErrorState();
/* Abort the inner trasaction */
RollbackAndReleaseCurrentSubTransaction();
MemoryContextSwitchTo(oldcontext);
CurrentResourceOwner = oldowner;
/*
* If AtEOSubXact_SPI() popped any SPI context of the subxact, it will
* have left us in a disconnected state. We need this hack to return
* to connected state.
*/
SPI_restore_connection();
/* bail PHP out */
zend_error(E_ERROR, "%s", strdup(edata->message));
/* Can't get here, but keep compiler quiet */
return;
}
PG_END_TRY();
/* This malloc'ed chunk is freed in php_SPIresult_destroy */
SPIres = (php_SPIresult *) malloc(sizeof(php_SPIresult));
if (!SPIres)
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
/* Prepare the return resource */
SPIres->SPI_processed = SPI_processed;
if (status == SPI_OK_SELECT)
SPIres->SPI_tuptable = SPI_tuptable;
else
SPIres->SPI_tuptable = NULL;
SPIres->current_row = 0;
SPIres->status = status;
REPORT_PHP_MEMUSAGE("spi_exec: creating resource");
/* Register the resource to PHP so it will be able to free it */
spi_id = ZEND_REGISTER_RESOURCE(return_value, (void *) SPIres,
SPIres_rtype);
REPORT_PHP_MEMUSAGE("spi_exec: returning");
RETURN_RESOURCE(spi_id);
}
/*
* spi_fetch_row
* Grab a row from a SPI result (from spi_exec).
*
* This function receives a resource Id and returns a PHP hash representing the
* next tuple in the result, or false if no tuples remain.
*
* XXX Apparently this is leaking memory. How do we tell PHP to free the tuple
* once the user is done with it?
*/
ZEND_FUNCTION(spi_fetch_row)
{
zval *row = NULL;
zval **z_spi = NULL;
php_SPIresult *SPIres;
REPORT_PHP_MEMUSAGE("spi_fetch_row: called");
if (ZEND_NUM_ARGS() != 1)
WRONG_PARAM_COUNT;
if (zend_get_parameters_ex(1, &z_spi) == FAILURE)
{
zend_error(E_WARNING, "Can not parse parameters in %s",
get_active_function_name(TSRMLS_C));
RETURN_FALSE;
}
if (z_spi == NULL)
{
zend_error(E_WARNING, "Could not get SPI resource in %s",
get_active_function_name(TSRMLS_C));
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(SPIres, php_SPIresult *, z_spi, -1, "SPI result",
SPIres_rtype);
if (SPIres->status != SPI_OK_SELECT)
{
zend_error(E_WARNING, "SPI status is not good");
RETURN_FALSE;
}
if (SPIres->current_row < SPIres->SPI_processed)
{
row = plphp_zval_from_tuple(SPIres->SPI_tuptable->vals[SPIres->current_row],
SPIres->SPI_tuptable->tupdesc);
SPIres->current_row++;
*return_value = *row;
zval_copy_ctor(return_value);
zval_dtor(row);
FREE_ZVAL(row);
}
else
RETURN_FALSE;
REPORT_PHP_MEMUSAGE("spi_fetch_row: finish");
}
/*
* spi_processed
* Return the number of tuples returned in a spi_exec call.
*/
ZEND_FUNCTION(spi_processed)
{
zval **z_spi = NULL;
php_SPIresult *SPIres;
REPORT_PHP_MEMUSAGE("spi_processed: start");
if (ZEND_NUM_ARGS() != 1)
WRONG_PARAM_COUNT;
if (zend_get_parameters_ex(1, &z_spi) == FAILURE)
{
zend_error(E_WARNING, "Cannot parse parameters in %s",
get_active_function_name(TSRMLS_C));
RETURN_FALSE;
}
if (z_spi == NULL)
{
zend_error(E_WARNING, "Could not get SPI resource in %s",
get_active_function_name(TSRMLS_C));
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(SPIres, php_SPIresult *, z_spi, -1, "SPI result",
SPIres_rtype);
REPORT_PHP_MEMUSAGE("spi_processed: finish");
RETURN_LONG(SPIres->SPI_processed);
}
/*
* spi_status
* Return the status returned by a previous spi_exec call, as a string.
*/
ZEND_FUNCTION(spi_status)
{
zval **z_spi = NULL;
php_SPIresult *SPIres;
REPORT_PHP_MEMUSAGE("spi_status: start");
if (ZEND_NUM_ARGS() != 1)
WRONG_PARAM_COUNT;
if (zend_get_parameters_ex(1, &z_spi) == FAILURE)
{
zend_error(E_WARNING, "Cannot parse parameters in %s",
get_active_function_name(TSRMLS_C));
RETURN_FALSE;
}
if (z_spi == NULL)
{
zend_error(E_WARNING, "Could not get SPI resource in %s",
get_active_function_name(TSRMLS_C));
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(SPIres, php_SPIresult *, z_spi, -1, "SPI result",
SPIres_rtype);
REPORT_PHP_MEMUSAGE("spi_status: finish");
/*
* XXX The cast is wrong, but we use it to prevent a compiler warning.
* Note that the second parameter to RETURN_STRING is "duplicate", so
* we are returning a copy of the string anyway.
*/
RETURN_STRING((char *) SPI_result_code_string(SPIres->status), true);
}
/*
* spi_rewind
* Resets the internal counter for spi_fetch_row, so the next
* spi_fetch_row call will start fetching from the beginning.
*/
ZEND_FUNCTION(spi_rewind)
{
zval **z_spi = NULL;
php_SPIresult *SPIres;
if (ZEND_NUM_ARGS() != 1)
WRONG_PARAM_COUNT;
if (zend_get_parameters_ex(1, &z_spi) == FAILURE)
{
zend_error(E_WARNING, "Cannot parse parameters in %s",
get_active_function_name(TSRMLS_C));
RETURN_FALSE;
}
if (z_spi == NULL)
{
zend_error(E_WARNING, "Could not get SPI resource in %s",
get_active_function_name(TSRMLS_C));
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(SPIres, php_SPIresult *, z_spi, -1, "SPI result",
SPIres_rtype);
SPIres->current_row = 0;
RETURN_NULL();
}
/*
* pg_raise
* User-callable function for sending messages to the Postgres log.
*/
ZEND_FUNCTION(pg_raise)
{
char *level = NULL,
*message = NULL;
int level_len,
message_len,
elevel = 0;
if (ZEND_NUM_ARGS() != 2)
{
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("wrong number of arguments to %s", "pg_raise")));
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss",
&level, &level_len,
&message, &message_len) == FAILURE)
{
zend_error(E_WARNING, "cannot parse parameters in %s",
get_active_function_name(TSRMLS_C));
}
if (strcasecmp(level, "ERROR") == 0)
elevel = E_ERROR;
else if (strcasecmp(level, "WARNING") == 0)
elevel = E_WARNING;
else if (strcasecmp(level, "NOTICE") == 0)
elevel = E_NOTICE;
else
zend_error(E_ERROR, "incorrect log level");
zend_error(elevel, "%s", message);
}
/*
* return_next
* Add a tuple to the current tuplestore
*/
ZEND_FUNCTION(return_next)
{
MemoryContext oldcxt;
zval *param;
HeapTuple tup;
ReturnSetInfo *rsi;
/*
* Disallow use of return_next inside non-SRF functions
*/
if (current_fcinfo == NULL || current_fcinfo->flinfo == NULL ||
!current_fcinfo->flinfo->fn_retset)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot use return_next in functions not declared to "
"return a set")));
rsi = (ReturnSetInfo *) current_fcinfo->resultinfo;
Assert(current_tupledesc != NULL);
Assert(rsi != NULL);
if (ZEND_NUM_ARGS() > 1)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("wrong number of arguments to %s", "return_next")));
if (ZEND_NUM_ARGS() == 0)
{
/*
* Called from the function declared with RETURNS TABLE
*/
param = get_table_arguments(current_attinmeta);
}
else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z",
¶m) == FAILURE)
{
zend_error(E_WARNING, "cannot parse parameters in %s",
get_active_function_name(TSRMLS_C));
}
/* Use the per-query context so that the tuplestore survives */
oldcxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
/* Form the tuple */
tup = plphp_srf_htup_from_zval(param, current_attinmeta, current_memcxt);
/* First call? Create the tuplestore. */
if (!current_tuplestore)
current_tuplestore = tuplestore_begin_heap(true, false, work_mem);
/* Save the tuple and clean up */
tuplestore_puttuple(current_tuplestore, tup);
heap_freetuple(tup);
MemoryContextSwitchTo(oldcxt);
}
/*
* php_SPIresult_destroy
* Free the resources allocated by a spi_exec call.
*
* This is automatically called when the resource goes out of scope
* or is overwritten by another resource.
*/
void
php_SPIresult_destroy(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
php_SPIresult *res = (php_SPIresult *) rsrc->ptr;
if (res->SPI_tuptable != NULL)
SPI_freetuptable(res->SPI_tuptable);
free(res);
}
/* Return an array of TABLE argument values for return_next */
static
zval *get_table_arguments(AttInMetadata *attinmeta)
{
zval *retval = NULL;
int i;
MAKE_STD_ZVAL(retval);
array_init(retval);
Assert(attinmeta->tupdesc);
Assert(saved_symbol_table != NULL);
/* Extract OUT argument names */
for (i = 0; i < attinmeta->tupdesc->natts; i++)
{
zval **val;
char *attname;
Assert(!attinmeta->tupdesc->attrs[i]->attisdropped);
attname = NameStr(attinmeta->tupdesc->attrs[i]->attname);
if (zend_hash_find(saved_symbol_table,
attname, strlen(attname) + 1,
(void **)&val) == SUCCESS)
add_next_index_zval(retval, *val);
else
add_next_index_unset(retval);
}
return retval;
}
/*
* vim:ts=4:sw=4:cino=(0
*/