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