check for PQconninfo
[m6w6/ext-pq] / src / php_pqconn.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: pq |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <php.h>
18
19 #define SMART_STR_PREALLOC 256
20 #include <ext/standard/php_smart_str.h>
21
22 #include <libpq-events.h>
23 #include <fnmatch.h>
24
25 #include "php_pq.h"
26 #include "php_pq_misc.h"
27 #include "php_pq_object.h"
28 #include "php_pqexc.h"
29 #include "php_pqconn.h"
30 #include "php_pqconn_event.h"
31 #include "php_pqres.h"
32 #include "php_pqstm.h"
33 #include "php_pqtxn.h"
34 #include "php_pqcur.h"
35
36 zend_class_entry *php_pqconn_class_entry;
37 static zend_object_handlers php_pqconn_object_handlers;
38 static HashTable php_pqconn_object_prophandlers;
39
40 /*
41 static void php_pqconn_del_eventhandler(php_pqconn_object_t *obj, const char *type_str, size_t type_len, ulong id TSRMLS_DC)
42 {
43 zval **evhs;
44
45 if (SUCCESS == zend_hash_find(&obj->intern->eventhandlers, type_str, type_len + 1, (void *) &evhs)) {
46 zend_hash_index_del(Z_ARRVAL_PP(evhs), id);
47 }
48 }
49 */
50
51 static ulong php_pqconn_add_eventhandler(php_pqconn_object_t *obj, const char *type_str, size_t type_len, php_pq_callback_t *cb TSRMLS_DC)
52 {
53 ulong h;
54 HashTable *evhs;
55
56 if (SUCCESS != zend_hash_find(&obj->intern->eventhandlers, type_str, type_len + 1, (void *) &evhs)) {
57 HashTable evh;
58
59 zend_hash_init(&evh, 1, NULL, (dtor_func_t) php_pq_callback_dtor, 0);
60 zend_hash_add(&obj->intern->eventhandlers, type_str, type_len + 1, (void *) &evh, sizeof(evh), (void *) &evhs);
61 }
62
63 php_pq_callback_addref(cb);
64 h = zend_hash_next_free_element(evhs);
65 zend_hash_index_update(evhs, h, (void *) cb, sizeof(*cb), NULL);
66
67 return h;
68 }
69
70 static void php_pqconn_object_free(void *o TSRMLS_DC)
71 {
72 php_pqconn_object_t *obj = o;
73 #if DBG_GC
74 fprintf(stderr, "FREE conn(#%d) %p\n", obj->zv.handle, obj);
75 #endif
76 if (obj->intern) {
77 php_pq_callback_dtor(&obj->intern->onevent);
78 php_resource_factory_handle_dtor(&obj->intern->factory, obj->intern->conn TSRMLS_CC);
79 php_resource_factory_dtor(&obj->intern->factory);
80 zend_hash_destroy(&obj->intern->listeners);
81 zend_hash_destroy(&obj->intern->converters);
82 zend_hash_destroy(&obj->intern->eventhandlers);
83 efree(obj->intern);
84 obj->intern = NULL;
85 }
86 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
87 efree(obj);
88 }
89
90
91 zend_object_value php_pqconn_create_object_ex(zend_class_entry *ce, php_pqconn_t *intern, php_pqconn_object_t **ptr TSRMLS_DC)
92 {
93 php_pqconn_object_t *o;
94
95 o = ecalloc(1, sizeof(*o));
96 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
97 object_properties_init((zend_object *) o, ce);
98 o->prophandler = &php_pqconn_object_prophandlers;
99
100 if (ptr) {
101 *ptr = o;
102 }
103
104 if (intern) {
105 o->intern = intern;
106 }
107
108 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqconn_object_free, NULL TSRMLS_CC);
109 o->zv.handlers = &php_pqconn_object_handlers;
110
111 return o->zv;
112 }
113
114 static zend_object_value php_pqconn_create_object(zend_class_entry *class_type TSRMLS_DC)
115 {
116 return php_pqconn_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
117 }
118
119 static void php_pqconn_object_read_status(zval *object, void *o, zval *return_value TSRMLS_DC)
120 {
121 php_pqconn_object_t *obj = o;
122
123 RETVAL_LONG(PQstatus(obj->intern->conn));
124 }
125
126 static void php_pqconn_object_read_transaction_status(zval *object, void *o, zval *return_value TSRMLS_DC)
127 {
128 php_pqconn_object_t *obj = o;
129
130 RETVAL_LONG(PQtransactionStatus(obj->intern->conn));
131 }
132
133 static void php_pqconn_object_read_error_message(zval *object, void *o, zval *return_value TSRMLS_DC)
134 {
135 php_pqconn_object_t *obj = o;
136 char *error = PHP_PQerrorMessage(obj->intern->conn);
137
138 if (error) {
139 RETVAL_STRING(error, 1);
140 } else {
141 RETVAL_NULL();
142 }
143 }
144
145 static int apply_notify_listener(void *p, void *arg TSRMLS_DC)
146 {
147 php_pq_callback_t *listener = p;
148 PGnotify *nfy = arg;
149 zval *zpid, *zchannel, *zmessage;
150
151 MAKE_STD_ZVAL(zpid);
152 ZVAL_LONG(zpid, nfy->be_pid);
153 MAKE_STD_ZVAL(zchannel);
154 ZVAL_STRING(zchannel, nfy->relname, 1);
155 MAKE_STD_ZVAL(zmessage);
156 ZVAL_STRING(zmessage, nfy->extra, 1);
157
158 zend_fcall_info_argn(&listener->fci TSRMLS_CC, 3, &zchannel, &zmessage, &zpid);
159 zend_fcall_info_call(&listener->fci, &listener->fcc, NULL, NULL TSRMLS_CC);
160
161 zval_ptr_dtor(&zchannel);
162 zval_ptr_dtor(&zmessage);
163 zval_ptr_dtor(&zpid);
164
165 return ZEND_HASH_APPLY_KEEP;
166 }
167
168 static int apply_notify_listeners(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
169 {
170 HashTable *listeners = p;
171 PGnotify *nfy = va_arg(argv, PGnotify *);
172
173 if (0 == fnmatch(key->arKey, nfy->relname, 0)) {
174 zend_hash_apply_with_argument(listeners, apply_notify_listener, nfy TSRMLS_CC);
175 }
176
177 return ZEND_HASH_APPLY_KEEP;
178 }
179
180 void php_pqconn_notify_listeners(php_pqconn_object_t *obj TSRMLS_DC)
181 {
182 PGnotify *nfy;
183
184 while ((nfy = PQnotifies(obj->intern->conn))) {
185 zend_hash_apply_with_arguments(&obj->intern->listeners TSRMLS_CC, apply_notify_listeners, 1, nfy);
186 PQfreemem(nfy);
187 }
188 }
189
190 static void php_pqconn_object_read_busy(zval *object, void *o, zval *return_value TSRMLS_DC)
191 {
192 php_pqconn_object_t *obj = o;
193
194 RETVAL_BOOL(PQisBusy(obj->intern->conn));
195 }
196
197 static void php_pqconn_object_read_encoding(zval *object, void *o, zval *return_value TSRMLS_DC)
198 {
199 php_pqconn_object_t *obj = o;
200
201 RETVAL_STRING(pg_encoding_to_char(PQclientEncoding(obj->intern->conn)), 1);
202 }
203
204 static void php_pqconn_object_write_encoding(zval *object, void *o, zval *value TSRMLS_DC)
205 {
206 php_pqconn_object_t *obj = o;
207 zval *zenc = value;
208
209 if (Z_TYPE_P(value) != IS_STRING) {
210 if (Z_REFCOUNT_P(value) > 1) {
211 zval *tmp;
212 MAKE_STD_ZVAL(tmp);
213 ZVAL_ZVAL(tmp, zenc, 1, 0);
214 convert_to_string(tmp);
215 zenc = tmp;
216 } else {
217 convert_to_string_ex(&zenc);
218 }
219 }
220
221 if (0 > PQsetClientEncoding(obj->intern->conn, Z_STRVAL_P(zenc))) {
222 php_error(E_NOTICE, "Unrecognized encoding '%s'", Z_STRVAL_P(zenc));
223 }
224
225 if (zenc != value) {
226 zval_ptr_dtor(&zenc);
227 }
228 }
229
230 static void php_pqconn_object_read_unbuffered(zval *object, void *o, zval *return_value TSRMLS_DC)
231 {
232 php_pqconn_object_t *obj = o;
233
234 RETVAL_BOOL(obj->intern->unbuffered);
235 }
236
237 static void php_pqconn_object_write_unbuffered(zval *object, void *o, zval *value TSRMLS_DC)
238 {
239 php_pqconn_object_t *obj = o;
240
241 obj->intern->unbuffered = z_is_true(value);
242 }
243
244 static void php_pqconn_object_read_db(zval *object, void *o, zval *return_value TSRMLS_DC)
245 {
246 php_pqconn_object_t *obj = o;
247 char *db = PQdb(obj->intern->conn);
248
249 if (db) {
250 RETVAL_STRING(db, 1);
251 } else {
252 RETVAL_EMPTY_STRING();
253 }
254 }
255
256 static void php_pqconn_object_read_user(zval *object, void *o, zval *return_value TSRMLS_DC)
257 {
258 php_pqconn_object_t *obj = o;
259 char *user = PQuser(obj->intern->conn);
260
261 if (user) {
262 RETVAL_STRING(user, 1);
263 } else {
264 RETVAL_EMPTY_STRING();
265 }
266 }
267
268 static void php_pqconn_object_read_pass(zval *object, void *o, zval *return_value TSRMLS_DC)
269 {
270 php_pqconn_object_t *obj = o;
271 char *pass = PQpass(obj->intern->conn);
272
273 if (pass) {
274 RETVAL_STRING(pass, 1);
275 } else {
276 RETVAL_EMPTY_STRING();
277 }
278 }
279
280 static void php_pqconn_object_read_host(zval *object, void *o, zval *return_value TSRMLS_DC)
281 {
282 php_pqconn_object_t *obj = o;
283 char *host = PQhost(obj->intern->conn);
284
285 if (host) {
286 RETVAL_STRING(host, 1);
287 } else {
288 RETVAL_EMPTY_STRING();
289 }
290 }
291
292 static void php_pqconn_object_read_port(zval *object, void *o, zval *return_value TSRMLS_DC)
293 {
294 php_pqconn_object_t *obj = o;
295 char *port = PQport(obj->intern->conn);
296
297 if (port) {
298 RETVAL_STRING(port, 1);
299 } else {
300 RETVAL_EMPTY_STRING();
301 }
302 }
303
304 #if HAVE_PQCONNINFO
305 static void php_pqconn_object_read_params(zval *object, void *o, zval *return_value TSRMLS_DC)
306 {
307 php_pqconn_object_t *obj = o;
308 PQconninfoOption *ptr, *params = PQconninfo(obj->intern->conn);
309
310 array_init(return_value);
311
312 if (params) {
313 for (ptr = params; ptr->keyword; ++ptr) {
314 if (ptr->val) {
315 add_assoc_string(return_value, ptr->keyword, ptr->val, 1);
316 } else {
317 add_assoc_null(return_value, ptr->keyword);
318 }
319 }
320 PQconninfoFree(params);
321 }
322 }
323 #endif
324
325 static void php_pqconn_object_read_options(zval *object, void *o, zval *return_value TSRMLS_DC)
326 {
327 php_pqconn_object_t *obj = o;
328 char *options = PQoptions(obj->intern->conn);
329
330 if (options) {
331 RETVAL_STRING(options, 1);
332 } else {
333 RETVAL_EMPTY_STRING();
334 }
335 }
336
337 static int apply_read_event_handler_ex(void *p, void *arg TSRMLS_DC)
338 {
339 HashTable *rv = arg;
340 zval *zcb = php_pq_callback_to_zval(p);
341
342 zend_hash_next_index_insert(rv, &zcb, sizeof(zval *), NULL);
343
344 return ZEND_HASH_APPLY_KEEP;
345 }
346
347 static int apply_read_event_handlers(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
348 {
349 HashTable *evhs = p, *rv = va_arg(argv, HashTable *);
350 zval *entry, **entry_ptr;
351
352 MAKE_STD_ZVAL(entry);
353 array_init_size(entry, zend_hash_num_elements(evhs));
354
355 if (key->nKeyLength) {
356 zend_hash_add(rv, key->arKey, key->nKeyLength, &entry, sizeof(zval *), (void *) &entry_ptr);
357 } else {
358 zend_hash_index_update(rv, key->h, &entry, sizeof(zval *), (void *) &entry_ptr);
359 }
360
361 zend_hash_apply_with_argument(evhs, apply_read_event_handler_ex, Z_ARRVAL_PP(entry_ptr) TSRMLS_CC);
362
363 return ZEND_HASH_APPLY_KEEP;
364 }
365 static void php_pqconn_object_read_event_handlers(zval *object, void *o, zval *return_value TSRMLS_DC)
366 {
367 php_pqconn_object_t *obj = o;
368
369 array_init(return_value);
370 zend_hash_apply_with_arguments(&obj->intern->eventhandlers TSRMLS_CC, apply_read_event_handlers, 1, Z_ARRVAL_P(return_value) TSRMLS_CC);
371 }
372
373 static void php_pqconn_object_read_def_fetch_type(zval *object, void *o, zval *return_value TSRMLS_DC)
374 {
375 php_pqconn_object_t *obj = o;
376
377 RETVAL_LONG(obj->intern->default_fetch_type);
378 }
379 static void php_pqconn_object_write_def_fetch_type(zval *object, void *o, zval *value TSRMLS_DC)
380 {
381 php_pqconn_object_t *obj = o;
382 zval *zft = value;
383
384 if (Z_TYPE_P(zft) != IS_LONG) {
385 if (Z_REFCOUNT_P(zft) > 1) {
386 zval *tmp;
387 MAKE_STD_ZVAL(tmp);
388 ZVAL_ZVAL(tmp, zft, 1, 0);
389 convert_to_long(tmp);
390 zft = tmp;
391 } else {
392 convert_to_long_ex(&zft);
393 }
394 }
395
396 obj->intern->default_fetch_type = Z_LVAL_P(zft) & 0x3; /* two bits only */
397
398 if (zft != value) {
399 zval_ptr_dtor(&zft);
400 }
401 }
402
403 static void php_pqconn_object_read_def_txn_isolation(zval *object, void *o, zval *return_value TSRMLS_DC)
404 {
405 php_pqconn_object_t *obj = o;
406
407 RETVAL_LONG(obj->intern->default_txn_isolation);
408 }
409 static void php_pqconn_object_write_def_txn_isolation(zval *object, void *o, zval *value TSRMLS_DC)
410 {
411 php_pqconn_object_t *obj = o;
412 zval *zti = value;
413
414 if (Z_TYPE_P(zti) != IS_LONG) {
415 if (Z_REFCOUNT_P(zti) > 1) {
416 zval *tmp;
417 MAKE_STD_ZVAL(tmp);
418 ZVAL_ZVAL(tmp, zti, 1, 0);
419 convert_to_long(tmp);
420 zti = tmp;
421 } else {
422 convert_to_long_ex(&zti);
423 }
424 }
425
426 obj->intern->default_txn_isolation = Z_LVAL_P(zti) & 0x3; /* two bits only */
427
428 if (zti != value) {
429 zval_ptr_dtor(&zti);
430 }
431 }
432
433 static void php_pqconn_object_read_def_txn_readonly(zval *object, void *o, zval *return_value TSRMLS_DC)
434 {
435 php_pqconn_object_t *obj = o;
436
437 RETVAL_BOOL(obj->intern->default_txn_readonly);
438 }
439 static void php_pqconn_object_write_def_txn_readonly(zval *object, void *o, zval *value TSRMLS_DC)
440 {
441 php_pqconn_object_t *obj = o;
442
443 obj->intern->default_txn_readonly = zend_is_true(value);
444 }
445
446 static void php_pqconn_object_read_def_txn_deferrable(zval *object, void *o, zval *return_value TSRMLS_DC)
447 {
448 php_pqconn_object_t *obj = o;
449
450 RETVAL_BOOL(obj->intern->default_txn_deferrable);
451 }
452 static void php_pqconn_object_write_def_txn_deferrable(zval *object, void *o, zval *value TSRMLS_DC)
453 {
454 php_pqconn_object_t *obj = o;
455
456 obj->intern->default_txn_deferrable = zend_is_true(value);
457 }
458
459 static void php_pqconn_object_read_def_auto_conv(zval *object, void *o, zval *return_value TSRMLS_DC)
460 {
461 php_pqconn_object_t *obj = o;
462
463 RETVAL_LONG(obj->intern->default_auto_convert);
464 }
465 static void php_pqconn_object_write_def_auto_conv(zval*object, void *o, zval *value TSRMLS_DC)
466 {
467 php_pqconn_object_t *obj = o;
468 zval *zac = value;
469
470 if (Z_TYPE_P(zac) != IS_LONG) {
471 if (Z_REFCOUNT_P(zac) > 1) {
472 zval *tmp;
473 MAKE_STD_ZVAL(tmp);
474 ZVAL_ZVAL(tmp, zac, 1, 0);
475 convert_to_long(tmp);
476 zac = tmp;
477 } else {
478 convert_to_long_ex(&zac);
479 }
480 }
481
482 obj->intern->default_auto_convert = Z_LVAL_P(zac) & PHP_PQRES_CONV_ALL;
483
484 if (zac != value) {
485 zval_ptr_dtor(&zac);
486 }
487 }
488
489 static STATUS php_pqconn_update_socket(zval *this_ptr, php_pqconn_object_t *obj TSRMLS_DC)
490 {
491 zval *zsocket, zmember;
492 php_stream *stream;
493 STATUS retval;
494 int socket;
495
496 if (!obj) {
497 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
498 }
499
500 INIT_PZVAL(&zmember);
501 ZVAL_STRINGL(&zmember, "socket", sizeof("socket")-1, 0);
502 MAKE_STD_ZVAL(zsocket);
503
504 if ((CONNECTION_BAD != PQstatus(obj->intern->conn))
505 && (-1 < (socket = PQsocket(obj->intern->conn)))
506 && (stream = php_stream_fopen_from_fd(socket, "r+b", NULL))) {
507 stream->flags |= PHP_STREAM_FLAG_NO_CLOSE;
508 php_stream_to_zval(stream, zsocket);
509 retval = SUCCESS;
510 } else {
511 ZVAL_NULL(zsocket);
512 retval = FAILURE;
513 }
514 zend_get_std_object_handlers()->write_property(getThis(), &zmember, zsocket, NULL TSRMLS_CC);
515 zval_ptr_dtor(&zsocket);
516
517 return retval;
518 }
519
520 static void *php_pqconn_resource_factory_ctor(void *data, void *init_arg TSRMLS_DC)
521 {
522 php_pqconn_resource_factory_data_t *o = init_arg;
523 PGconn *conn = NULL;;
524
525 if (o->flags & PHP_PQCONN_ASYNC) {
526 conn = PQconnectStart(o->dsn);
527 } else {
528 conn = PQconnectdb(o->dsn);
529 }
530
531 if (conn) {
532 PQregisterEventProc(conn, php_pqconn_event, "ext-pq", NULL);
533 }
534
535 return conn;
536 }
537
538 static void php_pqconn_resource_factory_dtor(void *opaque, void *handle TSRMLS_DC)
539 {
540 php_pqconn_event_data_t *evdata = PQinstanceData(handle, php_pqconn_event);
541
542 /* we don't care for anything, except free'ing evdata */
543 if (evdata) {
544 PQsetInstanceData(handle, php_pqconn_event, NULL);
545 memset(evdata, 0, sizeof(*evdata));
546 efree(evdata);
547 }
548
549 PQfinish(handle);
550 }
551
552 static php_resource_factory_ops_t php_pqconn_resource_factory_ops = {
553 php_pqconn_resource_factory_ctor,
554 NULL,
555 php_pqconn_resource_factory_dtor
556 };
557
558 php_resource_factory_ops_t *php_pqconn_get_resource_factory_ops(void)
559 {
560 return &php_pqconn_resource_factory_ops;
561 }
562
563 static void php_pqconn_wakeup(php_persistent_handle_factory_t *f, void **handle TSRMLS_DC)
564 {
565 PGresult *res = PQexec(*handle, "");
566 PHP_PQclear(res);
567
568 if (CONNECTION_OK != PQstatus(*handle)) {
569 PQreset(*handle);
570 }
571 }
572
573 static inline PGresult *unlisten(PGconn *conn, const char *channel_str, size_t channel_len TSRMLS_DC)
574 {
575 char *quoted_channel = PQescapeIdentifier(conn, channel_str, channel_len);
576 PGresult *res = NULL;
577
578 if (quoted_channel) {
579 smart_str cmd = {0};
580
581 smart_str_appends(&cmd, "UNLISTEN ");
582 smart_str_appends(&cmd, quoted_channel);
583 smart_str_0(&cmd);
584
585 res = PQexec(conn, cmd.c);
586
587 smart_str_free(&cmd);
588 PQfreemem(quoted_channel);
589 }
590
591 return res;
592 }
593
594 static int apply_unlisten(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
595 {
596 php_pqconn_object_t *obj = va_arg(argv, php_pqconn_object_t *);
597 PGresult *res = unlisten(obj->intern->conn, key->arKey, key->nKeyLength - 1 TSRMLS_CC);
598
599 if (res) {
600 PHP_PQclear(res);
601 }
602
603 return ZEND_HASH_APPLY_REMOVE;
604 }
605
606 static void php_pqconn_retire(php_persistent_handle_factory_t *f, void **handle TSRMLS_DC)
607 {
608 php_pqconn_event_data_t *evdata = PQinstanceData(*handle, php_pqconn_event);
609 PGcancel *cancel;
610 PGresult *res;
611
612 /* go away */
613 PQsetInstanceData(*handle, php_pqconn_event, NULL);
614
615 /* ignore notices */
616 PQsetNoticeReceiver(*handle, php_pqconn_notice_ignore, NULL);
617
618 /* cancel async queries */
619 if (PQisBusy(*handle) && (cancel = PQgetCancel(*handle))) {
620 char err[256] = {0};
621
622 PQcancel(cancel, err, sizeof(err));
623 PQfreeCancel(cancel);
624 }
625 /* clean up async results */
626 while ((res = PQgetResult(*handle))) {
627 PHP_PQclear(res);
628 }
629
630 /* clean up transaction & session */
631 switch (PQtransactionStatus(*handle)) {
632 case PQTRANS_IDLE:
633 res = PQexec(*handle, "RESET ALL");
634 break;
635 default:
636 res = PQexec(*handle, "ROLLBACK; RESET ALL");
637 break;
638 }
639
640 if (res) {
641 PHP_PQclear(res);
642 }
643
644 if (evdata) {
645 /* clean up notify listeners */
646 zend_hash_apply_with_arguments(&evdata->obj->intern->listeners TSRMLS_CC, apply_unlisten, 1, evdata->obj);
647
648 /* release instance data */
649 efree(evdata);
650 }
651 }
652
653 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct, 0, 0, 1)
654 ZEND_ARG_INFO(0, dsn)
655 ZEND_ARG_INFO(0, async)
656 ZEND_END_ARG_INFO();
657 static PHP_METHOD(pqconn, __construct) {
658 zend_error_handling zeh;
659 char *dsn_str = "";
660 int dsn_len = 0;
661 long flags = 0;
662 STATUS rv;
663
664 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
665 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &dsn_str, &dsn_len, &flags);
666 zend_restore_error_handling(&zeh TSRMLS_CC);
667
668 if (SUCCESS == rv) {
669 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
670
671 if (obj->intern) {
672 throw_exce(EX_BAD_METHODCALL TSRMLS_CC, "pq\\Connection already initialized");
673 } else {
674 php_pqconn_event_data_t *evdata = php_pqconn_event_data_init(obj TSRMLS_CC);
675 php_pqconn_resource_factory_data_t rfdata = {dsn_str, flags};
676
677 obj->intern = ecalloc(1, sizeof(*obj->intern));
678
679 obj->intern->default_auto_convert = PHP_PQRES_CONV_ALL;
680
681 zend_hash_init(&obj->intern->listeners, 0, NULL, (dtor_func_t) zend_hash_destroy, 0);
682 zend_hash_init(&obj->intern->converters, 0, NULL, ZVAL_PTR_DTOR, 0);
683 zend_hash_init(&obj->intern->eventhandlers, 0, NULL, (dtor_func_t) zend_hash_destroy, 0);
684
685 if (flags & PHP_PQCONN_PERSISTENT) {
686 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);
687 php_resource_factory_init(&obj->intern->factory, php_persistent_handle_get_resource_factory_ops(), phf, (void (*)(void*)) php_persistent_handle_abandon);
688 } else {
689 php_resource_factory_init(&obj->intern->factory, &php_pqconn_resource_factory_ops, NULL, NULL);
690 }
691
692 if (flags & PHP_PQCONN_ASYNC) {
693 obj->intern->poller = (int (*)(PGconn*)) PQconnectPoll;
694 }
695
696 obj->intern->conn = php_resource_factory_handle_ctor(&obj->intern->factory, &rfdata TSRMLS_CC);
697
698 PQsetInstanceData(obj->intern->conn, php_pqconn_event, evdata);
699 PQsetNoticeReceiver(obj->intern->conn, php_pqconn_notice_recv, evdata);
700
701 if (SUCCESS != php_pqconn_update_socket(getThis(), obj TSRMLS_CC)) {
702 throw_exce(EX_CONNECTION_FAILED TSRMLS_CC, "Connection failed (%s)", PHP_PQerrorMessage(obj->intern->conn));
703 }
704 }
705 }
706 }
707
708 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset, 0, 0, 0)
709 ZEND_END_ARG_INFO();
710 static PHP_METHOD(pqconn, reset) {
711 zend_error_handling zeh;
712 STATUS rv;
713
714 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
715 rv = zend_parse_parameters_none();
716 zend_restore_error_handling(&zeh TSRMLS_CC);
717
718 if (SUCCESS == rv) {
719 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
720
721 if (!obj->intern) {
722 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
723 } else {
724 PQreset(obj->intern->conn);
725
726 if (CONNECTION_OK != PQstatus(obj->intern->conn)) {
727 throw_exce(EX_CONNECTION_FAILED TSRMLS_CC, "Connection reset failed: (%s)", PHP_PQerrorMessage(obj->intern->conn));
728 }
729
730 php_pqconn_notify_listeners(obj TSRMLS_CC);
731 }
732 }
733 }
734
735 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset_async, 0, 0, 0)
736 ZEND_END_ARG_INFO();
737 static PHP_METHOD(pqconn, resetAsync) {
738 zend_error_handling zeh;
739 STATUS rv;
740
741 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
742 rv = zend_parse_parameters_none();
743 zend_restore_error_handling(&zeh TSRMLS_CC);
744
745 if (SUCCESS == rv) {
746 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
747
748 if (!obj->intern) {
749 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
750 } else {
751 if (!PQresetStart(obj->intern->conn)) {
752 throw_exce(EX_IO TSRMLS_CC, "Failed to start connection reset (%s)", PHP_PQerrorMessage(obj->intern->conn));
753 } else {
754 obj->intern->poller = (int (*)(PGconn*)) PQresetPoll;
755 }
756
757 php_pqconn_notify_listeners(obj TSRMLS_CC);
758 }
759 }
760 }
761
762 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten, 0, 0, 1)
763 ZEND_ARG_INFO(0, channel)
764 ZEND_END_ARG_INFO();
765 static PHP_METHOD(pqconn, unlisten)
766 {
767 zend_error_handling zeh;
768 char *channel_str;
769 int channel_len;
770 STATUS rv;
771
772 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
773 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &channel_str, &channel_len);
774 zend_restore_error_handling(&zeh TSRMLS_CC);
775
776 if (SUCCESS == rv) {
777 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
778
779 if (!obj->intern) {
780 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
781 } else if (SUCCESS == zend_hash_del(&obj->intern->listeners, channel_str, channel_len + 1)) {
782 PGresult *res = unlisten(obj->intern->conn, channel_str, channel_len TSRMLS_CC);
783
784 if (res) {
785 php_pqres_success(res TSRMLS_CC);
786 PHP_PQclear(res);
787 }
788 }
789 }
790 }
791
792 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten_async, 0, 0, 1)
793 ZEND_ARG_INFO(0, channel)
794 ZEND_END_ARG_INFO();
795 static PHP_METHOD(pqconn, unlistenAsync) {
796 zend_error_handling zeh;
797 char *channel_str;
798 int channel_len;
799 STATUS rv;
800
801 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
802 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &channel_str, &channel_len);
803 zend_restore_error_handling(&zeh TSRMLS_CC);
804
805 if (SUCCESS == rv) {
806 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
807
808 if (!obj->intern) {
809 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
810 } else {
811 char *quoted_channel = PQescapeIdentifier(obj->intern->conn, channel_str, channel_len);
812
813 if (!quoted_channel) {
814 throw_exce(EX_ESCAPE TSRMLS_CC, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj->intern->conn));
815 } else {
816 smart_str cmd = {0};
817
818 smart_str_appends(&cmd, "UNLISTEN ");
819 smart_str_appends(&cmd, quoted_channel);
820 smart_str_0(&cmd);
821
822 if (!PQsendQuery(obj->intern->conn, cmd.c)) {
823 throw_exce(EX_IO TSRMLS_CC, "Failed to uninstall listener (%s)", PHP_PQerrorMessage(obj->intern->conn));
824 } else {
825 obj->intern->poller = PQconsumeInput;
826 zend_hash_del(&obj->intern->listeners, channel_str, channel_len + 1);
827 }
828
829 smart_str_free(&cmd);
830 PQfreemem(quoted_channel);
831 php_pqconn_notify_listeners(obj TSRMLS_CC);
832 }
833 }
834 }
835 }
836
837 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)
838 {
839 HashTable ht, *existing_listeners;
840
841 php_pq_callback_addref(listener);
842
843 if (SUCCESS == zend_hash_find(&obj->intern->listeners, channel_str, channel_len + 1, (void *) &existing_listeners)) {
844 zend_hash_next_index_insert(existing_listeners, (void *) listener, sizeof(*listener), NULL);
845 } else {
846 zend_hash_init(&ht, 1, NULL, (dtor_func_t) php_pq_callback_dtor, 0);
847 zend_hash_next_index_insert(&ht, (void *) listener, sizeof(*listener), NULL);
848 zend_hash_add(&obj->intern->listeners, channel_str, channel_len + 1, (void *) &ht, sizeof(HashTable), NULL);
849 }
850 }
851
852 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen, 0, 0, 2)
853 ZEND_ARG_INFO(0, channel)
854 ZEND_ARG_INFO(0, callable)
855 ZEND_END_ARG_INFO();
856 static PHP_METHOD(pqconn, listen) {
857 zend_error_handling zeh;
858 char *channel_str = NULL;
859 int channel_len = 0;
860 php_pq_callback_t listener = {{0}};
861 STATUS rv;
862
863 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
864 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sf", &channel_str, &channel_len, &listener.fci, &listener.fcc);
865 zend_restore_error_handling(&zeh TSRMLS_CC);
866
867 if (SUCCESS == rv) {
868 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
869
870 if (!obj->intern) {
871 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
872 } else {
873 char *quoted_channel = PQescapeIdentifier(obj->intern->conn, channel_str, channel_len);
874
875 if (!quoted_channel) {
876 throw_exce(EX_ESCAPE TSRMLS_CC, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj->intern->conn));
877 } else {
878 PGresult *res;
879 smart_str cmd = {0};
880
881 smart_str_appends(&cmd, "LISTEN ");
882 smart_str_appends(&cmd, quoted_channel);
883 smart_str_0(&cmd);
884
885 res = PQexec(obj->intern->conn, cmd.c);
886
887 smart_str_free(&cmd);
888 PQfreemem(quoted_channel);
889
890 if (!res) {
891 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to install listener (%s)", PHP_PQerrorMessage(obj->intern->conn));
892 } else {
893 if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
894 obj->intern->poller = PQconsumeInput;
895 php_pqconn_add_listener(obj, channel_str, channel_len, &listener TSRMLS_CC);
896 }
897 PHP_PQclear(res);
898 }
899
900 php_pqconn_notify_listeners(obj TSRMLS_CC);
901 }
902 }
903 }
904 }
905
906 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen_async, 0, 0, 0)
907 ZEND_ARG_INFO(0, channel)
908 ZEND_ARG_INFO(0, callable)
909 ZEND_END_ARG_INFO();
910 static PHP_METHOD(pqconn, listenAsync) {
911 zend_error_handling zeh;
912 char *channel_str = NULL;
913 int channel_len = 0;
914 php_pq_callback_t listener = {{0}};
915 STATUS rv;
916
917 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
918 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sf", &channel_str, &channel_len, &listener.fci, &listener.fcc);
919 zend_restore_error_handling(&zeh TSRMLS_CC);
920
921 if (SUCCESS == rv) {
922 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
923
924 if (!obj->intern) {
925 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
926 } else {
927 char *quoted_channel = PQescapeIdentifier(obj->intern->conn, channel_str, channel_len);
928
929 if (!quoted_channel) {
930 throw_exce(EX_ESCAPE TSRMLS_CC, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj->intern->conn));
931 } else {
932 smart_str cmd = {0};
933
934 smart_str_appends(&cmd, "LISTEN ");
935 smart_str_appends(&cmd, quoted_channel);
936 smart_str_0(&cmd);
937
938 if (!PQsendQuery(obj->intern->conn, cmd.c)) {
939 throw_exce(EX_IO TSRMLS_CC, "Failed to install listener (%s)", PHP_PQerrorMessage(obj->intern->conn));
940 } else {
941 obj->intern->poller = PQconsumeInput;
942 php_pqconn_add_listener(obj, channel_str, channel_len, &listener TSRMLS_CC);
943 }
944
945 smart_str_free(&cmd);
946 PQfreemem(quoted_channel);
947 php_pqconn_notify_listeners(obj TSRMLS_CC);
948 }
949 }
950 }
951 }
952
953 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify, 0, 0, 2)
954 ZEND_ARG_INFO(0, channel)
955 ZEND_ARG_INFO(0, message)
956 ZEND_END_ARG_INFO();
957 static PHP_METHOD(pqconn, notify) {
958 zend_error_handling zeh;
959 char *channel_str, *message_str;
960 int channel_len, message_len;
961 STATUS rv;
962
963 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
964 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &channel_str, &channel_len, &message_str, &message_len);
965 zend_restore_error_handling(&zeh TSRMLS_CC);
966
967 if (SUCCESS == rv) {
968 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
969
970 if (!obj->intern) {
971 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
972 } else {
973 PGresult *res;
974 char *params[2] = {channel_str, message_str};
975
976 res = PQexecParams(obj->intern->conn, "select pg_notify($1, $2)", 2, NULL, (const char *const*) params, NULL, NULL, 0);
977
978 if (!res) {
979 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj->intern->conn));
980 } else {
981 php_pqres_success(res TSRMLS_CC);
982 PHP_PQclear(res);
983 }
984
985 php_pqconn_notify_listeners(obj TSRMLS_CC);
986 }
987 }
988 }
989
990 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify_async, 0, 0, 2)
991 ZEND_ARG_INFO(0, channel)
992 ZEND_ARG_INFO(0, message)
993 ZEND_END_ARG_INFO();
994 static PHP_METHOD(pqconn, notifyAsync) {
995 zend_error_handling zeh;
996 char *channel_str, *message_str;
997 int channel_len, message_len;
998 STATUS rv;
999
1000 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1001 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &channel_str, &channel_len, &message_str, &message_len);
1002 zend_restore_error_handling(&zeh TSRMLS_CC);
1003
1004 if (SUCCESS == rv) {
1005 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1006
1007 if (!obj->intern) {
1008 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1009 } else {
1010 char *params[2] = {channel_str, message_str};
1011
1012 if (!PQsendQueryParams(obj->intern->conn, "select pg_notify($1, $2)", 2, NULL, (const char *const*) params, NULL, NULL, 0)) {
1013 throw_exce(EX_IO TSRMLS_CC, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj->intern->conn));
1014 } else {
1015 obj->intern->poller = PQconsumeInput;
1016 }
1017
1018 php_pqconn_notify_listeners(obj TSRMLS_CC);
1019 }
1020 }
1021 }
1022
1023 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_poll, 0, 0, 0)
1024 ZEND_END_ARG_INFO();
1025 static PHP_METHOD(pqconn, poll) {
1026 zend_error_handling zeh;
1027 STATUS rv;
1028
1029 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1030 rv = zend_parse_parameters_none();
1031 zend_restore_error_handling(&zeh TSRMLS_CC);
1032
1033 if (SUCCESS == rv) {
1034 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1035
1036 if (!obj->intern) {
1037 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1038 } else if (!obj->intern->poller) {
1039 throw_exce(EX_RUNTIME TSRMLS_CC, "No asynchronous operation active");
1040 } else {
1041 if (obj->intern->poller == PQconsumeInput) {
1042 RETVAL_LONG(obj->intern->poller(obj->intern->conn) * PGRES_POLLING_OK);
1043 } else {
1044 RETVAL_LONG(obj->intern->poller(obj->intern->conn));
1045 }
1046 php_pqconn_notify_listeners(obj TSRMLS_CC);
1047 }
1048 }
1049 }
1050
1051 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec, 0, 0, 1)
1052 ZEND_ARG_INFO(0, query)
1053 ZEND_END_ARG_INFO();
1054 static PHP_METHOD(pqconn, exec) {
1055 zend_error_handling zeh;
1056 char *query_str;
1057 int query_len;
1058 STATUS rv;
1059
1060 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1061 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &query_str, &query_len);
1062 zend_restore_error_handling(&zeh TSRMLS_CC);
1063
1064 if (SUCCESS == rv) {
1065 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1066
1067 if (!obj->intern) {
1068 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1069 } else {
1070 PGresult *res = PQexec(obj->intern->conn, query_str);
1071
1072 if (!res) {
1073 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
1074 } else if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
1075 php_pq_object_to_zval_no_addref(PQresultInstanceData(res, php_pqconn_event), &return_value TSRMLS_CC);
1076 } else {
1077 PHP_PQclear(res);
1078 }
1079
1080 php_pqconn_notify_listeners(obj TSRMLS_CC);
1081 }
1082 }
1083 }
1084
1085 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_get_result, 0, 0, 0)
1086 ZEND_END_ARG_INFO();
1087 static PHP_METHOD(pqconn, getResult) {
1088 zend_error_handling zeh;
1089 STATUS rv;
1090
1091 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1092 rv = zend_parse_parameters_none();
1093 zend_restore_error_handling(&zeh TSRMLS_CC);
1094
1095 if (SUCCESS == rv) {
1096 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1097
1098 if (!obj->intern) {
1099 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1100 } else {
1101 PGresult *res = PQgetResult(obj->intern->conn);
1102
1103 if (!res) {
1104 RETVAL_NULL();
1105 } else {
1106 php_pq_object_to_zval_no_addref(PQresultInstanceData(res, php_pqconn_event), &return_value TSRMLS_CC);
1107 }
1108
1109 php_pqconn_notify_listeners(obj TSRMLS_CC);
1110 }
1111 }
1112 }
1113
1114 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_async, 0, 0, 1)
1115 ZEND_ARG_INFO(0, query)
1116 ZEND_ARG_INFO(0, callable)
1117 ZEND_END_ARG_INFO();
1118 static PHP_METHOD(pqconn, execAsync) {
1119 zend_error_handling zeh;
1120 php_pq_callback_t resolver = {{0}};
1121 char *query_str;
1122 int query_len;
1123 STATUS rv;
1124
1125 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1126 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|f", &query_str, &query_len, &resolver.fci, &resolver.fcc);
1127 zend_restore_error_handling(&zeh TSRMLS_CC);
1128
1129 if (SUCCESS == rv) {
1130 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1131
1132 if (!obj->intern) {
1133 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1134 } else if (!PQsendQuery(obj->intern->conn, query_str)) {
1135 throw_exce(EX_IO TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
1136 } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
1137 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
1138 } else {
1139 php_pq_callback_recurse(&obj->intern->onevent, &resolver TSRMLS_CC);
1140 obj->intern->poller = PQconsumeInput;
1141 php_pqconn_notify_listeners(obj TSRMLS_CC);
1142 }
1143 }
1144 }
1145
1146 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params, 0, 0, 2)
1147 ZEND_ARG_INFO(0, query)
1148 ZEND_ARG_ARRAY_INFO(0, params, 0)
1149 ZEND_ARG_ARRAY_INFO(0, types, 1)
1150 ZEND_END_ARG_INFO();
1151 static PHP_METHOD(pqconn, execParams) {
1152 zend_error_handling zeh;
1153 char *query_str;
1154 int query_len;
1155 zval *zparams;
1156 zval *ztypes = NULL;
1157 STATUS rv;
1158
1159 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1160 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|a/!", &query_str, &query_len, &zparams, &ztypes);
1161 zend_restore_error_handling(&zeh TSRMLS_CC);
1162
1163 if (SUCCESS == rv) {
1164 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1165
1166 if (!obj->intern) {
1167 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1168 } else {
1169 PGresult *res;
1170 php_pq_params_t *params;
1171
1172 params = php_pq_params_init(&obj->intern->converters, ztypes ? Z_ARRVAL_P(ztypes) : NULL, Z_ARRVAL_P(zparams) TSRMLS_CC);
1173 res = PQexecParams(obj->intern->conn, query_str, params->param.count, params->type.oids, (const char *const*) params->param.strings, NULL, NULL, 0);
1174 php_pq_params_free(&params);
1175
1176 if (!res) {
1177 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
1178 } else {
1179 if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
1180 php_pq_object_to_zval_no_addref(PQresultInstanceData(res, php_pqconn_event), &return_value TSRMLS_CC);
1181 } else {
1182 PHP_PQclear(res);
1183 }
1184
1185 php_pqconn_notify_listeners(obj TSRMLS_CC);
1186 }
1187 }
1188 }
1189 }
1190
1191 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async, 0, 0, 2)
1192 ZEND_ARG_INFO(0, query)
1193 ZEND_ARG_ARRAY_INFO(0, params, 0)
1194 ZEND_ARG_ARRAY_INFO(0, types, 1)
1195 ZEND_ARG_INFO(0, callable)
1196 ZEND_END_ARG_INFO();
1197 static PHP_METHOD(pqconn, execParamsAsync) {
1198 zend_error_handling zeh;
1199 php_pq_callback_t resolver = {{0}};
1200 char *query_str;
1201 int query_len;
1202 zval *zparams;
1203 zval *ztypes = NULL;
1204 STATUS rv;
1205
1206 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1207 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|a/!f", &query_str, &query_len, &zparams, &ztypes, &resolver.fci, &resolver.fcc);
1208 zend_restore_error_handling(&zeh TSRMLS_CC);
1209
1210 if (SUCCESS == rv) {
1211 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1212
1213 if (!obj->intern) {
1214 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1215 } else {
1216 int rc;
1217 php_pq_params_t *params;
1218
1219 params = php_pq_params_init(&obj->intern->converters, ztypes ? Z_ARRVAL_P(ztypes) : NULL, Z_ARRVAL_P(zparams) TSRMLS_CC);
1220 rc = PQsendQueryParams(obj->intern->conn, query_str, params->param.count, params->type.oids, (const char *const*) params->param.strings, NULL, NULL, 0);
1221 php_pq_params_free(&params);
1222
1223 if (!rc) {
1224 throw_exce(EX_IO TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
1225 } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
1226 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
1227 } else {
1228 php_pq_callback_recurse(&obj->intern->onevent, &resolver TSRMLS_CC);
1229 obj->intern->poller = PQconsumeInput;
1230 php_pqconn_notify_listeners(obj TSRMLS_CC);
1231 }
1232 }
1233 }
1234 zend_restore_error_handling(&zeh TSRMLS_CC);
1235 }
1236
1237 STATUS php_pqconn_prepare(zval *object, php_pqconn_object_t *obj, const char *name, const char *query, php_pq_params_t *params TSRMLS_DC)
1238 {
1239 PGresult *res;
1240 STATUS rv;
1241
1242 if (!obj) {
1243 obj = zend_object_store_get_object(object TSRMLS_CC);
1244 }
1245
1246 res = PQprepare(obj->intern->conn, name, query, params->type.count, params->type.oids);
1247
1248 if (!res) {
1249 rv = FAILURE;
1250 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj->intern->conn));
1251 } else {
1252 rv = php_pqres_success(res TSRMLS_CC);
1253 PHP_PQclear(res);
1254 php_pqconn_notify_listeners(obj TSRMLS_CC);
1255 }
1256
1257 return rv;
1258 }
1259
1260 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare, 0, 0, 2)
1261 ZEND_ARG_INFO(0, name)
1262 ZEND_ARG_INFO(0, query)
1263 ZEND_ARG_ARRAY_INFO(0, types, 1)
1264 ZEND_END_ARG_INFO();
1265 static PHP_METHOD(pqconn, prepare) {
1266 zend_error_handling zeh;
1267 zval *ztypes = NULL;
1268 char *name_str, *query_str;
1269 int name_len, *query_len;
1270 STATUS rv;
1271
1272 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1273 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!", &name_str, &name_len, &query_str, &query_len, &ztypes);
1274 zend_restore_error_handling(&zeh TSRMLS_CC);
1275
1276 if (SUCCESS == rv) {
1277 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1278
1279 if (!obj->intern) {
1280 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1281 } else {
1282 php_pq_params_t *params = php_pq_params_init(&obj->intern->converters, ztypes ? Z_ARRVAL_P(ztypes) : NULL, NULL TSRMLS_CC);
1283
1284 if (SUCCESS != php_pqconn_prepare(getThis(), obj, name_str, query_str, params TSRMLS_CC)) {
1285 php_pq_params_free(&params);
1286 } else {
1287 php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
1288
1289 php_pq_object_addref(obj TSRMLS_CC);
1290 stm->conn = obj;
1291 stm->name = estrdup(name_str);
1292 stm->params = params;
1293 ZEND_INIT_SYMTABLE(&stm->bound);
1294
1295 return_value->type = IS_OBJECT;
1296 return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
1297 }
1298 }
1299 }
1300 }
1301
1302 STATUS php_pqconn_prepare_async(zval *object, php_pqconn_object_t *obj, const char *name, const char *query, php_pq_params_t *params TSRMLS_DC)
1303 {
1304 STATUS rv;
1305
1306 if (!obj) {
1307 obj = zend_object_store_get_object(object TSRMLS_CC);
1308 }
1309
1310 if (!PQsendPrepare(obj->intern->conn, name, query, params->type.count, params->type.oids)) {
1311 rv = FAILURE;
1312 throw_exce(EX_IO TSRMLS_CC, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj->intern->conn));
1313 } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
1314 rv = FAILURE;
1315 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
1316 } else {
1317 rv = SUCCESS;
1318 obj->intern->poller = PQconsumeInput;
1319 php_pqconn_notify_listeners(obj TSRMLS_CC);
1320 }
1321
1322 return rv;
1323 }
1324
1325 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare_async, 0, 0, 2)
1326 ZEND_ARG_INFO(0, name)
1327 ZEND_ARG_INFO(0, query)
1328 ZEND_ARG_ARRAY_INFO(0, types, 1)
1329 ZEND_END_ARG_INFO();
1330 static PHP_METHOD(pqconn, prepareAsync) {
1331 zend_error_handling zeh;
1332 zval *ztypes = NULL;
1333 char *name_str, *query_str;
1334 int name_len, *query_len;
1335 STATUS rv;
1336
1337 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1338 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!", &name_str, &name_len, &query_str, &query_len, &ztypes);
1339 zend_restore_error_handling(&zeh TSRMLS_CC);
1340
1341 if (SUCCESS == rv) {
1342 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1343
1344 if (!obj->intern) {
1345 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1346 } else {
1347 php_pq_params_t *params = php_pq_params_init(&obj->intern->converters, ztypes ? Z_ARRVAL_P(ztypes) : NULL, NULL TSRMLS_CC);
1348
1349 if (SUCCESS != php_pqconn_prepare_async(getThis(), obj, name_str, query_str, params TSRMLS_CC)) {
1350 php_pq_params_free(&params);
1351 } else {
1352 php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
1353
1354 php_pq_object_addref(obj TSRMLS_CC);
1355 stm->conn = obj;
1356 stm->name = estrdup(name_str);
1357 stm->params = params;
1358 ZEND_INIT_SYMTABLE(&stm->bound);
1359
1360 return_value->type = IS_OBJECT;
1361 return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
1362 }
1363 }
1364 }
1365 }
1366
1367 STATUS php_pqconn_declare(zval *object, php_pqconn_object_t *obj, const char *decl TSRMLS_DC)
1368 {
1369 PGresult *res;
1370 STATUS rv;
1371
1372 if (!obj) {
1373 obj = zend_object_store_get_object(object TSRMLS_CC);
1374 }
1375
1376 res = PQexec(obj->intern->conn, decl);
1377
1378 if (!res) {
1379 rv = FAILURE;
1380 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj->intern->conn));
1381 } else {
1382 rv = php_pqres_success(res TSRMLS_CC);
1383 PHP_PQclear(res);
1384 php_pqconn_notify_listeners(obj TSRMLS_CC);
1385 }
1386
1387 return rv;
1388 }
1389
1390 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare, 0, 0, 3)
1391 ZEND_ARG_INFO(0, name)
1392 ZEND_ARG_INFO(0, flags)
1393 ZEND_ARG_INFO(0, query)
1394 ZEND_END_ARG_INFO();
1395 static PHP_METHOD(pqconn, declare) {
1396 zend_error_handling zeh;
1397 char *name_str, *query_str;
1398 int name_len, query_len;
1399 long flags;
1400 STATUS rv;
1401
1402 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1403 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sls", &name_str, &name_len, &flags, &query_str, &query_len);
1404 zend_restore_error_handling(&zeh TSRMLS_CC);
1405
1406 if (SUCCESS == rv) {
1407 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1408
1409 if (!obj->intern) {
1410 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1411 } else {
1412 char *decl = php_pqcur_declare_str(name_str, name_len, flags, query_str, query_len);
1413
1414 if (SUCCESS != php_pqconn_declare(getThis(), obj, decl TSRMLS_CC)) {
1415 efree(decl);
1416 } else {
1417 php_pqcur_t *cur = ecalloc(1, sizeof(*cur));
1418
1419 php_pq_object_addref(obj TSRMLS_CC);
1420 cur->conn = obj;
1421 cur->open = 1;
1422 cur->name = estrdup(name_str);
1423 cur->decl = decl;
1424
1425 return_value->type = IS_OBJECT;
1426 return_value->value.obj = php_pqcur_create_object_ex(php_pqcur_class_entry, cur, NULL TSRMLS_CC);
1427 }
1428 }
1429 }
1430 }
1431
1432 STATUS php_pqconn_declare_async(zval *object, php_pqconn_object_t *obj, const char *decl TSRMLS_DC)
1433 {
1434 STATUS rv;
1435
1436 if (!obj) {
1437 obj = zend_object_store_get_object(object TSRMLS_CC);
1438 }
1439
1440 if (!PQsendQuery(obj->intern->conn, decl)) {
1441 rv = FAILURE;
1442 throw_exce(EX_IO TSRMLS_CC, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj->intern->conn));
1443 } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
1444 rv = FAILURE;
1445 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
1446 } else {
1447 rv = SUCCESS;
1448 obj->intern->poller = PQconsumeInput;
1449 php_pqconn_notify_listeners(obj TSRMLS_CC);
1450 }
1451
1452 return rv;
1453 }
1454
1455 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare_async, 0, 0, 2)
1456 ZEND_ARG_INFO(0, name)
1457 ZEND_ARG_INFO(0, flags)
1458 ZEND_ARG_INFO(0, query)
1459 ZEND_END_ARG_INFO();
1460 static PHP_METHOD(pqconn, declareAsync) {
1461 zend_error_handling zeh;
1462 char *name_str, *query_str;
1463 int name_len, query_len;
1464 long flags;
1465 STATUS rv;
1466
1467 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1468 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sls", &name_str, &name_len, &flags, &query_str, &query_len);
1469 zend_restore_error_handling(&zeh TSRMLS_CC);
1470
1471 if (SUCCESS == rv) {
1472 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1473
1474 if (!obj->intern) {
1475 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1476 } else {
1477 char *decl = php_pqcur_declare_str(name_str, name_len, flags, query_str, query_len);
1478
1479 if (SUCCESS != php_pqconn_declare_async(getThis(), obj, decl TSRMLS_CC)) {
1480 efree(decl);
1481 } else {
1482 php_pqcur_t *cur = ecalloc(1, sizeof(*cur));
1483
1484 php_pq_object_addref(obj TSRMLS_CC);
1485 cur->conn = obj;
1486 cur->open = 1;
1487 cur->name = estrdup(name_str);
1488 cur->decl = decl;
1489
1490 return_value->type = IS_OBJECT;
1491 return_value->value.obj = php_pqcur_create_object_ex(php_pqcur_class_entry, cur, NULL TSRMLS_CC);
1492 }
1493 }
1494 }
1495 }
1496
1497 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote, 0, 0, 1)
1498 ZEND_ARG_INFO(0, string)
1499 ZEND_END_ARG_INFO();
1500 static PHP_METHOD(pqconn, quote) {
1501 char *str;
1502 int len;
1503
1504 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
1505 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1506
1507 if (!obj->intern) {
1508 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1509 } else {
1510 char *quoted = PQescapeLiteral(obj->intern->conn, str, len);
1511
1512 if (!quoted) {
1513 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to quote string (%s)", PHP_PQerrorMessage(obj->intern->conn));
1514 RETVAL_FALSE;
1515 } else {
1516 RETVAL_STRING(quoted, 1);
1517 PQfreemem(quoted);
1518 }
1519 }
1520 }
1521 }
1522
1523 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote_name, 0, 0, 1)
1524 ZEND_ARG_INFO(0, type)
1525 ZEND_END_ARG_INFO();
1526 static PHP_METHOD(pqconn, quoteName) {
1527 char *str;
1528 int len;
1529
1530 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
1531 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1532
1533 if (!obj->intern) {
1534 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1535 } else {
1536 char *quoted = PQescapeIdentifier(obj->intern->conn, str, len);
1537
1538 if (!quoted) {
1539 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to quote name (%s)", PHP_PQerrorMessage(obj->intern->conn));
1540 RETVAL_FALSE;
1541 } else {
1542 RETVAL_STRING(quoted, 1);
1543 PQfreemem(quoted);
1544 }
1545 }
1546 }
1547 }
1548
1549 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_escape_bytea, 0, 0, 1)
1550 ZEND_ARG_INFO(0, bytea)
1551 ZEND_END_ARG_INFO();
1552 static PHP_METHOD(pqconn, escapeBytea) {
1553 char *str;
1554 int len;
1555
1556 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
1557 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1558
1559 if (!obj->intern) {
1560 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1561 } else {
1562 size_t escaped_len;
1563 char *escaped_str = (char *) PQescapeByteaConn(obj->intern->conn, (unsigned char *) str, len, &escaped_len);
1564
1565 if (!escaped_str) {
1566 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to escape bytea (%s)", PHP_PQerrorMessage(obj->intern->conn));
1567 RETVAL_FALSE;
1568 } else {
1569 RETVAL_STRINGL(escaped_str, escaped_len - 1, 1);
1570 PQfreemem(escaped_str);
1571 }
1572 }
1573 }
1574 }
1575
1576 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unescape_bytea, 0, 0, 1)
1577 ZEND_ARG_INFO(0, bytea)
1578 ZEND_END_ARG_INFO();
1579 static PHP_METHOD(pqconn, unescapeBytea) {
1580 char *str;
1581 int len;
1582
1583 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
1584 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1585
1586 if (!obj->intern) {
1587 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1588 } else {
1589 size_t unescaped_len;
1590 char *unescaped_str = (char *) PQunescapeBytea((unsigned char *)str, &unescaped_len);
1591
1592 if (!unescaped_str) {
1593 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to unescape bytea (%s)", PHP_PQerrorMessage(obj->intern->conn));
1594 RETVAL_FALSE;
1595 } else {
1596 RETVAL_STRINGL(unescaped_str, unescaped_len, 1);
1597 PQfreemem(unescaped_str);
1598 }
1599 }
1600 }
1601 }
1602
1603 STATUS php_pqconn_start_transaction(zval *zconn, php_pqconn_object_t *conn_obj, long isolation, zend_bool readonly, zend_bool deferrable TSRMLS_DC)
1604 {
1605 STATUS rv = FAILURE;
1606
1607 if (!conn_obj) {
1608 conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
1609 }
1610
1611 if (!conn_obj->intern) {
1612 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1613 } else {
1614 PGresult *res;
1615 smart_str cmd = {0};
1616 const char *il = isolation_level(&isolation);
1617
1618 smart_str_appends(&cmd, "START TRANSACTION ISOLATION LEVEL ");
1619 smart_str_appends(&cmd, il);
1620 smart_str_appends(&cmd, ", READ ");
1621 smart_str_appends(&cmd, readonly ? "ONLY" : "WRITE");
1622 smart_str_appends(&cmd, ",");
1623 smart_str_appends(&cmd, deferrable ? "" : " NOT");
1624 smart_str_appends(&cmd, " DEFERRABLE");
1625 smart_str_0(&cmd);
1626
1627 res = PQexec(conn_obj->intern->conn, cmd.c);
1628
1629 if (!res) {
1630 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj->intern->conn));
1631 } else {
1632 rv = php_pqres_success(res TSRMLS_CC);
1633 PHP_PQclear(res);
1634 php_pqconn_notify_listeners(conn_obj TSRMLS_CC);
1635 }
1636
1637 smart_str_free(&cmd);
1638 }
1639
1640 return rv;
1641 }
1642
1643 STATUS php_pqconn_start_transaction_async(zval *zconn, php_pqconn_object_t *conn_obj, long isolation, zend_bool readonly, zend_bool deferrable TSRMLS_DC)
1644 {
1645 STATUS rv = FAILURE;
1646
1647 if (!conn_obj) {
1648 conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
1649 }
1650
1651 if (!conn_obj->intern) {
1652 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1653 } else {
1654 smart_str cmd = {0};
1655 const char *il = isolation_level(&isolation);
1656
1657 smart_str_appends(&cmd, "START TRANSACTION ISOLATION LEVEL ");
1658 smart_str_appends(&cmd, il);
1659 smart_str_appends(&cmd, ", READ ");
1660 smart_str_appends(&cmd, readonly ? "ONLY" : "WRITE");
1661 smart_str_appends(&cmd, ",");
1662 smart_str_appends(&cmd, deferrable ? "" : "NOT ");
1663 smart_str_appends(&cmd, " DEFERRABLE");
1664 smart_str_0(&cmd);
1665
1666 if (!PQsendQuery(conn_obj->intern->conn, cmd.c)) {
1667 throw_exce(EX_IO TSRMLS_CC, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj->intern->conn));
1668 } else {
1669 rv = SUCCESS;
1670 conn_obj->intern->poller = PQconsumeInput;
1671 php_pqconn_notify_listeners(conn_obj TSRMLS_CC);
1672 }
1673
1674 smart_str_free(&cmd);
1675 }
1676
1677 return rv;
1678 }
1679
1680 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction, 0, 0, 0)
1681 ZEND_ARG_INFO(0, isolation)
1682 ZEND_ARG_INFO(0, readonly)
1683 ZEND_ARG_INFO(0, deferrable)
1684 ZEND_END_ARG_INFO();
1685 static PHP_METHOD(pqconn, startTransaction) {
1686 zend_error_handling zeh;
1687 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1688 long isolation = obj->intern ? obj->intern->default_txn_isolation : PHP_PQTXN_READ_COMMITTED;
1689 zend_bool readonly = obj->intern ? obj->intern->default_txn_readonly : 0;
1690 zend_bool deferrable = obj->intern ? obj->intern->default_txn_deferrable : 0;
1691 STATUS rv;
1692
1693 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1694 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable);
1695 zend_restore_error_handling(&zeh TSRMLS_CC);
1696
1697 if (SUCCESS == rv) {
1698 rv = php_pqconn_start_transaction(getThis(), obj, isolation, readonly, deferrable TSRMLS_CC);
1699
1700 if (SUCCESS == rv) {
1701 php_pqtxn_t *txn = ecalloc(1, sizeof(*txn));
1702
1703 php_pq_object_addref(obj TSRMLS_CC);
1704 txn->conn = obj;
1705 txn->open = 1;
1706 txn->isolation = isolation;
1707 txn->readonly = readonly;
1708 txn->deferrable = deferrable;
1709
1710 return_value->type = IS_OBJECT;
1711 return_value->value.obj = php_pqtxn_create_object_ex(php_pqtxn_class_entry, txn, NULL TSRMLS_CC);
1712 }
1713 }
1714 }
1715
1716 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction_async, 0, 0, 0)
1717 ZEND_ARG_INFO(0, isolation)
1718 ZEND_ARG_INFO(0, readonly)
1719 ZEND_ARG_INFO(0, deferrable)
1720 ZEND_END_ARG_INFO();
1721 static PHP_METHOD(pqconn, startTransactionAsync) {
1722 zend_error_handling zeh;
1723 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1724 long isolation = obj->intern ? obj->intern->default_txn_isolation : PHP_PQTXN_READ_COMMITTED;
1725 zend_bool readonly = obj->intern ? obj->intern->default_txn_readonly : 0;
1726 zend_bool deferrable = obj->intern ? obj->intern->default_txn_deferrable : 0;
1727 STATUS rv;
1728
1729 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1730 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable);
1731 zend_restore_error_handling(&zeh TSRMLS_CC);
1732
1733 if (SUCCESS == rv) {
1734 rv = php_pqconn_start_transaction_async(getThis(), obj, isolation, readonly, deferrable TSRMLS_CC);
1735
1736 if (SUCCESS == rv) {
1737 php_pqtxn_t *txn = ecalloc(1, sizeof(*txn));
1738
1739 php_pq_object_addref(obj TSRMLS_CC);
1740 txn->conn = obj;
1741 txn->isolation = isolation;
1742 txn->readonly = readonly;
1743 txn->deferrable = deferrable;
1744
1745 return_value->type = IS_OBJECT;
1746 return_value->value.obj = php_pqtxn_create_object_ex(php_pqtxn_class_entry, txn, NULL TSRMLS_CC);
1747 }
1748 }
1749 }
1750
1751 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_trace, 0, 0, 0)
1752 ZEND_ARG_INFO(0, stdio_stream)
1753 ZEND_END_ARG_INFO();
1754 static PHP_METHOD(pqconn, trace) {
1755 zval *zstream = NULL;
1756
1757 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r!", &zstream)) {
1758 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1759
1760 if (!obj->intern) {
1761 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1762 } else {
1763 if (!zstream) {
1764 PQuntrace(obj->intern->conn);
1765 RETVAL_TRUE;
1766 } else {
1767 FILE *fp;
1768 php_stream *stream = NULL;
1769
1770 php_stream_from_zval(stream, &zstream);
1771
1772 if (SUCCESS != php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
1773 RETVAL_FALSE;
1774 } else {
1775 stream->flags |= PHP_STREAM_FLAG_NO_CLOSE;
1776 PQtrace(obj->intern->conn, fp);
1777 RETVAL_TRUE;
1778 }
1779 }
1780 }
1781 }
1782 }
1783
1784 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_off, 0, 0, 1)
1785 ZEND_ARG_INFO(0, type)
1786 ZEND_END_ARG_INFO();
1787 static PHP_METHOD(pqconn, off) {
1788 zend_error_handling zeh;
1789 char *type_str;
1790 int type_len;
1791 STATUS rv;
1792
1793 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1794 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &type_str, &type_len);
1795 zend_restore_error_handling(&zeh TSRMLS_CC);
1796
1797 if (SUCCESS == rv) {
1798 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1799
1800 if (!obj->intern) {
1801 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1802 } else {
1803 RETURN_BOOL(SUCCESS == zend_hash_del(&obj->intern->eventhandlers, type_str, type_len + 1));
1804 }
1805 }
1806 }
1807
1808 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_on, 0, 0, 2)
1809 ZEND_ARG_INFO(0, type)
1810 ZEND_ARG_INFO(0, callable)
1811 ZEND_END_ARG_INFO();
1812 static PHP_METHOD(pqconn, on) {
1813 zend_error_handling zeh;
1814 char *type_str;
1815 int type_len;
1816 php_pq_callback_t cb = {{0}};
1817 STATUS rv;
1818
1819 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1820 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sf", &type_str, &type_len, &cb.fci, &cb.fcc);
1821 zend_restore_error_handling(&zeh TSRMLS_CC);
1822
1823 if (SUCCESS == rv) {
1824 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1825
1826 if (!obj->intern) {
1827 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1828 } else {
1829 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1830
1831 RETVAL_LONG(php_pqconn_add_eventhandler(obj, type_str, type_len, &cb TSRMLS_CC));
1832 }
1833 }
1834 }
1835
1836 struct apply_set_converter_arg {
1837 HashTable *ht;
1838 zval **zconv;
1839 unsigned add:1;
1840 };
1841
1842 static int apply_set_converter(void *p, void *a TSRMLS_DC)
1843 {
1844 zval *tmp, **zoid = p;
1845 struct apply_set_converter_arg *arg = a;
1846
1847 tmp = *zoid;
1848 Z_ADDREF_P(tmp);
1849 convert_to_long_ex(&tmp);
1850 if (arg->add) {
1851 Z_ADDREF_PP(arg->zconv);
1852 zend_hash_index_update(arg->ht, Z_LVAL_P(tmp), arg->zconv, sizeof(zval *), NULL);
1853 } else {
1854 zend_hash_index_del(arg->ht, Z_LVAL_P(tmp));
1855 }
1856 zval_ptr_dtor(&tmp);
1857
1858 return ZEND_HASH_APPLY_KEEP;
1859 }
1860
1861 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_set_converter, 0, 0, 1)
1862 ZEND_ARG_OBJ_INFO(0, converter, pq\\Converter, 0)
1863 ZEND_END_ARG_INFO();
1864 static PHP_METHOD(pqconn, setConverter) {
1865 STATUS rv;
1866 zend_error_handling zeh;
1867 zval *zcnv;
1868
1869 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1870 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zcnv, php_pqconv_class_entry);
1871 zend_restore_error_handling(&zeh TSRMLS_CC);
1872
1873 if (SUCCESS == rv) {
1874 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1875
1876 if (!obj->intern) {
1877 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1878 } else {
1879 zval *tmp, *zoids = NULL;
1880 struct apply_set_converter_arg arg = {NULL};
1881
1882 zend_call_method_with_0_params(&zcnv, NULL, NULL, "converttypes", &zoids);
1883 tmp = zoids;
1884 Z_ADDREF_P(tmp);
1885 convert_to_array_ex(&tmp);
1886
1887 arg.ht = &obj->intern->converters;
1888 arg.zconv = &zcnv;
1889 arg.add = 1;
1890
1891 zend_hash_apply_with_argument(Z_ARRVAL_P(tmp), apply_set_converter, &arg TSRMLS_CC);
1892
1893 zval_ptr_dtor(&tmp);
1894 zval_ptr_dtor(&zoids);
1895 }
1896 }
1897 }
1898
1899 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unset_converter, 0, 0, 1)
1900 ZEND_ARG_OBJ_INFO(0, converter, pq\\Converter, 0)
1901 ZEND_END_ARG_INFO();
1902 static PHP_METHOD(pqconn, unsetConverter) {
1903 STATUS rv;
1904 zend_error_handling zeh;
1905 zval *zcnv;
1906
1907 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1908 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zcnv, php_pqconv_class_entry);
1909 zend_restore_error_handling(&zeh TSRMLS_CC);
1910
1911 if (SUCCESS == rv) {
1912 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1913
1914 if (!obj->intern) {
1915 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1916 } else {
1917 zval *tmp, *zoids = NULL;
1918 struct apply_set_converter_arg arg = {NULL};
1919
1920 zend_call_method_with_0_params(&zcnv, NULL, NULL, "converttypes", &zoids);
1921 tmp = zoids;
1922 Z_ADDREF_P(tmp);
1923 convert_to_array_ex(&tmp);
1924
1925 arg.ht = &obj->intern->converters;
1926 arg.zconv = &zcnv;
1927 arg.add = 0;
1928
1929 zend_hash_apply_with_argument(Z_ARRVAL_P(tmp), apply_set_converter, &arg TSRMLS_CC);
1930
1931 zval_ptr_dtor(&tmp);
1932 zval_ptr_dtor(&zoids);
1933 }
1934 }
1935 }
1936
1937 static zend_function_entry php_pqconn_methods[] = {
1938 PHP_ME(pqconn, __construct, ai_pqconn_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1939 PHP_ME(pqconn, reset, ai_pqconn_reset, ZEND_ACC_PUBLIC)
1940 PHP_ME(pqconn, resetAsync, ai_pqconn_reset_async, ZEND_ACC_PUBLIC)
1941 PHP_ME(pqconn, poll, ai_pqconn_poll, ZEND_ACC_PUBLIC)
1942 PHP_ME(pqconn, exec, ai_pqconn_exec, ZEND_ACC_PUBLIC)
1943 PHP_ME(pqconn, execAsync, ai_pqconn_exec_async, ZEND_ACC_PUBLIC)
1944 PHP_ME(pqconn, execParams, ai_pqconn_exec_params, ZEND_ACC_PUBLIC)
1945 PHP_ME(pqconn, execParamsAsync, ai_pqconn_exec_params_async, ZEND_ACC_PUBLIC)
1946 PHP_ME(pqconn, prepare, ai_pqconn_prepare, ZEND_ACC_PUBLIC)
1947 PHP_ME(pqconn, prepareAsync, ai_pqconn_prepare_async, ZEND_ACC_PUBLIC)
1948 PHP_ME(pqconn, declare, ai_pqconn_declare, ZEND_ACC_PUBLIC)
1949 PHP_ME(pqconn, declareAsync, ai_pqconn_declare_async, ZEND_ACC_PUBLIC)
1950 PHP_ME(pqconn, unlisten, ai_pqconn_unlisten, ZEND_ACC_PUBLIC)
1951 PHP_ME(pqconn, unlistenAsync, ai_pqconn_unlisten_async, ZEND_ACC_PUBLIC)
1952 PHP_ME(pqconn, listen, ai_pqconn_listen, ZEND_ACC_PUBLIC)
1953 PHP_ME(pqconn, listenAsync, ai_pqconn_listen_async, ZEND_ACC_PUBLIC)
1954 PHP_ME(pqconn, notify, ai_pqconn_notify, ZEND_ACC_PUBLIC)
1955 PHP_ME(pqconn, notifyAsync, ai_pqconn_notify_async, ZEND_ACC_PUBLIC)
1956 PHP_ME(pqconn, getResult, ai_pqconn_get_result, ZEND_ACC_PUBLIC)
1957 PHP_ME(pqconn, quote, ai_pqconn_quote, ZEND_ACC_PUBLIC)
1958 PHP_ME(pqconn, quoteName, ai_pqconn_quote_name, ZEND_ACC_PUBLIC)
1959 PHP_ME(pqconn, escapeBytea, ai_pqconn_escape_bytea, ZEND_ACC_PUBLIC)
1960 PHP_ME(pqconn, unescapeBytea, ai_pqconn_unescape_bytea, ZEND_ACC_PUBLIC)
1961 PHP_ME(pqconn, startTransaction, ai_pqconn_start_transaction, ZEND_ACC_PUBLIC)
1962 PHP_ME(pqconn, startTransactionAsync, ai_pqconn_start_transaction_async, ZEND_ACC_PUBLIC)
1963 PHP_ME(pqconn, trace, ai_pqconn_trace, ZEND_ACC_PUBLIC)
1964 PHP_ME(pqconn, off, ai_pqconn_off, ZEND_ACC_PUBLIC)
1965 PHP_ME(pqconn, on, ai_pqconn_on, ZEND_ACC_PUBLIC)
1966 PHP_ME(pqconn, setConverter, ai_pqconn_set_converter, ZEND_ACC_PUBLIC)
1967 PHP_ME(pqconn, unsetConverter, ai_pqconn_unset_converter, ZEND_ACC_PUBLIC)
1968 {0}
1969 };
1970
1971 PHP_MSHUTDOWN_FUNCTION(pqconn)
1972 {
1973 zend_hash_destroy(&php_pqconn_object_prophandlers);
1974 return SUCCESS;
1975 }
1976
1977 PHP_MINIT_FUNCTION(pqconn)
1978 {
1979 zend_class_entry ce = {0};
1980 php_pq_object_prophandler_t ph = {0};
1981
1982 INIT_NS_CLASS_ENTRY(ce, "pq", "Connection", php_pqconn_methods);
1983 php_pqconn_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
1984 php_pqconn_class_entry->create_object = php_pqconn_create_object;
1985
1986 memcpy(&php_pqconn_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1987 php_pqconn_object_handlers.read_property = php_pq_object_read_prop;
1988 php_pqconn_object_handlers.write_property = php_pq_object_write_prop;
1989 php_pqconn_object_handlers.clone_obj = NULL;
1990 php_pqconn_object_handlers.get_property_ptr_ptr = NULL;
1991 php_pqconn_object_handlers.get_gc = NULL;
1992 php_pqconn_object_handlers.get_properties = php_pq_object_properties;
1993 php_pqconn_object_handlers.get_debug_info = php_pq_object_debug_info;
1994
1995 zend_hash_init(&php_pqconn_object_prophandlers, 20, NULL, NULL, 1);
1996
1997 zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("status"), CONNECTION_BAD, ZEND_ACC_PUBLIC TSRMLS_CC);
1998 ph.read = php_pqconn_object_read_status;
1999 zend_hash_add(&php_pqconn_object_prophandlers, "status", sizeof("status"), (void *) &ph, sizeof(ph), NULL);
2000
2001 zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("transactionStatus"), PQTRANS_UNKNOWN, ZEND_ACC_PUBLIC TSRMLS_CC);
2002 ph.read = php_pqconn_object_read_transaction_status;
2003 zend_hash_add(&php_pqconn_object_prophandlers, "transactionStatus", sizeof("transactionStatus"), (void *) &ph, sizeof(ph), NULL);
2004
2005 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("socket"), ZEND_ACC_PUBLIC TSRMLS_CC);
2006 ph.read = NULL; /* forward to std prophandler */
2007 zend_hash_add(&php_pqconn_object_prophandlers, "socket", sizeof("socket"), (void *) &ph, sizeof(ph), NULL);
2008
2009 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("errorMessage"), ZEND_ACC_PUBLIC TSRMLS_CC);
2010 ph.read = php_pqconn_object_read_error_message;
2011 zend_hash_add(&php_pqconn_object_prophandlers, "errorMessage", sizeof("errorMessage"), (void *) &ph, sizeof(ph), NULL);
2012
2013 zend_declare_property_bool(php_pqconn_class_entry, ZEND_STRL("busy"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
2014 ph.read = php_pqconn_object_read_busy;
2015 zend_hash_add(&php_pqconn_object_prophandlers, "busy", sizeof("busy"), (void *) &ph, sizeof(ph), NULL);
2016
2017 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("encoding"), ZEND_ACC_PUBLIC TSRMLS_CC);
2018 ph.read = php_pqconn_object_read_encoding;
2019 ph.write = php_pqconn_object_write_encoding;
2020 zend_hash_add(&php_pqconn_object_prophandlers, "encoding", sizeof("encoding"), (void *) &ph, sizeof(ph), NULL);
2021 ph.write = NULL;
2022
2023 zend_declare_property_bool(php_pqconn_class_entry, ZEND_STRL("unbuffered"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
2024 ph.read = php_pqconn_object_read_unbuffered;
2025 ph.write = php_pqconn_object_write_unbuffered;
2026 zend_hash_add(&php_pqconn_object_prophandlers, "unbuffered", sizeof("unbuffered"), (void *) &ph, sizeof(ph), NULL);
2027 ph.write = NULL;
2028
2029 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("db"), ZEND_ACC_PUBLIC TSRMLS_CC);
2030 ph.read = php_pqconn_object_read_db;
2031 zend_hash_add(&php_pqconn_object_prophandlers, "db", sizeof("db"), (void *) &ph, sizeof(ph), NULL);
2032
2033 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("user"), ZEND_ACC_PUBLIC TSRMLS_CC);
2034 ph.read = php_pqconn_object_read_user;
2035 zend_hash_add(&php_pqconn_object_prophandlers, "user", sizeof("user"), (void *) &ph, sizeof(ph), NULL);
2036
2037 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("pass"), ZEND_ACC_PUBLIC TSRMLS_CC);
2038 ph.read = php_pqconn_object_read_pass;
2039 zend_hash_add(&php_pqconn_object_prophandlers, "pass", sizeof("pass"), (void *) &ph, sizeof(ph), NULL);
2040
2041 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("host"), ZEND_ACC_PUBLIC TSRMLS_CC);
2042 ph.read = php_pqconn_object_read_host;
2043 zend_hash_add(&php_pqconn_object_prophandlers, "host", sizeof("host"), (void *) &ph, sizeof(ph), NULL);
2044
2045 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("port"), ZEND_ACC_PUBLIC TSRMLS_CC);
2046 ph.read = php_pqconn_object_read_port;
2047 zend_hash_add(&php_pqconn_object_prophandlers, "port", sizeof("port"), (void *) &ph, sizeof(ph), NULL);
2048
2049 #if HAVE_PQCONNINFO
2050 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("params"), ZEND_ACC_PUBLIC TSRMLS_CC);
2051 ph.read = php_pqconn_object_read_params;
2052 zend_hash_add(&php_pqconn_object_prophandlers, "params", sizeof("params"), (void *) &ph, sizeof(ph), NULL);
2053 #endif
2054
2055 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("options"), ZEND_ACC_PUBLIC TSRMLS_CC);
2056 ph.read = php_pqconn_object_read_options;
2057 zend_hash_add(&php_pqconn_object_prophandlers, "options", sizeof("options"), (void *) &ph, sizeof(ph), NULL);
2058
2059 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("eventHandlers"), ZEND_ACC_PUBLIC TSRMLS_CC);
2060 ph.read = php_pqconn_object_read_event_handlers;
2061 zend_hash_add(&php_pqconn_object_prophandlers, "eventHandlers", sizeof("eventHandlers"), (void *) &ph, sizeof(ph), NULL);
2062
2063 zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("defaultFetchType"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
2064 ph.read = php_pqconn_object_read_def_fetch_type;
2065 ph.write = php_pqconn_object_write_def_fetch_type;
2066 zend_hash_add(&php_pqconn_object_prophandlers, "defaultFetchType", sizeof("defaultFetchType"), (void *) &ph, sizeof(ph), NULL);
2067 ph.write = NULL;
2068
2069 zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("defaultTransactionIsolation"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
2070 ph.read = php_pqconn_object_read_def_txn_isolation;
2071 ph.write = php_pqconn_object_write_def_txn_isolation;
2072 zend_hash_add(&php_pqconn_object_prophandlers, "defaultTransactionIsolation", sizeof("defaultTransactionIsolation"), (void *) &ph, sizeof(ph), NULL);
2073 ph.write = NULL;
2074
2075 zend_declare_property_bool(php_pqconn_class_entry, ZEND_STRL("defaultTransactionReadonly"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
2076 ph.read = php_pqconn_object_read_def_txn_readonly;
2077 ph.write = php_pqconn_object_write_def_txn_readonly;
2078 zend_hash_add(&php_pqconn_object_prophandlers, "defaultTransactionReadonly", sizeof("defaultTransactionReadonly"), (void *) &ph, sizeof(ph), NULL);
2079 ph.write = NULL;
2080
2081 zend_declare_property_bool(php_pqconn_class_entry, ZEND_STRL("defaultTransactionDeferrable"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
2082 ph.read = php_pqconn_object_read_def_txn_deferrable;
2083 ph.write = php_pqconn_object_write_def_txn_deferrable;
2084 zend_hash_add(&php_pqconn_object_prophandlers, "defaultTransactionDeferrable", sizeof("defaultTransactionDeferrable"), (void *) &ph, sizeof(ph), NULL);
2085 ph.write = NULL;
2086
2087 zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("defaultAutoConvert"), PHP_PQRES_CONV_ALL, ZEND_ACC_PUBLIC TSRMLS_CC);
2088 ph.read = php_pqconn_object_read_def_auto_conv;
2089 ph.write = php_pqconn_object_write_def_auto_conv;
2090 zend_hash_add(&php_pqconn_object_prophandlers, "defaultAutoConvert", sizeof("defaultAutoConvert"), (void *) &ph, sizeof(ph), NULL);
2091 ph.write = NULL;
2092
2093 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("OK"), CONNECTION_OK TSRMLS_CC);
2094 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("BAD"), CONNECTION_BAD TSRMLS_CC);
2095 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("STARTED"), CONNECTION_STARTED TSRMLS_CC);
2096 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("MADE"), CONNECTION_MADE TSRMLS_CC);
2097 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("AWAITING_RESPONSE"), CONNECTION_AWAITING_RESPONSE TSRMLS_CC);
2098 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("AUTH_OK"), CONNECTION_AUTH_OK TSRMLS_CC);
2099 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("SSL_STARTUP"), CONNECTION_SSL_STARTUP TSRMLS_CC);
2100 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("SETENV"), CONNECTION_SETENV TSRMLS_CC);
2101
2102 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("TRANS_IDLE"), PQTRANS_IDLE TSRMLS_CC);
2103 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("TRANS_ACTIVE"), PQTRANS_ACTIVE TSRMLS_CC);
2104 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("TRANS_INTRANS"), PQTRANS_INTRANS TSRMLS_CC);
2105 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("TRANS_INERROR"), PQTRANS_INERROR TSRMLS_CC);
2106 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("TRANS_UNKNOWN"), PQTRANS_UNKNOWN TSRMLS_CC);
2107
2108 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_FAILED"), PGRES_POLLING_FAILED TSRMLS_CC);
2109 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_READING"), PGRES_POLLING_READING TSRMLS_CC);
2110 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_WRITING"), PGRES_POLLING_WRITING TSRMLS_CC);
2111 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_OK"), PGRES_POLLING_OK TSRMLS_CC);
2112
2113 zend_declare_class_constant_stringl(php_pqconn_class_entry, ZEND_STRL("EVENT_NOTICE"), ZEND_STRL("notice") TSRMLS_CC);
2114 zend_declare_class_constant_stringl(php_pqconn_class_entry, ZEND_STRL("EVENT_RESULT"), ZEND_STRL("result") TSRMLS_CC);
2115 zend_declare_class_constant_stringl(php_pqconn_class_entry, ZEND_STRL("EVENT_RESET"), ZEND_STRL("reset") TSRMLS_CC);
2116
2117 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("ASYNC"), 0x1 TSRMLS_CC);
2118 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("PERSISTENT"), 0x2 TSRMLS_CC);
2119
2120 return SUCCESS;
2121 }
2122
2123 /*
2124 * Local variables:
2125 * tab-width: 4
2126 * c-basic-offset: 4
2127 * End:
2128 * vim600: noet sw=4 ts=4 fdm=marker
2129 * vim<600: noet sw=4 ts=4
2130 */