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