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