4e7275a5a6adb31e8511c5a3c5fd93e87d78c3bd
[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 void php_pqconn_object_free(void *o TSRMLS_DC)
526 {
527 php_pqconn_object_t *obj = o;
528
529 if (obj->intern) {
530 php_pqconn_event_data_t *evdata = PQinstanceData(obj->intern->conn, php_pqconn_event);
531
532 if (evdata) {
533 efree(evdata);
534 PQsetInstanceData(obj->intern->conn, php_pqconn_event, NULL);
535 }
536
537 php_resource_factory_handle_dtor(&obj->intern->factory, obj->intern->conn TSRMLS_CC);
538 php_resource_factory_dtor(&obj->intern->factory);
539 php_pq_callback_dtor(&obj->intern->onevent);
540 zend_hash_destroy(&obj->intern->listeners);
541 zend_hash_destroy(&obj->intern->eventhandlers);
542 efree(obj->intern);
543 obj->intern = NULL;
544 }
545 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
546 efree(obj);
547 }
548
549 static void php_pqtypes_object_free(void *o TSRMLS_DC)
550 {
551 php_pqtypes_object_t *obj = o;
552
553 if (obj->intern) {
554 zend_hash_destroy(&obj->intern->types);
555 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
556 efree(obj->intern);
557 obj->intern = NULL;
558 }
559 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
560 efree(obj);
561 }
562
563 static void php_pqres_object_free(void *o TSRMLS_DC)
564 {
565 php_pqres_object_t *obj = o;
566
567 if (obj->intern) {
568 if (obj->intern->res) {
569 zval *res = PQresultInstanceData(obj->intern->res, php_pqconn_event);
570 if (res) {
571 if (1 == Z_REFCOUNT_P(res)) {
572 PQresultSetInstanceData(obj->intern->res, php_pqconn_event, NULL);
573 }
574 zval_ptr_dtor(&res);
575 } else {
576 PQclear(obj->intern->res);
577 obj->intern->res = NULL;
578 }
579 }
580
581 if (obj->intern->iter) {
582 php_pqres_iterator_dtor((zend_object_iterator *) obj->intern->iter TSRMLS_CC);
583 obj->intern->iter = NULL;
584 }
585
586 zend_hash_destroy(&obj->intern->bound);
587
588 efree(obj->intern);
589 obj->intern = NULL;
590 }
591 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
592 efree(obj);
593 }
594
595 static void php_pqstm_object_free(void *o TSRMLS_DC)
596 {
597 php_pqstm_object_t *obj = o;
598
599 if (obj->intern) {
600 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
601 efree(obj->intern->name);
602 zend_hash_destroy(&obj->intern->bound);
603 efree(obj->intern);
604 obj->intern = NULL;
605 }
606 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
607 efree(obj);
608 }
609
610 static void php_pqtxn_object_free(void *o TSRMLS_DC)
611 {
612 php_pqtxn_object_t *obj = o;
613
614 if (obj->intern) {
615 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
616 efree(obj->intern);
617 obj->intern = NULL;
618 }
619 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
620 efree(obj);
621 }
622
623 static void php_pqcancel_object_free(void *o TSRMLS_DC)
624 {
625 php_pqcancel_object_t *obj = o;
626
627 if (obj->intern) {
628 PQfreeCancel(obj->intern->cancel);
629 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
630 efree(obj->intern);
631 obj->intern = NULL;
632 }
633 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
634 efree(obj);
635 }
636
637 static void php_pqevent_object_free(void *o TSRMLS_DC)
638 {
639 php_pqevent_object_t *obj = o;
640
641 if (obj->intern) {
642 php_pq_callback_dtor(&obj->intern->cb);
643 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
644 efree(obj->intern->type);
645 efree(obj->intern);
646 obj->intern = NULL;
647 }
648 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
649 efree(obj);
650 }
651
652 static void php_pqlob_object_free(void *o TSRMLS_DC)
653 {
654 php_pqlob_object_t *obj = o;
655
656 if (obj->intern) {
657 if (obj->intern->lofd) {
658 lo_close(obj->intern->txn->intern->conn->intern->conn, obj->intern->lofd);
659 }
660 php_pq_object_delref(obj->intern->txn TSRMLS_CC);
661 efree(obj->intern);
662 obj->intern = NULL;
663 }
664 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
665 efree(obj);
666 }
667
668 static void php_pqcopy_object_free(void *o TSRMLS_DC)
669 {
670 php_pqcopy_object_t *obj = o;
671
672 if (obj->intern) {
673 efree(obj->intern->expression);
674 efree(obj->intern->options);
675 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
676 efree(obj->intern);
677 obj->intern = NULL;
678 }
679 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
680 efree(obj);
681 }
682
683 static zend_object_value php_pqconn_create_object_ex(zend_class_entry *ce, php_pqconn_t *intern, php_pqconn_object_t **ptr TSRMLS_DC)
684 {
685 php_pqconn_object_t *o;
686
687 o = ecalloc(1, sizeof(*o));
688 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
689 object_properties_init((zend_object *) o, ce);
690 o->prophandler = &php_pqconn_object_prophandlers;
691
692 if (ptr) {
693 *ptr = o;
694 }
695
696 if (intern) {
697 o->intern = intern;
698 }
699
700 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqconn_object_free, NULL TSRMLS_CC);
701 o->zv.handlers = &php_pqconn_object_handlers;
702
703 return o->zv;
704 }
705
706 static zend_object_value php_pqtypes_create_object_ex(zend_class_entry *ce, php_pqtypes_t *intern, php_pqtypes_object_t **ptr TSRMLS_DC)
707 {
708 php_pqtypes_object_t *o;
709
710 o = ecalloc(1, sizeof(*o));
711 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
712 object_properties_init((zend_object *) o, ce);
713 o->prophandler = &php_pqtypes_object_prophandlers;
714
715 if (ptr) {
716 *ptr = o;
717 }
718
719 if (intern) {
720 o->intern = intern;
721 }
722
723 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqtypes_object_free, NULL TSRMLS_CC);
724 o->zv.handlers = &php_pqtypes_object_handlers;
725
726 return o->zv;
727 }
728
729 static zend_object_value php_pqres_create_object_ex(zend_class_entry *ce, php_pqres_t *intern, php_pqres_object_t **ptr TSRMLS_DC)
730 {
731 php_pqres_object_t *o;
732
733 o = ecalloc(1, sizeof(*o));
734 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
735 object_properties_init((zend_object *) o, ce);
736 o->prophandler = &php_pqres_object_prophandlers;
737
738 if (ptr) {
739 *ptr = o;
740 }
741
742 if (intern) {
743 o->intern = intern;
744 }
745
746 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqres_object_free, NULL TSRMLS_CC);
747 o->zv.handlers = &php_pqres_object_handlers;
748
749 return o->zv;
750 }
751
752 static zend_object_value php_pqstm_create_object_ex(zend_class_entry *ce, php_pqstm_t *intern, php_pqstm_object_t **ptr TSRMLS_DC)
753 {
754 php_pqstm_object_t *o;
755
756 o = ecalloc(1, sizeof(*o));
757 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
758 object_properties_init((zend_object *) o, ce);
759 o->prophandler = &php_pqstm_object_prophandlers;
760
761 if (ptr) {
762 *ptr = o;
763 }
764
765 if (intern) {
766 o->intern = intern;
767 }
768
769 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqstm_object_free, NULL TSRMLS_CC);
770 o->zv.handlers = &php_pqstm_object_handlers;
771
772 return o->zv;
773 }
774
775 static zend_object_value php_pqtxn_create_object_ex(zend_class_entry *ce, php_pqtxn_t *intern, php_pqtxn_object_t **ptr TSRMLS_DC)
776 {
777 php_pqtxn_object_t *o;
778
779 o = ecalloc(1, sizeof(*o));
780 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
781 object_properties_init((zend_object *) o, ce);
782 o->prophandler = &php_pqtxn_object_prophandlers;
783
784 if (ptr) {
785 *ptr = o;
786 }
787
788 if (intern) {
789 o->intern = intern;
790 }
791
792 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqtxn_object_free, NULL TSRMLS_CC);
793 o->zv.handlers = &php_pqtxn_object_handlers;
794
795 return o->zv;
796 }
797
798 static zend_object_value php_pqcancel_create_object_ex(zend_class_entry *ce, php_pqcancel_t *intern, php_pqcancel_object_t **ptr TSRMLS_DC)
799 {
800 php_pqcancel_object_t *o;
801
802 o = ecalloc(1, sizeof(*o));
803 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
804 object_properties_init((zend_object *) o, ce);
805 o->prophandler = &php_pqcancel_object_prophandlers;
806
807 if (ptr) {
808 *ptr = o;
809 }
810
811 if (intern) {
812 o->intern = intern;
813 }
814
815 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqcancel_object_free, NULL TSRMLS_CC);
816 o->zv.handlers = &php_pqcancel_object_handlers;
817
818 return o->zv;
819 }
820
821 static zend_object_value php_pqevent_create_object_ex(zend_class_entry *ce, php_pqevent_t *intern, php_pqevent_object_t **ptr TSRMLS_DC)
822 {
823 php_pqevent_object_t *o;
824
825 o = ecalloc(1, sizeof(*o));
826 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
827 object_properties_init((zend_object *) o, ce);
828 o->prophandler = &php_pqevent_object_prophandlers;
829
830 if (ptr) {
831 *ptr = o;
832 }
833
834 if (intern) {
835 o->intern = intern;
836 }
837
838 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqevent_object_free, NULL TSRMLS_CC);
839 o->zv.handlers = &php_pqevent_object_handlers;
840
841 return o->zv;
842 }
843
844 static zend_object_value php_pqlob_create_object_ex(zend_class_entry *ce, php_pqlob_t *intern, php_pqlob_object_t **ptr TSRMLS_DC)
845 {
846 php_pqlob_object_t *o;
847
848 o = ecalloc(1, sizeof(*o));
849 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
850 object_properties_init((zend_object *) o, ce);
851 o->prophandler = &php_pqlob_object_prophandlers;
852
853 if (ptr) {
854 *ptr = o;
855 }
856
857 if (intern) {
858 o->intern = intern;
859 }
860
861 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqlob_object_free, NULL TSRMLS_CC);
862 o->zv.handlers = &php_pqlob_object_handlers;
863
864 return o->zv;
865 }
866
867 static zend_object_value php_pqcopy_create_object_ex(zend_class_entry *ce, php_pqcopy_t *intern, php_pqcopy_object_t **ptr TSRMLS_DC)
868 {
869 php_pqcopy_object_t *o;
870
871 o = ecalloc(1, sizeof(*o));
872 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
873 object_properties_init((zend_object *) o, ce);
874 o->prophandler = &php_pqcopy_object_prophandlers;
875
876 if (ptr) {
877 *ptr = o;
878 }
879
880 if (intern) {
881 o->intern = intern;
882 }
883
884 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqcopy_object_free, NULL TSRMLS_CC);
885 o->zv.handlers = &php_pqcopy_object_handlers;
886
887 return o->zv;
888 }
889
890 static zend_object_value php_pqconn_create_object(zend_class_entry *class_type TSRMLS_DC)
891 {
892 return php_pqconn_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
893 }
894
895 static zend_object_value php_pqtypes_create_object(zend_class_entry *class_type TSRMLS_DC)
896 {
897 return php_pqtypes_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
898 }
899
900 static zend_object_value php_pqres_create_object(zend_class_entry *class_type TSRMLS_DC)
901 {
902 return php_pqres_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
903 }
904
905 static zend_object_value php_pqstm_create_object(zend_class_entry *class_type TSRMLS_DC)
906 {
907 return php_pqstm_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
908 }
909
910 static zend_object_value php_pqtxn_create_object(zend_class_entry *class_type TSRMLS_DC)
911 {
912 return php_pqtxn_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
913 }
914
915 static zend_object_value php_pqcancel_create_object(zend_class_entry *class_type TSRMLS_DC)
916 {
917 return php_pqcancel_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
918 }
919
920 static zend_object_value php_pqevent_create_object(zend_class_entry *class_type TSRMLS_DC)
921 {
922 return php_pqevent_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
923 }
924
925 static zend_object_value php_pqlob_create_object(zend_class_entry *class_type TSRMLS_DC)
926 {
927 return php_pqlob_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
928 }
929
930 static zend_object_value php_pqcopy_create_object(zend_class_entry *class_type TSRMLS_DC)
931 {
932 return php_pqcopy_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
933 }
934
935 static int apply_ph_to_debug(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
936 {
937 php_pq_object_prophandler_t *ph = p;
938 HashTable *ht = va_arg(argv, HashTable *);
939 zval **return_value, *object = va_arg(argv, zval *);
940 php_pq_object_t *obj = va_arg(argv, php_pq_object_t *);
941
942 if (SUCCESS == zend_hash_find(ht, key->arKey, key->nKeyLength, (void *) &return_value)) {
943
944 if (ph->read) {
945 zval_ptr_dtor(return_value);
946 MAKE_STD_ZVAL(*return_value);
947 ZVAL_NULL(*return_value);
948
949 ph->read(object, obj, *return_value TSRMLS_CC);
950 }
951 }
952
953 return ZEND_HASH_APPLY_KEEP;
954 }
955
956 static int apply_pi_to_debug(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
957 {
958 zend_property_info *pi = p;
959 HashTable *ht = va_arg(argv, HashTable *);
960 zval *object = va_arg(argv, zval *);
961 php_pq_object_t *obj = va_arg(argv, php_pq_object_t *);
962 zval *property = zend_read_property(obj->zo.ce, object, pi->name, pi->name_length, 0 TSRMLS_CC);
963
964 if (1||!Z_REFCOUNT_P(property)) {
965 Z_ADDREF_P(property);
966 }
967 zend_hash_add(ht, pi->name, pi->name_length + 1, (void *) &property, sizeof(zval *), NULL);
968
969 return ZEND_HASH_APPLY_KEEP;
970 }
971
972 static HashTable *php_pq_object_debug_info(zval *object, int *temp TSRMLS_DC)
973 {
974 HashTable *ht;
975 php_pq_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
976
977 *temp = 1;
978 ALLOC_HASHTABLE(ht);
979 ZEND_INIT_SYMTABLE(ht);
980
981 zend_hash_apply_with_arguments(&obj->zo.ce->properties_info TSRMLS_CC, apply_pi_to_debug, 3, ht, object, obj);
982 zend_hash_apply_with_arguments(obj->prophandler TSRMLS_CC, apply_ph_to_debug, 3, ht, object, obj);
983
984 return ht;
985 }
986
987 static void php_pqconn_object_read_status(zval *object, void *o, zval *return_value TSRMLS_DC)
988 {
989 php_pqconn_object_t *obj = o;
990
991 RETVAL_LONG(PQstatus(obj->intern->conn));
992 }
993
994 static void php_pqconn_object_read_transaction_status(zval *object, void *o, zval *return_value TSRMLS_DC)
995 {
996 php_pqconn_object_t *obj = o;
997
998 RETVAL_LONG(PQtransactionStatus(obj->intern->conn));
999 }
1000
1001 static void php_pqconn_object_read_error_message(zval *object, void *o, zval *return_value TSRMLS_DC)
1002 {
1003 php_pqconn_object_t *obj = o;
1004 char *error = PHP_PQerrorMessage(obj->intern->conn);
1005
1006 if (error) {
1007 RETVAL_STRING(error, 1);
1008 } else {
1009 RETVAL_NULL();
1010 }
1011 }
1012
1013 static int apply_notify_listener(void *p, void *arg TSRMLS_DC)
1014 {
1015 php_pq_callback_t *listener = p;
1016 PGnotify *nfy = arg;
1017 zval *zpid, *zchannel, *zmessage;
1018
1019 MAKE_STD_ZVAL(zpid);
1020 ZVAL_LONG(zpid, nfy->be_pid);
1021 MAKE_STD_ZVAL(zchannel);
1022 ZVAL_STRING(zchannel, nfy->relname, 1);
1023 MAKE_STD_ZVAL(zmessage);
1024 ZVAL_STRING(zmessage, nfy->extra, 1);
1025
1026 zend_fcall_info_argn(&listener->fci TSRMLS_CC, 3, &zchannel, &zmessage, &zpid);
1027 zend_fcall_info_call(&listener->fci, &listener->fcc, NULL, NULL TSRMLS_CC);
1028
1029 zval_ptr_dtor(&zchannel);
1030 zval_ptr_dtor(&zmessage);
1031 zval_ptr_dtor(&zpid);
1032
1033 return ZEND_HASH_APPLY_KEEP;
1034 }
1035
1036 static int apply_notify_listeners(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
1037 {
1038 HashTable *listeners = p;
1039 PGnotify *nfy = va_arg(argv, PGnotify *);
1040
1041 if (0 == fnmatch(key->arKey, nfy->relname, 0)) {
1042 zend_hash_apply_with_argument(listeners, apply_notify_listener, nfy TSRMLS_CC);
1043 }
1044
1045 return ZEND_HASH_APPLY_KEEP;
1046 }
1047
1048 static void php_pqconn_notify_listeners(php_pqconn_object_t *obj TSRMLS_DC)
1049 {
1050 PGnotify *nfy;
1051
1052 while ((nfy = PQnotifies(obj->intern->conn))) {
1053 zend_hash_apply_with_arguments(&obj->intern->listeners TSRMLS_CC, apply_notify_listeners, 1, nfy);
1054 PQfreemem(nfy);
1055 }
1056 }
1057
1058 static void php_pqconn_object_read_busy(zval *object, void *o, zval *return_value TSRMLS_DC)
1059 {
1060 php_pqconn_object_t *obj = o;
1061
1062 RETVAL_BOOL(PQisBusy(obj->intern->conn));
1063 }
1064
1065 static void php_pqconn_object_read_encoding(zval *object, void *o, zval *return_value TSRMLS_DC)
1066 {
1067 php_pqconn_object_t *obj = o;
1068
1069 RETVAL_STRING(pg_encoding_to_char(PQclientEncoding(obj->intern->conn)), 1);
1070 }
1071
1072 static void php_pqconn_object_write_encoding(zval *object, void *o, zval *value TSRMLS_DC)
1073 {
1074 php_pqconn_object_t *obj = o;
1075 zval *zenc = value;
1076
1077 if (Z_TYPE_P(value) != IS_STRING) {
1078 convert_to_string_ex(&zenc);
1079 }
1080
1081 if (0 > PQsetClientEncoding(obj->intern->conn, Z_STRVAL_P(zenc))) {
1082 zend_error(E_NOTICE, "Unrecognized encoding '%s'", Z_STRVAL_P(zenc));
1083 }
1084
1085 if (zenc != value) {
1086 zval_ptr_dtor(&zenc);
1087 }
1088 }
1089
1090 static void php_pqconn_object_read_unbuffered(zval *object, void *o, zval *return_value TSRMLS_DC)
1091 {
1092 php_pqconn_object_t *obj = o;
1093
1094 RETVAL_BOOL(obj->intern->unbuffered);
1095 }
1096
1097 static void php_pqconn_object_write_unbuffered(zval *object, void *o, zval *value TSRMLS_DC)
1098 {
1099 php_pqconn_object_t *obj = o;
1100
1101 obj->intern->unbuffered = zend_is_true(value);
1102 }
1103
1104 static void php_pqconn_object_read_db(zval *objec, void *o, zval *return_value TSRMLS_DC)
1105 {
1106 php_pqconn_object_t *obj = o;
1107 char *db = PQdb(obj->intern->conn);
1108
1109 if (db) {
1110 RETVAL_STRING(db, 1);
1111 } else {
1112 RETVAL_EMPTY_STRING();
1113 }
1114 }
1115
1116 static void php_pqconn_object_read_user(zval *objec, void *o, zval *return_value TSRMLS_DC)
1117 {
1118 php_pqconn_object_t *obj = o;
1119 char *user = PQuser(obj->intern->conn);
1120
1121 if (user) {
1122 RETVAL_STRING(user, 1);
1123 } else {
1124 RETVAL_EMPTY_STRING();
1125 }
1126 }
1127
1128 static void php_pqconn_object_read_pass(zval *objec, void *o, zval *return_value TSRMLS_DC)
1129 {
1130 php_pqconn_object_t *obj = o;
1131 char *pass = PQpass(obj->intern->conn);
1132
1133 if (pass) {
1134 RETVAL_STRING(pass, 1);
1135 } else {
1136 RETVAL_EMPTY_STRING();
1137 }
1138 }
1139
1140 static void php_pqconn_object_read_host(zval *objec, void *o, zval *return_value TSRMLS_DC)
1141 {
1142 php_pqconn_object_t *obj = o;
1143 char *host = PQhost(obj->intern->conn);
1144
1145 if (host) {
1146 RETVAL_STRING(host, 1);
1147 } else {
1148 RETVAL_EMPTY_STRING();
1149 }
1150 }
1151
1152 static void php_pqconn_object_read_port(zval *objec, void *o, zval *return_value TSRMLS_DC)
1153 {
1154 php_pqconn_object_t *obj = o;
1155 char *port = PQport(obj->intern->conn);
1156
1157 if (port) {
1158 RETVAL_STRING(port, 1);
1159 } else {
1160 RETVAL_EMPTY_STRING();
1161 }
1162 }
1163
1164 static void php_pqconn_object_read_options(zval *objec, void *o, zval *return_value TSRMLS_DC)
1165 {
1166 php_pqconn_object_t *obj = o;
1167 char *options = PQoptions(obj->intern->conn);
1168
1169 if (options) {
1170 RETVAL_STRING(options, 1);
1171 } else {
1172 RETVAL_EMPTY_STRING();
1173 }
1174 }
1175
1176 static void php_pqtypes_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
1177 {
1178 php_pqtypes_object_t *obj = o;
1179
1180 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
1181 }
1182
1183 static int has_dimension(HashTable *ht, zval *member, char **key_str, int *key_len, long *index TSRMLS_DC)
1184 {
1185 long lval = 0;
1186 zval *tmp = member;
1187
1188 switch (Z_TYPE_P(member)) {
1189 default:
1190 convert_to_string_ex(&tmp);
1191 /* no break */
1192 case IS_STRING:
1193 if (!is_numeric_string(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp), &lval, NULL, 0)) {
1194 if (member != tmp) {
1195 zval_ptr_dtor(&tmp);
1196 }
1197 if (key_str) {
1198 *key_str = estrndup(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
1199 if (key_len) {
1200 *key_len = Z_STRLEN_P(tmp) + 1;
1201 }
1202 }
1203 return zend_hash_exists(ht, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp) + 1);
1204 }
1205 /* no break */
1206 case IS_LONG:
1207 lval = Z_LVAL_P(member);
1208 break;
1209 }
1210
1211 if (member != tmp) {
1212 zval_ptr_dtor(&tmp);
1213 }
1214 if (index) {
1215 *index = lval;
1216 }
1217 return zend_hash_index_exists(ht, lval);
1218 }
1219
1220 static int php_pqtypes_object_has_dimension(zval *object, zval *member, int check_empty TSRMLS_DC)
1221 {
1222 php_pqtypes_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
1223 char *key_str = NULL;
1224 int key_len = 0;
1225 long index = 0;
1226
1227 if (check_empty) {
1228 if (has_dimension(&obj->intern->types, member, &key_str, &key_len, &index TSRMLS_CC)) {
1229 zval **data;
1230
1231 if (key_str && key_len) {
1232 if (SUCCESS == zend_hash_find(&obj->intern->types, key_str, key_len, (void *) &data)) {
1233 efree(key_str);
1234 return Z_TYPE_PP(data) != IS_NULL;
1235 }
1236 efree(key_str);
1237 } else {
1238 if (SUCCESS == zend_hash_index_find(&obj->intern->types, index, (void *) data)) {
1239 return Z_TYPE_PP(data) != IS_NULL;
1240 }
1241 }
1242 }
1243 } else {
1244 return has_dimension(&obj->intern->types, member, NULL, NULL, NULL TSRMLS_CC);
1245 }
1246
1247 return 0;
1248 }
1249
1250 static zval *php_pqtypes_object_read_dimension(zval *object, zval *member, int type TSRMLS_DC)
1251 {
1252 long index = 0;
1253 char *key_str = NULL;
1254 int key_len = 0;
1255 php_pqtypes_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
1256
1257 if (has_dimension(&obj->intern->types, member, &key_str, &key_len, &index TSRMLS_CC)) {
1258 zval **data;
1259
1260 if (key_str && key_len) {
1261 if (SUCCESS == zend_hash_find(&obj->intern->types, key_str, key_len, (void *) &data)) {
1262 efree(key_str);
1263 return *data;
1264 }
1265 } else {
1266 if (SUCCESS == zend_hash_index_find(&obj->intern->types, index, (void *) &data)) {
1267 return *data;
1268 }
1269 }
1270 }
1271
1272 return NULL;
1273 }
1274
1275 static void php_pqres_object_read_status(zval *object, void *o, zval *return_value TSRMLS_DC)
1276 {
1277 php_pqres_object_t *obj = o;
1278
1279 RETVAL_LONG(PQresultStatus(obj->intern->res));
1280 }
1281
1282 static void php_pqres_object_read_error_message(zval *object, void *o, zval *return_value TSRMLS_DC)
1283 {
1284 php_pqres_object_t *obj = o;
1285 char *error = PHP_PQresultErrorMessage(obj->intern->res);
1286
1287 if (error) {
1288 RETVAL_STRING(error, 1);
1289 } else {
1290 RETVAL_NULL();
1291 }
1292 }
1293
1294 static void php_pqres_object_read_num_rows(zval *object, void *o, zval *return_value TSRMLS_DC)
1295 {
1296 php_pqres_object_t *obj = o;
1297
1298 RETVAL_LONG(PQntuples(obj->intern->res));
1299 }
1300
1301 static void php_pqres_object_read_num_cols(zval *object, void *o, zval *return_value TSRMLS_DC)
1302 {
1303 php_pqres_object_t *obj = o;
1304
1305 RETVAL_LONG(PQnfields(obj->intern->res));
1306 }
1307
1308 static void php_pqres_object_read_affected_rows(zval *object, void *o, zval *return_value TSRMLS_DC)
1309 {
1310 php_pqres_object_t *obj = o;
1311
1312 RETVAL_LONG(atoi(PQcmdTuples(obj->intern->res)));
1313 }
1314
1315 static void php_pqres_object_read_fetch_type(zval *object, void *o, zval *return_value TSRMLS_DC)
1316 {
1317 php_pqres_object_t *obj = o;
1318
1319 if (obj->intern->iter) {
1320 RETVAL_LONG(obj->intern->iter->fetch_type);
1321 } else {
1322 RETVAL_LONG(PHP_PQRES_FETCH_ARRAY);
1323 }
1324 }
1325
1326 static void php_pqres_object_write_fetch_type(zval *object, void *o, zval *value TSRMLS_DC)
1327 {
1328 php_pqres_object_t *obj = o;
1329 zval *zfetch_type = value;
1330
1331 if (Z_TYPE_P(zfetch_type) != IS_LONG) {
1332 convert_to_long_ex(&zfetch_type);
1333 }
1334
1335 if (!obj->intern->iter) {
1336 obj->intern->iter = (php_pqres_iterator_t *) php_pqres_iterator_init(Z_OBJCE_P(object), object, 0 TSRMLS_CC);
1337 obj->intern->iter->zi.funcs->rewind((zend_object_iterator *) obj->intern->iter TSRMLS_CC);
1338 }
1339 obj->intern->iter->fetch_type = Z_LVAL_P(zfetch_type);
1340
1341 if (zfetch_type != value) {
1342 zval_ptr_dtor(&zfetch_type);
1343 }
1344 }
1345
1346 static void php_pqstm_object_read_name(zval *object, void *o, zval *return_value TSRMLS_DC)
1347 {
1348 php_pqstm_object_t *obj = o;
1349
1350 RETVAL_STRING(obj->intern->name, 1);
1351 }
1352
1353 static void php_pqstm_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
1354 {
1355 php_pqstm_object_t *obj = o;
1356
1357 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
1358 }
1359
1360 static void php_pqtxn_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
1361 {
1362 php_pqtxn_object_t *obj = o;
1363
1364 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
1365 }
1366
1367 static void php_pqtxn_object_read_isolation(zval *object, void *o, zval *return_value TSRMLS_DC)
1368 {
1369 php_pqtxn_object_t *obj = o;
1370
1371 RETVAL_LONG(obj->intern->isolation);
1372 }
1373
1374 static void php_pqtxn_object_read_readonly(zval *object, void *o, zval *return_value TSRMLS_DC)
1375 {
1376 php_pqtxn_object_t *obj = o;
1377
1378 RETVAL_LONG(obj->intern->readonly);
1379 }
1380
1381 static void php_pqtxn_object_read_deferrable(zval *object, void *o, zval *return_value TSRMLS_DC)
1382 {
1383 php_pqtxn_object_t *obj = o;
1384
1385 RETVAL_LONG(obj->intern->deferrable);
1386 }
1387
1388 static void php_pqtxn_object_write_isolation(zval *object, void *o, zval *value TSRMLS_DC)
1389 {
1390 php_pqtxn_object_t *obj = o;
1391 php_pqtxn_isolation_t orig = obj->intern->isolation;
1392 zval *zisolation = value;
1393 PGresult *res;
1394
1395 if (Z_TYPE_P(zisolation) != IS_LONG) {
1396 convert_to_long_ex(&zisolation);
1397 }
1398
1399 switch ((obj->intern->isolation = Z_LVAL_P(zisolation))) {
1400 case PHP_PQTXN_READ_COMMITTED:
1401 res = PQexec(obj->intern->conn->intern->conn, "SET TRANSACTION READ COMMITED");
1402 break;
1403 case PHP_PQTXN_REPEATABLE_READ:
1404 res = PQexec(obj->intern->conn->intern->conn, "SET TRANSACTION REPEATABLE READ");
1405 break;
1406 case PHP_PQTXN_SERIALIZABLE:
1407 res = PQexec(obj->intern->conn->intern->conn, "SET TRANSACTION SERIALIZABLE");
1408 break;
1409 default:
1410 obj->intern->isolation = orig;
1411 res = NULL;
1412 break;
1413 }
1414
1415 if (zisolation != value) {
1416 zval_ptr_dtor(&zisolation);
1417 }
1418
1419 if (res) {
1420 php_pqres_success(res TSRMLS_CC);
1421 PHP_PQclear(res);
1422 }
1423 }
1424
1425 static void php_pqtxn_object_write_readonly(zval *object, void *o, zval *value TSRMLS_DC)
1426 {
1427 php_pqtxn_object_t *obj = o;
1428 PGresult *res;
1429
1430 if ((obj->intern->readonly = zend_is_true(value))) {
1431 res = PQexec(obj->intern->conn->intern->conn, "SET TRANSACTION READ ONLY");
1432 } else {
1433 res = PQexec(obj->intern->conn->intern->conn, "SET TRANSACTION READ WRITE");
1434 }
1435
1436 if (res) {
1437 php_pqres_success(res TSRMLS_CC);
1438 PHP_PQclear(res);
1439 }
1440 }
1441
1442 static void php_pqtxn_object_write_deferrable(zval *object, void *o, zval *value TSRMLS_DC)
1443 {
1444 php_pqtxn_object_t *obj = o;
1445 PGresult *res;
1446
1447 if ((obj->intern->deferrable = zend_is_true(value))) {
1448 res = PQexec(obj->intern->conn->intern->conn, "SET TRANSACTION DEFERRABLE");
1449 } else {
1450 res = PQexec(obj->intern->conn->intern->conn, "SET TRANSACTION NOT DEFERRABLE");
1451 }
1452
1453 if (res) {
1454 php_pqres_success(res TSRMLS_CC);
1455 PHP_PQclear(res);
1456 }
1457 }
1458
1459 static void php_pqcancel_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
1460 {
1461 php_pqcancel_object_t *obj = o;
1462
1463 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
1464 }
1465
1466 static void php_pqevent_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
1467 {
1468 php_pqevent_object_t *obj = o;
1469
1470 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
1471 }
1472
1473 static void php_pqevent_object_read_type(zval *object, void *o, zval *return_value TSRMLS_DC)
1474 {
1475 php_pqevent_object_t *obj = o;
1476
1477 RETVAL_STRING(obj->intern->type, 1);
1478 }
1479
1480 static void php_pqlob_object_read_transaction(zval *object, void *o, zval *return_value TSRMLS_DC)
1481 {
1482 php_pqlob_object_t *obj = o;
1483
1484 php_pq_object_to_zval(obj->intern->txn, &return_value TSRMLS_CC);
1485 }
1486
1487 static void php_pqlob_object_read_oid(zval *object, void *o, zval *return_value TSRMLS_DC)
1488 {
1489 php_pqlob_object_t *obj = o;
1490
1491 RETVAL_LONG(obj->intern->loid);
1492 }
1493
1494 static void php_pqcopy_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
1495 {
1496 php_pqcopy_object_t *obj = o;
1497
1498 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
1499 }
1500
1501 static void php_pqcopy_object_read_direction(zval *object, void *o, zval *return_value TSRMLS_DC)
1502 {
1503 php_pqcopy_object_t *obj = o;
1504
1505 RETVAL_LONG(obj->intern->direction);
1506 }
1507
1508 static void php_pqcopy_object_read_expression(zval *object, void *o, zval *return_value TSRMLS_DC)
1509 {
1510 php_pqcopy_object_t *obj = o;
1511
1512 RETURN_STRING(obj->intern->expression, 1);
1513 }
1514
1515 static void php_pqcopy_object_read_options(zval *object, void *o, zval *return_value TSRMLS_DC)
1516 {
1517 php_pqcopy_object_t *obj = o;
1518
1519 RETURN_STRING(obj->intern->options, 1);
1520 }
1521
1522 static zend_class_entry *ancestor(zend_class_entry *ce) {
1523 while (ce->parent) {
1524 ce = ce->parent;
1525 }
1526 return ce;
1527 }
1528
1529 static zval *php_pq_object_read_prop(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC)
1530 {
1531 php_pq_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
1532 php_pq_object_prophandler_t *handler;
1533 zval *return_value;
1534
1535 if (!obj->intern) {
1536 zend_error(E_WARNING, "%s not initialized", ancestor(obj->zo.ce)->name);
1537 } else if ((SUCCESS == zend_hash_find(obj->prophandler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void *) &handler)) && handler->read) {
1538 if (type == BP_VAR_R) {
1539 ALLOC_ZVAL(return_value);
1540 Z_SET_REFCOUNT_P(return_value, 0);
1541 Z_UNSET_ISREF_P(return_value);
1542
1543 handler->read(object, obj, return_value TSRMLS_CC);
1544 } else {
1545 zend_error(E_ERROR, "Cannot access %s properties by reference or array key/index", ancestor(obj->zo.ce)->name);
1546 return_value = NULL;
1547 }
1548 } else {
1549 return_value = zend_get_std_object_handlers()->read_property(object, member, type, key TSRMLS_CC);
1550 }
1551
1552 return return_value;
1553 }
1554
1555 static void php_pq_object_write_prop(zval *object, zval *member, zval *value, const zend_literal *key TSRMLS_DC)
1556 {
1557 php_pq_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
1558 php_pq_object_prophandler_t *handler;
1559
1560 if (SUCCESS == zend_hash_find(obj->prophandler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void *) &handler)) {
1561 if (handler->write) {
1562 handler->write(object, obj, value TSRMLS_CC);
1563 }
1564 } else {
1565 zend_get_std_object_handlers()->write_property(object, member, value, key TSRMLS_CC);
1566 }
1567 }
1568
1569 static STATUS php_pqconn_update_socket(zval *this_ptr, php_pqconn_object_t *obj TSRMLS_DC)
1570 {
1571 zval *zsocket, zmember;
1572 php_stream *stream;
1573 STATUS retval;
1574 int socket;
1575
1576 if (!obj) {
1577 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1578 }
1579
1580 INIT_PZVAL(&zmember);
1581 ZVAL_STRINGL(&zmember, "socket", sizeof("socket")-1, 0);
1582 MAKE_STD_ZVAL(zsocket);
1583
1584 if ((CONNECTION_BAD != PQstatus(obj->intern->conn))
1585 && (-1 < (socket = PQsocket(obj->intern->conn)))
1586 && (stream = php_stream_fopen_from_fd(socket, "r+b", NULL))) {
1587 php_stream_to_zval(stream, zsocket);
1588 retval = SUCCESS;
1589 } else {
1590 ZVAL_NULL(zsocket);
1591 retval = FAILURE;
1592 }
1593 zend_get_std_object_handlers()->write_property(getThis(), &zmember, zsocket, NULL TSRMLS_CC);
1594 zval_ptr_dtor(&zsocket);
1595
1596 return retval;
1597 }
1598
1599 #ifdef ZTS
1600 # define TSRMLS_DF(d) TSRMLS_D = (d)->ts
1601 # define TSRMLS_CF(d) (d)->ts = TSRMLS_C
1602 #else
1603 # define TSRMLS_DF(d)
1604 # define TSRMLS_CF(d)
1605 #endif
1606
1607 static int apply_event(void *p, void *a TSRMLS_DC)
1608 {
1609 zval **evh = p;
1610 zval *args = a;
1611 zval *retval = NULL;
1612
1613 zend_call_method_with_1_params(evh, Z_OBJCE_PP(evh), NULL, "trigger", &retval, args);
1614 if (retval) {
1615 zval_ptr_dtor(&retval);
1616 }
1617
1618 return ZEND_HASH_APPLY_KEEP;
1619 }
1620
1621 static void php_pqconn_event_connreset(PGEventConnReset *event, php_pqconn_event_data_t *data)
1622 {
1623 zval **evhs;
1624 TSRMLS_DF(data);
1625
1626 if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("reset"), (void *) &evhs)) {
1627 zval *args, *connection = NULL;
1628
1629 MAKE_STD_ZVAL(args);
1630 array_init(args);
1631 php_pq_object_to_zval(data->obj, &connection TSRMLS_CC);
1632 add_next_index_zval(args, connection);
1633 zend_hash_apply_with_argument(Z_ARRVAL_PP(evhs), apply_event, args TSRMLS_CC);
1634 zval_ptr_dtor(&args);
1635 }
1636 }
1637
1638 static zval *result_instance_zval(PGresult *res TSRMLS_DC)
1639 {
1640 zval *rid = PQresultInstanceData(res, php_pqconn_event);
1641
1642 if (!rid) {
1643 php_pqres_t *r = ecalloc(1, sizeof(*r));
1644
1645 MAKE_STD_ZVAL(rid);
1646 r->res = res;
1647 ZEND_INIT_SYMTABLE(&r->bound);
1648 rid->type = IS_OBJECT;
1649 rid->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC);
1650
1651 PQresultSetInstanceData(res, php_pqconn_event, rid);
1652 }
1653
1654 Z_ADDREF_P(rid);
1655 return rid;
1656 }
1657
1658 static void php_pqconn_event_resultcreate(PGEventResultCreate *event, php_pqconn_event_data_t *data)
1659 {
1660 zval **evhs;
1661 TSRMLS_DF(data);
1662
1663 /* event listener */
1664 if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("result"), (void *) &evhs)) {
1665 zval *args, *connection = NULL, *res = result_instance_zval(event->result TSRMLS_CC);
1666
1667 MAKE_STD_ZVAL(args);
1668 array_init(args);
1669 php_pq_object_to_zval(data->obj, &connection TSRMLS_CC);
1670 add_next_index_zval(args, connection);
1671 add_next_index_zval(args, res);
1672 zend_hash_apply_with_argument(Z_ARRVAL_PP(evhs), apply_event, args TSRMLS_CC);
1673 zval_ptr_dtor(&args);
1674 }
1675
1676 /* async callback */
1677 if (data->obj->intern->onevent.fci.size > 0) {
1678 zval *res = result_instance_zval(event->result TSRMLS_CC);
1679
1680 zend_fcall_info_argn(&data->obj->intern->onevent.fci TSRMLS_CC, 1, &res);
1681 zend_fcall_info_call(&data->obj->intern->onevent.fci, &data->obj->intern->onevent.fcc, NULL, NULL TSRMLS_CC);
1682 zval_ptr_dtor(&res);
1683 }
1684 }
1685
1686 static int php_pqconn_event(PGEventId id, void *e, void *data)
1687 {
1688 switch (id) {
1689 case PGEVT_CONNRESET:
1690 php_pqconn_event_connreset(e, data);
1691 break;
1692 case PGEVT_RESULTCREATE:
1693 php_pqconn_event_resultcreate(e, data);
1694 break;
1695 default:
1696 break;
1697 }
1698
1699 return 1;
1700 }
1701
1702 static php_pqconn_event_data_t *php_pqconn_event_data_init(php_pqconn_object_t *obj TSRMLS_DC)
1703 {
1704 php_pqconn_event_data_t *data = emalloc(sizeof(*data));
1705
1706 data->obj = obj;
1707 TSRMLS_CF(data);
1708
1709 return data;
1710 }
1711
1712 static void php_pqconn_notice_recv(void *p, const PGresult *res)
1713 {
1714 php_pqconn_event_data_t *data = p;
1715 zval **evhs;
1716 TSRMLS_DF(data);
1717
1718 if (SUCCESS == zend_hash_find(&data->obj->intern->eventhandlers, ZEND_STRS("notice"), (void *) &evhs)) {
1719 zval *args, *connection = NULL;
1720
1721 MAKE_STD_ZVAL(args);
1722 array_init(args);
1723 php_pq_object_to_zval(data->obj, &connection TSRMLS_CC);
1724 add_next_index_zval(args, connection);
1725 add_next_index_string(args, PHP_PQresultErrorMessage(res), 1);
1726 zend_hash_apply_with_argument(Z_ARRVAL_PP(evhs), apply_event, args TSRMLS_CC);
1727 zval_ptr_dtor(&args);
1728 }
1729 }
1730
1731 typedef struct php_pqconn_resource_factory_data {
1732 char *dsn;
1733 long flags;
1734 } php_pqconn_resource_factory_data_t;
1735
1736 static void *php_pqconn_resource_factory_ctor(void *data, void *init_arg TSRMLS_DC)
1737 {
1738 php_pqconn_resource_factory_data_t *o = init_arg;
1739
1740 if (o->flags & PHP_PQCONN_ASYNC) {
1741 return PQconnectStart(o->dsn);
1742 } else {
1743 return PQconnectdb(o->dsn);
1744 }
1745 }
1746
1747 static void php_pqconn_resource_factory_dtor(void *opaque, void *handle TSRMLS_DC)
1748 {
1749 PQfinish(handle);
1750 }
1751
1752 static php_resource_factory_ops_t php_pqconn_resource_factory_ops = {
1753 php_pqconn_resource_factory_ctor,
1754 NULL,
1755 php_pqconn_resource_factory_dtor
1756 };
1757
1758 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct, 0, 0, 1)
1759 ZEND_ARG_INFO(0, dsn)
1760 ZEND_ARG_INFO(0, async)
1761 ZEND_END_ARG_INFO();
1762 static PHP_METHOD(pqconn, __construct) {
1763 zend_error_handling zeh;
1764 char *dsn_str = "";
1765 int dsn_len = 0;
1766 long flags = 0;
1767
1768 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
1769 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &dsn_str, &dsn_len, &flags)) {
1770 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1771 php_pqconn_event_data_t *evdata = php_pqconn_event_data_init(obj TSRMLS_CC);
1772 php_pqconn_resource_factory_data_t rfdata = {dsn_str, flags};
1773
1774 obj->intern = ecalloc(1, sizeof(*obj->intern));
1775
1776 zend_hash_init(&obj->intern->listeners, 0, NULL, (dtor_func_t) zend_hash_destroy, 0);
1777 zend_hash_init(&obj->intern->eventhandlers, 0, NULL, ZVAL_PTR_DTOR, 0);
1778
1779 if (flags & PHP_PQCONN_PERSISTENT) {
1780 php_persistent_handle_factory_t *phf = php_persistent_handle_concede(NULL, ZEND_STRL("pq\\Connection"), dsn_str, dsn_len TSRMLS_CC);
1781 php_resource_factory_init(&obj->intern->factory, php_persistent_handle_get_resource_factory_ops(), phf, (void (*)(void*)) php_persistent_handle_abandon);
1782 } else{
1783 php_resource_factory_init(&obj->intern->factory, &php_pqconn_resource_factory_ops, NULL, NULL);
1784 }
1785 if (flags & PHP_PQCONN_ASYNC) {
1786 obj->intern->poller = (int (*)(PGconn*)) PQconnectPoll;
1787 }
1788
1789 obj->intern->conn = php_resource_factory_handle_ctor(&obj->intern->factory, &rfdata TSRMLS_CC);
1790
1791 PQregisterEventProc(obj->intern->conn, php_pqconn_event, "ext-pq", evdata);
1792 /* the connection might be persistent, so reset event_proc instance data */
1793 PQsetInstanceData(obj->intern->conn, php_pqconn_event, evdata);
1794 PQsetNoticeReceiver(obj->intern->conn, php_pqconn_notice_recv, evdata);
1795
1796 if (SUCCESS != php_pqconn_update_socket(getThis(), obj TSRMLS_CC)) {
1797 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection failed (%s)", PHP_PQerrorMessage(obj->intern->conn));
1798 }
1799 }
1800 zend_restore_error_handling(&zeh TSRMLS_CC);
1801 }
1802
1803 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset, 0, 0, 0)
1804 ZEND_END_ARG_INFO();
1805 static PHP_METHOD(pqconn, reset) {
1806 if (SUCCESS == zend_parse_parameters_none()) {
1807 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1808
1809 if (obj->intern) {
1810 PQreset(obj->intern->conn);
1811
1812 if (CONNECTION_OK == PQstatus(obj->intern->conn)) {
1813 RETURN_TRUE;
1814 } else {
1815 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection reset failed: (%s)", PHP_PQerrorMessage(obj->intern->conn));
1816 }
1817 } else {
1818 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
1819 }
1820 RETURN_FALSE;
1821 }
1822 }
1823
1824 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset_async, 0, 0, 0)
1825 ZEND_END_ARG_INFO();
1826 static PHP_METHOD(pqconn, resetAsync) {
1827 if (SUCCESS == zend_parse_parameters_none()) {
1828 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1829
1830 if (obj->intern) {
1831 if (PQresetStart(obj->intern->conn)) {
1832 obj->intern->poller = (int (*)(PGconn*)) PQresetPoll;
1833 RETURN_TRUE;
1834 }
1835 } else {
1836 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
1837 }
1838 RETURN_FALSE;
1839 }
1840 }
1841
1842 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)
1843 {
1844 HashTable ht, *existing_listeners;
1845
1846 php_pq_callback_addref(listener);
1847
1848 if (SUCCESS == zend_hash_find(&obj->intern->listeners, channel_str, channel_len + 1, (void *) &existing_listeners)) {
1849 zend_hash_next_index_insert(existing_listeners, (void *) listener, sizeof(*listener), NULL);
1850 } else {
1851 zend_hash_init(&ht, 1, NULL, (dtor_func_t) php_pq_callback_dtor, 0);
1852 zend_hash_next_index_insert(&ht, (void *) listener, sizeof(*listener), NULL);
1853 zend_hash_add(&obj->intern->listeners, channel_str, channel_len + 1, (void *) &ht, sizeof(HashTable), NULL);
1854 }
1855 }
1856
1857 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen, 0, 0, 0)
1858 ZEND_ARG_INFO(0, channel)
1859 ZEND_ARG_INFO(0, callable)
1860 ZEND_END_ARG_INFO();
1861 static PHP_METHOD(pqconn, listen) {
1862 char *channel_str = NULL;
1863 int channel_len = 0;
1864 php_pq_callback_t listener;
1865
1866 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sf", &channel_str, &channel_len, &listener.fci, &listener.fcc)) {
1867 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1868
1869 obj->intern->poller = PQconsumeInput;
1870
1871 if (obj->intern) {
1872 char *quoted_channel = PQescapeIdentifier(obj->intern->conn, channel_str, channel_len);
1873
1874 if (quoted_channel) {
1875 PGresult *res;
1876 char *cmd;
1877
1878 spprintf(&cmd, 0, "LISTEN %s", channel_str);
1879 res = PQexec(obj->intern->conn, cmd);
1880
1881 efree(cmd);
1882 PQfreemem(quoted_channel);
1883
1884 if (res) {
1885 if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
1886 php_pqconn_add_listener(obj, channel_str, channel_len, &listener TSRMLS_CC);
1887 RETVAL_TRUE;
1888 } else {
1889 RETVAL_FALSE;
1890 }
1891 PHP_PQclear(res);
1892 } else {
1893 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not install listener (%s)", PHP_PQerrorMessage(obj->intern->conn));
1894 RETVAL_FALSE;
1895 }
1896
1897 php_pqconn_notify_listeners(obj TSRMLS_CC);
1898 } else {
1899 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not escape channel identifier (%s)", PHP_PQerrorMessage(obj->intern->conn));
1900 }
1901 } else {
1902 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
1903 RETVAL_FALSE;
1904 }
1905 }
1906 }
1907
1908 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen_async, 0, 0, 0)
1909 ZEND_ARG_INFO(0, channel)
1910 ZEND_ARG_INFO(0, callable)
1911 ZEND_END_ARG_INFO();
1912 static PHP_METHOD(pqconn, listenAsync) {
1913 char *channel_str = NULL;
1914 int channel_len = 0;
1915 php_pq_callback_t listener;
1916
1917 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sf", &channel_str, &channel_len, &listener.fci, &listener.fcc)) {
1918 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1919
1920 obj->intern->poller = PQconsumeInput;
1921
1922 if (obj->intern) {
1923 char *quoted_channel = PQescapeIdentifier(obj->intern->conn, channel_str, channel_len);
1924
1925 if (quoted_channel) {
1926 char *cmd;
1927
1928 obj->intern->poller = PQconsumeInput;
1929
1930 spprintf(&cmd, 0, "LISTEN %s", channel_str);
1931 if (PQsendQuery(obj->intern->conn, cmd)) {
1932 php_pqconn_add_listener(obj, channel_str, channel_len, &listener TSRMLS_CC);
1933 RETVAL_TRUE;
1934 } else {
1935 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not install listener (%s)", PHP_PQerrorMessage(obj->intern->conn));
1936 RETVAL_FALSE;
1937 }
1938
1939 efree(cmd);
1940 PQfreemem(quoted_channel);
1941
1942 php_pqconn_notify_listeners(obj TSRMLS_CC);
1943 } else {
1944 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not escape channel identifier (%s)", PHP_PQerrorMessage(obj->intern->conn));
1945 }
1946 } else {
1947 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
1948 RETVAL_FALSE;
1949 }
1950 }
1951 }
1952
1953 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify, 0, 0, 2)
1954 ZEND_ARG_INFO(0, channel)
1955 ZEND_ARG_INFO(0, message)
1956 ZEND_END_ARG_INFO();
1957 static PHP_METHOD(pqconn, notify) {
1958 char *channel_str, *message_str;
1959 int channel_len, message_len;
1960
1961 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &channel_str, &channel_len, &message_str, &message_len)) {
1962 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1963
1964 if (obj->intern) {
1965 PGresult *res;
1966 char *params[2] = {channel_str, message_str};
1967
1968 res = PQexecParams(obj->intern->conn, "select pg_notify($1, $2)", 2, NULL, (const char *const*) params, NULL, NULL, 0);
1969
1970 if (res) {
1971 if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
1972 RETVAL_TRUE;
1973 } else {
1974 RETVAL_FALSE;
1975 }
1976 PHP_PQclear(res);
1977 } else {
1978 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not notify listeners (%s)", PHP_PQerrorMessage(obj->intern->conn));
1979 RETVAL_FALSE;
1980 }
1981
1982 php_pqconn_notify_listeners(obj TSRMLS_CC);
1983
1984 } else {
1985 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
1986 RETVAL_FALSE;
1987 }
1988 }
1989 }
1990
1991 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify_async, 0, 0, 2)
1992 ZEND_ARG_INFO(0, channel)
1993 ZEND_ARG_INFO(0, message)
1994 ZEND_END_ARG_INFO();
1995 static PHP_METHOD(pqconn, notifyAsync) {
1996 char *channel_str, *message_str;
1997 int channel_len, message_len;
1998
1999 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &channel_str, &channel_len, &message_str, &message_len)) {
2000 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2001
2002 if (obj->intern) {
2003 char *params[2] = {channel_str, message_str};
2004
2005 obj->intern->poller = PQconsumeInput;
2006
2007 if (PQsendQueryParams(obj->intern->conn, "select pg_notify($1, $2)", 2, NULL, (const char *const*) params, NULL, NULL, 0)) {
2008 RETVAL_TRUE;
2009 } else {
2010 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not notify listeners (%s)", PHP_PQerrorMessage(obj->intern->conn));
2011 RETVAL_FALSE;
2012 }
2013
2014 php_pqconn_notify_listeners(obj TSRMLS_CC);
2015
2016 } else {
2017 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2018 RETVAL_FALSE;
2019 }
2020 }
2021 }
2022
2023 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_poll, 0, 0, 0)
2024 ZEND_END_ARG_INFO();
2025 static PHP_METHOD(pqconn, poll) {
2026 if (SUCCESS == zend_parse_parameters_none()) {
2027 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2028
2029 if (obj->intern) {
2030 if (obj->intern->poller) {
2031 if (obj->intern->poller == PQconsumeInput) {
2032 RETVAL_LONG(obj->intern->poller(obj->intern->conn) * PGRES_POLLING_OK);
2033 php_pqconn_notify_listeners(obj TSRMLS_CC);
2034 return;
2035 } else {
2036 RETURN_LONG(obj->intern->poller(obj->intern->conn));
2037 }
2038 } else {
2039 php_error_docref(NULL TSRMLS_CC, E_WARNING, "No asynchronous operation active");
2040 }
2041 } else {
2042 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2043 }
2044 RETURN_FALSE;
2045 }
2046 }
2047
2048 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec, 0, 0, 1)
2049 ZEND_ARG_INFO(0, query)
2050 ZEND_END_ARG_INFO();
2051 static PHP_METHOD(pqconn, exec) {
2052 zend_error_handling zeh;
2053 char *query_str;
2054 int query_len;
2055
2056 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
2057 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &query_str, &query_len)) {
2058 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2059
2060 if (obj->intern) {
2061 PGresult *res = PQexec(obj->intern->conn, query_str);
2062
2063 php_pqconn_notify_listeners(obj TSRMLS_CC);
2064
2065 if (res) {
2066 if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
2067 php_pqres_t *r = ecalloc(1, sizeof(*r));
2068
2069 r->res = res;
2070 ZEND_INIT_SYMTABLE(&r->bound);
2071 return_value->type = IS_OBJECT;
2072 return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC);
2073 }
2074 } else {
2075 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
2076 }
2077 } else {
2078 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2079 }
2080 }
2081 zend_restore_error_handling(&zeh TSRMLS_CC);
2082 }
2083
2084 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_get_result, 0, 0, 0)
2085 ZEND_END_ARG_INFO();
2086 static PHP_METHOD(pqconn, getResult) {
2087 if (SUCCESS == zend_parse_parameters_none()) {
2088 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2089
2090 if (obj->intern) {
2091 PGresult *res = PQgetResult(obj->intern->conn);
2092
2093 if (res) {
2094 php_pqres_t *r = ecalloc(1, sizeof(*r));
2095
2096 r->res = res;
2097 ZEND_INIT_SYMTABLE(&r->bound);
2098 return_value->type = IS_OBJECT;
2099 return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC);
2100 } else {
2101 RETVAL_NULL();
2102 }
2103 } else {
2104 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2105 RETVAL_FALSE;
2106 }
2107 }
2108 }
2109
2110 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_async, 0, 0, 1)
2111 ZEND_ARG_INFO(0, query)
2112 ZEND_ARG_INFO(0, callable)
2113 ZEND_END_ARG_INFO();
2114 static PHP_METHOD(pqconn, execAsync) {
2115 zend_error_handling zeh;
2116 php_pq_callback_t resolver = {{0}};
2117 char *query_str;
2118 int query_len;
2119
2120 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
2121 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|f", &query_str, &query_len, &resolver.fci, &resolver.fcc)) {
2122 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2123
2124 if (obj->intern) {
2125 php_pq_callback_dtor(&obj->intern->onevent);
2126 if (resolver.fci.size > 0) {
2127 obj->intern->onevent = resolver;
2128 php_pq_callback_addref(&obj->intern->onevent);
2129 }
2130
2131 obj->intern->poller = PQconsumeInput;
2132
2133 if (PQsendQuery(obj->intern->conn, query_str)) {
2134 if (obj->intern->unbuffered) {
2135 if (!PQsetSingleRowMode(obj->intern->conn)) {
2136 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
2137 }
2138 }
2139 RETVAL_TRUE;
2140 } else {
2141 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
2142 RETVAL_FALSE;
2143 }
2144 } else {
2145 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2146 RETVAL_FALSE;
2147 }
2148 }
2149 zend_restore_error_handling(&zeh TSRMLS_CC);
2150 }
2151
2152 static int apply_to_oid(void *p, void *arg TSRMLS_DC)
2153 {
2154 Oid **types = arg;
2155 zval **ztype = p;
2156
2157 if (Z_TYPE_PP(ztype) != IS_LONG) {
2158 convert_to_long_ex(ztype);
2159 }
2160
2161 **types = Z_LVAL_PP(ztype);
2162 ++*types;
2163
2164 if (*ztype != *(zval **)p) {
2165 zval_ptr_dtor(ztype);
2166 }
2167 return ZEND_HASH_APPLY_KEEP;
2168 }
2169
2170 static int apply_to_param(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
2171 {
2172 char ***params;
2173 HashTable *zdtor;
2174 zval **zparam = p;
2175
2176 params = (char ***) va_arg(argv, char ***);
2177 zdtor = (HashTable *) va_arg(argv, HashTable *);
2178
2179 if (Z_TYPE_PP(zparam) == IS_NULL) {
2180 **params = NULL;
2181 ++*params;
2182 } else {
2183 if (Z_TYPE_PP(zparam) != IS_STRING) {
2184 convert_to_string_ex(zparam);
2185 }
2186
2187 **params = Z_STRVAL_PP(zparam);
2188 ++*params;
2189
2190 if (*zparam != *(zval **)p) {
2191 zend_hash_next_index_insert(zdtor, zparam, sizeof(zval *), NULL);
2192 }
2193 }
2194 return ZEND_HASH_APPLY_KEEP;
2195 }
2196
2197 static int php_pq_types_to_array(HashTable *ht, Oid **types TSRMLS_DC)
2198 {
2199 int count = zend_hash_num_elements(ht);
2200
2201 *types = NULL;
2202
2203 if (count) {
2204 Oid *tmp;
2205
2206 /* +1 for when less types than params are specified */
2207 *types = tmp = ecalloc(count + 1, sizeof(Oid));
2208 zend_hash_apply_with_argument(ht, apply_to_oid, &tmp TSRMLS_CC);
2209 }
2210
2211 return count;
2212 }
2213
2214 static int php_pq_params_to_array(HashTable *ht, char ***params, HashTable *zdtor TSRMLS_DC)
2215 {
2216 int count = zend_hash_num_elements(ht);
2217
2218 *params = NULL;
2219
2220 if (count) {
2221 char **tmp;
2222
2223 *params = tmp = ecalloc(count, sizeof(char *));
2224 zend_hash_apply_with_arguments(ht TSRMLS_CC, apply_to_param, 2, &tmp, zdtor);
2225 }
2226
2227 return count;
2228 }
2229
2230 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params, 0, 0, 2)
2231 ZEND_ARG_INFO(0, query)
2232 ZEND_ARG_ARRAY_INFO(0, params, 0)
2233 ZEND_ARG_ARRAY_INFO(0, types, 1)
2234 ZEND_END_ARG_INFO();
2235 static PHP_METHOD(pqconn, execParams) {
2236 zend_error_handling zeh;
2237 char *query_str;
2238 int query_len;
2239 zval *zparams;
2240 zval *ztypes = NULL;
2241
2242 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
2243 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|a/!", &query_str, &query_len, &zparams, &ztypes)) {
2244 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2245
2246 if (obj->intern) {
2247 PGresult *res;
2248 int count;
2249 Oid *types = NULL;
2250 char **params = NULL;
2251 HashTable zdtor;
2252
2253 ZEND_INIT_SYMTABLE(&zdtor);
2254 count = php_pq_params_to_array(Z_ARRVAL_P(zparams), &params, &zdtor TSRMLS_CC);
2255
2256 if (ztypes) {
2257 php_pq_types_to_array(Z_ARRVAL_P(ztypes), &types TSRMLS_CC);
2258 }
2259
2260 res = PQexecParams(obj->intern->conn, query_str, count, types, (const char *const*) params, NULL, NULL, 0);
2261
2262 zend_hash_destroy(&zdtor);
2263 if (types) {
2264 efree(types);
2265 }
2266 if (params) {
2267 efree(params);
2268 }
2269
2270 php_pqconn_notify_listeners(obj TSRMLS_CC);
2271
2272 if (res) {
2273 if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
2274 php_pqres_t *r = ecalloc(1, sizeof(*r));
2275
2276 r->res = res;
2277 ZEND_INIT_SYMTABLE(&r->bound);
2278 return_value->type = IS_OBJECT;
2279 return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC);
2280 }
2281 } else {
2282 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
2283 RETVAL_FALSE;
2284 }
2285 } else {
2286 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2287 RETVAL_FALSE;
2288 }
2289 }
2290 zend_restore_error_handling(&zeh TSRMLS_CC);
2291 }
2292
2293 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async, 0, 0, 2)
2294 ZEND_ARG_INFO(0, query)
2295 ZEND_ARG_ARRAY_INFO(0, params, 0)
2296 ZEND_ARG_ARRAY_INFO(0, types, 1)
2297 ZEND_ARG_INFO(0, callable)
2298 ZEND_END_ARG_INFO();
2299 static PHP_METHOD(pqconn, execParamsAsync) {
2300 zend_error_handling zeh;
2301 php_pq_callback_t resolver = {{0}};
2302 char *query_str;
2303 int query_len;
2304 zval *zparams;
2305 zval *ztypes = NULL;
2306
2307 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
2308 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|a/!f", &query_str, &query_len, &zparams, &ztypes, &resolver.fci, &resolver.fcc)) {
2309 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2310
2311 if (obj->intern) {
2312 int count;
2313 Oid *types = NULL;
2314 char **params = NULL;
2315 HashTable zdtor;
2316
2317 ZEND_INIT_SYMTABLE(&zdtor);
2318 count = php_pq_params_to_array(Z_ARRVAL_P(zparams), &params, &zdtor TSRMLS_CC);
2319
2320 if (ztypes) {
2321 php_pq_types_to_array(Z_ARRVAL_P(ztypes), &types TSRMLS_CC);
2322 }
2323
2324 php_pq_callback_dtor(&obj->intern->onevent);
2325 if (resolver.fci.size > 0) {
2326 obj->intern->onevent = resolver;
2327 php_pq_callback_addref(&obj->intern->onevent);
2328 }
2329
2330 obj->intern->poller = PQconsumeInput;
2331
2332 if (PQsendQueryParams(obj->intern->conn, query_str, count, types, (const char *const*) params, NULL, NULL, 0)) {
2333 if (obj->intern->unbuffered) {
2334 if (!PQsetSingleRowMode(obj->intern->conn)) {
2335 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
2336 }
2337 }
2338 RETVAL_TRUE;
2339 } else {
2340 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
2341 RETVAL_FALSE;
2342 }
2343
2344 zend_hash_destroy(&zdtor);
2345 if (types) {
2346 efree(types);
2347 }
2348 if (params) {
2349 efree(params);
2350 }
2351
2352 php_pqconn_notify_listeners(obj TSRMLS_CC);
2353
2354 } else {
2355 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2356 RETVAL_FALSE;
2357 }
2358 }
2359 zend_restore_error_handling(&zeh TSRMLS_CC);
2360 }
2361
2362 static STATUS php_pqconn_prepare(zval *object, php_pqconn_object_t *obj, const char *name, const char *query, HashTable *typest TSRMLS_DC)
2363 {
2364 Oid *types = NULL;
2365 int count = 0;
2366 PGresult *res;
2367 STATUS rv;
2368
2369 if (!obj) {
2370 obj = zend_object_store_get_object(object TSRMLS_CC);
2371 }
2372
2373 if (typest) {
2374 count = zend_hash_num_elements(typest);
2375 php_pq_types_to_array(typest, &types TSRMLS_CC);
2376 }
2377
2378 res = PQprepare(obj->intern->conn, name, query, count, types);
2379
2380 if (types) {
2381 efree(types);
2382 }
2383
2384 if (res) {
2385 rv = php_pqres_success(res TSRMLS_CC);
2386 PHP_PQclear(res);
2387 } else {
2388 rv = FAILURE;
2389 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not prepare statement (%s)", PHP_PQerrorMessage(obj->intern->conn));
2390 }
2391
2392 return rv;
2393 }
2394
2395 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare, 0, 0, 2)
2396 ZEND_ARG_INFO(0, type)
2397 ZEND_ARG_INFO(0, query)
2398 ZEND_ARG_ARRAY_INFO(0, types, 1)
2399 ZEND_END_ARG_INFO();
2400 static PHP_METHOD(pqconn, prepare) {
2401 zend_error_handling zeh;
2402 zval *ztypes = NULL;
2403 char *name_str, *query_str;
2404 int name_len, *query_len;
2405
2406 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
2407 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!", &name_str, &name_len, &query_str, &query_len, &ztypes)) {
2408 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2409
2410 if (obj->intern) {
2411 if (SUCCESS == php_pqconn_prepare(getThis(), obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC)) {
2412 php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
2413
2414 php_pq_object_addref(obj TSRMLS_CC);
2415 stm->conn = obj;
2416 stm->name = estrdup(name_str);
2417 ZEND_INIT_SYMTABLE(&stm->bound);
2418
2419 return_value->type = IS_OBJECT;
2420 return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
2421 }
2422 php_pqconn_notify_listeners(obj TSRMLS_CC);
2423 } else {
2424 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2425 }
2426 }
2427 zend_restore_error_handling(&zeh TSRMLS_CC);
2428 }
2429
2430 static STATUS php_pqconn_prepare_async(zval *object, php_pqconn_object_t *obj, const char *name, const char *query, HashTable *typest TSRMLS_DC)
2431 {
2432 STATUS rv;
2433 int count;
2434 Oid *types = NULL;
2435
2436 if (!obj) {
2437 obj = zend_object_store_get_object(object TSRMLS_CC);
2438 }
2439
2440 if (typest) {
2441 count = php_pq_types_to_array(typest, &types TSRMLS_CC);
2442 }
2443
2444 if (PQsendPrepare(obj->intern->conn, name, query, count, types)) {
2445 if (obj->intern->unbuffered) {
2446 if (!PQsetSingleRowMode(obj->intern->conn)) {
2447 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
2448 }
2449 }
2450 rv = SUCCESS;
2451 } else {
2452 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not prepare statement (%s)", PHP_PQerrorMessage(obj->intern->conn));
2453 rv = FAILURE;
2454 }
2455
2456 if (types) {
2457 efree(types);
2458 }
2459
2460 return rv;
2461 }
2462
2463 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare_async, 0, 0, 2)
2464 ZEND_ARG_INFO(0, type)
2465 ZEND_ARG_INFO(0, query)
2466 ZEND_ARG_ARRAY_INFO(0, types, 1)
2467 ZEND_END_ARG_INFO();
2468 static PHP_METHOD(pqconn, prepareAsync) {
2469 zend_error_handling zeh;
2470 zval *ztypes = NULL;
2471 char *name_str, *query_str;
2472 int name_len, *query_len;
2473
2474 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
2475 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!", &name_str, &name_len, &query_str, &query_len, &ztypes)) {
2476 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2477
2478 if (obj->intern) {
2479 obj->intern->poller = PQconsumeInput;
2480 if (SUCCESS == php_pqconn_prepare_async(getThis(), obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC)) {
2481 php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
2482
2483 php_pq_object_addref(obj TSRMLS_CC);
2484 stm->conn = obj;
2485 stm->name = estrdup(name_str);
2486 ZEND_INIT_SYMTABLE(&stm->bound);
2487
2488 return_value->type = IS_OBJECT;
2489 return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
2490 }
2491 php_pqconn_notify_listeners(obj TSRMLS_CC);
2492 } else {
2493 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2494 }
2495 }
2496 zend_restore_error_handling(&zeh TSRMLS_CC);
2497 }
2498
2499 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote, 0, 0, 1)
2500 ZEND_ARG_INFO(0, string)
2501 ZEND_END_ARG_INFO();
2502 static PHP_METHOD(pqconn, quote) {
2503 char *str;
2504 int len;
2505
2506 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
2507 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2508
2509 if (obj->intern) {
2510 char *quoted = PQescapeLiteral(obj->intern->conn, str, len);
2511
2512 if (quoted) {
2513 RETVAL_STRING(quoted, 1);
2514 PQfreemem(quoted);
2515 } else {
2516 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not quote string (%s)", PHP_PQerrorMessage(obj->intern->conn));
2517 RETVAL_FALSE;
2518 }
2519 } else {
2520 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2521 RETVAL_FALSE;
2522 }
2523 }
2524 }
2525
2526 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote_name, 0, 0, 1)
2527 ZEND_ARG_INFO(0, type)
2528 ZEND_END_ARG_INFO();
2529 static PHP_METHOD(pqconn, quoteName) {
2530 char *str;
2531 int len;
2532
2533 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
2534 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2535
2536 if (obj->intern) {
2537 char *quoted = PQescapeIdentifier(obj->intern->conn, str, len);
2538
2539 if (quoted) {
2540 RETVAL_STRING(quoted, 1);
2541 PQfreemem(quoted);
2542 } else {
2543 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not quote name (%s)", PHP_PQerrorMessage(obj->intern->conn));
2544 RETVAL_FALSE;
2545 }
2546 } else {
2547 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2548 RETVAL_FALSE;
2549 }
2550 }
2551 }
2552
2553 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_escape_bytea, 0, 0, 1)
2554 ZEND_ARG_INFO(0, bytea)
2555 ZEND_END_ARG_INFO();
2556 static PHP_METHOD(pqconn, escapeBytea) {
2557 char *str;
2558 int len;
2559
2560 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
2561 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2562
2563 if (obj->intern) {
2564 size_t escaped_len;
2565 char *escaped_str = (char *) PQescapeByteaConn(obj->intern->conn, (unsigned char *) str, len, &escaped_len);
2566
2567 if (escaped_str) {
2568 RETVAL_STRINGL(escaped_str, escaped_len - 1, 1);
2569 PQfreemem(escaped_str);
2570 } else {
2571 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not escape bytea (%s)", PHP_PQerrorMessage(obj->intern->conn));
2572 RETVAL_FALSE;
2573 }
2574 } else {
2575 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2576 RETVAL_FALSE;
2577 }
2578 }
2579 }
2580
2581 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unescape_bytea, 0, 0, 1)
2582 ZEND_ARG_INFO(0, bytea)
2583 ZEND_END_ARG_INFO();
2584 static PHP_METHOD(pqconn, unescapeBytea) {
2585 char *str;
2586 int len;
2587
2588 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
2589 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2590
2591 if (obj->intern) {
2592 size_t unescaped_len;
2593 char *unescaped_str = (char *) PQunescapeBytea((unsigned char *)str, &unescaped_len);
2594
2595 if (unescaped_str) {
2596 RETVAL_STRINGL(unescaped_str, unescaped_len, 1);
2597 PQfreemem(unescaped_str);
2598 } else {
2599 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not unescape bytea (%s)", PHP_PQerrorMessage(obj->intern->conn));
2600 RETVAL_FALSE;
2601 }
2602 } else {
2603 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2604 RETVAL_FALSE;
2605 }
2606 }
2607 }
2608
2609 static const char *isolation_level(long *isolation) {
2610 switch (*isolation) {
2611 case PHP_PQTXN_SERIALIZABLE:
2612 return "SERIALIZABLE";
2613 case PHP_PQTXN_REPEATABLE_READ:
2614 return "REPEATABLE READ";
2615 default:
2616 *isolation = PHP_PQTXN_READ_COMMITTED;
2617 /* no break */
2618 case PHP_PQTXN_READ_COMMITTED:
2619 return "READ COMMITTED";
2620 }
2621 }
2622
2623 static STATUS php_pqconn_start_transaction(zval *zconn, php_pqconn_object_t *conn_obj, long isolation, zend_bool readonly, zend_bool deferrable TSRMLS_DC)
2624 {
2625 if (!conn_obj) {
2626 conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
2627 }
2628
2629 if (conn_obj->intern) {
2630 PGresult *res;
2631 char *cmd;
2632
2633 spprintf(&cmd, 0, "START TRANSACTION ISOLATION LEVEL %s, READ %s, %s DEFERRABLE",
2634 isolation_level(&isolation), readonly ? "ONLY" : "WRITE", deferrable ? "": "NOT");
2635
2636 res = PQexec(conn_obj->intern->conn, cmd);
2637
2638 efree(cmd);
2639
2640 if (res) {
2641 STATUS rv = php_pqres_success(res TSRMLS_CC);
2642
2643 PHP_PQclear(res);
2644 return rv;
2645 } else {
2646 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not start transaction (%s)", PHP_PQerrorMessage(conn_obj->intern->conn));
2647 return FAILURE;
2648 }
2649 } else {
2650 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2651 return FAILURE;
2652 }
2653 }
2654
2655 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)
2656 {
2657 if (!conn_obj) {
2658 conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
2659 }
2660
2661 if (conn_obj->intern->conn) {
2662 char *cmd;
2663
2664 spprintf(&cmd, 0, "START TRANSACTION ISOLATION LEVEL %s, READ %s, %s DEFERRABLE",
2665 isolation_level(&isolation), readonly ? "ONLY" : "WRITE", deferrable ? "": "NOT");
2666
2667 if (PQsendQuery(conn_obj->intern->conn, cmd)) {
2668 conn_obj->intern->poller = PQconsumeInput;
2669 efree(cmd);
2670 return SUCCESS;
2671 } else {
2672 efree(cmd);
2673 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not start transaction (%s)", PHP_PQerrorMessage(conn_obj->intern->conn));
2674 return FAILURE;
2675 }
2676 } else {
2677 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2678 return FAILURE;
2679 }
2680 }
2681
2682 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction, 0, 0, 0)
2683 ZEND_ARG_INFO(0, isolation)
2684 ZEND_ARG_INFO(0, readonly)
2685 ZEND_ARG_INFO(0, deferrable)
2686 ZEND_END_ARG_INFO();
2687 static PHP_METHOD(pqconn, startTransaction) {
2688 zend_error_handling zeh;
2689 long isolation = PHP_PQTXN_READ_COMMITTED;
2690 zend_bool readonly = 0, deferrable = 0;
2691
2692 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
2693 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable)) {
2694 STATUS rv;
2695 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2696
2697 rv = php_pqconn_start_transaction(getThis(), obj, isolation, readonly, deferrable TSRMLS_CC);
2698
2699 if (SUCCESS == rv) {
2700 php_pqtxn_t *txn = ecalloc(1, sizeof(*txn));
2701
2702 php_pq_object_addref(obj TSRMLS_CC);
2703 txn->conn = obj;
2704 txn->isolation = isolation;
2705 txn->readonly = readonly;
2706 txn->deferrable = deferrable;
2707
2708 return_value->type = IS_OBJECT;
2709 return_value->value.obj = php_pqtxn_create_object_ex(php_pqtxn_class_entry, txn, NULL TSRMLS_CC);
2710 }
2711 }
2712 zend_restore_error_handling(&zeh TSRMLS_CC);
2713 }
2714
2715
2716 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction_async, 0, 0, 0)
2717 ZEND_ARG_INFO(0, isolation)
2718 ZEND_ARG_INFO(0, readonly)
2719 ZEND_ARG_INFO(0, deferrable)
2720 ZEND_END_ARG_INFO();
2721 static PHP_METHOD(pqconn, startTransactionAsync) {
2722 zend_error_handling zeh;
2723 long isolation = PHP_PQTXN_READ_COMMITTED;
2724 zend_bool readonly = 0, deferrable = 0;
2725
2726 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
2727 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable)) {
2728 STATUS rv;
2729 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2730
2731 rv = php_pqconn_start_transaction_async(getThis(), obj, isolation, readonly, deferrable TSRMLS_CC);
2732
2733 if (SUCCESS == rv) {
2734 php_pqtxn_t *txn = ecalloc(1, sizeof(*txn));
2735
2736 php_pq_object_addref(obj TSRMLS_CC);
2737 txn->conn = obj;
2738 txn->isolation = isolation;
2739 txn->readonly = readonly;
2740 txn->deferrable = deferrable;
2741
2742 return_value->type = IS_OBJECT;
2743 return_value->value.obj = php_pqtxn_create_object_ex(php_pqtxn_class_entry, txn, NULL TSRMLS_CC);
2744 }
2745 }
2746 zend_restore_error_handling(&zeh TSRMLS_CC);
2747 }
2748
2749 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_trace, 0, 0, 0)
2750 ZEND_ARG_INFO(0, stdio_stream)
2751 ZEND_END_ARG_INFO();
2752 static PHP_METHOD(pqconn, trace) {
2753 zval *zstream = NULL;
2754
2755 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r!", &zstream)) {
2756 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2757 if (obj->intern) {
2758 if (zstream) {
2759 FILE *fp;
2760 php_stream *stream = NULL;
2761
2762 php_stream_from_zval(stream, &zstream);
2763
2764 if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
2765 stream->flags |= PHP_STREAM_FLAG_NO_CLOSE;
2766 PQtrace(obj->intern->conn, fp);
2767 RETVAL_TRUE;
2768 } else {
2769 RETVAL_FALSE;
2770 }
2771 } else {
2772 PQuntrace(obj->intern->conn);
2773 RETVAL_TRUE;
2774 }
2775 } else {
2776 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2777 RETVAL_FALSE;
2778 }
2779 }
2780 }
2781
2782 static zend_function_entry php_pqconn_methods[] = {
2783 PHP_ME(pqconn, __construct, ai_pqconn_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
2784 PHP_ME(pqconn, reset, ai_pqconn_reset, ZEND_ACC_PUBLIC)
2785 PHP_ME(pqconn, resetAsync, ai_pqconn_reset_async, ZEND_ACC_PUBLIC)
2786 PHP_ME(pqconn, poll, ai_pqconn_poll, ZEND_ACC_PUBLIC)
2787 PHP_ME(pqconn, exec, ai_pqconn_exec, ZEND_ACC_PUBLIC)
2788 PHP_ME(pqconn, execAsync, ai_pqconn_exec_async, ZEND_ACC_PUBLIC)
2789 PHP_ME(pqconn, execParams, ai_pqconn_exec_params, ZEND_ACC_PUBLIC)
2790 PHP_ME(pqconn, execParamsAsync, ai_pqconn_exec_params_async, ZEND_ACC_PUBLIC)
2791 PHP_ME(pqconn, prepare, ai_pqconn_prepare, ZEND_ACC_PUBLIC)
2792 PHP_ME(pqconn, prepareAsync, ai_pqconn_prepare_async, ZEND_ACC_PUBLIC)
2793 PHP_ME(pqconn, listen, ai_pqconn_listen, ZEND_ACC_PUBLIC)
2794 PHP_ME(pqconn, listenAsync, ai_pqconn_listen_async, ZEND_ACC_PUBLIC)
2795 PHP_ME(pqconn, notify, ai_pqconn_notify, ZEND_ACC_PUBLIC)
2796 PHP_ME(pqconn, notifyAsync, ai_pqconn_notify_async, ZEND_ACC_PUBLIC)
2797 PHP_ME(pqconn, getResult, ai_pqconn_get_result, ZEND_ACC_PUBLIC)
2798 PHP_ME(pqconn, quote, ai_pqconn_quote, ZEND_ACC_PUBLIC)
2799 PHP_ME(pqconn, quoteName, ai_pqconn_quote_name, ZEND_ACC_PUBLIC)
2800 PHP_ME(pqconn, escapeBytea, ai_pqconn_escape_bytea, ZEND_ACC_PUBLIC)
2801 PHP_ME(pqconn, unescapeBytea, ai_pqconn_unescape_bytea, ZEND_ACC_PUBLIC)
2802 PHP_ME(pqconn, startTransaction, ai_pqconn_start_transaction, ZEND_ACC_PUBLIC)
2803 PHP_ME(pqconn, startTransactionAsync, ai_pqconn_start_transaction_async, ZEND_ACC_PUBLIC)
2804 PHP_ME(pqconn, trace, ai_pqconn_trace, ZEND_ACC_PUBLIC)
2805 {0}
2806 };
2807
2808 ZEND_BEGIN_ARG_INFO_EX(ai_pqtypes_construct, 0, 0, 1)
2809 ZEND_ARG_OBJ_INFO(0, connection, pq\\Connection, 0)
2810 ZEND_END_ARG_INFO();
2811 static PHP_METHOD(pqtypes, __construct) {
2812 zend_error_handling zeh;
2813 zval *zconn;
2814
2815 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
2816 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zconn, php_pqconn_class_entry)) {
2817 php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
2818
2819 if (conn_obj->intern) {
2820 zval *retval = NULL;
2821 php_pqtypes_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2822
2823 obj->intern = ecalloc(1, sizeof(*obj->intern));
2824 obj->intern->conn = conn_obj;
2825 php_pq_object_addref(conn_obj TSRMLS_CC);
2826 zend_hash_init(&obj->intern->types, 300, NULL, ZVAL_PTR_DTOR, 0);
2827
2828 zend_call_method_with_0_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "refresh", &retval);
2829 if (retval) {
2830 zval_ptr_dtor(&retval);
2831 }
2832 } else {
2833 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
2834 }
2835 }
2836 zend_restore_error_handling(&zeh TSRMLS_CC);
2837 }
2838
2839 #define PHP_PQ_TYPES_QUERY \
2840 "select t.oid, t.* " \
2841 "from pg_type t join pg_namespace n on t.typnamespace=n.oid " \
2842 "where typisdefined " \
2843 "and typrelid=0 " \
2844 "and nspname in ('public', 'pg_catalog')"
2845
2846 ZEND_BEGIN_ARG_INFO_EX(ai_pqtypes_refresh, 0, 0, 0)
2847 ZEND_END_ARG_INFO();
2848 static PHP_METHOD(pqtypes, refresh) {
2849 if (SUCCESS == zend_parse_parameters_none()) {
2850 php_pqtypes_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2851
2852 if (obj->intern) {
2853 PGresult *res = PQexec(obj->intern->conn->intern->conn, PHP_PQ_TYPES_QUERY);
2854
2855 php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
2856
2857 if (res) {
2858 if (PGRES_TUPLES_OK == PQresultStatus(res)) {
2859 int r, rows;
2860
2861 for (r = 0, rows = PQntuples(res); r < rows; ++r) {
2862 zval *row = php_pqres_row_to_zval(res, r, PHP_PQRES_FETCH_OBJECT, NULL TSRMLS_CC);
2863 long oid = atol(PQgetvalue(res, r, 0 ));
2864 char *name = PQgetvalue(res, r, 1);
2865
2866 Z_ADDREF_P(row);
2867
2868 zend_hash_index_update(&obj->intern->types, oid, (void *) &row, sizeof(zval *), NULL);
2869 zend_hash_add(&obj->intern->types, name, strlen(name) + 1, (void *) &row, sizeof(zval *), NULL);
2870 }
2871 } else {
2872 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch types (%s)", PHP_PQresultErrorMessage(res));
2873 }
2874 PHP_PQclear(res);
2875 } else {
2876 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch types (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
2877 }
2878
2879 } else {
2880 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Types not initialized");
2881 }
2882 }
2883 }
2884
2885 static zend_function_entry php_pqtypes_methods[] = {
2886 PHP_ME(pqtypes, __construct, ai_pqtypes_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
2887 PHP_ME(pqtypes, refresh, ai_pqtypes_refresh, ZEND_ACC_PUBLIC)
2888 {0}
2889 };
2890
2891 static STATUS php_pqres_iteration(zval *this_ptr, php_pqres_object_t *obj, php_pqres_fetch_t fetch_type, zval ***row TSRMLS_DC)
2892 {
2893 STATUS rv;
2894 php_pqres_fetch_t orig_fetch;
2895
2896 if (!obj) {
2897 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2898 }
2899
2900 if (!obj->intern->iter) {
2901 obj->intern->iter = (php_pqres_iterator_t *) php_pqres_iterator_init(Z_OBJCE_P(getThis()), getThis(), 0 TSRMLS_CC);
2902 obj->intern->iter->zi.funcs->rewind((zend_object_iterator *) obj->intern->iter TSRMLS_CC);
2903 }
2904 orig_fetch = obj->intern->iter->fetch_type;
2905 obj->intern->iter->fetch_type = fetch_type;
2906 if (SUCCESS == (rv = obj->intern->iter->zi.funcs->valid((zend_object_iterator *) obj->intern->iter TSRMLS_CC))) {
2907 obj->intern->iter->zi.funcs->get_current_data((zend_object_iterator *) obj->intern->iter, row TSRMLS_CC);
2908 obj->intern->iter->zi.funcs->move_forward((zend_object_iterator *) obj->intern->iter TSRMLS_CC);
2909 }
2910 obj->intern->iter->fetch_type = orig_fetch;
2911
2912 return rv;
2913 }
2914
2915 typedef struct php_pqres_col {
2916 char *name;
2917 int num;
2918 } php_pqres_col_t;
2919
2920 static STATUS column_nn(php_pqres_object_t *obj, zval *zcol, php_pqres_col_t *col TSRMLS_DC)
2921 {
2922 long index = -1;
2923 char *name = NULL;
2924
2925 switch (Z_TYPE_P(zcol)) {
2926 default:
2927 convert_to_string(zcol);
2928 /* no break */
2929
2930 case IS_STRING:
2931 if (!is_numeric_string(Z_STRVAL_P(zcol), Z_STRLEN_P(zcol), &index, NULL, 0)) {
2932 name = Z_STRVAL_P(zcol);
2933 }
2934 break;
2935
2936 case IS_LONG:
2937 index = Z_LVAL_P(zcol);
2938 break;
2939 }
2940
2941 if (name) {
2942 col->name = name;
2943 col->num = PQfnumber(obj->intern->res, name);
2944 } else {
2945 col->name = PQfname(obj->intern->res, index);
2946 col->num = index;
2947 }
2948
2949 if (!col->name) {
2950 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to find column at index %ld", index);
2951 return FAILURE;
2952 }
2953 if (col->num == -1) {
2954 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to find column with name '%s'", name);
2955 return FAILURE;
2956 }
2957 return SUCCESS;
2958 }
2959
2960 static int compare_index(const void *lptr, const void *rptr TSRMLS_DC)
2961 {
2962 const Bucket *l = *(const Bucket **) lptr;
2963 const Bucket *r = *(const Bucket **) rptr;
2964
2965 if (l->h < r->h) {
2966 return -1;
2967 }
2968 if (l->h > r->h) {
2969 return 1;
2970 }
2971 return 0;
2972 }
2973
2974 ZEND_BEGIN_ARG_INFO_EX(ai_pqres_bind, 0, 0, 2)
2975 ZEND_ARG_INFO(0, col)
2976 ZEND_ARG_INFO(1, ref)
2977 ZEND_END_ARG_INFO();
2978 static PHP_METHOD(pqres, bind) {
2979 zval *zcol, *zref;
2980
2981 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/z", &zcol, &zref)) {
2982 php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
2983
2984 if (obj->intern) {
2985 php_pqres_col_t col;
2986
2987 if (SUCCESS == column_nn(obj, zcol, &col TSRMLS_CC)) {
2988 Z_ADDREF_P(zref);
2989 if (SUCCESS == zend_hash_index_update(&obj->intern->bound, col.num, (void *) &zref, sizeof(zval *), NULL)) {
2990 zend_hash_sort(&obj->intern->bound, zend_qsort, compare_index, 0 TSRMLS_CC);
2991 RETVAL_TRUE;
2992 } else {
2993 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to bind column %s@%d", col.name, col.num);
2994 RETVAL_FALSE;
2995 }
2996 } else {
2997 RETVAL_FALSE;
2998 }
2999 } else {
3000 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Result not initialized");
3001 RETVAL_FALSE;
3002 }
3003 }
3004 }
3005
3006 static int apply_bound(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
3007 {
3008 zval **zvalue, **zbound = p;
3009 zval **zrow = va_arg(argv, zval **);
3010
3011 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(zrow), key->h, (void *) &zvalue)) {
3012 zval_dtor(*zbound);
3013 ZVAL_COPY_VALUE(*zbound, *zvalue);
3014 ZVAL_NULL(*zvalue);
3015 zval_ptr_dtor(zvalue);
3016 Z_ADDREF_P(*zbound);
3017 *zvalue = *zbound;
3018 return ZEND_HASH_APPLY_KEEP;
3019 } else {
3020 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to find column ad index %lu", key->h);
3021 return ZEND_HASH_APPLY_STOP;
3022 }
3023 }
3024
3025 ZEND_BEGIN_ARG_INFO_EX(ai_pqres_fetch_bound, 0, 0, 0)
3026 ZEND_END_ARG_INFO();
3027 static PHP_METHOD(pqres, fetchBound) {
3028 zend_error_handling zeh;
3029
3030 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
3031 if (SUCCESS == zend_parse_parameters_none()) {
3032 php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3033
3034 if (obj->intern) {
3035 zval **row = NULL;
3036
3037 if (SUCCESS == php_pqres_iteration(getThis(), obj, PHP_PQRES_FETCH_ARRAY, &row TSRMLS_CC)) {
3038 if (row) {
3039 zend_hash_apply_with_arguments(&obj->intern->bound TSRMLS_CC, apply_bound, 1, row);
3040 RETVAL_ZVAL(*row, 1, 0);
3041 }
3042 }
3043 }
3044 }
3045 zend_restore_error_handling(&zeh TSRMLS_CC);
3046 }
3047
3048 ZEND_BEGIN_ARG_INFO_EX(ai_pqres_fetch_row, 0, 0, 0)
3049 ZEND_ARG_INFO(0, fetch_type)
3050 ZEND_END_ARG_INFO();
3051 static PHP_METHOD(pqres, fetchRow) {
3052 zend_error_handling zeh;
3053 php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3054 long fetch_type = -1;
3055
3056 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
3057 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &fetch_type)) {
3058 if (obj->intern) {
3059 zval **row = NULL;
3060
3061 if (fetch_type == -1) {
3062 fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : PHP_PQRES_FETCH_ARRAY;
3063 }
3064 php_pqres_iteration(getThis(), obj, fetch_type, &row TSRMLS_CC);
3065
3066 if (row) {
3067 RETVAL_ZVAL(*row, 1, 0);
3068 }
3069 } else {
3070 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Result not initialized");
3071 }
3072 }
3073 zend_restore_error_handling(&zeh TSRMLS_CC);
3074 }
3075
3076 static zval **column_at(zval *row, int col TSRMLS_DC)
3077 {
3078 zval **data = NULL;
3079 HashTable *ht = HASH_OF(row);
3080 int count = zend_hash_num_elements(ht);
3081
3082 if (col < count) {
3083 zend_hash_internal_pointer_reset(ht);
3084 while (col-- > 0) {
3085 zend_hash_move_forward(ht);
3086 }
3087 zend_hash_get_current_data(ht, (void *) &data);
3088 } else {
3089 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Column index %d exceeds column count %d", col, count);
3090 }
3091 return data;
3092 }
3093
3094 ZEND_BEGIN_ARG_INFO_EX(ai_pqres_fetch_col, 0, 0, 0)
3095 ZEND_ARG_INFO(0, col_num)
3096 ZEND_END_ARG_INFO();
3097 static PHP_METHOD(pqres, fetchCol) {
3098 zend_error_handling zeh;
3099 long fetch_col = 0;
3100
3101 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
3102 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &fetch_col)) {
3103 php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3104
3105 if (obj->intern) {
3106 zval **row = NULL;
3107
3108 php_pqres_iteration(getThis(), obj, obj->intern->iter ? obj->intern->iter->fetch_type : 0, &row TSRMLS_CC);
3109
3110 if (row) {
3111 zval **col = column_at(*row, fetch_col TSRMLS_CC);
3112
3113 if (col) {
3114 RETVAL_ZVAL(*col, 1, 0);
3115 }
3116 }
3117 } else {
3118 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Result not initialized");
3119 }
3120 }
3121 zend_restore_error_handling(&zeh TSRMLS_CC);
3122 }
3123
3124 static int apply_to_col(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
3125 {
3126 zval **c = p;
3127 php_pqres_object_t *obj = va_arg(argv, php_pqres_object_t *);
3128 php_pqres_col_t *col, **cols = va_arg(argv, php_pqres_col_t **);
3129 STATUS *rv = va_arg(argv, STATUS *);
3130
3131 col = *cols;
3132 if (SUCCESS == column_nn(obj, *c, col TSRMLS_CC)) {
3133 *rv = SUCCESS;
3134 } else {
3135 *rv = FAILURE;
3136 return ZEND_HASH_APPLY_STOP;
3137 }
3138 ++*cols;
3139
3140 return ZEND_HASH_APPLY_KEEP;
3141 }
3142
3143 static php_pqres_col_t *php_pqres_convert_to_cols(php_pqres_object_t *obj, HashTable *ht TSRMLS_DC)
3144 {
3145 php_pqres_col_t *tmp, *cols = ecalloc(zend_hash_num_elements(ht), sizeof(*cols));
3146 STATUS rv = SUCCESS;
3147
3148 tmp = cols;
3149 zend_hash_apply_with_arguments(ht TSRMLS_CC, apply_to_col, 2, obj, &tmp, &rv);
3150
3151 if (SUCCESS == rv) {
3152 return cols;
3153 } else {
3154 efree(cols);
3155 return NULL;
3156 }
3157 }
3158
3159 ZEND_BEGIN_ARG_INFO_EX(ai_pqres_map, 0, 0, 0)
3160 ZEND_ARG_INFO(0, keys)
3161 ZEND_ARG_INFO(0, vals)
3162 ZEND_ARG_INFO(0, fetch_type)
3163 ZEND_END_ARG_INFO();
3164 static PHP_METHOD(pqres, map) {
3165 zend_error_handling zeh;
3166 zval *zkeys = 0, *zvals = 0;
3167 long fetch_type = -1;
3168
3169
3170 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
3171 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z/!z/!l", &zkeys, &zvals, &fetch_type)) {
3172 php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3173
3174 if (obj->intern) {
3175 int ks = 0, vs = 0;
3176 php_pqres_col_t def = {PQfname(obj->intern->res, 0), 0}, *keys = NULL, *vals = NULL;
3177
3178 if (zkeys) {
3179 convert_to_array(zkeys);
3180
3181 if ((ks = zend_hash_num_elements(Z_ARRVAL_P(zkeys)))) {
3182 keys = php_pqres_convert_to_cols(obj, Z_ARRVAL_P(zkeys) TSRMLS_CC);
3183 } else {
3184 ks = 1;
3185 keys = &def;
3186 }
3187 } else {
3188 ks = 1;
3189 keys = &def;
3190 }
3191 if (zvals) {
3192 convert_to_array(zvals);
3193
3194 if ((vs = zend_hash_num_elements(Z_ARRVAL_P(zvals)))) {
3195 vals = php_pqres_convert_to_cols(obj, Z_ARRVAL_P(zvals) TSRMLS_CC);
3196 }
3197 }
3198
3199 if (fetch_type == -1) {
3200 fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : PHP_PQRES_FETCH_ARRAY;
3201 }
3202
3203 if (keys) {
3204 int rows, r;
3205 zval **cur;
3206
3207 switch (fetch_type) {
3208 case PHP_PQRES_FETCH_ARRAY:
3209 case PHP_PQRES_FETCH_ASSOC:
3210 array_init(return_value);
3211 break;
3212 case PHP_PQRES_FETCH_OBJECT:
3213 object_init(return_value);
3214 break;
3215 }
3216 for (r = 0, rows = PQntuples(obj->intern->res); r < rows; ++r) {
3217 int k, v;
3218
3219 cur = &return_value;
3220 for (k = 0; k < ks; ++k) {
3221 char *key = PQgetvalue(obj->intern->res, r, keys[k].num);
3222 int len = PQgetlength(obj->intern->res, r, keys[k].num);
3223
3224 if (SUCCESS != zend_symtable_find(HASH_OF(*cur), key, len + 1, (void *) &cur)) {
3225 zval *tmp;
3226
3227 MAKE_STD_ZVAL(tmp);
3228 switch (fetch_type) {
3229 case PHP_PQRES_FETCH_ARRAY:
3230 case PHP_PQRES_FETCH_ASSOC:
3231 array_init(tmp);
3232 break;
3233 case PHP_PQRES_FETCH_OBJECT:
3234 object_init(tmp);
3235 break;
3236 }
3237 if (SUCCESS != zend_symtable_update(HASH_OF(*cur), key, len + 1, (void *) &tmp, sizeof(zval *), (void *) &cur)) {
3238 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create map");
3239 goto err;
3240 }
3241 }
3242 }
3243 if (vals && vs) {
3244 for (v = 0; v < vs; ++v) {
3245 char *val = PQgetvalue(obj->intern->res, r, vals[v].num);
3246 int len = PQgetlength(obj->intern->res, r, vals[v].num);
3247
3248 switch (fetch_type) {
3249 case PHP_PQRES_FETCH_ARRAY:
3250 add_index_stringl(*cur, vals[v].num, val, len, 1);
3251 break;
3252 case PHP_PQRES_FETCH_ASSOC:
3253 add_assoc_stringl(*cur, vals[v].name, val, len, 1);
3254 break;
3255 case PHP_PQRES_FETCH_OBJECT:
3256 add_property_stringl(*cur, vals[v].name, val, len, 1);
3257 break;
3258 }
3259 }
3260 } else {
3261 php_pqres_row_to_zval(obj->intern->res, r, fetch_type, cur TSRMLS_CC);
3262 }
3263 }
3264 }
3265
3266 err:
3267 if (keys && keys != &def) {
3268 efree(keys);
3269 }
3270 if (vals) {
3271 efree(vals);
3272 }
3273 } else {
3274 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Result not initialized");
3275 }
3276 }
3277 zend_restore_error_handling(&zeh TSRMLS_CC);
3278 }
3279
3280 ZEND_BEGIN_ARG_INFO_EX(ai_pqres_count, 0, 0, 0)
3281 ZEND_END_ARG_INFO();
3282 static PHP_METHOD(pqres, count) {
3283 if (SUCCESS == zend_parse_parameters_none()) {
3284 long count;
3285
3286 if (SUCCESS == php_pqres_count_elements(getThis(), &count TSRMLS_CC)) {
3287 RETVAL_LONG(count);
3288 } else {
3289 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Result not initialized");
3290 }
3291 }
3292 }
3293
3294 static zend_function_entry php_pqres_methods[] = {
3295 PHP_ME(pqres, bind, ai_pqres_bind, ZEND_ACC_PUBLIC)
3296 PHP_ME(pqres, fetchBound, ai_pqres_fetch_bound, ZEND_ACC_PUBLIC)
3297 PHP_ME(pqres, fetchRow, ai_pqres_fetch_row, ZEND_ACC_PUBLIC)
3298 PHP_ME(pqres, fetchCol, ai_pqres_fetch_col, ZEND_ACC_PUBLIC)
3299 PHP_ME(pqres, count, ai_pqres_count, ZEND_ACC_PUBLIC)
3300 PHP_ME(pqres, map, ai_pqres_map, ZEND_ACC_PUBLIC)
3301 {0}
3302 };
3303
3304 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_construct, 0, 0, 3)
3305 ZEND_ARG_OBJ_INFO(0, Connection, pq\\Connection, 0)
3306 ZEND_ARG_INFO(0, type)
3307 ZEND_ARG_INFO(0, query)
3308 ZEND_ARG_ARRAY_INFO(0, types, 1)
3309 ZEND_ARG_INFO(0, async)
3310 ZEND_END_ARG_INFO();
3311 static PHP_METHOD(pqstm, __construct) {
3312 zend_error_handling zeh;
3313 zval *zconn, *ztypes = NULL;
3314 char *name_str, *query_str;
3315 int name_len, *query_len;
3316 zend_bool async = 0;
3317
3318 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
3319 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oss|a/!b", &zconn, php_pqconn_class_entry, &name_str, &name_len, &query_str, &query_len, &ztypes, &async)) {
3320 php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3321 php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
3322
3323 if (conn_obj->intern) {
3324 STATUS rv;
3325 if (async) {
3326 conn_obj->intern->poller = PQconsumeInput;
3327 rv = php_pqconn_prepare_async(zconn, conn_obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC);
3328 } else {
3329 rv = php_pqconn_prepare(zconn, conn_obj, name_str, query_str, ztypes ? Z_ARRVAL_P(ztypes) : NULL TSRMLS_CC);
3330 php_pqconn_notify_listeners(conn_obj TSRMLS_CC);
3331 }
3332
3333 if (SUCCESS == rv) {
3334 php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
3335
3336 php_pq_object_addref(conn_obj TSRMLS_CC);
3337 stm->conn = conn_obj;
3338 stm->name = estrdup(name_str);
3339 ZEND_INIT_SYMTABLE(&stm->bound);
3340 obj->intern = stm;
3341 }
3342 } else {
3343 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
3344 }
3345 }
3346 zend_restore_error_handling(&zeh TSRMLS_CC);
3347 }
3348 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_bind, 0, 0, 2)
3349 ZEND_ARG_INFO(0, param_no)
3350 ZEND_ARG_INFO(1, param_ref)
3351 ZEND_END_ARG_INFO();
3352 static PHP_METHOD(pqstm, bind) {
3353 long param_no;
3354 zval *param_ref;
3355
3356 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", &param_no, &param_ref)) {
3357 php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3358
3359 if (obj->intern) {
3360 Z_ADDREF_P(param_ref);
3361 zend_hash_index_update(&obj->intern->bound, param_no, (void *) &param_ref, sizeof(zval *), NULL);
3362 zend_hash_sort(&obj->intern->bound, zend_qsort, compare_index, 0 TSRMLS_CC);
3363 } else {
3364 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Statement not initialized");
3365 }
3366 }
3367 }
3368
3369 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_exec, 0, 0, 0)
3370 ZEND_ARG_ARRAY_INFO(0, params, 1)
3371 ZEND_END_ARG_INFO();
3372 static PHP_METHOD(pqstm, exec) {
3373 zend_error_handling zeh;
3374 zval *zparams = NULL;
3375
3376 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
3377 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &zparams)) {
3378 php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3379
3380 if (obj->intern) {
3381 if (obj->intern->conn->intern) {
3382 int count = 0;
3383 char **params = NULL;
3384 HashTable zdtor;
3385 PGresult *res;
3386
3387 ZEND_INIT_SYMTABLE(&zdtor);
3388
3389 if (zparams) {
3390 count = php_pq_params_to_array(Z_ARRVAL_P(zparams), &params, &zdtor TSRMLS_CC);
3391 } else {
3392 count = php_pq_params_to_array(&obj->intern->bound, &params, &zdtor TSRMLS_CC);
3393 }
3394
3395 res = PQexecPrepared(obj->intern->conn->intern->conn, obj->intern->name, count, (const char *const*) params, NULL, NULL, 0);
3396
3397 if (params) {
3398 efree(params);
3399 }
3400 zend_hash_destroy(&zdtor);
3401
3402 php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
3403
3404 if (res) {
3405 if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
3406 php_pqres_t *r = ecalloc(1, sizeof(*r));
3407
3408 r->res = res;
3409 ZEND_INIT_SYMTABLE(&r->bound);
3410 return_value->type = IS_OBJECT;
3411 return_value->value.obj = php_pqres_create_object_ex(php_pqres_class_entry, r, NULL TSRMLS_CC);
3412 }
3413 } else {
3414 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute statement (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
3415 }
3416 } else {
3417 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
3418 }
3419 } else {
3420 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Statement not initialized");
3421 }
3422 }
3423 zend_restore_error_handling(&zeh TSRMLS_CC);
3424 }
3425
3426 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_exec_async, 0, 0, 0)
3427 ZEND_ARG_ARRAY_INFO(0, params, 1)
3428 ZEND_ARG_INFO(0, callable)
3429 ZEND_END_ARG_INFO();
3430 static PHP_METHOD(pqstm, execAsync) {
3431 zend_error_handling zeh;
3432 zval *zparams = NULL;
3433 php_pq_callback_t resolver = {{0}};
3434
3435 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
3436 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!f", &zparams, &resolver.fci, &resolver.fcc)) {
3437 php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3438
3439 if (obj->intern) {
3440 if (obj->intern->conn->intern) {
3441 int count;
3442 char **params = NULL;
3443 HashTable zdtor;
3444
3445 if (zparams) {
3446 ZEND_INIT_SYMTABLE(&zdtor);
3447 count = php_pq_params_to_array(Z_ARRVAL_P(zparams), &params, &zdtor TSRMLS_CC);
3448 }
3449
3450 php_pq_callback_dtor(&obj->intern->conn->intern->onevent);
3451 if (resolver.fci.size > 0) {
3452 obj->intern->conn->intern->onevent = resolver;
3453 php_pq_callback_addref(&obj->intern->conn->intern->onevent);
3454 }
3455
3456 obj->intern->conn->intern->poller = PQconsumeInput;
3457
3458 if (PQsendQueryPrepared(obj->intern->conn->intern->conn, obj->intern->name, count, (const char *const*) params, NULL, NULL, 0)) {
3459 if (obj->intern->conn->intern->unbuffered) {
3460 if (!PQsetSingleRowMode(obj->intern->conn->intern->conn)) {
3461 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
3462 }
3463 }
3464 RETVAL_TRUE;
3465 } else {
3466 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute statement (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
3467 RETVAL_FALSE;
3468 }
3469
3470 if (params) {
3471 efree(params);
3472 }
3473 if (zparams) {
3474 zend_hash_destroy(&zdtor);
3475 }
3476
3477 php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
3478
3479 } else {
3480 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
3481 RETVAL_FALSE;
3482 }
3483 } else {
3484 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Statement not initialized");
3485 RETVAL_FALSE;
3486 }
3487 }
3488 zend_restore_error_handling(&zeh TSRMLS_CC);
3489 }
3490
3491 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_desc, 0, 0, 0)
3492 ZEND_END_ARG_INFO();
3493 static PHP_METHOD(pqstm, desc) {
3494 zend_error_handling zeh;
3495
3496 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
3497 if (SUCCESS == zend_parse_parameters_none()) {
3498 php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3499
3500 if (obj->intern) {
3501 if (obj->intern->conn->intern) {
3502 PGresult *res = PQdescribePrepared(obj->intern->conn->intern->conn, obj->intern->name);
3503
3504 php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
3505
3506 if (res) {
3507 if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
3508 int p, params;
3509
3510 array_init(return_value);
3511 for (p = 0, params = PQnparams(res); p < params; ++p) {
3512 add_next_index_long(return_value, PQparamtype(res, p));
3513 }
3514 }
3515 } else {
3516 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not describe statement (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
3517 }
3518 } else {
3519 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
3520 }
3521 } else {
3522 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Statement not initialized");
3523 }
3524 }
3525 zend_restore_error_handling(&zeh TSRMLS_CC);
3526 }
3527
3528 static zend_function_entry php_pqstm_methods[] = {
3529 PHP_ME(pqstm, __construct, ai_pqstm_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
3530 PHP_ME(pqstm, bind, ai_pqstm_bind, ZEND_ACC_PUBLIC)
3531 PHP_ME(pqstm, exec, ai_pqstm_exec, ZEND_ACC_PUBLIC)
3532 PHP_ME(pqstm, desc, ai_pqstm_desc, ZEND_ACC_PUBLIC)
3533 PHP_ME(pqstm, execAsync, ai_pqstm_exec_async, ZEND_ACC_PUBLIC)
3534 {0}
3535 };
3536
3537 ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_construct, 0, 0, 1)
3538 ZEND_ARG_OBJ_INFO(0, connection, pq\\Connection, 0)
3539 ZEND_ARG_INFO(0, async)
3540 ZEND_ARG_INFO(0, isolation)
3541 ZEND_ARG_INFO(0, readonly)
3542 ZEND_ARG_INFO(0, deferrable)
3543 ZEND_END_ARG_INFO();
3544 static PHP_METHOD(pqtxn, __construct) {
3545 zend_error_handling zeh;
3546 zval *zconn;
3547 long isolation = PHP_PQTXN_READ_COMMITTED;
3548 zend_bool async = 0, readonly = 0, deferrable = 0;
3549
3550 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
3551 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|blbb", &zconn, php_pqconn_class_entry, &async, &isolation, &readonly, &deferrable)) {
3552 STATUS rv;
3553 php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3554 php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
3555
3556 if (conn_obj->intern) {
3557 if (async) {
3558 rv = php_pqconn_start_transaction_async(zconn, conn_obj, isolation, readonly, deferrable TSRMLS_CC);
3559 } else {
3560 rv = php_pqconn_start_transaction(zconn, conn_obj, isolation, readonly, deferrable TSRMLS_CC);
3561 }
3562
3563 if (SUCCESS == rv) {
3564 obj->intern = ecalloc(1, sizeof(*obj->intern));
3565
3566 php_pq_object_addref(conn_obj TSRMLS_CC);
3567 obj->intern->conn = conn_obj;
3568 obj->intern->isolation = isolation;
3569 obj->intern->readonly = readonly;
3570 obj->intern->deferrable = deferrable;
3571 }
3572 } else {
3573 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Connection not initialized");
3574 }
3575 }
3576 zend_restore_error_handling(&zeh TSRMLS_CC);
3577 }
3578
3579 ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_savepoint, 0, 0, 0)
3580 ZEND_END_ARG_INFO();
3581 static PHP_METHOD(pqtxn, savepoint) {
3582 zend_error_handling zeh;
3583
3584 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
3585 if (SUCCESS == zend_parse_parameters_none()) {
3586 php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3587
3588 if (obj->intern) {
3589 char *cmd;
3590 PGresult *res;
3591
3592 spprintf(&cmd, 0, "SAVEPOINT \"%u\"", ++obj->intern->savepoint);
3593 res = PQexec(obj->intern->conn->intern->conn, cmd);
3594
3595 if (res) {
3596 php_pqres_success(res TSRMLS_CC);
3597 PHP_PQclear(res);
3598 } else {
3599 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn));
3600 }
3601
3602 efree(cmd);
3603 } else {
3604 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
3605 }
3606 }
3607 zend_restore_error_handling(&zeh TSRMLS_CC);
3608 }
3609
3610 ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_savepoint_async, 0, 0, 0)
3611 ZEND_END_ARG_INFO();
3612 static PHP_METHOD(pqtxn, savepointAsync) {
3613 zend_error_handling zeh;
3614
3615 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
3616 if (SUCCESS == zend_parse_parameters_none()) {
3617 php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3618
3619 if (obj->intern) {
3620 char *cmd;
3621
3622 spprintf(&cmd, 0, "SAVEPOINT \"%u\"", ++obj->intern->savepoint);
3623 if (!PQsendQuery(obj->intern->conn->intern->conn, cmd)) {
3624 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to create %s (%s)", cmd, PHP_PQerrorMessage(obj->intern->conn->intern->conn));
3625 }
3626
3627 efree(cmd);
3628 } else {
3629 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pq\\Transaction not initialized");
3630 }
3631 }
3632 zend_restore_error_handling(&zeh TSRMLS_CC);
3633 }
3634
3635 ZEND_BEGIN_ARG_INFO_EX(ai_pqtxn_commit, 0, 0, 0)
3636 ZEND_END_ARG_INFO();
3637 static PHP_METHOD(pqtxn, commit) {
3638 zend_error_handling zeh;
3639
3640 zend_replace_error_handling(EH_THROW, NULL, &zeh TSRMLS_CC);
3641 if (SUCCESS == zend_parse_parameters_none()) {
3642 php_pqtxn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
3643
3644 if (obj->intern) {
3645 PGresult *res;
3646
3647 if (obj->intern->savepoint) {
3648 char *cmd;
3649
3650 spprintf(&cmd, 0, "RELEASE SAVEPOINT \"%u\"", obj->intern->savepoint--);
3651 res =