create test db
[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 ZEND_RESULT_CODE php_pqconn_update_socket(zval *this_ptr, php_pqconn_object_t *obj TSRMLS_DC)
490 {
491 zval *zsocket, zmember;
492 php_stream *stream;
493 ZEND_RESULT_CODE 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, flags)
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 ZEND_RESULT_CODE 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_persistent_handle_resource_factory_init(&obj->intern->factory, phf);
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 ZEND_RESULT_CODE 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 ZEND_RESULT_CODE 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 ZEND_RESULT_CODE 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 ZEND_RESULT_CODE 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 ZEND_RESULT_CODE 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 ZEND_RESULT_CODE 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 ZEND_RESULT_CODE 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 ZEND_RESULT_CODE 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 ZEND_RESULT_CODE 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 ZEND_RESULT_CODE 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 ZEND_RESULT_CODE 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 ZEND_RESULT_CODE 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 #if HAVE_PQSETSINGLEROWMODE
1137 } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
1138 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
1139 #endif
1140 } else {
1141 php_pq_callback_recurse(&obj->intern->onevent, &resolver TSRMLS_CC);
1142 obj->intern->poller = PQconsumeInput;
1143 php_pqconn_notify_listeners(obj TSRMLS_CC);
1144 }
1145 }
1146 }
1147
1148 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params, 0, 0, 2)
1149 ZEND_ARG_INFO(0, query)
1150 ZEND_ARG_ARRAY_INFO(0, params, 0)
1151 ZEND_ARG_ARRAY_INFO(0, types, 1)
1152 ZEND_END_ARG_INFO();
1153 static PHP_METHOD(pqconn, execParams) {
1154 zend_error_handling zeh;
1155 char *query_str;
1156 int query_len;
1157 zval *zparams;
1158 zval *ztypes = NULL;
1159 ZEND_RESULT_CODE rv;
1160
1161 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1162 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|a/!", &query_str, &query_len, &zparams, &ztypes);
1163 zend_restore_error_handling(&zeh TSRMLS_CC);
1164
1165 if (SUCCESS == rv) {
1166 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1167
1168 if (!obj->intern) {
1169 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1170 } else {
1171 PGresult *res;
1172 php_pq_params_t *params;
1173
1174 params = php_pq_params_init(&obj->intern->converters, ztypes ? Z_ARRVAL_P(ztypes) : NULL, Z_ARRVAL_P(zparams) TSRMLS_CC);
1175 res = PQexecParams(obj->intern->conn, query_str, params->param.count, params->type.oids, (const char *const*) params->param.strings, NULL, NULL, 0);
1176 php_pq_params_free(&params);
1177
1178 if (!res) {
1179 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
1180 } else {
1181 if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
1182 php_pq_object_to_zval_no_addref(PQresultInstanceData(res, php_pqconn_event), &return_value TSRMLS_CC);
1183 } else {
1184 PHP_PQclear(res);
1185 }
1186
1187 php_pqconn_notify_listeners(obj TSRMLS_CC);
1188 }
1189 }
1190 }
1191 }
1192
1193 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async, 0, 0, 2)
1194 ZEND_ARG_INFO(0, query)
1195 ZEND_ARG_ARRAY_INFO(0, params, 0)
1196 ZEND_ARG_ARRAY_INFO(0, types, 1)
1197 ZEND_ARG_INFO(0, callable)
1198 ZEND_END_ARG_INFO();
1199 static PHP_METHOD(pqconn, execParamsAsync) {
1200 zend_error_handling zeh;
1201 php_pq_callback_t resolver = {{0}};
1202 char *query_str;
1203 int query_len;
1204 zval *zparams;
1205 zval *ztypes = NULL;
1206 ZEND_RESULT_CODE rv;
1207
1208 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1209 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|a/!f", &query_str, &query_len, &zparams, &ztypes, &resolver.fci, &resolver.fcc);
1210 zend_restore_error_handling(&zeh TSRMLS_CC);
1211
1212 if (SUCCESS == rv) {
1213 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1214
1215 if (!obj->intern) {
1216 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1217 } else {
1218 int rc;
1219 php_pq_params_t *params;
1220
1221 params = php_pq_params_init(&obj->intern->converters, ztypes ? Z_ARRVAL_P(ztypes) : NULL, Z_ARRVAL_P(zparams) TSRMLS_CC);
1222 rc = PQsendQueryParams(obj->intern->conn, query_str, params->param.count, params->type.oids, (const char *const*) params->param.strings, NULL, NULL, 0);
1223 php_pq_params_free(&params);
1224
1225 if (!rc) {
1226 throw_exce(EX_IO TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
1227 #if HAVE_PQSETSINGLEROWMODE
1228 } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
1229 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
1230 #endif
1231 } else {
1232 php_pq_callback_recurse(&obj->intern->onevent, &resolver TSRMLS_CC);
1233 obj->intern->poller = PQconsumeInput;
1234 php_pqconn_notify_listeners(obj TSRMLS_CC);
1235 }
1236 }
1237 }
1238 zend_restore_error_handling(&zeh TSRMLS_CC);
1239 }
1240
1241 ZEND_RESULT_CODE php_pqconn_prepare(zval *object, php_pqconn_object_t *obj, const char *name, const char *query, php_pq_params_t *params TSRMLS_DC)
1242 {
1243 PGresult *res;
1244 ZEND_RESULT_CODE rv;
1245
1246 if (!obj) {
1247 obj = zend_object_store_get_object(object TSRMLS_CC);
1248 }
1249
1250 res = PQprepare(obj->intern->conn, name, query, params->type.count, params->type.oids);
1251
1252 if (!res) {
1253 rv = FAILURE;
1254 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj->intern->conn));
1255 } else {
1256 rv = php_pqres_success(res TSRMLS_CC);
1257 PHP_PQclear(res);
1258 php_pqconn_notify_listeners(obj TSRMLS_CC);
1259 }
1260
1261 return rv;
1262 }
1263
1264 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare, 0, 0, 2)
1265 ZEND_ARG_INFO(0, name)
1266 ZEND_ARG_INFO(0, query)
1267 ZEND_ARG_ARRAY_INFO(0, types, 1)
1268 ZEND_END_ARG_INFO();
1269 static PHP_METHOD(pqconn, prepare) {
1270 zend_error_handling zeh;
1271 zval *ztypes = NULL;
1272 char *name_str, *query_str;
1273 int name_len, *query_len;
1274 ZEND_RESULT_CODE rv;
1275
1276 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1277 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!", &name_str, &name_len, &query_str, &query_len, &ztypes);
1278 zend_restore_error_handling(&zeh TSRMLS_CC);
1279
1280 if (SUCCESS == rv) {
1281 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1282
1283 if (!obj->intern) {
1284 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1285 } else {
1286 php_pq_params_t *params = php_pq_params_init(&obj->intern->converters, ztypes ? Z_ARRVAL_P(ztypes) : NULL, NULL TSRMLS_CC);
1287
1288 if (SUCCESS != php_pqconn_prepare(getThis(), obj, name_str, query_str, params TSRMLS_CC)) {
1289 php_pq_params_free(&params);
1290 } else {
1291 php_pqstm_t *stm = php_pqstm_init(obj, name_str, query_str, params TSRMLS_CC);
1292
1293 return_value->type = IS_OBJECT;
1294 return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
1295 }
1296 }
1297 }
1298 }
1299
1300 ZEND_RESULT_CODE php_pqconn_prepare_async(zval *object, php_pqconn_object_t *obj, const char *name, const char *query, php_pq_params_t *params TSRMLS_DC)
1301 {
1302 ZEND_RESULT_CODE rv;
1303
1304 if (!obj) {
1305 obj = zend_object_store_get_object(object TSRMLS_CC);
1306 }
1307
1308 if (!PQsendPrepare(obj->intern->conn, name, query, params->type.count, params->type.oids)) {
1309 rv = FAILURE;
1310 throw_exce(EX_IO TSRMLS_CC, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj->intern->conn));
1311 } else {
1312 rv = SUCCESS;
1313 obj->intern->poller = PQconsumeInput;
1314 php_pqconn_notify_listeners(obj TSRMLS_CC);
1315 }
1316
1317 return rv;
1318 }
1319
1320 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare_async, 0, 0, 2)
1321 ZEND_ARG_INFO(0, name)
1322 ZEND_ARG_INFO(0, query)
1323 ZEND_ARG_ARRAY_INFO(0, types, 1)
1324 ZEND_END_ARG_INFO();
1325 static PHP_METHOD(pqconn, prepareAsync) {
1326 zend_error_handling zeh;
1327 zval *ztypes = NULL;
1328 char *name_str, *query_str;
1329 int name_len, *query_len;
1330 ZEND_RESULT_CODE rv;
1331
1332 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1333 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!", &name_str, &name_len, &query_str, &query_len, &ztypes);
1334 zend_restore_error_handling(&zeh TSRMLS_CC);
1335
1336 if (SUCCESS == rv) {
1337 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1338
1339 if (!obj->intern) {
1340 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1341 } else {
1342 php_pq_params_t *params = php_pq_params_init(&obj->intern->converters, ztypes ? Z_ARRVAL_P(ztypes) : NULL, NULL TSRMLS_CC);
1343
1344 if (SUCCESS != php_pqconn_prepare_async(getThis(), obj, name_str, query_str, params TSRMLS_CC)) {
1345 php_pq_params_free(&params);
1346 } else {
1347 php_pqstm_t *stm = php_pqstm_init(obj, name_str, query_str, params TSRMLS_CC);
1348
1349 return_value->type = IS_OBJECT;
1350 return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
1351 }
1352 }
1353 }
1354 }
1355
1356 ZEND_RESULT_CODE php_pqconn_declare(zval *object, php_pqconn_object_t *obj, const char *decl TSRMLS_DC)
1357 {
1358 PGresult *res;
1359 ZEND_RESULT_CODE rv;
1360
1361 if (!obj) {
1362 obj = zend_object_store_get_object(object TSRMLS_CC);
1363 }
1364
1365 res = PQexec(obj->intern->conn, decl);
1366
1367 if (!res) {
1368 rv = FAILURE;
1369 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj->intern->conn));
1370 } else {
1371 rv = php_pqres_success(res TSRMLS_CC);
1372 PHP_PQclear(res);
1373 php_pqconn_notify_listeners(obj TSRMLS_CC);
1374 }
1375
1376 return rv;
1377 }
1378
1379 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare, 0, 0, 3)
1380 ZEND_ARG_INFO(0, name)
1381 ZEND_ARG_INFO(0, flags)
1382 ZEND_ARG_INFO(0, query)
1383 ZEND_END_ARG_INFO();
1384 static PHP_METHOD(pqconn, declare) {
1385 zend_error_handling zeh;
1386 char *name_str, *query_str;
1387 int name_len, query_len;
1388 long flags;
1389 ZEND_RESULT_CODE rv;
1390
1391 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1392 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sls", &name_str, &name_len, &flags, &query_str, &query_len);
1393 zend_restore_error_handling(&zeh TSRMLS_CC);
1394
1395 if (SUCCESS == rv) {
1396 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1397
1398 if (!obj->intern) {
1399 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1400 } else {
1401 int query_offset;
1402 char *decl = php_pqcur_declare_str(name_str, name_len, flags, query_str, query_len, &query_offset);
1403
1404 if (SUCCESS != php_pqconn_declare(getThis(), obj, decl TSRMLS_CC)) {
1405 efree(decl);
1406 } else {
1407 php_pqcur_t *cur = php_pqcur_init(obj, name_str, decl, query_offset, flags TSRMLS_CC);
1408
1409 return_value->type = IS_OBJECT;
1410 return_value->value.obj = php_pqcur_create_object_ex(php_pqcur_class_entry, cur, NULL TSRMLS_CC);
1411 }
1412 }
1413 }
1414 }
1415
1416 ZEND_RESULT_CODE php_pqconn_declare_async(zval *object, php_pqconn_object_t *obj, const char *decl TSRMLS_DC)
1417 {
1418 ZEND_RESULT_CODE rv;
1419
1420 if (!obj) {
1421 obj = zend_object_store_get_object(object TSRMLS_CC);
1422 }
1423
1424 if (!PQsendQuery(obj->intern->conn, decl)) {
1425 rv = FAILURE;
1426 throw_exce(EX_IO TSRMLS_CC, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj->intern->conn));
1427 } else {
1428 rv = SUCCESS;
1429 obj->intern->poller = PQconsumeInput;
1430 php_pqconn_notify_listeners(obj TSRMLS_CC);
1431 }
1432
1433 return rv;
1434 }
1435
1436 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare_async, 0, 0, 2)
1437 ZEND_ARG_INFO(0, name)
1438 ZEND_ARG_INFO(0, flags)
1439 ZEND_ARG_INFO(0, query)
1440 ZEND_END_ARG_INFO();
1441 static PHP_METHOD(pqconn, declareAsync) {
1442 zend_error_handling zeh;
1443 char *name_str, *query_str;
1444 int name_len, query_len;
1445 long flags;
1446 ZEND_RESULT_CODE rv;
1447
1448 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1449 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sls", &name_str, &name_len, &flags, &query_str, &query_len);
1450 zend_restore_error_handling(&zeh TSRMLS_CC);
1451
1452 if (SUCCESS == rv) {
1453 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1454
1455 if (!obj->intern) {
1456 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1457 } else {
1458 int query_offset;
1459 char *decl = php_pqcur_declare_str(name_str, name_len, flags, query_str, query_len, &query_offset);
1460
1461 if (SUCCESS != php_pqconn_declare_async(getThis(), obj, decl TSRMLS_CC)) {
1462 efree(decl);
1463 } else {
1464 php_pqcur_t *cur = php_pqcur_init(obj, name_str, decl, query_offset, flags TSRMLS_CC);
1465
1466 return_value->type = IS_OBJECT;
1467 return_value->value.obj = php_pqcur_create_object_ex(php_pqcur_class_entry, cur, NULL TSRMLS_CC);
1468 }
1469 }
1470 }
1471 }
1472
1473 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote, 0, 0, 1)
1474 ZEND_ARG_INFO(0, string)
1475 ZEND_END_ARG_INFO();
1476 static PHP_METHOD(pqconn, quote) {
1477 char *str;
1478 int len;
1479
1480 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
1481 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1482
1483 if (!obj->intern) {
1484 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1485 } else {
1486 char *quoted = PQescapeLiteral(obj->intern->conn, str, len);
1487
1488 if (!quoted) {
1489 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to quote string (%s)", PHP_PQerrorMessage(obj->intern->conn));
1490 RETVAL_FALSE;
1491 } else {
1492 RETVAL_STRING(quoted, 1);
1493 PQfreemem(quoted);
1494 }
1495 }
1496 }
1497 }
1498
1499 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote_name, 0, 0, 1)
1500 ZEND_ARG_INFO(0, type)
1501 ZEND_END_ARG_INFO();
1502 static PHP_METHOD(pqconn, quoteName) {
1503 char *str;
1504 int len;
1505
1506 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
1507 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1508
1509 if (!obj->intern) {
1510 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1511 } else {
1512 char *quoted = PQescapeIdentifier(obj->intern->conn, str, len);
1513
1514 if (!quoted) {
1515 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to quote name (%s)", PHP_PQerrorMessage(obj->intern->conn));
1516 RETVAL_FALSE;
1517 } else {
1518 RETVAL_STRING(quoted, 1);
1519 PQfreemem(quoted);
1520 }
1521 }
1522 }
1523 }
1524
1525 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_escape_bytea, 0, 0, 1)
1526 ZEND_ARG_INFO(0, bytea)
1527 ZEND_END_ARG_INFO();
1528 static PHP_METHOD(pqconn, escapeBytea) {
1529 char *str;
1530 int len;
1531
1532 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
1533 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1534
1535 if (!obj->intern) {
1536 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1537 } else {
1538 size_t escaped_len;
1539 char *escaped_str = (char *) PQescapeByteaConn(obj->intern->conn, (unsigned char *) str, len, &escaped_len);
1540
1541 if (!escaped_str) {
1542 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to escape bytea (%s)", PHP_PQerrorMessage(obj->intern->conn));
1543 RETVAL_FALSE;
1544 } else {
1545 RETVAL_STRINGL(escaped_str, escaped_len - 1, 1);
1546 PQfreemem(escaped_str);
1547 }
1548 }
1549 }
1550 }
1551
1552 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unescape_bytea, 0, 0, 1)
1553 ZEND_ARG_INFO(0, bytea)
1554 ZEND_END_ARG_INFO();
1555 static PHP_METHOD(pqconn, unescapeBytea) {
1556 char *str;
1557 int len;
1558
1559 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
1560 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1561
1562 if (!obj->intern) {
1563 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1564 } else {
1565 size_t unescaped_len;
1566 char *unescaped_str = (char *) PQunescapeBytea((unsigned char *)str, &unescaped_len);
1567
1568 if (!unescaped_str) {
1569 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to unescape bytea (%s)", PHP_PQerrorMessage(obj->intern->conn));
1570 RETVAL_FALSE;
1571 } else {
1572 RETVAL_STRINGL(unescaped_str, unescaped_len, 1);
1573 PQfreemem(unescaped_str);
1574 }
1575 }
1576 }
1577 }
1578
1579 ZEND_RESULT_CODE php_pqconn_start_transaction(zval *zconn, php_pqconn_object_t *conn_obj, long isolation, zend_bool readonly, zend_bool deferrable TSRMLS_DC)
1580 {
1581 ZEND_RESULT_CODE rv = FAILURE;
1582
1583 if (!conn_obj) {
1584 conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
1585 }
1586
1587 if (!conn_obj->intern) {
1588 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1589 } else {
1590 PGresult *res;
1591 smart_str cmd = {0};
1592 const char *il = php_pq_isolation_level(&isolation);
1593
1594 smart_str_appends(&cmd, "START TRANSACTION ISOLATION LEVEL ");
1595 smart_str_appends(&cmd, il);
1596 smart_str_appends(&cmd, ", READ ");
1597 smart_str_appends(&cmd, readonly ? "ONLY" : "WRITE");
1598 smart_str_appends(&cmd, ",");
1599 smart_str_appends(&cmd, deferrable ? "" : " NOT");
1600 smart_str_appends(&cmd, " DEFERRABLE");
1601 smart_str_0(&cmd);
1602
1603 res = PQexec(conn_obj->intern->conn, cmd.c);
1604
1605 if (!res) {
1606 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj->intern->conn));
1607 } else {
1608 rv = php_pqres_success(res TSRMLS_CC);
1609 PHP_PQclear(res);
1610 php_pqconn_notify_listeners(conn_obj TSRMLS_CC);
1611 }
1612
1613 smart_str_free(&cmd);
1614 }
1615
1616 return rv;
1617 }
1618
1619 ZEND_RESULT_CODE php_pqconn_start_transaction_async(zval *zconn, php_pqconn_object_t *conn_obj, long isolation, zend_bool readonly, zend_bool deferrable TSRMLS_DC)
1620 {
1621 ZEND_RESULT_CODE rv = FAILURE;
1622
1623 if (!conn_obj) {
1624 conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
1625 }
1626
1627 if (!conn_obj->intern) {
1628 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1629 } else {
1630 smart_str cmd = {0};
1631 const char *il = php_pq_isolation_level(&isolation);
1632
1633 smart_str_appends(&cmd, "START TRANSACTION ISOLATION LEVEL ");
1634 smart_str_appends(&cmd, il);
1635 smart_str_appends(&cmd, ", READ ");
1636 smart_str_appends(&cmd, readonly ? "ONLY" : "WRITE");
1637 smart_str_appends(&cmd, ",");
1638 smart_str_appends(&cmd, deferrable ? "" : "NOT ");
1639 smart_str_appends(&cmd, " DEFERRABLE");
1640 smart_str_0(&cmd);
1641
1642 if (!PQsendQuery(conn_obj->intern->conn, cmd.c)) {
1643 throw_exce(EX_IO TSRMLS_CC, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj->intern->conn));
1644 } else {
1645 rv = SUCCESS;
1646 conn_obj->intern->poller = PQconsumeInput;
1647 php_pqconn_notify_listeners(conn_obj TSRMLS_CC);
1648 }
1649
1650 smart_str_free(&cmd);
1651 }
1652
1653 return rv;
1654 }
1655
1656 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction, 0, 0, 0)
1657 ZEND_ARG_INFO(0, isolation)
1658 ZEND_ARG_INFO(0, readonly)
1659 ZEND_ARG_INFO(0, deferrable)
1660 ZEND_END_ARG_INFO();
1661 static PHP_METHOD(pqconn, startTransaction) {
1662 zend_error_handling zeh;
1663 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1664 long isolation = obj->intern ? obj->intern->default_txn_isolation : PHP_PQTXN_READ_COMMITTED;
1665 zend_bool readonly = obj->intern ? obj->intern->default_txn_readonly : 0;
1666 zend_bool deferrable = obj->intern ? obj->intern->default_txn_deferrable : 0;
1667 ZEND_RESULT_CODE rv;
1668
1669 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1670 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable);
1671 zend_restore_error_handling(&zeh TSRMLS_CC);
1672
1673 if (SUCCESS == rv) {
1674 rv = php_pqconn_start_transaction(getThis(), obj, isolation, readonly, deferrable TSRMLS_CC);
1675
1676 if (SUCCESS == rv) {
1677 php_pqtxn_t *txn = ecalloc(1, sizeof(*txn));
1678
1679 php_pq_object_addref(obj TSRMLS_CC);
1680 txn->conn = obj;
1681 txn->open = 1;
1682 txn->isolation = isolation;
1683 txn->readonly = readonly;
1684 txn->deferrable = deferrable;
1685
1686 return_value->type = IS_OBJECT;
1687 return_value->value.obj = php_pqtxn_create_object_ex(php_pqtxn_class_entry, txn, NULL TSRMLS_CC);
1688 }
1689 }
1690 }
1691
1692 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction_async, 0, 0, 0)
1693 ZEND_ARG_INFO(0, isolation)
1694 ZEND_ARG_INFO(0, readonly)
1695 ZEND_ARG_INFO(0, deferrable)
1696 ZEND_END_ARG_INFO();
1697 static PHP_METHOD(pqconn, startTransactionAsync) {
1698 zend_error_handling zeh;
1699 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1700 long isolation = obj->intern ? obj->intern->default_txn_isolation : PHP_PQTXN_READ_COMMITTED;
1701 zend_bool readonly = obj->intern ? obj->intern->default_txn_readonly : 0;
1702 zend_bool deferrable = obj->intern ? obj->intern->default_txn_deferrable : 0;
1703 ZEND_RESULT_CODE rv;
1704
1705 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1706 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable);
1707 zend_restore_error_handling(&zeh TSRMLS_CC);
1708
1709 if (SUCCESS == rv) {
1710 rv = php_pqconn_start_transaction_async(getThis(), obj, isolation, readonly, deferrable TSRMLS_CC);
1711
1712 if (SUCCESS == rv) {
1713 php_pqtxn_t *txn = ecalloc(1, sizeof(*txn));
1714
1715 php_pq_object_addref(obj TSRMLS_CC);
1716 txn->conn = obj;
1717 txn->open = 1;
1718 txn->isolation = isolation;
1719 txn->readonly = readonly;
1720 txn->deferrable = deferrable;
1721
1722 return_value->type = IS_OBJECT;
1723 return_value->value.obj = php_pqtxn_create_object_ex(php_pqtxn_class_entry, txn, NULL TSRMLS_CC);
1724 }
1725 }
1726 }
1727
1728 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_trace, 0, 0, 0)
1729 ZEND_ARG_INFO(0, stdio_stream)
1730 ZEND_END_ARG_INFO();
1731 static PHP_METHOD(pqconn, trace) {
1732 zval *zstream = NULL;
1733
1734 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r!", &zstream)) {
1735 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1736
1737 if (!obj->intern) {
1738 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1739 } else {
1740 if (!zstream) {
1741 PQuntrace(obj->intern->conn);
1742 RETVAL_TRUE;
1743 } else {
1744 FILE *fp;
1745 php_stream *stream = NULL;
1746
1747 php_stream_from_zval(stream, &zstream);
1748
1749 if (SUCCESS != php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
1750 RETVAL_FALSE;
1751 } else {
1752 stream->flags |= PHP_STREAM_FLAG_NO_CLOSE;
1753 PQtrace(obj->intern->conn, fp);
1754 RETVAL_TRUE;
1755 }
1756 }
1757 }
1758 }
1759 }
1760
1761 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_off, 0, 0, 1)
1762 ZEND_ARG_INFO(0, type)
1763 ZEND_END_ARG_INFO();
1764 static PHP_METHOD(pqconn, off) {
1765 zend_error_handling zeh;
1766 char *type_str;
1767 int type_len;
1768 ZEND_RESULT_CODE rv;
1769
1770 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1771 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &type_str, &type_len);
1772 zend_restore_error_handling(&zeh TSRMLS_CC);
1773
1774 if (SUCCESS == rv) {
1775 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1776
1777 if (!obj->intern) {
1778 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1779 } else {
1780 RETURN_BOOL(SUCCESS == zend_hash_del(&obj->intern->eventhandlers, type_str, type_len + 1));
1781 }
1782 }
1783 }
1784
1785 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_on, 0, 0, 2)
1786 ZEND_ARG_INFO(0, type)
1787 ZEND_ARG_INFO(0, callable)
1788 ZEND_END_ARG_INFO();
1789 static PHP_METHOD(pqconn, on) {
1790 zend_error_handling zeh;
1791 char *type_str;
1792 int type_len;
1793 php_pq_callback_t cb = {{0}};
1794 ZEND_RESULT_CODE rv;
1795
1796 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1797 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sf", &type_str, &type_len, &cb.fci, &cb.fcc);
1798 zend_restore_error_handling(&zeh TSRMLS_CC);
1799
1800 if (SUCCESS == rv) {
1801 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1802
1803 if (!obj->intern) {
1804 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1805 } else {
1806 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1807
1808 RETVAL_LONG(php_pqconn_add_eventhandler(obj, type_str, type_len, &cb TSRMLS_CC));
1809 }
1810 }
1811 }
1812
1813 struct apply_set_converter_arg {
1814 HashTable *ht;
1815 zval **zconv;
1816 unsigned add:1;
1817 };
1818
1819 static int apply_set_converter(void *p, void *a TSRMLS_DC)
1820 {
1821 zval *tmp, **zoid = p;
1822 struct apply_set_converter_arg *arg = a;
1823
1824 tmp = *zoid;
1825 Z_ADDREF_P(tmp);
1826 convert_to_long_ex(&tmp);
1827 if (arg->add) {
1828 Z_ADDREF_PP(arg->zconv);
1829 zend_hash_index_update(arg->ht, Z_LVAL_P(tmp), arg->zconv, sizeof(zval *), NULL);
1830 } else {
1831 zend_hash_index_del(arg->ht, Z_LVAL_P(tmp));
1832 }
1833 zval_ptr_dtor(&tmp);
1834
1835 return ZEND_HASH_APPLY_KEEP;
1836 }
1837
1838 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_set_converter, 0, 0, 1)
1839 ZEND_ARG_OBJ_INFO(0, converter, pq\\Converter, 0)
1840 ZEND_END_ARG_INFO();
1841 static PHP_METHOD(pqconn, setConverter) {
1842 ZEND_RESULT_CODE rv;
1843 zend_error_handling zeh;
1844 zval *zcnv;
1845
1846 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1847 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zcnv, php_pqconv_class_entry);
1848 zend_restore_error_handling(&zeh TSRMLS_CC);
1849
1850 if (SUCCESS == rv) {
1851 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1852
1853 if (!obj->intern) {
1854 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1855 } else {
1856 zval *tmp, *zoids = NULL;
1857 struct apply_set_converter_arg arg = {NULL};
1858
1859 zend_call_method_with_0_params(&zcnv, NULL, NULL, "converttypes", &zoids);
1860 tmp = zoids;
1861 Z_ADDREF_P(tmp);
1862 convert_to_array_ex(&tmp);
1863
1864 arg.ht = &obj->intern->converters;
1865 arg.zconv = &zcnv;
1866 arg.add = 1;
1867
1868 zend_hash_apply_with_argument(Z_ARRVAL_P(tmp), apply_set_converter, &arg TSRMLS_CC);
1869
1870 zval_ptr_dtor(&tmp);
1871 zval_ptr_dtor(&zoids);
1872 }
1873 }
1874 }
1875
1876 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unset_converter, 0, 0, 1)
1877 ZEND_ARG_OBJ_INFO(0, converter, pq\\Converter, 0)
1878 ZEND_END_ARG_INFO();
1879 static PHP_METHOD(pqconn, unsetConverter) {
1880 ZEND_RESULT_CODE rv;
1881 zend_error_handling zeh;
1882 zval *zcnv;
1883
1884 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1885 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zcnv, php_pqconv_class_entry);
1886 zend_restore_error_handling(&zeh TSRMLS_CC);
1887
1888 if (SUCCESS == rv) {
1889 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1890
1891 if (!obj->intern) {
1892 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1893 } else {
1894 zval *tmp, *zoids = NULL;
1895 struct apply_set_converter_arg arg = {NULL};
1896
1897 zend_call_method_with_0_params(&zcnv, NULL, NULL, "converttypes", &zoids);
1898 tmp = zoids;
1899 Z_ADDREF_P(tmp);
1900 convert_to_array_ex(&tmp);
1901
1902 arg.ht = &obj->intern->converters;
1903 arg.zconv = &zcnv;
1904 arg.add = 0;
1905
1906 zend_hash_apply_with_argument(Z_ARRVAL_P(tmp), apply_set_converter, &arg TSRMLS_CC);
1907
1908 zval_ptr_dtor(&tmp);
1909 zval_ptr_dtor(&zoids);
1910 }
1911 }
1912 }
1913
1914 static zend_function_entry php_pqconn_methods[] = {
1915 PHP_ME(pqconn, __construct, ai_pqconn_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1916 PHP_ME(pqconn, reset, ai_pqconn_reset, ZEND_ACC_PUBLIC)
1917 PHP_ME(pqconn, resetAsync, ai_pqconn_reset_async, ZEND_ACC_PUBLIC)
1918 PHP_ME(pqconn, poll, ai_pqconn_poll, ZEND_ACC_PUBLIC)
1919 PHP_ME(pqconn, exec, ai_pqconn_exec, ZEND_ACC_PUBLIC)
1920 PHP_ME(pqconn, execAsync, ai_pqconn_exec_async, ZEND_ACC_PUBLIC)
1921 PHP_ME(pqconn, execParams, ai_pqconn_exec_params, ZEND_ACC_PUBLIC)
1922 PHP_ME(pqconn, execParamsAsync, ai_pqconn_exec_params_async, ZEND_ACC_PUBLIC)
1923 PHP_ME(pqconn, prepare, ai_pqconn_prepare, ZEND_ACC_PUBLIC)
1924 PHP_ME(pqconn, prepareAsync, ai_pqconn_prepare_async, ZEND_ACC_PUBLIC)
1925 PHP_ME(pqconn, declare, ai_pqconn_declare, ZEND_ACC_PUBLIC)
1926 PHP_ME(pqconn, declareAsync, ai_pqconn_declare_async, ZEND_ACC_PUBLIC)
1927 PHP_ME(pqconn, unlisten, ai_pqconn_unlisten, ZEND_ACC_PUBLIC)
1928 PHP_ME(pqconn, unlistenAsync, ai_pqconn_unlisten_async, ZEND_ACC_PUBLIC)
1929 PHP_ME(pqconn, listen, ai_pqconn_listen, ZEND_ACC_PUBLIC)
1930 PHP_ME(pqconn, listenAsync, ai_pqconn_listen_async, ZEND_ACC_PUBLIC)
1931 PHP_ME(pqconn, notify, ai_pqconn_notify, ZEND_ACC_PUBLIC)
1932 PHP_ME(pqconn, notifyAsync, ai_pqconn_notify_async, ZEND_ACC_PUBLIC)
1933 PHP_ME(pqconn, getResult, ai_pqconn_get_result, ZEND_ACC_PUBLIC)
1934 PHP_ME(pqconn, quote, ai_pqconn_quote, ZEND_ACC_PUBLIC)
1935 PHP_ME(pqconn, quoteName, ai_pqconn_quote_name, ZEND_ACC_PUBLIC)
1936 PHP_ME(pqconn, escapeBytea, ai_pqconn_escape_bytea, ZEND_ACC_PUBLIC)
1937 PHP_ME(pqconn, unescapeBytea, ai_pqconn_unescape_bytea, ZEND_ACC_PUBLIC)
1938 PHP_ME(pqconn, startTransaction, ai_pqconn_start_transaction, ZEND_ACC_PUBLIC)
1939 PHP_ME(pqconn, startTransactionAsync, ai_pqconn_start_transaction_async, ZEND_ACC_PUBLIC)
1940 PHP_ME(pqconn, trace, ai_pqconn_trace, ZEND_ACC_PUBLIC)
1941 PHP_ME(pqconn, off, ai_pqconn_off, ZEND_ACC_PUBLIC)
1942 PHP_ME(pqconn, on, ai_pqconn_on, ZEND_ACC_PUBLIC)
1943 PHP_ME(pqconn, setConverter, ai_pqconn_set_converter, ZEND_ACC_PUBLIC)
1944 PHP_ME(pqconn, unsetConverter, ai_pqconn_unset_converter, ZEND_ACC_PUBLIC)
1945 {0}
1946 };
1947
1948 PHP_MSHUTDOWN_FUNCTION(pqconn)
1949 {
1950 zend_hash_destroy(&php_pqconn_object_prophandlers);
1951 return SUCCESS;
1952 }
1953
1954 PHP_MINIT_FUNCTION(pqconn)
1955 {
1956 zend_class_entry ce = {0};
1957 php_pq_object_prophandler_t ph = {0};
1958
1959 INIT_NS_CLASS_ENTRY(ce, "pq", "Connection", php_pqconn_methods);
1960 php_pqconn_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
1961 php_pqconn_class_entry->create_object = php_pqconn_create_object;
1962
1963 memcpy(&php_pqconn_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1964 php_pqconn_object_handlers.read_property = php_pq_object_read_prop;
1965 php_pqconn_object_handlers.write_property = php_pq_object_write_prop;
1966 php_pqconn_object_handlers.clone_obj = NULL;
1967 php_pqconn_object_handlers.get_property_ptr_ptr = NULL;
1968 php_pqconn_object_handlers.get_gc = NULL;
1969 php_pqconn_object_handlers.get_properties = php_pq_object_properties;
1970 php_pqconn_object_handlers.get_debug_info = php_pq_object_debug_info;
1971
1972 zend_hash_init(&php_pqconn_object_prophandlers, 20, NULL, NULL, 1);
1973
1974 zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("status"), CONNECTION_BAD, ZEND_ACC_PUBLIC TSRMLS_CC);
1975 ph.read = php_pqconn_object_read_status;
1976 zend_hash_add(&php_pqconn_object_prophandlers, "status", sizeof("status"), (void *) &ph, sizeof(ph), NULL);
1977
1978 zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("transactionStatus"), PQTRANS_UNKNOWN, ZEND_ACC_PUBLIC TSRMLS_CC);
1979 ph.read = php_pqconn_object_read_transaction_status;
1980 zend_hash_add(&php_pqconn_object_prophandlers, "transactionStatus", sizeof("transactionStatus"), (void *) &ph, sizeof(ph), NULL);
1981
1982 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("socket"), ZEND_ACC_PUBLIC TSRMLS_CC);
1983 ph.read = NULL; /* forward to std prophandler */
1984 zend_hash_add(&php_pqconn_object_prophandlers, "socket", sizeof("socket"), (void *) &ph, sizeof(ph), NULL);
1985
1986 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("errorMessage"), ZEND_ACC_PUBLIC TSRMLS_CC);
1987 ph.read = php_pqconn_object_read_error_message;
1988 zend_hash_add(&php_pqconn_object_prophandlers, "errorMessage", sizeof("errorMessage"), (void *) &ph, sizeof(ph), NULL);
1989
1990 zend_declare_property_bool(php_pqconn_class_entry, ZEND_STRL("busy"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
1991 ph.read = php_pqconn_object_read_busy;
1992 zend_hash_add(&php_pqconn_object_prophandlers, "busy", sizeof("busy"), (void *) &ph, sizeof(ph), NULL);
1993
1994 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("encoding"), ZEND_ACC_PUBLIC TSRMLS_CC);
1995 ph.read = php_pqconn_object_read_encoding;
1996 ph.write = php_pqconn_object_write_encoding;
1997 zend_hash_add(&php_pqconn_object_prophandlers, "encoding", sizeof("encoding"), (void *) &ph, sizeof(ph), NULL);
1998 ph.write = NULL;
1999
2000 zend_declare_property_bool(php_pqconn_class_entry, ZEND_STRL("unbuffered"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
2001 ph.read = php_pqconn_object_read_unbuffered;
2002 ph.write = php_pqconn_object_write_unbuffered;
2003 zend_hash_add(&php_pqconn_object_prophandlers, "unbuffered", sizeof("unbuffered"), (void *) &ph, sizeof(ph), NULL);
2004 ph.write = NULL;
2005
2006 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("db"), ZEND_ACC_PUBLIC TSRMLS_CC);
2007 ph.read = php_pqconn_object_read_db;
2008 zend_hash_add(&php_pqconn_object_prophandlers, "db", sizeof("db"), (void *) &ph, sizeof(ph), NULL);
2009
2010 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("user"), ZEND_ACC_PUBLIC TSRMLS_CC);
2011 ph.read = php_pqconn_object_read_user;
2012 zend_hash_add(&php_pqconn_object_prophandlers, "user", sizeof("user"), (void *) &ph, sizeof(ph), NULL);
2013
2014 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("pass"), ZEND_ACC_PUBLIC TSRMLS_CC);
2015 ph.read = php_pqconn_object_read_pass;
2016 zend_hash_add(&php_pqconn_object_prophandlers, "pass", sizeof("pass"), (void *) &ph, sizeof(ph), NULL);
2017
2018 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("host"), ZEND_ACC_PUBLIC TSRMLS_CC);
2019 ph.read = php_pqconn_object_read_host;
2020 zend_hash_add(&php_pqconn_object_prophandlers, "host", sizeof("host"), (void *) &ph, sizeof(ph), NULL);
2021
2022 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("port"), ZEND_ACC_PUBLIC TSRMLS_CC);
2023 ph.read = php_pqconn_object_read_port;
2024 zend_hash_add(&php_pqconn_object_prophandlers, "port", sizeof("port"), (void *) &ph, sizeof(ph), NULL);
2025
2026 #if HAVE_PQCONNINFO
2027 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("params"), ZEND_ACC_PUBLIC TSRMLS_CC);
2028 ph.read = php_pqconn_object_read_params;
2029 zend_hash_add(&php_pqconn_object_prophandlers, "params", sizeof("params"), (void *) &ph, sizeof(ph), NULL);
2030 #endif
2031
2032 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("options"), ZEND_ACC_PUBLIC TSRMLS_CC);
2033 ph.read = php_pqconn_object_read_options;
2034 zend_hash_add(&php_pqconn_object_prophandlers, "options", sizeof("options"), (void *) &ph, sizeof(ph), NULL);
2035
2036 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("eventHandlers"), ZEND_ACC_PUBLIC TSRMLS_CC);
2037 ph.read = php_pqconn_object_read_event_handlers;
2038 zend_hash_add(&php_pqconn_object_prophandlers, "eventHandlers", sizeof("eventHandlers"), (void *) &ph, sizeof(ph), NULL);
2039
2040 zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("defaultFetchType"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
2041 ph.read = php_pqconn_object_read_def_fetch_type;
2042 ph.write = php_pqconn_object_write_def_fetch_type;
2043 zend_hash_add(&php_pqconn_object_prophandlers, "defaultFetchType", sizeof("defaultFetchType"), (void *) &ph, sizeof(ph), NULL);
2044 ph.write = NULL;
2045
2046 zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("defaultTransactionIsolation"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
2047 ph.read = php_pqconn_object_read_def_txn_isolation;
2048 ph.write = php_pqconn_object_write_def_txn_isolation;
2049 zend_hash_add(&php_pqconn_object_prophandlers, "defaultTransactionIsolation", sizeof("defaultTransactionIsolation"), (void *) &ph, sizeof(ph), NULL);
2050 ph.write = NULL;
2051
2052 zend_declare_property_bool(php_pqconn_class_entry, ZEND_STRL("defaultTransactionReadonly"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
2053 ph.read = php_pqconn_object_read_def_txn_readonly;
2054 ph.write = php_pqconn_object_write_def_txn_readonly;
2055 zend_hash_add(&php_pqconn_object_prophandlers, "defaultTransactionReadonly", sizeof("defaultTransactionReadonly"), (void *) &ph, sizeof(ph), NULL);
2056 ph.write = NULL;
2057
2058 zend_declare_property_bool(php_pqconn_class_entry, ZEND_STRL("defaultTransactionDeferrable"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
2059 ph.read = php_pqconn_object_read_def_txn_deferrable;
2060 ph.write = php_pqconn_object_write_def_txn_deferrable;
2061 zend_hash_add(&php_pqconn_object_prophandlers, "defaultTransactionDeferrable", sizeof("defaultTransactionDeferrable"), (void *) &ph, sizeof(ph), NULL);
2062 ph.write = NULL;
2063
2064 zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("defaultAutoConvert"), PHP_PQRES_CONV_ALL, ZEND_ACC_PUBLIC TSRMLS_CC);
2065 ph.read = php_pqconn_object_read_def_auto_conv;
2066 ph.write = php_pqconn_object_write_def_auto_conv;
2067 zend_hash_add(&php_pqconn_object_prophandlers, "defaultAutoConvert", sizeof("defaultAutoConvert"), (void *) &ph, sizeof(ph), NULL);
2068 ph.write = NULL;
2069
2070 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("OK"), CONNECTION_OK TSRMLS_CC);
2071 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("BAD"), CONNECTION_BAD TSRMLS_CC);
2072 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("STARTED"), CONNECTION_STARTED TSRMLS_CC);
2073 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("MADE"), CONNECTION_MADE TSRMLS_CC);
2074 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("AWAITING_RESPONSE"), CONNECTION_AWAITING_RESPONSE TSRMLS_CC);
2075 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("AUTH_OK"), CONNECTION_AUTH_OK TSRMLS_CC);
2076 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("SSL_STARTUP"), CONNECTION_SSL_STARTUP TSRMLS_CC);
2077 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("SETENV"), CONNECTION_SETENV TSRMLS_CC);
2078
2079 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("TRANS_IDLE"), PQTRANS_IDLE TSRMLS_CC);
2080 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("TRANS_ACTIVE"), PQTRANS_ACTIVE TSRMLS_CC);
2081 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("TRANS_INTRANS"), PQTRANS_INTRANS TSRMLS_CC);
2082 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("TRANS_INERROR"), PQTRANS_INERROR TSRMLS_CC);
2083 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("TRANS_UNKNOWN"), PQTRANS_UNKNOWN TSRMLS_CC);
2084
2085 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_FAILED"), PGRES_POLLING_FAILED TSRMLS_CC);
2086 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_READING"), PGRES_POLLING_READING TSRMLS_CC);
2087 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_WRITING"), PGRES_POLLING_WRITING TSRMLS_CC);
2088 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_OK"), PGRES_POLLING_OK TSRMLS_CC);
2089
2090 zend_declare_class_constant_stringl(php_pqconn_class_entry, ZEND_STRL("EVENT_NOTICE"), ZEND_STRL("notice") TSRMLS_CC);
2091 zend_declare_class_constant_stringl(php_pqconn_class_entry, ZEND_STRL("EVENT_RESULT"), ZEND_STRL("result") TSRMLS_CC);
2092 zend_declare_class_constant_stringl(php_pqconn_class_entry, ZEND_STRL("EVENT_RESET"), ZEND_STRL("reset") TSRMLS_CC);
2093
2094 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("ASYNC"), 0x1 TSRMLS_CC);
2095 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("PERSISTENT"), 0x2 TSRMLS_CC);
2096
2097 return SUCCESS;
2098 }
2099
2100 /*
2101 * Local variables:
2102 * tab-width: 4
2103 * c-basic-offset: 4
2104 * End:
2105 * vim600: noet sw=4 ts=4 fdm=marker
2106 * vim<600: noet sw=4 ts=4
2107 */