half way through the exception refactoring
[m6w6/ext-pq] / src / php_pq.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: pq |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #define SMART_STR_PREALLOC 256
18
19 #include <php.h>
20 #include <Zend/zend_interfaces.h>
21 #include <Zend/zend_exceptions.h>
22 #include <ext/standard/info.h>
23 #include <ext/standard/php_smart_str.h>
24 #include <ext/spl/spl_array.h>
25 #include <ext/spl/spl_exceptions.h>
26 #include <ext/raphf/php_raphf.h>
27
28 #include <libpq-events.h>
29 #include <libpq/libpq-fs.h>
30 #include <fnmatch.h>
31
32 #include "php_pq.h"
33
34 typedef int STATUS; /* SUCCESS/FAILURE */
35
36 static char *rtrim(char *e) {
37 size_t l = strlen(e);
38
39 while (l-- > 0 && e[l] == '\n') {
40 e[l] = '\0';
41 }
42 return e;
43 }
44
45 #define PHP_PQerrorMessage(c) rtrim(PQerrorMessage((c)))
46 #define PHP_PQresultErrorMessage(r) rtrim(PQresultErrorMessage((r)))
47
48 static int php_pqconn_event(PGEventId id, void *e, void *data);
49
50 #define PHP_PQclear(_r) \
51 do { \
52 zval *_resinszv = PQresultInstanceData((_r), php_pqconn_event); \
53 if (!_resinszv) PQclear((_r)); \
54 } while (0)
55
56 /*
57 ZEND_DECLARE_MODULE_GLOBALS(pq)
58 */
59
60 /* {{{ PHP_INI
61 */
62 /* Remove comments and fill if you need to have entries in php.ini
63 PHP_INI_BEGIN()
64 STD_PHP_INI_ENTRY("pq.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_pq_globals, pq_globals)
65 STD_PHP_INI_ENTRY("pq.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_pq_globals, pq_globals)
66 PHP_INI_END()
67 */
68 /* }}} */
69
70 /* {{{ php_pq_init_globals
71 */
72 /* Uncomment this function if you have INI entries
73 static void php_pq_init_globals(zend_pq_globals *pq_globals)
74 {
75 pq_globals->global_value = 0;
76 pq_globals->global_string = NULL;
77 }
78 */
79 /* }}} */
80
81 static zend_class_entry *php_pqconn_class_entry;
82 static zend_class_entry *php_pqtypes_class_entry;
83 static zend_class_entry *php_pqres_class_entry;
84 static zend_class_entry *php_pqstm_class_entry;
85 static zend_class_entry *php_pqtxn_class_entry;
86 static zend_class_entry *php_pqcancel_class_entry;
87 static zend_class_entry *php_pqevent_class_entry;
88 static zend_class_entry *php_pqlob_class_entry;
89 static zend_class_entry *php_pqcopy_class_entry;
90
91 typedef enum php_pqexc_type {
92 EX_DEFAULT,
93 EX_INVALID_ARGUMENT,
94 EX_RUNTIME,
95 EX_CONNECTION_FAILED,
96 EX_BAD_METHODCALL,
97 } php_pqexc_type_t;
98
99 static zend_class_entry *php_pqexc_interface_class_entry;
100 static zend_class_entry *php_pqexc_default_class_entry;
101 static zend_class_entry *php_pqexc_invalid_argument_class_entry;
102 static zend_class_entry *php_pqexc_runtime_class_entry;
103 static zend_class_entry *php_pqexc_bad_methodcall_class_entry;
104
105 static zend_class_entry *exce(php_pqexc_type_t type)
106 {
107 switch (type) {
108 case EX_DEFAULT:
109 default:
110 return php_pqexc_default_class_entry;
111 case EX_INVALID_ARGUMENT:
112 return php_pqexc_invalid_argument_class_entry;
113 case EX_RUNTIME:
114 case EX_CONNECTION_FAILED:
115 return php_pqexc_runtime_class_entry;
116 case EX_BAD_METHODCALL:
117 return php_pqexc_bad_methodcall_class_entry;
118 }
119 }
120
121 static zend_object_handlers php_pqconn_object_handlers;
122 static zend_object_handlers php_pqtypes_object_handlers;
123 static zend_object_handlers php_pqres_object_handlers;
124 static zend_object_handlers php_pqstm_object_handlers;
125 static zend_object_handlers php_pqtxn_object_handlers;
126 static zend_object_handlers php_pqcancel_object_handlers;
127 static zend_object_handlers php_pqevent_object_handlers;
128 static zend_object_handlers php_pqlob_object_handlers;
129 static zend_object_handlers php_pqcopy_object_handlers;
130
131 typedef struct php_pq_callback {
132 zend_fcall_info fci;
133 zend_fcall_info_cache fcc;
134 void *data;
135 } php_pq_callback_t;
136
137 typedef struct php_pq_object {
138 zend_object zo;
139 zend_object_value zv;
140 HashTable *prophandler;
141 void *intern;
142 } php_pq_object_t;
143
144 #define PHP_PQCONN_ASYNC 0x01
145 #define PHP_PQCONN_PERSISTENT 0x02
146
147 typedef struct php_pqconn {
148 PGconn *conn;
149 int (*poller)(PGconn *);
150 php_resource_factory_t factory;
151 HashTable listeners;
152 HashTable eventhandlers;
153 php_pq_callback_t onevent;
154 unsigned unbuffered:1;
155 } php_pqconn_t;
156
157 typedef struct php_pqconn_object {
158 zend_object zo;
159 zend_object_value zv;
160 HashTable *prophandler;
161 php_pqconn_t *intern;
162 } php_pqconn_object_t;
163
164 typedef struct php_pqtypes {
165 HashTable types;
166 php_pqconn_object_t *conn;
167 } php_pqtypes_t;
168
169 typedef struct php_pqtypes_object {
170 zend_object zo;
171 zend_object_value zv;
172 HashTable *prophandler;
173 php_pqtypes_t *intern;
174 } php_pqtypes_object_t;
175
176 typedef struct php_pqconn_event_data {
177 php_pqconn_object_t *obj;
178 #ifdef ZTS
179 void ***ts;
180 #endif
181 } php_pqconn_event_data_t;
182
183 typedef enum php_pqres_fetch {
184 PHP_PQRES_FETCH_ARRAY,
185 PHP_PQRES_FETCH_ASSOC,
186 PHP_PQRES_FETCH_OBJECT
187 } php_pqres_fetch_t;
188
189 typedef struct php_pqres_iterator {
190 zend_object_iterator zi;
191 zval *current_val;
192 unsigned index;
193 php_pqres_fetch_t fetch_type;
194 } php_pqres_iterator_t;
195
196 typedef struct php_pqres {
197 PGresult *res;
198 php_pqres_iterator_t *iter;
199 HashTable bound;
200 } php_pqres_t;
201
202 typedef struct php_pqres_object {
203 zend_object zo;
204 zend_object_value zv;
205 HashTable *prophandler;
206 php_pqres_t *intern;
207 } php_pqres_object_t;
208
209 typedef struct php_pqstm {
210 php_pqconn_object_t *conn;
211 char *name;
212 HashTable bound;
213 } php_pqstm_t;
214
215 typedef struct php_pqstm_object {
216 zend_object zo;
217 zend_object_value zv;
218 HashTable *prophandler;
219 php_pqstm_t *intern;
220 } php_pqstm_object_t;
221
222 typedef enum php_pqtxn_isolation {
223 PHP_PQTXN_READ_COMMITTED,
224 PHP_PQTXN_REPEATABLE_READ,
225 PHP_PQTXN_SERIALIZABLE,
226 } php_pqtxn_isolation_t;
227
228 typedef struct php_pqtxn {
229 php_pqconn_object_t *conn;
230 php_pqtxn_isolation_t isolation;
231 unsigned savepoint;
232 unsigned open:1;
233 unsigned readonly:1;
234 unsigned deferrable:1;
235 } php_pqtxn_t;
236
237 typedef struct php_pqtxn_object {
238 zend_object zo;
239 zend_object_value zv;
240 HashTable *prophandler;
241 php_pqtxn_t *intern;
242 } php_pqtxn_object_t;
243
244 typedef struct php_pqcancel {
245 PGcancel *cancel;
246 php_pqconn_object_t *conn;
247 } php_pqcancel_t;
248
249 typedef struct php_pqcancel_object {
250 zend_object zo;
251 zend_object_value zv;
252 HashTable *prophandler;
253 php_pqcancel_t *intern;
254 } php_pqcancel_object_t;
255
256 typedef struct php_pqevent {
257 php_pq_callback_t cb;
258 php_pqconn_object_t *conn;
259 char *type;
260 } php_pqevent_t;
261
262 typedef struct php_pqevent_object {
263 zend_object zo;
264 zend_object_value zv;
265 HashTable *prophandler;
266 php_pqevent_t *intern;
267 } php_pqevent_object_t;
268
269 typedef struct php_pqlob {
270 int lofd;
271 Oid loid;
272 php_pqtxn_object_t *txn;
273 } php_pqlob_t;
274
275 typedef struct php_pqlob_object {
276 zend_object zo;
277 zend_object_value zv;
278 HashTable *prophandler;
279 php_pqlob_t *intern;
280 } php_pqlob_object_t;
281
282 typedef enum php_pqcopy_direction {
283 PHP_PQCOPY_FROM_STDIN,
284 PHP_PQCOPY_TO_STDOUT
285 } php_pqcopy_direction_t;
286
287 typedef enum php_pqcopy_status {
288 PHP_PQCOPY_FAIL,
289 PHP_PQCOPY_CONT,
290 PHP_PQCOPY_DONE
291 } php_pqcopy_status_t;
292
293 typedef struct php_pqcopy {
294 php_pqcopy_direction_t direction;
295 char *expression;
296 char *options;
297 php_pqconn_object_t *conn;
298 } php_pqcopy_t;
299
300 typedef struct php_pqcopy_object {
301 zend_object zo;
302 zend_object_value zv;
303 HashTable *prophandler;
304 php_pqcopy_t *intern;
305 } php_pqcopy_object_t;
306
307 static HashTable php_pqconn_object_prophandlers;
308 static HashTable php_pqtypes_object_prophandlers;
309 static HashTable php_pqres_object_prophandlers;
310 static HashTable php_pqstm_object_prophandlers;
311 static HashTable php_pqtxn_object_prophandlers;
312 static HashTable php_pqcancel_object_prophandlers;
313 static HashTable php_pqevent_object_prophandlers;
314 static HashTable php_pqlob_object_prophandlers;
315 static HashTable php_pqcopy_object_prophandlers;
316
317 typedef void (*php_pq_object_prophandler_func_t)(zval *object, void *o, zval *return_value TSRMLS_DC);
318
319 typedef struct php_pq_object_prophandler {
320 php_pq_object_prophandler_func_t read;
321 php_pq_object_prophandler_func_t write;
322 } php_pq_object_prophandler_t;
323
324 static zend_object_iterator_funcs php_pqres_iterator_funcs;
325
326 static zend_object_iterator *php_pqres_iterator_init(zend_class_entry *ce, zval *object, int by_ref TSRMLS_DC)
327 {
328 php_pqres_iterator_t *iter;
329 zval *prop, *zfetch_type;
330
331 iter = ecalloc(1, sizeof(*iter));
332 iter->zi.funcs = &php_pqres_iterator_funcs;
333 iter->zi.data = object;
334 Z_ADDREF_P(object);
335
336 zfetch_type = prop = zend_read_property(ce, object, ZEND_STRL("fetchType"), 0 TSRMLS_CC);
337 if (Z_TYPE_P(zfetch_type) != IS_LONG) {
338 convert_to_long_ex(&zfetch_type);
339 }
340 iter->fetch_type = Z_LVAL_P(zfetch_type);
341 if (zfetch_type != prop) {
342 zval_ptr_dtor(&zfetch_type);
343 }
344 if (Z_REFCOUNT_P(prop)) {
345 zval_ptr_dtor(&prop);
346 } else {
347 zval_dtor(prop);
348 FREE_ZVAL(prop);
349 }
350
351 return (zend_object_iterator *) iter;
352 }
353
354 static void php_pqres_iterator_dtor(zend_object_iterator *i TSRMLS_DC)
355 {
356 php_pqres_iterator_t *iter = (php_pqres_iterator_t *) i;
357
358 if (iter->current_val) {
359 zval_ptr_dtor(&iter->current_val);
360 iter->current_val = NULL;
361 }
362 zval_ptr_dtor((zval **) &iter->zi.data);
363 efree(iter);
364 }
365
366 static STATUS php_pqres_iterator_valid(zend_object_iterator *i TSRMLS_DC)
367 {
368 php_pqres_iterator_t *iter = (php_pqres_iterator_t *) i;
369 php_pqres_object_t *obj = zend_object_store_get_object(iter->zi.data TSRMLS_CC);
370
371 if (PQresultStatus(obj->intern->res) != PGRES_TUPLES_OK) {
372 return FAILURE;
373 }
374 if (PQntuples(obj->intern->res) <= iter->index) {
375 return FAILURE;
376 }
377
378 return SUCCESS;
379 }
380
381 static zval *php_pqres_row_to_zval(PGresult *res, unsigned row, php_pqres_fetch_t fetch_type, zval **data_ptr TSRMLS_DC)
382 {
383 zval *data = NULL;
384 int c, cols;
385
386 if (data_ptr) {
387 data = *data_ptr;
388 }
389 if (!data) {
390 MAKE_STD_ZVAL(data);
391 if (PHP_PQRES_FETCH_OBJECT == fetch_type) {
392 object_init(data);
393 } else {
394 array_init(data);
395 }
396 if (data_ptr) {
397 *data_ptr = data;
398 }
399 }
400
401 for (c = 0, cols = PQnfields(res); c < cols; ++c) {
402 if (PQgetisnull(res, row, c)) {
403 switch (fetch_type) {
404 case PHP_PQRES_FETCH_OBJECT:
405 add_property_null(data, PQfname(res, c));
406 break;
407
408 case PHP_PQRES_FETCH_ASSOC:
409 add_assoc_null(data, PQfname(res, c));
410 break;
411
412 case PHP_PQRES_FETCH_ARRAY:
413 add_index_null(data, c);
414 break;
415 }
416 } else {
417 char *val = PQgetvalue(res, row, c);
418 int len = PQgetlength(res, row, c);
419
420 switch (fetch_type) {
421 case PHP_PQRES_FETCH_OBJECT:
422 add_property_stringl(data, PQfname(res, c), val, len, 1);
423 break;
424
425 case PHP_PQRES_FETCH_ASSOC:
426 add_assoc_stringl(data, PQfname(res, c), val, len, 1);
427 break;
428
429 case PHP_PQRES_FETCH_ARRAY:
430 add_index_stringl(data, c, val, len ,1);
431 break;
432 }
433 }
434 }
435
436 return data;
437 }
438
439 static void php_pqres_iterator_current(zend_object_iterator *i, zval ***data_ptr TSRMLS_DC)
440 {
441 php_pqres_iterator_t *iter = (php_pqres_iterator_t *) i;
442 php_pqres_object_t *obj = zend_object_store_get_object(iter->zi.data TSRMLS_CC);
443
444 if (iter->current_val) {
445 zval_ptr_dtor(&iter->current_val);
446 }
447 iter->current_val = php_pqres_row_to_zval(obj->intern->res, iter->index, iter->fetch_type, NULL TSRMLS_CC);
448 *data_ptr = &iter->current_val;
449 }
450
451 static int php_pqres_iterator_key(zend_object_iterator *i, char **key_str, uint *key_len, ulong *key_num TSRMLS_DC)
452 {
453 php_pqres_iterator_t *iter = (php_pqres_iterator_t *) i;
454
455 *key_num = (ulong) iter->index;
456
457 return HASH_KEY_IS_LONG;
458 }
459
460 static void php_pqres_iterator_next(zend_object_iterator *i TSRMLS_DC)
461 {
462 php_pqres_iterator_t *iter = (php_pqres_iterator_t *) i;
463
464 ++iter->index;
465 }
466
467 static void php_pqres_iterator_rewind(zend_object_iterator *i TSRMLS_DC)
468 {
469 php_pqres_iterator_t *iter = (php_pqres_iterator_t *) i;
470
471 iter->index = 0;
472 }
473
474 static zend_object_iterator_funcs php_pqres_iterator_funcs = {
475 php_pqres_iterator_dtor,
476 /* check for end of iteration (FAILURE or SUCCESS if data is valid) */
477 php_pqres_iterator_valid,
478 /* fetch the item data for the current element */
479 php_pqres_iterator_current,
480 /* fetch the key for the current element (return HASH_KEY_IS_STRING or HASH_KEY_IS_LONG) (optional, may be NULL) */
481 php_pqres_iterator_key,
482 /* step forwards to next element */
483 php_pqres_iterator_next,
484 /* rewind to start of data (optional, may be NULL) */
485 php_pqres_iterator_rewind,
486 /* invalidate current value/key (optional, may be NULL) */
487 NULL
488 };
489
490 static int php_pqres_count_elements(zval *object, long *count TSRMLS_DC)
491 {
492 php_pqres_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
493
494 if (!obj->intern) {
495 return FAILURE;
496 } else {
497 *count = (long) PQntuples(obj->intern->res);
498 return SUCCESS;
499 }
500 }
501
502 static STATUS php_pqres_success(PGresult *res TSRMLS_DC)
503 {
504 switch (PQresultStatus(res)) {
505 case PGRES_BAD_RESPONSE:
506 case PGRES_NONFATAL_ERROR:
507 case PGRES_FATAL_ERROR:
508 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "%s", PHP_PQresultErrorMessage(res));
509 return FAILURE;
510 default:
511 return SUCCESS;
512 }
513 }
514
515 static void php_pq_callback_dtor(php_pq_callback_t *cb) {
516 if (cb->fci.size > 0) {
517 zend_fcall_info_args_clear(&cb->fci, 1);
518 zval_ptr_dtor(&cb->fci.function_name);
519 if (cb->fci.object_ptr) {
520 zval_ptr_dtor(&cb->fci.object_ptr);
521 }
522 }
523 cb->fci.size = 0;
524 }
525
526 static void php_pq_callback_addref(php_pq_callback_t *cb)
527 {
528 Z_ADDREF_P(cb->fci.function_name);
529 if (cb->fci.object_ptr) {
530 Z_ADDREF_P(cb->fci.object_ptr);
531 }
532 }
533
534 static void php_pq_object_to_zval(void *o, zval **zv TSRMLS_DC)
535 {
536 php_pq_object_t *obj = o;
537
538 if (!*zv) {
539 MAKE_STD_ZVAL(*zv);
540 }
541
542 zend_objects_store_add_ref_by_handle(obj->zv.handle TSRMLS_CC);
543
544 (*zv)->type = IS_OBJECT;
545 (*zv)->value.obj = obj->zv;
546 }
547
548 static void php_pq_object_addref(void *o TSRMLS_DC)
549 {
550 php_pq_object_t *obj = o;
551 zend_objects_store_add_ref_by_handle(obj->zv.handle TSRMLS_CC);
552 }
553
554 static void php_pq_object_delref(void *o TSRMLS_DC)
555 {
556 php_pq_object_t *obj = o;
557 zend_objects_store_del_ref_by_handle_ex(obj->zv.handle, obj->zv.handlers TSRMLS_CC);
558 }
559
560 static void php_pqconn_object_free(void *o TSRMLS_DC)
561 {
562 php_pqconn_object_t *obj = o;
563
564 if (obj->intern) {
565 php_resource_factory_handle_dtor(&obj->intern->factory, obj->intern->conn TSRMLS_CC);
566 php_resource_factory_dtor(&obj->intern->factory);
567 php_pq_callback_dtor(&obj->intern->onevent);
568 zend_hash_destroy(&obj->intern->listeners);
569 zend_hash_destroy(&obj->intern->eventhandlers);
570 efree(obj->intern);
571 obj->intern = NULL;
572 }
573 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
574 efree(obj);
575 }
576
577 static void php_pqtypes_object_free(void *o TSRMLS_DC)
578 {
579 php_pqtypes_object_t *obj = o;
580
581 if (obj->intern) {
582 zend_hash_destroy(&obj->intern->types);
583 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
584 efree(obj->intern);
585 obj->intern = NULL;
586 }
587 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
588 efree(obj);
589 }
590
591 static void php_pqres_object_free(void *o TSRMLS_DC)
592 {
593 php_pqres_object_t *obj = o;
594
595 if (obj->intern) {
596 if (obj->intern->res) {
597 zval *res = PQresultInstanceData(obj->intern->res, php_pqconn_event);
598 if (res) {
599 if (1 == Z_REFCOUNT_P(res)) {
600 PQresultSetInstanceData(obj->intern->res, php_pqconn_event, NULL);
601 }
602 zval_ptr_dtor(&res);
603 } else {
604 PQclear(obj->intern->res);
605 obj->intern->res = NULL;
606 }
607 }
608
609 if (obj->intern->iter) {
610 php_pqres_iterator_dtor((zend_object_iterator *) obj->intern->iter TSRMLS_CC);
611 obj->intern->iter = NULL;
612 }
613
614 zend_hash_destroy(&obj->intern->bound);
615
616 efree(obj->intern);
617 obj->intern = NULL;
618 }
619 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
620 efree(obj);
621 }
622
623 static void php_pqstm_object_free(void *o TSRMLS_DC)
624 {
625 php_pqstm_object_t *obj = o;
626
627 if (obj->intern) {
628 char *quoted_name = PQescapeIdentifier(obj->intern->conn->intern->conn, obj->intern->name, strlen(obj->intern->name));
629
630 php_pq_callback_dtor(&obj->intern->conn->intern->onevent);
631
632 if (quoted_name) {
633 PGresult *res;
634 smart_str cmd = {0};
635
636 smart_str_appends(&cmd, "DEALLOCATE ");
637 smart_str_appends(&cmd, quoted_name);
638 smart_str_0(&cmd);
639 PQfreemem(quoted_name);
640
641 if ((res = PQexec(obj->intern->conn->intern->conn, cmd.c))) {
642 PHP_PQclear(res);
643 }
644 smart_str_free(&cmd);
645 }
646
647 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
648 efree(obj->intern->name);
649 zend_hash_destroy(&obj->intern->bound);
650 efree(obj->intern);
651 obj->intern = NULL;
652 }
653 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
654 efree(obj);
655 }
656
657 static void php_pqtxn_object_free(void *o TSRMLS_DC)
658 {
659 php_pqtxn_object_t *obj = o;
660
661 if (obj->intern) {
662 if (obj->intern->open) {
663 PGresult *res = PQexec(obj->intern->conn->intern->conn, "ROLLBACK");
664
665 if (res) {
666 PHP_PQclear(res);
667 }
668 }
669 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
670 efree(obj->intern);
671 obj->intern = NULL;
672 }
673 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
674 efree(obj);
675 }
676
677 static void php_pqcancel_object_free(void *o TSRMLS_DC)
678 {
679 php_pqcancel_object_t *obj = o;
680
681 if (obj->intern) {
682 PQfreeCancel(obj->intern->cancel);
683 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
684 efree(obj->intern);
685 obj->intern = NULL;
686 }
687 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
688 efree(obj);
689 }
690
691 static void php_pqevent_object_free(void *o TSRMLS_DC)
692 {
693 php_pqevent_object_t *obj = o;
694
695 if (obj->intern) {
696 php_pq_callback_dtor(&obj->intern->cb);
697 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
698 efree(obj->intern->type);
699 efree(obj->intern);
700 obj->intern = NULL;
701 }
702 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
703 efree(obj);
704 }
705
706 static void php_pqlob_object_free(void *o TSRMLS_DC)
707 {
708 php_pqlob_object_t *obj = o;
709
710 if (obj->intern) {
711 if (obj->intern->lofd) {
712 lo_close(obj->intern->txn->intern->conn->intern->conn, obj->intern->lofd);
713 }
714 php_pq_object_delref(obj->intern->txn TSRMLS_CC);
715 efree(obj->intern);
716 obj->intern = NULL;
717 }
718 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
719 efree(obj);
720 }
721
722 static void php_pqcopy_object_free(void *o TSRMLS_DC)
723 {
724 php_pqcopy_object_t *obj = o;
725
726 if (obj->intern) {
727 efree(obj->intern->expression);
728 efree(obj->intern->options);
729 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
730 efree(obj->intern);
731 obj->intern = NULL;
732 }
733 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
734 efree(obj);
735 }
736
737 static zend_object_value php_pqconn_create_object_ex(zend_class_entry *ce, php_pqconn_t *intern, php_pqconn_object_t **ptr TSRMLS_DC)
738 {
739 php_pqconn_object_t *o;
740
741 o = ecalloc(1, sizeof(*o));
742 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
743 object_properties_init((zend_object *) o, ce);
744 o->prophandler = &php_pqconn_object_prophandlers;
745
746 if (ptr) {
747 *ptr = o;
748 }
749
750 if (intern) {
751 o->intern = intern;
752 }
753
754 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqconn_object_free, NULL TSRMLS_CC);
755 o->zv.handlers = &php_pqconn_object_handlers;
756
757 return o->zv;
758 }
759
760 static zend_object_value php_pqtypes_create_object_ex(zend_class_entry *ce, php_pqtypes_t *intern, php_pqtypes_object_t **ptr TSRMLS_DC)
761 {
762 php_pqtypes_object_t *o;
763
764 o = ecalloc(1, sizeof(*o));
765 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
766 object_properties_init((zend_object *) o, ce);
767 o->prophandler = &php_pqtypes_object_prophandlers;
768
769 if (ptr) {
770 *ptr = o;
771 }
772
773 if (intern) {
774 o->intern = intern;
775 }
776
777 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqtypes_object_free, NULL TSRMLS_CC);
778 o->zv.handlers = &php_pqtypes_object_handlers;
779
780 return o->zv;
781 }
782
783 static zend_object_value php_pqres_create_object_ex(zend_class_entry *ce, php_pqres_t *intern, php_pqres_object_t **ptr TSRMLS_DC)
784 {
785 php_pqres_object_t *o;
786
787 o = ecalloc(1, sizeof(*o));
788 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
789 object_properties_init((zend_object *) o, ce);
790 o->prophandler = &php_pqres_object_prophandlers;
791
792 if (ptr) {
793 *ptr = o;
794 }
795
796 if (intern) {
797 o->intern = intern;
798 }
799
800 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqres_object_free, NULL TSRMLS_CC);
801 o->zv.handlers = &php_pqres_object_handlers;
802
803 return o->zv;
804 }
805
806 static zend_object_value php_pqstm_create_object_ex(zend_class_entry *ce, php_pqstm_t *intern, php_pqstm_object_t **ptr TSRMLS_DC)
807 {
808 php_pqstm_object_t *o;
809
810 o = ecalloc(1, sizeof(*o));
811 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
812 object_properties_init((zend_object *) o, ce);
813 o->prophandler = &php_pqstm_object_prophandlers;
814
815 if (ptr) {
816 *ptr = o;
817 }
818
819 if (intern) {
820 o->intern = intern;
821 }
822
823 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqstm_object_free, NULL TSRMLS_CC);
824 o->zv.handlers = &php_pqstm_object_handlers;
825
826 return o->zv;
827 }
828
829 static zend_object_value php_pqtxn_create_object_ex(zend_class_entry *ce, php_pqtxn_t *intern, php_pqtxn_object_t **ptr TSRMLS_DC)
830 {
831 php_pqtxn_object_t *o;
832
833 o = ecalloc(1, sizeof(*o));
834 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
835 object_properties_init((zend_object *) o, ce);
836 o->prophandler = &php_pqtxn_object_prophandlers;
837
838 if (ptr) {
839 *ptr = o;
840 }
841
842 if (intern) {
843 o->intern = intern;
844 }
845
846 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqtxn_object_free, NULL TSRMLS_CC);
847 o->zv.handlers = &php_pqtxn_object_handlers;
848
849 return o->zv;
850 }
851
852 static zend_object_value php_pqcancel_create_object_ex(zend_class_entry *ce, php_pqcancel_t *intern, php_pqcancel_object_t **ptr TSRMLS_DC)
853 {
854 php_pqcancel_object_t *o;
855
856 o = ecalloc(1, sizeof(*o));
857 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
858 object_properties_init((zend_object *) o, ce);
859 o->prophandler = &php_pqcancel_object_prophandlers;
860
861 if (ptr) {
862 *ptr = o;
863 }
864
865 if (intern) {
866 o->intern = intern;
867 }
868
869 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqcancel_object_free, NULL TSRMLS_CC);
870 o->zv.handlers = &php_pqcancel_object_handlers;
871
872 return o->zv;
873 }
874
875 static zend_object_value php_pqevent_create_object_ex(zend_class_entry *ce, php_pqevent_t *intern, php_pqevent_object_t **ptr TSRMLS_DC)
876 {
877 php_pqevent_object_t *o;
878
879 o = ecalloc(1, sizeof(*o));
880 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
881 object_properties_init((zend_object *) o, ce);
882 o->prophandler = &php_pqevent_object_prophandlers;
883
884 if (ptr) {
885 *ptr = o;
886 }
887
888 if (intern) {
889 o->intern = intern;
890 }
891
892 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqevent_object_free, NULL TSRMLS_CC);
893 o->zv.handlers = &php_pqevent_object_handlers;
894
895 return o->zv;
896 }
897
898 static zend_object_value php_pqlob_create_object_ex(zend_class_entry *ce, php_pqlob_t *intern, php_pqlob_object_t **ptr TSRMLS_DC)
899 {
900 php_pqlob_object_t *o;
901
902 o = ecalloc(1, sizeof(*o));
903 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
904 object_properties_init((zend_object *) o, ce);
905 o->prophandler = &php_pqlob_object_prophandlers;
906
907 if (ptr) {
908 *ptr = o;
909 }
910
911 if (intern) {
912 o->intern = intern;
913 }
914
915 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqlob_object_free, NULL TSRMLS_CC);
916 o->zv.handlers = &php_pqlob_object_handlers;
917
918 return o->zv;
919 }
920
921 static zend_object_value php_pqcopy_create_object_ex(zend_class_entry *ce, php_pqcopy_t *intern, php_pqcopy_object_t **ptr TSRMLS_DC)
922 {
923 php_pqcopy_object_t *o;
924
925 o = ecalloc(1, sizeof(*o));
926 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
927 object_properties_init((zend_object *) o, ce);
928 o->prophandler = &php_pqcopy_object_prophandlers;
929
930 if (ptr) {
931 *ptr = o;
932 }
933
934 if (intern) {
935 o->intern = intern;
936 }
937
938 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqcopy_object_free, NULL TSRMLS_CC);
939 o->zv.handlers = &php_pqcopy_object_handlers;
940
941 return o->zv;
942 }
943
944 static zend_object_value php_pqconn_create_object(zend_class_entry *class_type TSRMLS_DC)
945 {
946 return php_pqconn_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
947 }
948
949 static zend_object_value php_pqtypes_create_object(zend_class_entry *class_type TSRMLS_DC)
950 {
951 return php_pqtypes_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
952 }
953
954 static zend_object_value php_pqres_create_object(zend_class_entry *class_type TSRMLS_DC)
955 {
956 return php_pqres_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
957 }
958
959 static zend_object_value php_pqstm_create_object(zend_class_entry *class_type TSRMLS_DC)
960 {
961 return php_pqstm_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
962 }
963
964 static zend_object_value php_pqtxn_create_object(zend_class_entry *class_type TSRMLS_DC)
965 {
966 return php_pqtxn_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
967 }
968
969 static zend_object_value php_pqcancel_create_object(zend_class_entry *class_type TSRMLS_DC)
970 {
971 return php_pqcancel_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
972 }
973
974 static zend_object_value php_pqevent_create_object(zend_class_entry *class_type TSRMLS_DC)
975 {
976 return php_pqevent_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
977 }
978
979 static zend_object_value php_pqlob_create_object(zend_class_entry *class_type TSRMLS_DC)
980 {
981 return php_pqlob_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
982 }
983
984 static zend_object_value php_pqcopy_create_object(zend_class_entry *class_type TSRMLS_DC)
985 {
986 return php_pqcopy_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
987 }
988
989 static int apply_ph_to_debug(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
990 {
991 php_pq_object_prophandler_t *ph = p;
992 HashTable *ht = va_arg(argv, HashTable *);
993 zval **return_value, *object = va_arg(argv, zval *);
994 php_pq_object_t *obj = va_arg(argv, php_pq_object_t *);
995
996 if (SUCCESS == zend_hash_find(ht, key->arKey, key->nKeyLength, (void *) &return_value)) {
997
998 if (ph->read) {
999 zval_ptr_dtor(return_value);
1000 MAKE_STD_ZVAL(*return_value);
1001 ZVAL_NULL(*return_value);
1002
1003 ph->read(object, obj, *return_value TSRMLS_CC);
1004 }
1005 }
1006
1007 return ZEND_HASH_APPLY_KEEP;
1008 }
1009
1010 static int apply_pi_to_debug(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
1011 {
1012 zend_property_info *pi = p;
1013 HashTable *ht = va_arg(argv, HashTable *);
1014 zval *object = va_arg(argv, zval *);
1015 php_pq_object_t *obj = va_arg(argv, php_pq_object_t *);
1016 zval *property = zend_read_property(obj->zo.ce, object, pi->name, pi->name_length, 0 TSRMLS_CC);
1017
1018 if (1||!Z_REFCOUNT_P(property)) {
1019 Z_ADDREF_P(property);
1020 }
1021 zend_hash_add(ht, pi->name, pi->name_length + 1, (void *) &property, sizeof(zval *), NULL);
1022
1023 return ZEND_HASH_APPLY_KEEP;
1024 }
1025
1026 static HashTable *php_pq_object_debug_info(zval *object, int *temp TSRMLS_DC)
1027 {
1028 HashTable *ht;
1029 php_pq_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
1030
1031 *temp = 1;
1032 ALLOC_HASHTABLE(ht);
1033 ZEND_INIT_SYMTABLE(ht);
1034
1035 zend_hash_apply_with_arguments(&obj->zo.ce->properties_info TSRMLS_CC, apply_pi_to_debug, 3, ht, object, obj);
1036 zend_hash_apply_with_arguments(obj->prophandler TSRMLS_CC, apply_ph_to_debug, 3, ht, object, obj);
1037
1038 return ht;
1039 }
1040
1041 static void php_pqconn_object_read_status(zval *object, void *o, zval *return_value TSRMLS_DC)
1042 {
1043 php_pqconn_object_t *obj = o;
1044
1045 RETVAL_LONG(PQstatus(obj->intern->conn));
1046 }
1047
1048 static void php_pqconn_object_read_transaction_status(zval *object, void *o, zval *return_value TSRMLS_DC)
1049 {
1050 php_pqconn_object_t *obj = o;
1051
1052 RETVAL_LONG(PQtransactionStatus(obj->intern->conn));
1053 }
1054
1055 static void php_pqconn_object_read_error_message(zval *object, void *o, zval *return_value TSRMLS_DC)
1056 {
1057 php_pqconn_object_t *obj = o;
1058 char *error = PHP_PQerrorMessage(obj->intern->conn);
1059
1060 if (error) {
1061 RETVAL_STRING(error, 1);
1062 } else {
1063 RETVAL_NULL();
1064 }
1065 }
1066
1067 static int apply_notify_listener(void *p, void *arg TSRMLS_DC)
1068 {
1069 php_pq_callback_t *listener = p;
1070 PGnotify *nfy = arg;
1071 zval *zpid, *zchannel, *zmessage;
1072
1073 MAKE_STD_ZVAL(zpid);
1074 ZVAL_LONG(zpid, nfy->be_pid);
1075 MAKE_STD_ZVAL(zchannel);
1076 ZVAL_STRING(zchannel, nfy->relname, 1);
1077 MAKE_STD_ZVAL(zmessage);
1078 ZVAL_STRING(zmessage, nfy->extra, 1);
1079
1080 zend_fcall_info_argn(&listener->fci TSRMLS_CC, 3, &zchannel, &zmessage, &zpid);
1081 zend_fcall_info_call(&listener->fci, &listener->fcc, NULL, NULL TSRMLS_CC);
1082
1083 zval_ptr_dtor(&zchannel);
1084 zval_ptr_dtor(&zmessage);
1085 zval_ptr_dtor(&zpid);
1086
1087 return ZEND_HASH_APPLY_KEEP;
1088 }
1089
1090 static int apply_notify_listeners(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
1091 {
1092 HashTable *listeners = p;
1093 PGnotify *nfy = va_arg(argv, PGnotify *);
1094
1095 if (0 == fnmatch(key->arKey, nfy->relname, 0)) {
1096 zend_hash_apply_with_argument(listeners, apply_notify_listener, nfy TSRMLS_CC);
1097 }
1098
1099 return ZEND_HASH_APPLY_KEEP;
1100 }
1101
1102 static void php_pqconn_notify_listeners(php_pqconn_object_t *obj TSRMLS_DC)
1103 {
1104 PGnotify *nfy;
1105
1106 while ((nfy = PQnotifies(obj->intern->conn))) {
1107 zend_hash_apply_with_arguments(&obj->intern->listeners TSRMLS_CC, apply_notify_listeners, 1, nfy);
1108 PQfreemem(nfy);
1109 }
1110 }
1111
1112 static void php_pqconn_object_read_busy(zval *object, void *o, zval *return_value TSRMLS_DC)
1113 {
1114 php_pqconn_object_t *obj = o;
1115
1116 RETVAL_BOOL(PQisBusy(obj->intern->conn));
1117 }
1118
1119 static void php_pqconn_object_read_encoding(zval *object, void *o, zval *return_value TSRMLS_DC)
1120 {
1121 php_pqconn_object_t *obj = o;
1122
1123 RETVAL_STRING(pg_encoding_to_char(PQclientEncoding(obj->intern->conn)), 1);
1124 }
1125
1126 static void php_pqconn_object_write_encoding(zval *object, void *o, zval *value TSRMLS_DC)
1127 {
1128 php_pqconn_object_t *obj = o;
1129 zval *zenc = value;
1130
1131 if (Z_TYPE_P(value) != IS_STRING) {
1132 convert_to_string_ex(&zenc);
1133 }
1134
1135 if (0 > PQsetClientEncoding(obj->intern->conn, Z_STRVAL_P(zenc))) {
1136 zend_error(E_NOTICE, "Unrecognized encoding '%s'", Z_STRVAL_P(zenc));
1137 }
1138
1139 if (zenc != value) {
1140 zval_ptr_dtor(&zenc);
1141 }
1142 }
1143
1144 static void php_pqconn_object_read_unbuffered(zval *object, void *o, zval *return_value TSRMLS_DC)
1145 {
1146 php_pqconn_object_t *obj = o;
1147
1148 RETVAL_BOOL(obj->intern->unbuffered);
1149 }
1150
1151 static void php_pqconn_object_write_unbuffered(zval *object, void *o, zval *value TSRMLS_DC)
1152 {
1153 php_pqconn_object_t *obj = o;
1154
1155 obj->intern->unbuffered = zend_is_true(value);
1156 }
1157
1158 static void php_pqconn_object_read_db(zval *objec, void *o, zval *return_value TSRMLS_DC)
1159 {
1160 php_pqconn_object_t *obj = o;
1161 char *db = PQdb(obj->intern->conn);
1162
1163 if (db) {
1164 RETVAL_STRING(db, 1);
1165 } else {
1166 RETVAL_EMPTY_STRING();
1167 }
1168 }
1169
1170 static void php_pqconn_object_read_user(zval *objec, void *o, zval *return_value TSRMLS_DC)
1171 {
1172 php_pqconn_object_t *obj = o;
1173 char *user = PQuser(obj->intern->conn);
1174
1175 if (user) {
1176 RETVAL_STRING(user, 1);
1177 } else {
1178 RETVAL_EMPTY_STRING();
1179 }
1180 }
1181
1182 static void php_pqconn_object_read_pass(zval *objec, void *o, zval *return_value TSRMLS_DC)
1183 {
1184 php_pqconn_object_t *obj = o;
1185 char *pass = PQpass(obj->intern->conn);
1186
1187 if (pass) {
1188 RETVAL_STRING(pass, 1);
1189 } else {
1190 RETVAL_EMPTY_STRING();
1191 }
1192 }
1193
1194 static void php_pqconn_object_read_host(zval *objec, void *o, zval *return_value TSRMLS_DC)
1195 {
1196 php_pqconn_object_t *obj = o;
1197 char *host = PQhost(obj->intern->conn);
1198
1199 if (host) {
1200 RETVAL_STRING(host, 1);
1201 } else {
1202 RETVAL_EMPTY_STRING();
1203 }
1204 }
1205
1206 static void php_pqconn_object_read_port(zval *objec, void *o, zval *return_value TSRMLS_DC)
1207 {
1208 php_pqconn_object_t *obj = o;
1209 char *port = PQport(obj->intern->conn);
1210
1211 if (port) {
1212 RETVAL_STRING(port, 1);
1213 } else {
1214 RETVAL_EMPTY_STRING();
1215 }
1216 }
1217
1218 static void php_pqconn_object_read_options(zval *objec, void *o, zval *return_value TSRMLS_DC)
1219 {
1220 php_pqconn_object_t *obj = o;
1221 char *options = PQoptions(obj->intern->conn);
1222
1223 if (options) {
1224 RETVAL_STRING(options, 1);
1225 } else {
1226 RETVAL_EMPTY_STRING();
1227 }
1228 }
1229
1230 static void php_pqtypes_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
1231 {
1232 php_pqtypes_object_t *obj = o;
1233
1234 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
1235 }
1236
1237 static int has_dimension(HashTable *ht, zval *member, char **key_str, int *key_len, long *index TSRMLS_DC)
1238 {
1239 long lval = 0;
1240 zval *tmp = member;
1241
1242 switch (Z_TYPE_P(member)) {
1243 default:
1244 convert_to_string_ex(&tmp);
1245 /* no break */
1246 case IS_STRING:
1247 if (!is_numeric_string(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp), &lval, NULL, 0)) {
1248 if (member != tmp) {
1249 zval_ptr_dtor(&tmp);
1250 }
1251 if (key_str) {
1252 *key_str = estrndup(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
1253 if (key_len) {
1254 *key_len = Z_STRLEN_P(tmp) + 1;
1255 }
1256 }
1257 return zend_hash_exists(ht, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp) + 1);
1258 }
1259 /* no break */
1260 case IS_LONG:
1261 lval = Z_LVAL_P(member);
1262 break;
1263 }
1264
1265 if (member != tmp) {
1266 zval_ptr_dtor(&tmp);
1267 }
1268 if (index) {
1269 *index = lval;
1270 }
1271 return zend_hash_index_exists(ht, lval);
1272 }
1273
1274 static int php_pqtypes_object_has_dimension(zval *object, zval *member, int check_empty TSRMLS_DC)
1275 {
1276 php_pqtypes_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
1277 char *key_str = NULL;
1278 int key_len = 0;
1279 long index = 0;
1280
1281 if (check_empty) {
1282 if (has_dimension(&obj->intern->types, member, &key_str, &key_len, &index TSRMLS_CC)) {
1283 zval **data;
1284
1285 if (key_str && key_len) {
1286 if (SUCCESS == zend_hash_find(&obj->intern->types, key_str, key_len, (void *) &data)) {
1287 efree(key_str);
1288 return Z_TYPE_PP(data) != IS_NULL;
1289 }
1290 efree(key_str);
1291 } else {
1292 if (SUCCESS == zend_hash_index_find(&obj->intern->types, index, (void *) data)) {
1293 return Z_TYPE_PP(data) != IS_NULL;
1294 }
1295 }
1296 }
1297 } else {
1298 return has_dimension(&obj->intern->types, member, NULL, NULL, NULL TSRMLS_CC);
1299 }
1300
1301 return 0;
1302 }
1303
1304 static zval *php_pqtypes_object_read_dimension(zval *object, zval *member, int type TSRMLS_DC)
1305 {
1306 long index = 0;
1307 char *key_str = NULL;
1308 int key_len = 0;
1309 php_pqtypes_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
1310
1311 if (has_dimension(&obj->intern->types, member, &key_str, &key_len, &index TSRMLS_CC)) {
1312 zval **data;
1313
1314 if (key_str && key_len) {
1315 if (SUCCESS == zend_hash_find(&obj->intern->types, key_str, key_len, (void *) &data)) {
1316 efree(key_str);
1317 return *data;
1318 }
1319 } else {
1320 if (SUCCESS == zend_hash_index_find(&obj->intern->types, index, (void *) &data)) {
1321 return *data;
1322 }
1323 }
1324 }
1325
1326 return NULL;
1327 }
1328
1329 static void php_pqres_object_read_status(zval *object, void *o, zval *return_value TSRMLS_DC)
1330 {
1331 php_pqres_object_t *obj = o;
1332
1333 RETVAL_LONG(PQresultStatus(obj->intern->res));
1334 }
1335
1336 static void php_pqres_object_read_status_message(zval *object, void *o, zval *return_value TSRMLS_DC)
1337 {
1338 php_pqres_object_t *obj = o;
1339
1340 RETVAL_STRING(PQresStatus(PQresultStatus(obj->intern->res))+sizeof("PGRES"), 1);
1341 }
1342
1343 static void php_pqres_object_read_error_message(zval *object, void *o, zval *return_value TSRMLS_DC)
1344 {
1345 php_pqres_object_t *obj = o;
1346 char *error = PHP_PQresultErrorMessage(obj->intern->res);
1347
1348 if (error) {
1349 RETVAL_STRING(error, 1);
1350 } else {
1351 RETVAL_NULL();
1352 }
1353 }
1354
1355 static void php_pqres_object_read_num_rows(zval *object, void *o, zval *return_value TSRMLS_DC)
1356 {
1357 php_pqres_object_t *obj = o;
1358
1359 RETVAL_LONG(PQntuples(obj->intern->res));
1360 }
1361
1362 static void php_pqres_object_read_num_cols(zval *object, void *o, zval *return_value TSRMLS_DC)
1363 {
1364 php_pqres_object_t *obj = o;
1365
1366 RETVAL_LONG(PQnfields(obj->intern->res));
1367 }
1368
1369 static void php_pqres_object_read_affected_rows(zval *object, void *o, zval *return_value TSRMLS_DC)
1370 {
1371 php_pqres_object_t *obj = o;
1372
1373 RETVAL_LONG(atoi(PQcmdTuples(obj->intern->res)));
1374 }
1375
1376 static void php_pqres_object_read_fetch_type(zval *object, void *o, zval *return_value TSRMLS_DC)
1377 {
1378 php_pqres_object_t *obj = o;
1379
1380 if (obj->intern->iter) {
1381 RETVAL_LONG(obj->intern->iter->fetch_type);
1382 } else {
1383 RETVAL_LONG(PHP_PQRES_FETCH_ARRAY);
1384 }
1385 }
1386
1387 static void php_pqres_object_write_fetch_type(zval *object, void *o, zval *value TSRMLS_DC)
1388 {
1389 php_pqres_object_t *obj = o;
1390 zval *zfetch_type = value;
1391
1392 if (Z_TYPE_P(zfetch_type) != IS_LONG) {
1393 convert_to_long_ex(&zfetch_type);
1394 }
1395
1396 if (!obj->intern->iter) {
1397 obj->intern->iter = (php_pqres_iterator_t *) php_pqres_iterator_init(Z_OBJCE_P(object), object, 0 TSRMLS_CC);
1398 obj->intern->iter->zi.funcs->rewind((zend_object_iterator *) obj->intern->iter TSRMLS_CC);
1399 }
1400 obj->intern->iter->fetch_type = Z_LVAL_P(zfetch_type);
1401
1402 if (zfetch_type != value) {
1403 zval_ptr_dtor(&zfetch_type);
1404 }
1405 }
1406
1407 static void php_pqstm_object_read_name(zval *object, void *o, zval *return_value TSRMLS_DC)
1408 {
1409 php_pqstm_object_t *obj = o;
1410
1411 RETVAL_STRING(obj->intern->name, 1);
1412 }
1413
1414 static void php_pqstm_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
1415 {
1416 php_pqstm_object_t *obj = o;
1417
1418 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
1419 }
1420
1421 static void php_pqtxn_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
1422 {
1423 php_pqtxn_object_t *obj = o;
1424
1425 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
1426 }
1427
1428 static void php_pqtxn_object_read_isolation(zval *object, void *o, zval *return_value TSRMLS_DC)
1429 {
1430 php_pqtxn_object_t *obj = o;
1431
1432 RETVAL_LONG(obj->intern->isolation);
1433 }
1434
1435 static void php_pqtxn_object_read_readonly(zval *object, void *o, zval *return_value TSRMLS_DC)
1436 {
1437 php_pqtxn_object_t *obj = o;
1438
1439 RETVAL_LONG(obj->intern->readonly);
1440 }
1441
1442 static void php_pqtxn_object_read_deferrable(zval *object, void *o, zval *return_value TSRMLS_DC)
1443 {
1444 php_pqtxn_object_t *obj = o;
1445
1446 RETVAL_LONG(obj->intern->deferrable);
1447 }
1448
1449 static void php_pqtxn_object_write_isolation(zval *object, void *o, zval *value TSRMLS_DC)
1450 {
1451 php_pqtxn_object_t *obj = o;
1452 php_pqtxn_isolation_t orig = obj->intern->isolation;
1453 zval *zisolation = value;
1454 PGresult *res;
1455
1456 if (Z_TYPE_P(zisolation) != IS_LONG) {
1457 convert_to_long_ex(&zisolation);
1458 }
1459
1460 switch ((obj->intern->isolation = Z_LVAL_P(zisolation))) {
1461 case PHP_PQTXN_READ_COMMITTED:
1462 res = PQexec(obj->intern->conn->intern->conn, "SET TRANSACTION READ COMMITED");
1463 break;
1464 case PHP_PQTXN_REPEATABLE_READ:
1465 res = PQexec(obj->intern->conn->intern->conn, "SET TRANSACTION REPEATABLE READ");
1466 break;
1467 case PHP_PQTXN_SERIALIZABLE:
1468 res = PQexec(obj->intern->conn->intern->conn, "SET TRANSACTION SERIALIZABLE");
1469 break;
1470 default:
1471 obj->intern->isolation = orig;
1472 res = NULL;
1473 break;
1474 }
1475
1476 if (zisolation != value) {
1477 zval_ptr_dtor(&zisolation);
1478 }
1479
1480 if (res) {
1481 php_pqres_success(res TSRMLS_CC);
1482 PHP_PQclear(res);
1483 }
1484 }
1485
1486 static void php_pqtxn_object_write_readonly(zval *object, void *o, zval *value TSRMLS_DC)
1487 {
1488 php_pqtxn_object_t *obj = o;
1489 PGresult *res;
1490
1491 if ((obj->intern->readonly = zend_is_true(value))) {
1492 res = PQexec(obj->intern->conn->intern->conn, "SET TRANSACTION READ ONLY");
1493 } else {
1494 res = PQexec(obj->intern->conn->intern->conn, "SET TRANSACTION READ WRITE");
1495 }
1496
1497 if (res) {
1498 php_pqres_success(res TSRMLS_CC);
1499 PHP_PQclear(res);
1500 }
1501 }
1502
1503 static void php_pqtxn_object_write_deferrable(zval *object, void *o, zval *value TSRMLS_DC)
1504 {
1505 php_pqtxn_object_t *obj = o;
1506 PGresult *res;
1507
1508 if ((obj->intern->deferrable = zend_is_true(value))) {
1509 res = PQexec(obj->intern->conn->intern->conn, "SET TRANSACTION DEFERRABLE");
1510 } else {
1511 res = PQexec(obj->intern->conn->intern->conn, "SET TRANSACTION NOT DEFERRABLE");
1512 }
1513
1514 if (res) {
1515 php_pqres_success(res TSRMLS_CC);
1516 PHP_PQclear(res);
1517 }
1518 }
1519
1520 static void php_pqcancel_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
1521 {
1522 php_pqcancel_object_t *obj = o;
1523
1524 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
1525 }
1526
1527 static void php_pqevent_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
1528 {
1529 php_pqevent_object_t *obj = o;
1530
1531 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
1532 }
1533
1534 static void php_pqevent_object_read_type(zval *object, void *o, zval *return_value TSRMLS_DC)
1535 {
1536 php_pqevent_object_t *obj = o;
1537
1538 RETVAL_STRING(obj->intern->type, 1);
1539 }
1540
1541 static void php_pqlob_object_read_transaction(zval *object, void *o, zval *return_value TSRMLS_DC)
1542 {
1543 php_pqlob_object_t *obj = o;
1544
1545 php_pq_object_to_zval(obj->intern->txn, &return_value TSRMLS_CC);
1546 }
1547
1548 static void php_pqlob_object_read_oid(zval *object, void *o, zval *return_value TSRMLS_DC)
1549 {
1550 php_pqlob_object_t *obj = o;
1551
1552 RETVAL_LONG(obj->intern->loid);
1553 }
1554
1555 static void php_pqcopy_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
1556 {
1557 php_pqcopy_object_t *obj = o;
1558
1559 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
1560 }
1561
1562 static void php_pqcopy_object_read_direction(zval *object, void *o, zval *return_value TSRMLS_DC)
1563 {
1564 php_pqcopy_object_t *obj = o;
1565
1566 RETVAL_LONG(obj->intern->direction);
1567 }
1568
1569 static void php_pqcopy_object_read_expression(zval *object, void *o, zval *return_value TSRMLS_DC)
1570 {
1571 php_pqcopy_object_t *obj = o;
1572
1573 RETURN_STRING(obj->intern->expression, 1);
1574 }
1575
1576 static void php_pqcopy_object_read_options(zval *object, void *o, zval *return_value TSRMLS_DC)
1577 {
1578 php_pqcopy_object_t *obj = o;
1579
1580 RETURN_STRING(obj->intern->options, 1);
1581 }
1582
1583 static zend_class_entry *ancestor(zend_class_entry *ce) {
1584 while (ce->parent) {
1585 ce = ce->parent;
1586 }
1587 return ce;
1588 }
1589
1590 static zval *php_pq_object_read_prop(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC)
1591 {
1592 php_pq_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
1593 php_pq_object_prophandler_t *handler;
1594 zval *return_value;
1595
1596 if (!obj->intern) {
1597 zend_error(E_WARNING, "%s not initialized", ancestor(obj->zo.ce)->name);
1598 } else if ((SUCCESS == zend_hash_find(obj->prophandler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void *) &handler)) && handler->read) {
1599 if (type == BP_VAR_R) {
1600 ALLOC_ZVAL(return_value);
1601 Z_SET_REFCOUNT_P(return_value, 0);
1602 Z_UNSET_ISREF_P(return_value);
1603
1604 handler->read(object, obj, return_value TSRMLS_CC);
1605 } else {
1606 zend_error(E_ERROR, "Cannot access %s properties by reference or array key/index", ancestor(obj->zo.ce)->name);
1607 return_value = NULL;
1608 }
1609 } else {
1610 return_value = zend_get_std_object_handlers()->read_property(object, member, type, key TSRMLS_CC);
1611 }
1612
1613 return return_value;
1614 }
1615
1616 static void php_pq_object_write_prop(zval *object, zval *member, zval *value, const zend_literal *key TSRMLS_DC)
1617 {
1618 php_pq_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
1619 php_pq_object_prophandler_t *handler;
1620
1621 if (SUCCESS == zend_hash_find(obj->prophandler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void *) &handler)) {
1622 if (handler->write) {
1623 handler->write(object, obj, value TSRMLS_CC);
1624 }
1625 } else {
1626 zend_get_std_object_handlers()->write_property(object, member, value, key TSRMLS_CC);
1627 }
1628 }
1629
1630 static STATUS php_pqconn_update_socket(zval *this_ptr, php_pqconn_object_t *obj TSRMLS_DC)
1631 {
1632 zval *zsocket, zmember;
1633 php_stream *stream;
1634 STATUS retval;
1635 int socket;
1636
1637 if (!obj) {
1638 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1639 }
1640
1641 INIT_PZVAL(&zmember);
1642 ZVAL_STRINGL(&zmember, "socket", sizeof("socket")-1, 0);
1643 MAKE_STD_ZVAL(zsocket);
1644
1645 if ((CONNECTION_BAD != PQstatus(obj->intern->conn))
1646 && (-1 < (socket = PQsocket(obj->intern->conn)))
1647 && (stream = php_stream_fopen_from_fd(socket, "r+b", NULL))) {
1648 stream->flags |= PHP_STREAM_FLAG_NO_CLOSE;
1649 php_stream_to_zval(stream, zsocket);
1650 retval = SUCCESS;
1651 } else {
1652 ZVAL_NULL(zsocket);
1653 retval = FAILURE;
1654 }
1655 zend_get_std_object_handlers()->write_property(getThis(), &zmember, zsocket, NULL TSRMLS_CC);
1656 zval_ptr_dtor(&zsocket);
1657
1658 return retval;
1659 }
1660
1661 #ifdef ZTS
1662 # define TSRMLS_DF(d) TSRMLS_D = (d)->ts
1663 # define TSRMLS_CF(d) (d)->ts = TSRMLS_C
1664 #else
1665 # define TSRMLS_DF(d)
1666 # define TSRMLS_CF(d)
1667 #endif
1668
1669 static int apply_event(void *p, void *a TSRMLS_DC)
1670 {
1671 zval **evh = p;
1672 zval *args = a;
1673 zval *retval = NULL;
1674
1675 zend_call_method_with_1_params(evh, Z_OBJCE_PP(evh), NULL, "trigger", &retval, args);
1676 if (retval) {
1677 zval_ptr_dtor(&retval);
1678 }
1679
1680 return ZEND_HASH_APPLY_KEEP;
1681 }
1682
1683 static void php_pqconn_event_connreset(PGEventConnReset *event)
1684 {
1685 php_pqconn_event_data_t *data = PQinstanceData(event->conn, php_pqconn_event);
1686
1687 if (data) {
1688 zval **evhs;
1689 TSRMLS_DF(data);
1690
1691 if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("reset"), (void *) &evhs)) {
1692 zval *args, *connection = NULL;
1693
1694 MAKE_STD_ZVAL(args);
1695 array_init(args);
1696 php_pq_object_to_zval(data->obj, &connection TSRMLS_CC);
1697 add_next_index_zval(args, connection);
1698 zend_hash_apply_with_argument(Z_ARRVAL_PP(evhs), apply_event, args TSRMLS_CC);
1699 zval_ptr_dtor(&args);
1700 }
1701 }
1702 }
1703
1704 static zval *result_instance_zval(PGresult *res TSRMLS_DC)
1705 {
1706 zval *rid = PQresultInstanceData(res, php_pqconn_event);
1707
1708 if (!rid) {
1709 php_pqres_t *r = ecalloc(1, sizeof(*r));
1710
1711 MAKE_STD_ZVAL(rid);
1712 r->res = res;
1713 ZEND_INIT_SYMTABLE(&r->bound);
1714 rid->type = IS_OBJECT;
1715 rid->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC);
1716
1717 PQresultSetInstanceData(res, php_pqconn_event, rid);
1718 }
1719
1720 Z_ADDREF_P(rid);
1721 return rid;
1722 }
1723
1724 static void php_pqconn_event_resultcreate(PGEventResultCreate *event)
1725 {
1726 php_pqconn_event_data_t *data = PQinstanceData(event->conn, php_pqconn_event);
1727
1728 if (data) {
1729 zval **evhs;
1730 TSRMLS_DF(data);
1731
1732 /* event listener */
1733 if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("result"), (void *) &evhs)) {
1734 zval *args, *connection = NULL, *res = result_instance_zval(event->result TSRMLS_CC);
1735
1736 MAKE_STD_ZVAL(args);
1737 array_init(args);
1738 php_pq_object_to_zval(data->obj, &connection TSRMLS_CC);
1739 add_next_index_zval(args, connection);
1740 add_next_index_zval(args, res);
1741 zend_hash_apply_with_argument(Z_ARRVAL_PP(evhs), apply_event, args TSRMLS_CC);
1742 zval_ptr_dtor(&args);
1743 }
1744
1745 /* async callback */
1746 if (data->obj->intern->onevent.fci.size > 0) {
1747 zval *res = result_instance_zval(event->result TSRMLS_CC);
1748
1749 zend_fcall_info_argn(&data->obj->intern->onevent.fci TSRMLS_CC, 1, &res);
1750 zend_fcall_info_call(&data->obj->intern->onevent.fci, &data->obj->intern->onevent.fcc, NULL, NULL TSRMLS_CC);
1751 zval_ptr_dtor(&res);
1752 }
1753 }
1754 }
1755
1756 static int php_pqconn_event(PGEventId id, void *e, void *data)
1757 {
1758 switch (id) {
1759 case PGEVT_CONNRESET:
1760 php_pqconn_event_connreset(e);
1761 break;
1762 case PGEVT_RESULTCREATE:
1763 php_pqconn_event_resultcreate(e);
1764 break;
1765 default:
1766 break;
1767 }
1768
1769 return 1;
1770 }
1771
1772 static php_pqconn_event_data_t *php_pqconn_event_data_init(php_pqconn_object_t *obj TSRMLS_DC)
1773 {
1774 php_pqconn_event_data_t *data = emalloc(sizeof(*data));
1775
1776 data->obj = obj;
1777 TSRMLS_CF(data);
1778
1779 return data;
1780 }
1781
1782 static void php_pqconn_notice_recv(void *p, const PGresult *res)
1783 {
1784 php_pqconn_event_data_t *data = p;
1785
1786 if (data) {
1787 zval **evhs;
1788 TSRMLS_DF(data);
1789
1790 if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("notice"), (void *) &evhs)) {
1791 zval *args, *connection = NULL;
1792
1793 MAKE_STD_ZVAL(args);
1794 array_init(args);
1795 php_pq_object_to_zval(data->obj, &connection TSRMLS_CC);
1796 add_next_index_zval(args, connection);
1797 add_next_index_string(args, PHP_PQresultErrorMessage(res), 1);
1798 zend_hash_apply_with_argument(Z_ARRVAL_PP(evhs), apply_event, args TSRMLS_CC);
1799 zval_ptr_dtor(&args);
1800 }
1801 }
1802 }
1803
1804 typedef struct php_pqconn_resource_factory_data {
1805 char *dsn;
1806 long flags;
1807 } php_pqconn_resource_factory_data_t;
1808
1809 static void *php_pqconn_resource_factory_ctor(void *data, void *init_arg TSRMLS_DC)
1810 {
1811 php_pqconn_resource_factory_data_t *o = init_arg;
1812 PGconn *conn = NULL;;
1813
1814 if (o->flags & PHP_PQCONN_ASYNC) {
1815 conn = PQconnectStart(o->dsn);
1816 } else {
1817 conn = PQconnectdb(o->dsn);
1818 }
1819
1820 if (conn) {
1821 PQregisterEventProc(conn, php_pqconn_event, "ext-pq", NULL);
1822 }
1823
1824 return conn;
1825 }
1826
1827 static void php_pqconn_resource_factory_dtor(void *opaque, void *handle TSRMLS_DC)
1828 {
1829 php_pqconn_event_data_t *evdata = PQinstanceData(handle, php_pqconn_event);
1830
1831 /* we don't care for anything, except free'ing evdata */
1832 if (evdata) {
1833 PQsetInstanceData(handle, php_pqconn_event, NULL);
1834 memset(evdata, 0, sizeof(*evdata));
1835 efree(evdata);
1836 }
1837
1838 PQfinish(handle);
1839 }
1840
1841 static php_resource_factory_ops_t php_pqconn_resource_factory_ops = {
1842 php_pqconn_resource_factory_ctor,
1843 NULL,
1844 php_pqconn_resource_factory_dtor
1845 };
1846
1847 static void php_pqconn_wakeup(php_persistent_handle_factory_t *f, void **handle TSRMLS_DC)
1848 {
1849 // FIXME: ping server
1850 }
1851
1852 static int apply_unlisten(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
1853 {
1854 php_pqconn_object_t *obj = va_arg(argv, php_pqconn_object_t *);
1855 char *quoted_channel = PQescapeIdentifier(obj->intern->conn, key->arKey, key->nKeyLength - 1);
1856
1857 if (quoted_channel) {
1858 PGresult *res;
1859 char *cmd;
1860
1861 spprintf(&cmd, 0, "UNLISTEN %s", quoted_channel);
1862 if ((res = PQexec(obj->intern->conn, cmd))) {
1863 PHP_PQclear(res);
1864 }
1865
1866 efree(cmd);
1867 PQfreemem(quoted_channel);
1868 }
1869
1870 return ZEND_HASH_APPLY_REMOVE;
1871 }
1872
1873 static void php_pqconn_notice_ignore(void *p, const PGresult *res)
1874 {
1875 }
1876
1877 static void php_pqconn_retire(php_persistent_handle_factory_t *f, void **handle TSRMLS_DC)
1878 {
1879 php_pqconn_event_data_t *evdata = PQinstanceData(*handle, php_pqconn_event);
1880 PGcancel *cancel;
1881 PGresult *res;
1882
1883 /* go away */
1884 PQsetInstanceData(*handle, php_pqconn_event, NULL);
1885
1886 /* ignore notices */
1887 PQsetNoticeReceiver(*handle, php_pqconn_notice_ignore, NULL);
1888
1889 /* cancel async queries */
1890 if (PQisBusy(*handle) && (cancel = PQgetCancel(*handle))) {
1891 PQcancel(cancel, ZEND_STRL("retiring persistent connection"));
1892 PQfreeCancel(cancel);
1893 }
1894 /* clean up async results */
1895 while ((res = PQgetResult(*handle))) {
1896 PHP_PQclear(res);
1897 }
1898
1899 /* clean up transaction & session */
1900 switch (PQtransactionStatus(*handle)) {
1901 case PQTRANS_IDLE:
1902 res = PQexec(*handle, "RESET ALL");
1903 break;
1904 default:
1905 res = PQexec(*handle, "ROLLBACK; RESET ALL");
1906 break;
1907 }
1908
1909 if (res) {
1910 PHP_PQclear(res);
1911 }
1912
1913 if (evdata) {
1914 /* clean up notify listeners */
1915 zend_hash_apply_with_arguments(&evdata->obj->intern->listeners TSRMLS_CC, apply_unlisten, 1, evdata->obj);
1916
1917 /* release instance data */
1918 memset(evdata, 0, sizeof(*evdata));
1919 efree(evdata);
1920 }
1921 }
1922
1923 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct, 0, 0, 1)
1924 ZEND_ARG_INFO(0, dsn)
1925 ZEND_ARG_INFO(0, async)
1926 ZEND_END_ARG_INFO();
1927 static PHP_METHOD(pqconn, __construct) {
1928 zend_error_handling zeh;
1929 char *dsn_str = "";
1930 int dsn_len = 0;
1931 long flags = 0;
1932 STATUS rv;
1933
1934 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1935 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &dsn_str, &dsn_len, &flags);
1936 zend_restore_error_handling(&zeh TSRMLS_CC);
1937
1938 if (SUCCESS == rv) {
1939 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1940
1941 if (obj->intern) {
1942 zend_throw_exception_ex(exce(EX_BAD_METHODCALL), EX_BAD_METHODCALL TSRMLS_CC, "pq\\Connection already initialized");
1943 } else {
1944 php_pqconn_event_data_t *evdata = php_pqconn_event_data_init(obj TSRMLS_CC);
1945 php_pqconn_resource_factory_data_t rfdata = {dsn_str, flags};
1946
1947 obj->intern = ecalloc(1, sizeof(*obj->intern));
1948
1949 zend_hash_init(&obj->intern->listeners, 0, NULL, (dtor_func_t) zend_hash_destroy, 0);
1950 zend_hash_init(&obj->intern->eventhandlers, 0, NULL, ZVAL_PTR_DTOR, 0);
1951
1952 if (flags & PHP_PQCONN_PERSISTENT) {
1953 php_persistent_handle_factory_t *phf = php_persistent_handle_concede(NULL, ZEND_STRL("pq\\Connection"), dsn_str, dsn_len, php_pqconn_wakeup, php_pqconn_retire TSRMLS_CC);
1954 php_resource_factory_init(&obj->intern->factory, php_persistent_handle_get_resource_factory_ops(), phf, (void (*)(void*)) php_persistent_handle_abandon);
1955 } else {
1956 php_resource_factory_init(&obj->intern->factory, &php_pqconn_resource_factory_ops, NULL, NULL);
1957 }
1958
1959 if (flags & PHP_PQCONN_ASYNC) {
1960 obj->intern->poller = (int (*)(PGconn*)) PQconnectPoll;
1961 }
1962
1963 obj->intern->conn = php_resource_factory_handle_ctor(&obj->intern->factory, &rfdata TSRMLS_CC);
1964
1965 PQsetInstanceData(obj->intern->conn, php_pqconn_event, evdata);
1966 PQsetNoticeReceiver(obj->intern->conn, php_pqconn_notice_recv, evdata);
1967
1968 if (SUCCESS != php_pqconn_update_socket(getThis(), obj TSRMLS_CC)) {
1969 zend_throw_exception_ex(exce(EX_CONNECTION_FAILED), EX_CONNECTION_FAILED TSRMLS_CC, "Connection failed (%s)", PHP_PQerrorMessage(obj->intern->conn));
1970 }
1971 }
1972 }
1973 }
1974
1975 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset, 0, 0, 0)
1976 ZEND_END_ARG_INFO();
1977 static PHP_METHOD(pqconn, reset) {
1978 if (SUCCESS == zend_parse_parameters_none()) {
1979 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1980
1981 if (obj->intern) {
1982 PQreset(obj->intern->conn);
1983
1984 if (CONNECTION_OK == PQstatus(obj->intern->conn)) {
1985 RETURN_TRUE;
1986 } else {
1987 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection reset failed: (%s)", PHP_PQerrorMessage(obj->intern->conn));
1988 }
1989 } else {
1990 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
1991 }
1992 RETURN_FALSE;
1993 }
1994 }
1995
1996 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset_async, 0, 0, 0)
1997 ZEND_END_ARG_INFO();
1998 static PHP_METHOD(pqconn, resetAsync) {
1999 if (SUCCESS == zend_parse_parameters_none()) {
2000 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2001
2002 if (obj->intern) {
2003 if (PQresetStart(obj->intern->conn)) {
2004 obj->intern->poller = (int (*)(PGconn*)) PQresetPoll;
2005 RETURN_TRUE;
2006 }
2007 } else {
2008 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2009 }
2010 RETURN_FALSE;
2011 }
2012 }
2013
2014 static void php_pqconn_add_listener(php_pqconn_object_t *obj, const char *channel_str, size_t channel_len, php_pq_callback_t *listener TSRMLS_DC)
2015 {
2016 HashTable ht, *existing_listeners;
2017
2018 php_pq_callback_addref(listener);
2019
2020 if (SUCCESS == zend_hash_find(&obj->intern->listeners, channel_str, channel_len + 1, (void *) &existing_listeners)) {
2021 zend_hash_next_index_insert(existing_listeners, (void *) listener, sizeof(*listener), NULL);
2022 } else {
2023 zend_hash_init(&ht, 1, NULL, (dtor_func_t) php_pq_callback_dtor, 0);
2024 zend_hash_next_index_insert(&ht, (void *) listener, sizeof(*listener), NULL);
2025 zend_hash_add(&obj->intern->listeners, channel_str, channel_len + 1, (void *) &ht, sizeof(HashTable), NULL);
2026 }
2027 }
2028
2029 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen, 0, 0, 0)
2030 ZEND_ARG_INFO(0, channel)
2031 ZEND_ARG_INFO(0, callable)
2032 ZEND_END_ARG_INFO();
2033 static PHP_METHOD(pqconn, listen) {
2034 char *channel_str = NULL;
2035 int channel_len = 0;
2036 php_pq_callback_t listener;
2037
2038 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sf", &channel_str, &channel_len, &listener.fci, &listener.fcc)) {
2039 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2040
2041 if (!obj->intern) {
2042 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2043 } else {
2044 char *quoted_channel = PQescapeIdentifier(obj->intern->conn, channel_str, channel_len);
2045
2046 if (!quoted_channel) {
2047 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj->intern->conn));
2048 RETVAL_FALSE;
2049 } else {
2050 PGresult *res;
2051 smart_str cmd = {0};
2052
2053 smart_str_appends(&cmd, "LISTEN ");
2054 smart_str_appends(&cmd, quoted_channel);
2055 smart_str_0(&cmd);
2056
2057 res = PQexec(obj->intern->conn, cmd.c);
2058
2059 smart_str_free(&cmd);
2060 PQfreemem(quoted_channel);
2061
2062 if (!res) {
2063 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to install listener (%s)", PHP_PQerrorMessage(obj->intern->conn));
2064 RETVAL_FALSE;
2065 } else {
2066 if (SUCCESS != php_pqres_success(res TSRMLS_CC)) {
2067 RETVAL_FALSE;
2068 } else {
2069 obj->intern->poller = PQconsumeInput;
2070 php_pqconn_add_listener(obj, channel_str, channel_len, &listener TSRMLS_CC);
2071 RETVAL_TRUE;
2072 }
2073 PHP_PQclear(res);
2074 }
2075
2076 php_pqconn_notify_listeners(obj TSRMLS_CC);
2077 }
2078 }
2079 }
2080 }
2081
2082 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen_async, 0, 0, 0)
2083 ZEND_ARG_INFO(0, channel)
2084 ZEND_ARG_INFO(0, callable)
2085 ZEND_END_ARG_INFO();
2086 static PHP_METHOD(pqconn, listenAsync) {
2087 char *channel_str = NULL;
2088 int channel_len = 0;
2089 php_pq_callback_t listener;
2090
2091 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sf", &channel_str, &channel_len, &listener.fci, &listener.fcc)) {
2092 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2093
2094 if (!obj->intern) {
2095 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2096 } else {
2097 char *quoted_channel = PQescapeIdentifier(obj->intern->conn, channel_str, channel_len);
2098
2099 if (!quoted_channel) {
2100 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj->intern->conn));
2101 } else {
2102 smart_str cmd = {0};
2103
2104 smart_str_appends(&cmd, "LISTEN ");
2105 smart_str_appends(&cmd, quoted_channel);
2106 smart_str_0(&cmd);
2107
2108 if (!PQsendQuery(obj->intern->conn, cmd.c)) {
2109 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to install listener (%s)", PHP_PQerrorMessage(obj->intern->conn));
2110 RETVAL_FALSE;
2111 } else {
2112 obj->intern->poller = PQconsumeInput;
2113 php_pqconn_add_listener(obj, channel_str, channel_len, &listener TSRMLS_CC);
2114 RETVAL_TRUE;
2115 }
2116
2117 smart_str_free(&cmd);
2118 PQfreemem(quoted_channel);
2119 }
2120 }
2121 }
2122 }
2123
2124 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify, 0, 0, 2)
2125 ZEND_ARG_INFO(0, channel)
2126 ZEND_ARG_INFO(0, message)
2127 ZEND_END_ARG_INFO();
2128 static PHP_METHOD(pqconn, notify) {
2129 char *channel_str, *message_str;
2130 int channel_len, message_len;
2131
2132 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &channel_str, &channel_len, &message_str, &message_len)) {
2133 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2134
2135 if (!obj->intern) {
2136 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2137 } else {
2138 PGresult *res;
2139 char *params[2] = {channel_str, message_str};
2140
2141 res = PQexecParams(obj->intern->conn, "select pg_notify($1, $2)", 2, NULL, (const char *const*) params, NULL, NULL, 0);
2142
2143 if (!res) {
2144 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj->intern->conn));
2145 RETVAL_FALSE;
2146 } else {
2147 if (SUCCESS != php_pqres_success(res TSRMLS_CC)) {
2148 RETVAL_FALSE;
2149 } else {
2150 RETVAL_TRUE;
2151 }
2152 PHP_PQclear(res);
2153 }
2154
2155 php_pqconn_notify_listeners(obj TSRMLS_CC);
2156 }
2157 }
2158 }
2159
2160 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify_async, 0, 0, 2)
2161 ZEND_ARG_INFO(0, channel)
2162 ZEND_ARG_INFO(0, message)
2163 ZEND_END_ARG_INFO();
2164 static PHP_METHOD(pqconn, notifyAsync) {
2165 char *channel_str, *message_str;
2166 int channel_len, message_len;
2167
2168 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &channel_str, &channel_len, &message_str, &message_len)) {
2169 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2170
2171 if (!obj->intern) {
2172 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2173 } else {
2174 char *params[2] = {channel_str, message_str};
2175
2176 if (!PQsendQueryParams(obj->intern->conn, "select pg_notify($1, $2)", 2, NULL, (const char *const*) params, NULL, NULL, 0)) {
2177 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj->intern->conn));
2178 RETVAL_FALSE;
2179 } else {
2180 obj->intern->poller = PQconsumeInput;
2181 RETVAL_TRUE;
2182 }
2183
2184 php_pqconn_notify_listeners(obj TSRMLS_CC);
2185 }
2186 }
2187 }
2188
2189 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_poll, 0, 0, 0)
2190 ZEND_END_ARG_INFO();
2191 static PHP_METHOD(pqconn, poll) {
2192 if (SUCCESS == zend_parse_parameters_none()) {
2193 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2194
2195 if (!obj->intern) {
2196 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2197 } else if (!obj->intern->poller) {
2198 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "No asynchronous operation active");
2199 } else if (obj->intern->poller == PQconsumeInput) {
2200 RETVAL_LONG(obj->intern->poller(obj->intern->conn) * PGRES_POLLING_OK);
2201 } else {
2202 RETVAL_LONG(obj->intern->poller(obj->intern->conn));
2203 }
2204 php_pqconn_notify_listeners(obj TSRMLS_CC);
2205 }
2206 }
2207
2208 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec, 0, 0, 1)
2209 ZEND_ARG_INFO(0, query)
2210 ZEND_END_ARG_INFO();
2211 static PHP_METHOD(pqconn, exec) {
2212 zend_error_handling zeh;
2213 char *query_str;
2214 int query_len;
2215 STATUS rv;
2216
2217 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
2218 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &query_str, &query_len);
2219 zend_restore_error_handling(&zeh TSRMLS_CC);
2220
2221 if (SUCCESS == rv) {
2222 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2223
2224 if (!obj->intern) {
2225 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2226 } else {
2227 PGresult *res = PQexec(obj->intern->conn, query_str);
2228
2229 if (!res) {
2230 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
2231 } else if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
2232 php_pqres_t *r = ecalloc(1, sizeof(*r));
2233
2234 r->res = res;
2235 ZEND_INIT_SYMTABLE(&r->bound);
2236 return_value->type = IS_OBJECT;
2237 return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC);
2238 }
2239
2240 php_pqconn_notify_listeners(obj TSRMLS_CC);
2241 }
2242 }
2243 }
2244
2245 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_get_result, 0, 0, 0)
2246 ZEND_END_ARG_INFO();
2247 static PHP_METHOD(pqconn, getResult) {
2248 if (SUCCESS == zend_parse_parameters_none()) {
2249 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2250
2251 if (!obj->intern) {
2252 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connectio not initialized");
2253 } else {
2254 PGresult *res = PQgetResult(obj->intern->conn);
2255
2256 if (!res) {
2257 RETVAL_NULL();
2258 } else {
2259 php_pqres_t *r = ecalloc(1, sizeof(*r));
2260
2261 r->res = res;
2262 ZEND_INIT_SYMTABLE(&r->bound);
2263 return_value->type = IS_OBJECT;
2264 return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC);
2265 }
2266
2267 php_pqconn_notify_listeners(obj TSRMLS_CC);
2268 }
2269 }
2270 }
2271
2272 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_async, 0, 0, 1)
2273 ZEND_ARG_INFO(0, query)
2274 ZEND_ARG_INFO(0, callable)
2275 ZEND_END_ARG_INFO();
2276 static PHP_METHOD(pqconn, execAsync) {
2277 zend_error_handling zeh;
2278 php_pq_callback_t resolver = {{0}};
2279 char *query_str;
2280 int query_len;
2281 STATUS rv;
2282
2283 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
2284 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|f", &query_str, &query_len, &resolver.fci, &resolver.fcc);
2285 zend_restore_error_handling(&zeh TSRMLS_CC);
2286
2287 if (SUCCESS == rv) {
2288 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2289
2290 if (!obj->intern) {
2291 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2292 } else if (!PQsendQuery(obj->intern->conn, query_str)) {
2293 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
2294 } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
2295 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to request unbuffered result handling (%s)", PHP_PQerrorMessage(obj->intern->conn));
2296 } else {
2297 obj->intern->poller = PQconsumeInput;
2298 php_pq_callback_dtor(&obj->intern->onevent);
2299 if (resolver.fci.size > 0) {
2300 obj->intern->onevent = resolver;
2301 php_pq_callback_addref(&obj->intern->onevent);
2302 }
2303 php_pqconn_notify_listeners(obj TSRMLS_CC);
2304 }
2305 }
2306 }
2307
2308 static int apply_to_oid(void *p, void *arg TSRMLS_DC)
2309 {
2310 Oid **types = arg;
2311 zval **ztype = p;
2312
2313 if (Z_TYPE_PP(ztype) != IS_LONG) {
2314 convert_to_long_ex(ztype);
2315 }
2316
2317 **types = Z_LVAL_PP(ztype);
2318 ++*types;
2319
2320 if (*ztype != *(zval **)p) {
2321 zval_ptr_dtor(ztype);
2322 }
2323 return ZEND_HASH_APPLY_KEEP;
2324 }
2325
2326 static int apply_to_param(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
2327 {
2328 char ***params;
2329 HashTable *zdtor;
2330 zval **zparam = p;
2331
2332 params = (char ***) va_arg(argv, char ***);
2333 zdtor = (HashTable *) va_arg(argv, HashTable *);
2334
2335 if (Z_TYPE_PP(zparam) == IS_NULL) {
2336 **params = NULL;
2337 ++*params;
2338 } else {
2339 if (Z_TYPE_PP(zparam) != IS_STRING) {
2340 convert_to_string_ex(zparam);
2341 }
2342
2343 **params = Z_STRVAL_PP(zparam);
2344 ++*params;
2345
2346 if (*zparam != *(zval **)p) {
2347 zend_hash_next_index_insert(zdtor, zparam, sizeof(zval *), NULL);
2348 }
2349 }
2350 return ZEND_HASH_APPLY_KEEP;
2351 }
2352
2353 static int php_pq_types_to_array(HashTable *ht, Oid **types TSRMLS_DC)
2354 {
2355 int count = zend_hash_num_elements(ht);
2356
2357 *types = NULL;
2358
2359 if (count) {
2360 Oid *tmp;
2361
2362 /* +1 for when less types than params are specified */
2363 *types = tmp = ecalloc(count + 1, sizeof(**types));
2364 zend_hash_apply_with_argument(ht, apply_to_oid, &tmp TSRMLS_CC);
2365 }
2366
2367 return count;
2368 }
2369
2370 static int php_pq_params_to_array(HashTable *ht, char ***params, HashTable *zdtor TSRMLS_DC)
2371 {
2372 int count = zend_hash_num_elements(ht);
2373
2374 *params = NULL;
2375
2376 if (count) {
2377 char **tmp;
2378
2379 *params = tmp = ecalloc(count, sizeof(char *));
2380 zend_hash_apply_with_arguments(ht TSRMLS_CC, apply_to_param, 2, &tmp, zdtor);
2381 }
2382
2383 return count;
2384 }
2385 /*
2386 static Oid *php_pq_ntypes_to_array(zend_bool fill, int argc, ...)
2387 {
2388 int i;
2389 Oid *oids = ecalloc(argc + 1, sizeof(*oids));
2390 va_list argv;
2391
2392 va_start(argv, argc);
2393 for (i = 0; i < argc; ++i) {
2394 if (!fill || !i) {
2395 oids[i] = va_arg(argv, Oid);
2396 } else {
2397 oids[i] = oids[0];
2398 }
2399 }
2400 va_end(argv);
2401
2402 return oids;
2403 }
2404 */
2405 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params, 0, 0, 2)
2406 ZEND_ARG_INFO(0, query)
2407 ZEND_ARG_ARRAY_INFO(0, params, 0)
2408 ZEND_ARG_ARRAY_INFO(0, types, 1)
2409 ZEND_END_ARG_INFO();
2410 static PHP_METHOD(pqconn, execParams) {
2411 zend_error_handling zeh;
2412 char *query_str;
2413 int query_len;
2414 zval *zparams;
2415 zval *ztypes = NULL;
2416 STATUS rv;
2417
2418 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
2419 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|a/!", &query_str, &query_len, &zparams, &ztypes);
2420 zend_restore_error_handling(&zeh TSRMLS_CC);
2421
2422 if (SUCCESS == rv) {
2423 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2424
2425 if (!obj->intern) {
2426 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2427 } else {
2428 PGresult *res;
2429 int count;
2430 Oid *types = NULL;
2431 char **params = NULL;
2432 HashTable zdtor;
2433
2434 ZEND_INIT_SYMTABLE(&zdtor);
2435 count = php_pq_params_to_array(Z_ARRVAL_P(zparams), &params, &zdtor TSRMLS_CC);
2436
2437 if (ztypes) {
2438 php_pq_types_to_array(Z_ARRVAL_P(ztypes), &types TSRMLS_CC);
2439 }
2440
2441 res = PQexecParams(obj->intern->conn, query_str, count, types, (const char *const*) params, NULL, NULL, 0);
2442
2443 zend_hash_destroy(&zdtor);
2444 if (types) {
2445 efree(types);
2446 }
2447 if (params) {
2448 efree(params);
2449 }
2450
2451 if (!res) {
2452 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
2453 } else {
2454 if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
2455 php_pqres_t *r = ecalloc(1, sizeof(*r));
2456
2457 r->res = res;
2458 ZEND_INIT_SYMTABLE(&r->bound);
2459 return_value->type = IS_OBJECT;
2460 return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC);
2461 }
2462
2463 php_pqconn_notify_listeners(obj TSRMLS_CC);
2464 }
2465 }
2466 }
2467 }
2468
2469 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async, 0, 0, 2)
2470 ZEND_ARG_INFO(0, query)
2471 ZEND_ARG_ARRAY_INFO(0, params, 0)
2472 ZEND_ARG_ARRAY_INFO(0, types, 1)
2473 ZEND_ARG_INFO(0, callable)
2474 ZEND_END_ARG_INFO();
2475 static PHP_METHOD(pqconn, execParamsAsync) {
2476 zend_error_handling zeh;
2477 php_pq_callback_t resolver = {{0}};
2478 char *query_str;
2479 int query_len;
2480 zval *zparams;
2481 zval *ztypes = NULL;
2482 STATUS rv;
2483
2484 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
2485 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|a/!f", &query_str, &query_len, &zparams, &ztypes, &resolver.fci, &resolver.fcc);
2486 zend_restore_error_handling(&zeh TSRMLS_CC);
2487
2488 if (SUCCESS == rv) {
2489 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2490
2491 if (!obj->intern) {
2492 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2493 } else {
2494 int count;
2495 Oid *types = NULL;
2496 char **params = NULL;
2497 HashTable zdtor;
2498
2499 ZEND_INIT_SYMTABLE(&zdtor);
2500 count = php_pq_params_to_array(Z_ARRVAL_P(zparams), &params, &zdtor TSRMLS_CC);
2501
2502 if (ztypes) {
2503 php_pq_types_to_array(Z_ARRVAL_P(ztypes), &types TSRMLS_CC);
2504 }
2505
2506 if (!PQsendQueryParams(obj->intern->conn, query_str, count, types, (const char *const*) params, NULL, NULL, 0)) {
2507 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
2508 } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
2509 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
2510 } else {
2511 obj->intern->poller = PQconsumeInput;
2512 php_pq_callback_dtor(&obj->intern->onevent);
2513 if (resolver.fci.size > 0) {
2514 obj->intern->onevent = resolver;
2515 php_pq_callback_addref(&obj->intern->onevent);
2516 }
2517 php_pqconn_notify_listeners(obj TSRMLS_CC);
2518 }
2519
2520 zend_hash_destroy(&zdtor);
2521 if (types) {
2522 efree(types);
2523 }
2524 if (params) {
2525 efree(params);
2526 }
2527 }
2528 }
2529 zend_restore_error_handling(&zeh TSRMLS_CC);
2530 }
2531
2532 static STATUS php_pqconn_prepare(zval *object, php_pqconn_object_t *obj, const char *name, const char *query, HashTable *typest TSRMLS_DC)
2533 {
2534 Oid *types = NULL;
2535 int count = 0;
2536 PGresult *res;
2537 STATUS rv;
2538
2539 if (!obj) {
2540 obj = zend_object_store_get_object(object TSRMLS_CC);
2541 }
2542
2543 if (typest) {
2544 count = zend_hash_num_elements(typest);
2545 php_pq_types_to_array(typest, &types TSRMLS_CC);
2546 }
2547
2548 res = PQprepare(obj->intern->conn, name, query, count, types);
2549
2550 if (types) {
2551 efree(types);
2552 }
2553
2554 if (!res) {
2555 rv = FAILURE;
2556 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj->intern->conn));
2557 } else {
2558 rv = php_pqres_success(res TSRMLS_CC);
2559 PHP_PQclear(res);
2560 php_pqconn_notify_listeners(obj TSRMLS_CC);
2561 }
2562
2563 return rv;
2564 }
2565
2566 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare, 0, 0, 2)
2567 ZEND_ARG_INFO(0, type)
2568 ZEND_ARG_INFO(0, query)
2569 ZEND_ARG_ARRAY_INFO(0, types, 1)
2570 ZEND_END_ARG_INFO();
2571 static PHP_METHOD(pqconn, prepare) {
2572 zend_error_handling zeh;
2573 zval *ztypes = NULL;
2574 char *name_str, *query_str;
2575 int name_len, *query_len;
2576 STATUS rv;
2577
2578 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
2579 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!", &name_str, &name_len, &query_str, &query_len, &ztypes);
2580 zend_restore_error_handling(&zeh TSRMLS_CC);
2581
2582 if (SUCCESS == rv) {
2583 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2584
2585 if (!obj->intern) {
2586 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2587 } else if (SUCCESS == php_pqconn_prepare(getThis(), obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC)) {
2588 php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
2589
2590 php_pq_object_addref(obj TSRMLS_CC);
2591 stm->conn = obj;
2592 stm->name = estrdup(name_str);
2593 ZEND_INIT_SYMTABLE(&stm->bound);
2594
2595 return_value->type = IS_OBJECT;
2596 return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
2597 }
2598 }
2599 }
2600
2601 static STATUS php_pqconn_prepare_async(zval *object, php_pqconn_object_t *obj, const char *name, const char *query, HashTable *typest TSRMLS_DC)
2602 {
2603 STATUS rv;
2604 int count;
2605 Oid *types = NULL;
2606
2607 if (!obj) {
2608 obj = zend_object_store_get_object(object TSRMLS_CC);
2609 }
2610
2611 if (typest) {
2612 count = php_pq_types_to_array(typest, &types TSRMLS_CC);
2613 }
2614
2615 if (!PQsendPrepare(obj->intern->conn, name, query, count, types)) {
2616 rv = FAILURE;
2617 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj->intern->conn));
2618 } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
2619 rv = FAILURE;
2620 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
2621 } else {
2622 rv = SUCCESS;
2623 obj->intern->poller = PQconsumeInput;
2624 php_pqconn_notify_listeners(obj TSRMLS_CC);
2625 }
2626
2627 if (types) {
2628 efree(types);
2629 }
2630
2631 return rv;
2632 }
2633
2634 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare_async, 0, 0, 2)
2635 ZEND_ARG_INFO(0, type)
2636 ZEND_ARG_INFO(0, query)
2637 ZEND_ARG_ARRAY_INFO(0, types, 1)
2638 ZEND_END_ARG_INFO();
2639 static PHP_METHOD(pqconn, prepareAsync) {
2640 zend_error_handling zeh;
2641 zval *ztypes = NULL;
2642 char *name_str, *query_str;
2643 int name_len, *query_len;
2644 STATUS rv;
2645
2646 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
2647 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!", &name_str, &name_len, &query_str, &query_len, &ztypes);
2648 zend_restore_error_handling(&zeh TSRMLS_CC);
2649
2650 if (SUCCESS == rv) {
2651 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2652
2653 if (!obj->intern) {
2654 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2655 } else if (SUCCESS == php_pqconn_prepare_async(getThis(), obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC)) {
2656 php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
2657
2658 php_pq_object_addref(obj TSRMLS_CC);
2659 stm->conn = obj;
2660 stm->name = estrdup(name_str);
2661 ZEND_INIT_SYMTABLE(&stm->bound);
2662
2663 return_value->type = IS_OBJECT;
2664 return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
2665 }
2666 }
2667 }
2668
2669 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote, 0, 0, 1)
2670 ZEND_ARG_INFO(0, string)
2671 ZEND_END_ARG_INFO();
2672 static PHP_METHOD(pqconn, quote) {
2673 char *str;
2674 int len;
2675
2676 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
2677 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2678
2679 if (!obj->intern) {
2680 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2681 } else {
2682 char *quoted = PQescapeLiteral(obj->intern->conn, str, len);
2683
2684 if (!quoted) {
2685 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to quote string (%s)", PHP_PQerrorMessage(obj->intern->conn));
2686 RETVAL_FALSE;
2687 } else {
2688 RETVAL_STRING(quoted, 1);
2689 PQfreemem(quoted);
2690 }
2691 }
2692 }
2693 }
2694
2695 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote_name, 0, 0, 1)
2696 ZEND_ARG_INFO(0, type)
2697 ZEND_END_ARG_INFO();
2698 static PHP_METHOD(pqconn, quoteName) {
2699 char *str;
2700 int len;
2701
2702 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
2703 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2704
2705 if (!obj->intern) {
2706 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2707 } else {
2708 char *quoted = PQescapeIdentifier(obj->intern->conn, str, len);
2709
2710 if (!quoted) {
2711 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to quote name (%s)", PHP_PQerrorMessage(obj->intern->conn));
2712 RETVAL_FALSE;
2713 } else {
2714 RETVAL_STRING(quoted, 1);
2715 PQfreemem(quoted);
2716 }
2717 }
2718 }
2719 }
2720
2721 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_escape_bytea, 0, 0, 1)
2722 ZEND_ARG_INFO(0, bytea)
2723 ZEND_END_ARG_INFO();
2724 static PHP_METHOD(pqconn, escapeBytea) {
2725 char *str;
2726 int len;
2727
2728 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
2729 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2730
2731 if (!obj->intern) {
2732 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2733 } else {
2734 size_t escaped_len;
2735 char *escaped_str = (char *) PQescapeByteaConn(obj->intern->conn, (unsigned char *) str, len, &escaped_len);
2736
2737 if (!escaped_str) {
2738 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to escape bytea (%s)", PHP_PQerrorMessage(obj->intern->conn));
2739 RETVAL_FALSE;
2740 } else {
2741 RETVAL_STRINGL(escaped_str, escaped_len - 1, 1);
2742 PQfreemem(escaped_str);
2743 }
2744 }
2745 }
2746 }
2747
2748 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unescape_bytea, 0, 0, 1)
2749 ZEND_ARG_INFO(0, bytea)
2750 ZEND_END_ARG_INFO();
2751 static PHP_METHOD(pqconn, unescapeBytea) {
2752 char *str;
2753 int len;
2754
2755 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
2756 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2757
2758 if (!obj->intern) {
2759 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2760 } else {
2761 size_t unescaped_len;
2762 char *unescaped_str = (char *) PQunescapeBytea((unsigned char *)str, &unescaped_len);
2763
2764 if (!unescaped_str) {
2765 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to unescape bytea (%s)", PHP_PQerrorMessage(obj->intern->conn));
2766 RETVAL_FALSE;
2767 } else {
2768 RETVAL_STRINGL(unescaped_str, unescaped_len, 1);
2769 PQfreemem(unescaped_str);
2770 }
2771 }
2772 }
2773 }
2774
2775 static const char *isolation_level(long *isolation) {
2776 switch (*isolation) {
2777 case PHP_PQTXN_SERIALIZABLE:
2778 return "SERIALIZABLE";
2779 case PHP_PQTXN_REPEATABLE_READ:
2780 return "REPEATABLE READ";
2781 default:
2782 *isolation = PHP_PQTXN_READ_COMMITTED;
2783 /* no break */
2784 case PHP_PQTXN_READ_COMMITTED:
2785 return "READ COMMITTED";
2786 }
2787 }
2788
2789 static STATUS php_pqconn_start_transaction(zval *zconn, php_pqconn_object_t *conn_obj, long isolation, zend_bool readonly, zend_bool deferrable TSRMLS_DC)
2790 {
2791 STATUS rv = FAILURE;
2792
2793 if (!conn_obj) {
2794 conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
2795 }
2796
2797 if (!conn_obj->intern) {
2798 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2799 } else {
2800 PGresult *res;
2801 smart_str cmd = {0};
2802 const char *il = isolation_level(&isolation);
2803
2804 smart_str_appends(&cmd, "START TRANSACTION ISOLATION LEVEL ");
2805 smart_str_appends(&cmd, il);
2806 smart_str_appends(&cmd, ", READ ");
2807 smart_str_appends(&cmd, readonly ? "ONLY" : "WRITE");
2808 smart_str_appends(&cmd, ",");
2809 smart_str_appends(&cmd, deferrable ? "" : " NOT");
2810 smart_str_appends(&cmd, " DEFERRABLE");
2811 smart_str_0(&cmd);
2812
2813 res = PQexec(conn_obj->intern->conn, cmd.c);
2814
2815 if (!res) {
2816 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj->intern->conn));
2817 } else {
2818 rv = php_pqres_success(res TSRMLS_CC);
2819 PHP_PQclear(res);
2820 php_pqconn_notify_listeners(conn_obj TSRMLS_CC);
2821 }
2822
2823 smart_str_free(&cmd);
2824 }
2825
2826 return rv;
2827 }
2828
2829 static STATUS php_pqconn_start_transaction_async(zval *zconn, php_pqconn_object_t *conn_obj, long isolation, zend_bool readonly, zend_bool deferrable TSRMLS_DC)
2830 {
2831 STATUS rv = FAILURE;
2832
2833 if (!conn_obj) {
2834 conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
2835 }
2836
2837 if (!conn_obj->intern) {
2838 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2839 } else {
2840 smart_str cmd = {0};
2841 const char *il = isolation_level(&isolation);
2842
2843 smart_str_appends(&cmd, "START TRANSACTION ISOLATION LEVEL ");
2844 smart_str_appends(&cmd, il);
2845 smart_str_appends(&cmd, ", READ ");
2846 smart_str_appends(&cmd, readonly ? "ONLY" : "WRITE");
2847 smart_str_appends(&cmd, ",");
2848 smart_str_appends(&cmd, deferrable ? "" : "NOT ");
2849 smart_str_appends(&cmd, " DEFERRABLE");
2850 smart_str_0(&cmd);
2851
2852 if (!PQsendQuery(conn_obj->intern->conn, cmd.c)) {
2853 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj->intern->conn));
2854 } else {
2855 rv = SUCCESS;
2856 conn_obj->intern->poller = PQconsumeInput;
2857 php_pqconn_notify_listeners(conn_obj TSRMLS_CC);
2858 }
2859
2860 smart_str_free(&cmd);
2861 }
2862
2863 return rv;
2864 }
2865
2866 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction, 0, 0, 0)
2867 ZEND_ARG_INFO(0, isolation)
2868 ZEND_ARG_INFO(0, readonly)
2869 ZEND_ARG_INFO(0, deferrable)
2870 ZEND_END_ARG_INFO();
2871 static PHP_METHOD(pqconn, startTransaction) {
2872 zend_error_handling zeh;
2873 long isolation = PHP_PQTXN_READ_COMMITTED;
2874 zend_bool readonly = 0, deferrable = 0;
2875 STATUS rv;
2876
2877 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
2878 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable);
2879 zend_restore_error_handling(&zeh TSRMLS_CC);
2880
2881 if (SUCCESS == rv) {
2882 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2883
2884 rv = php_pqconn_start_transaction(getThis(), obj, isolation, readonly, deferrable TSRMLS_CC);
2885
2886 if (SUCCESS == rv) {
2887 php_pqtxn_t *txn = ecalloc(1, sizeof(*txn));
2888
2889 php_pq_object_addref(obj TSRMLS_CC);
2890 txn->conn = obj;
2891 txn->open = 1;
2892 txn->isolation = isolation;
2893 txn->readonly = readonly;
2894 txn->deferrable = deferrable;
2895
2896 return_value->type = IS_OBJECT;
2897 return_value->value.obj = php_pqtxn_create_object_ex(php_pqtxn_class_entry, txn, NULL TSRMLS_CC);
2898 }
2899 }
2900 }
2901
2902
2903 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction_async, 0, 0, 0)
2904 ZEND_ARG_INFO(0, isolation)
2905 ZEND_ARG_INFO(0, readonly)
2906 ZEND_ARG_INFO(0, deferrable)
2907 ZEND_END_ARG_INFO();
2908 static PHP_METHOD(pqconn, startTransactionAsync) {
2909 zend_error_handling zeh;
2910 long isolation = PHP_PQTXN_READ_COMMITTED;
2911 zend_bool readonly = 0, deferrable = 0;
2912 STATUS rv;
2913
2914 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
2915 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable);
2916 zend_restore_error_handling(&zeh TSRMLS_CC);
2917 if (SUCCESS == rv) {
2918 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2919
2920 rv = php_pqconn_start_transaction_async(getThis(), obj, isolation, readonly, deferrable TSRMLS_CC);
2921
2922 if (SUCCESS == rv) {
2923 php_pqtxn_t *txn = ecalloc(1, sizeof(*txn));
2924
2925 php_pq_object_addref(obj TSRMLS_CC);
2926 txn->conn = obj;
2927 txn->isolation = isolation;
2928 txn->readonly = readonly;
2929 txn->deferrable = deferrable;
2930
2931 return_value->type = IS_OBJECT;
2932 return_value->value.obj = php_pqtxn_create_object_ex(php_pqtxn_class_entry, txn, NULL TSRMLS_CC);
2933 }
2934 }
2935 }
2936
2937 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_trace, 0, 0, 0)
2938 ZEND_ARG_INFO(0, stdio_stream)
2939 ZEND_END_ARG_INFO();
2940 static PHP_METHOD(pqconn, trace) {
2941 zval *zstream = NULL;
2942
2943 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r!", &zstream)) {
2944 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2945
2946 if (!obj->intern) {
2947 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
2948 } else {
2949 if (!zstream) {
2950 PQuntrace(obj->intern->conn);
2951 RETVAL_TRUE;
2952 } else {
2953 FILE *fp;
2954 php_stream *stream = NULL;
2955
2956 php_stream_from_zval(stream, &zstream);
2957
2958 if (SUCCESS != php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
2959 RETVAL_FALSE;
2960 } else {
2961 stream->flags |= PHP_STREAM_FLAG_NO_CLOSE;
2962 PQtrace(obj->intern->conn, fp);
2963 RETVAL_TRUE;
2964 }
2965 }
2966 }
2967 }
2968 }
2969
2970 static zend_function_entry php_pqconn_methods[] = {
2971 PHP_ME(pqconn, __construct, ai_pqconn_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
2972 PHP_ME(pqconn, reset, ai_pqconn_reset, ZEND_ACC_PUBLIC)
2973 PHP_ME(pqconn, resetAsync, ai_pqconn_reset_async, ZEND_ACC_PUBLIC)
2974 PHP_ME(pqconn, poll, ai_pqconn_poll, ZEND_ACC_PUBLIC)
2975 PHP_ME(pqconn, exec, ai_pqconn_exec, ZEND_ACC_PUBLIC)
2976 PHP_ME(pqconn, execAsync, ai_pqconn_exec_async, ZEND_ACC_PUBLIC)
2977 PHP_ME(pqconn, execParams, ai_pqconn_exec_params, ZEND_ACC_PUBLIC)
2978 PHP_ME(pqconn, execParamsAsync, ai_pqconn_exec_params_async, ZEND_ACC_PUBLIC)
2979 PHP_ME(pqconn, prepare, ai_pqconn_prepare, ZEND_ACC_PUBLIC)
2980 PHP_ME(pqconn, prepareAsync, ai_pqconn_prepare_async, ZEND_ACC_PUBLIC)
2981 PHP_ME(pqconn, listen, ai_pqconn_listen, ZEND_ACC_PUBLIC)
2982 PHP_ME(pqconn, listenAsync, ai_pqconn_listen_async, ZEND_ACC_PUBLIC)
2983 PHP_ME(pqconn, notify, ai_pqconn_notify, ZEND_ACC_PUBLIC)
2984 PHP_ME(pqconn, notifyAsync, ai_pqconn_notify_async, ZEND_ACC_PUBLIC)
2985 PHP_ME(pqconn, getResult, ai_pqconn_get_result, ZEND_ACC_PUBLIC)
2986 PHP_ME(pqconn, quote, ai_pqconn_quote, ZEND_ACC_PUBLIC)
2987 PHP_ME(pqconn, quoteName, ai_pqconn_quote_name, ZEND_ACC_PUBLIC)
2988 PHP_ME(pqconn, escapeBytea, ai_pqconn_escape_bytea, ZEND_ACC_PUBLIC)
2989 PHP_ME(pqconn, unescapeBytea, ai_pqconn_unescape_bytea, ZEND_ACC_PUBLIC)
2990 PHP_ME(pqconn, startTransaction, ai_pqconn_start_transaction, ZEND_ACC_PUBLIC)
2991 PHP_ME(pqconn, startTransactionAsync, ai_pqconn_start_transaction_async, ZEND_ACC_PUBLIC)
2992 PHP_ME(pqconn, trace, ai_pqconn_trace, ZEND_ACC_PUBLIC)
2993 {0}
2994 };
2995
2996 ZEND_BEGIN_ARG_INFO_EX(ai_pqtypes_construct, 0, 0, 1)
2997 ZEND_ARG_OBJ_INFO(0, connection, pq\\Connection, 0)
2998 ZEND_ARG_ARRAY_INFO(0, namespaces, 1)
2999 ZEND_END_ARG_INFO();
3000 static PHP_METHOD(pqtypes, __construct) {
3001 zend_error_handling zeh;
3002 zval *zconn, *znsp = NULL;
3003 STATUS rv;
3004
3005 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
3006 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|a!", &zconn, php_pqconn_class_entry, &znsp);
3007 zend_restore_error_handling(&zeh TSRMLS_CC);
3008
3009 if (SUCCESS == rv) {
3010 php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
3011
3012 if (!conn_obj->intern) {
3013 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Connection not initialized");
3014 } else {
3015 php_pqtypes_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3016 zval *retval = NULL;
3017
3018 obj->intern = ecalloc(1, sizeof(*obj->intern));
3019 obj->intern->conn = conn_obj;
3020 php_pq_object_addref(conn_obj TSRMLS_CC);
3021 zend_hash_init(&obj->intern->types, 300, NULL, ZVAL_PTR_DTOR, 0);
3022
3023 zend_replace_error_handling(EH_THROW, exce(EX_RUNTIME), &zeh TSRMLS_CC);
3024 if (znsp) {
3025 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "refresh", &retval, znsp);
3026 } else {
3027 zend_call_method_with_0_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "refresh", &retval);
3028 }
3029 zend_restore_error_handling(&zeh TSRMLS_CC);
3030
3031 if (retval) {
3032 zval_ptr_dtor(&retval);
3033 }
3034 }
3035 }
3036 }
3037
3038 #define PHP_PQ_TYPES_QUERY \
3039 "select t.oid, t.* " \
3040 "from pg_type t join pg_namespace n on t.typnamespace=n.oid " \
3041 "where typisdefined " \
3042 "and typrelid=0"
3043 #define PHP_PQ_OID_TEXT 25
3044
3045 ZEND_BEGIN_ARG_INFO_EX(ai_pqtypes_refresh, 0, 0, 0)
3046 ZEND_ARG_ARRAY_INFO(0, namespaces, 1)
3047 ZEND_END_ARG_INFO();
3048 static PHP_METHOD(pqtypes, refresh) {
3049 HashTable *nsp = NULL;
3050 zend_error_handling zeh;
3051 STATUS rv;
3052
3053 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
3054 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|H/!", &nsp);
3055 zend_restore_error_handling(&zeh TSRMLS_CC);
3056
3057 if (SUCCESS == rv) {
3058 php_pqtypes_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3059
3060 if (!obj->intern) {
3061 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "pq\\Types not initialized");
3062 } else {
3063 PGresult *res;
3064
3065 if (!nsp || !zend_hash_num_elements(nsp)) {
3066 res = PQexec(obj->intern->conn->intern->conn, PHP_PQ_TYPES_QUERY " and nspname in ('public', 'pg_catalog')");
3067 } else {
3068 int i, count;
3069 Oid *oids;
3070 char **params = NULL;
3071 HashTable zdtor;
3072 smart_str str = {0};
3073
3074 smart_str_appends(&str, PHP_PQ_TYPES_QUERY " and nspname in(");
3075 zend_hash_init(&zdtor, 0, NULL, ZVAL_PTR_DTOR, 0);
3076 count = php_pq_params_to_array(nsp, &params, &zdtor TSRMLS_CC);
3077 oids = ecalloc(count + 1, sizeof(*oids));
3078 for (i = 0; i < count; ++i) {
3079 oids[i] = PHP_PQ_OID_TEXT;
3080 if (i) {
3081 smart_str_appendc(&str, ',');
3082 }
3083 smart_str_appendc(&str, '$');
3084 smart_str_append_unsigned(&str, i+1);
3085 }
3086 smart_str_appendc(&str, ')');
3087 smart_str_0(&str);
3088
3089 res = PQexecParams(obj->intern->conn->intern->conn, str.c, count, oids, (const char *const*) params, NULL, NULL, 0);
3090
3091 smart_str_free(&str);
3092 efree(oids);
3093 efree(params);
3094 zend_hash_destroy(&zdtor);
3095 }
3096
3097 if (!res) {
3098 zend_throw_exception_ex(exce(EX_RUNTIME), EX_RUNTIME TSRMLS_CC, "Failed to fetch types (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
3099 } else {
3100 if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
3101 int r, rows;
3102
3103 for (r = 0, rows = PQntuples(res); r < rows; ++r) {
3104 zval *row = php_pqres_row_to_zval(res, r, PHP_PQRES_FETCH_OBJECT, NULL TSRMLS_CC);
3105 long oid = atol(PQgetvalue(res, r, 0 ));
3106 char *name = PQgetvalue(res, r, 1);
3107
3108 Z_ADDREF_P(row);
3109
3110 zend_hash_index_update(&obj->intern->types, oid, (void *) &row, sizeof(zval *), NULL);
3111 zend_hash_add(&obj->intern->types, name, strlen(name) + 1, (void *) &row, sizeof(zval *), NULL);
3112 }
3113 }
3114
3115 PHP_PQclear(res);
3116 php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
3117 }
3118 }
3119 }
3120 }
3121
3122 static zend_function_entry php_pqtypes_methods[] = {
3123 PHP_ME(pqtypes, __construct, ai_pqtypes_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
3124 PHP_ME(pqtypes, refresh, ai_pqtypes_refresh, ZEND_ACC_PUBLIC)
3125 {0}
3126 };
3127
3128 static STATUS php_pqres_iteration(zval *this_ptr, php_pqres_object_t *obj, php_pqres_fetch_t fetch_type, zval ***row TSRMLS_DC)
3129 {
3130 STATUS rv;
3131 php_pqres_fetch_t orig_fetch;
3132
3133 if (!obj) {
3134 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3135 }
3136
3137 if (!obj->intern->iter) {
3138 obj->intern->iter = (php_pqres_iterator_t *) php_pqres_iterator_init(Z_OBJCE_P(getThis()), getThis(), 0 TSRMLS_CC);
3139 obj->intern->iter->zi.funcs->rewind((zend_object_iterator *) obj->intern->iter TSRMLS_CC);
3140 }
3141 orig_fetch = obj->intern->iter->fetch_type;
3142 obj->intern->iter->fetch_type = fetch_type;
3143 if (SUCCESS == (rv = obj->intern->iter->zi.funcs->valid((zend_object_iterator *) obj->intern->iter TSRMLS_CC))) {
3144 obj->intern->iter->zi.funcs->get_current_data((zend_object_iterator *) obj->intern->iter, row TSRMLS_CC);
3145 obj->intern->iter->zi.funcs->move_forward((zend_object_iterator *) obj->intern->iter TSRMLS_CC);
3146 }
3147 obj->intern->iter->fetch_type = orig_fetch;
3148
3149 return rv;
3150 }
3151
3152 typedef struct php_pqres_col {
3153 char *name;
3154 int num;
3155 } php_pqres_col_t;
3156
3157 static STATUS column_nn(php_pqres_object_t *obj, zval *zcol, php_pqres_col_t *col TSRMLS_DC)
3158 {
3159 long index = -1;
3160 char *name = NULL;
3161
3162 switch (Z_TYPE_P(zcol)) {
3163 default:
3164 convert_to_string(zcol);
3165 /* no break */
3166
3167 case IS_STRING:
3168 if (!is_numeric_string(Z_STRVAL_P(zcol), Z_STRLEN_P(zcol), &index, NULL, 0)) {
3169 name = Z_STRVAL_P(zcol);
3170 }
3171 break;
3172
3173 case IS_LONG:
3174 index = Z_LVAL_P(zcol);
3175 break;
3176 }
3177
3178 if (name) {
3179 col->name = name;
3180 col->num = PQfnumber(obj->intern->res, name);
3181 } else {
3182 col->name = PQfname(obj->intern->res, index);
3183 col->num = index;
3184 }
3185
3186 if (!col->name) {
3187 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to find column at index %ld", index);
3188 return FAILURE;
3189 }
3190 if (col->num == -1) {
3191 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to find column with name '%s'", name);
3192 return FAILURE;
3193 }
3194 return SUCCESS;
3195 }
3196
3197 static int compare_index(const void *lptr, const void *rptr TSRMLS_DC)
3198 {
3199 const Bucket *l = *(const Bucket **) lptr;
3200 const Bucket *r = *(const Bucket **) rptr;
3201
3202 if (l->h < r->h) {
3203 return -1;