66b5d9f63e106eda534ed1cd41acf39c33c4c2c0
[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 zend_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 static void php_pqconn_object_read_options(zval *object, void *o, zval *return_value TSRMLS_DC)
305 {
306 php_pqconn_object_t *obj = o;
307 char *options = PQoptions(obj->intern->conn);
308
309 if (options) {
310 RETVAL_STRING(options, 1);
311 } else {
312 RETVAL_EMPTY_STRING();
313 }
314 }
315
316 static int apply_read_event_handler_ex(void *p, void *arg TSRMLS_DC)
317 {
318 HashTable *rv = arg;
319 zval *zcb = php_pq_callback_to_zval(p);
320
321 zend_hash_next_index_insert(rv, &zcb, sizeof(zval *), NULL);
322
323 return ZEND_HASH_APPLY_KEEP;
324 }
325
326 static int apply_read_event_handlers(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
327 {
328 HashTable *evhs = p, *rv = va_arg(argv, HashTable *);
329 zval *entry, **entry_ptr;
330
331 MAKE_STD_ZVAL(entry);
332 array_init_size(entry, zend_hash_num_elements(evhs));
333
334 if (key->nKeyLength) {
335 zend_hash_add(rv, key->arKey, key->nKeyLength, &entry, sizeof(zval *), (void *) &entry_ptr);
336 } else {
337 zend_hash_index_update(rv, key->h, &entry, sizeof(zval *), (void *) &entry_ptr);
338 }
339
340 zend_hash_apply_with_argument(evhs, apply_read_event_handler_ex, Z_ARRVAL_PP(entry_ptr) TSRMLS_CC);
341
342 return ZEND_HASH_APPLY_KEEP;
343 }
344 static void php_pqconn_object_read_event_handlers(zval *object, void *o, zval *return_value TSRMLS_DC)
345 {
346 php_pqconn_object_t *obj = o;
347
348 array_init(return_value);
349 zend_hash_apply_with_arguments(&obj->intern->eventhandlers TSRMLS_CC, apply_read_event_handlers, 1, Z_ARRVAL_P(return_value) TSRMLS_CC);
350 }
351
352 static STATUS php_pqconn_update_socket(zval *this_ptr, php_pqconn_object_t *obj TSRMLS_DC)
353 {
354 zval *zsocket, zmember;
355 php_stream *stream;
356 STATUS retval;
357 int socket;
358
359 if (!obj) {
360 obj = zend_object_store_get_object(getThis() TSRMLS_CC);
361 }
362
363 INIT_PZVAL(&zmember);
364 ZVAL_STRINGL(&zmember, "socket", sizeof("socket")-1, 0);
365 MAKE_STD_ZVAL(zsocket);
366
367 if ((CONNECTION_BAD != PQstatus(obj->intern->conn))
368 && (-1 < (socket = PQsocket(obj->intern->conn)))
369 && (stream = php_stream_fopen_from_fd(socket, "r+b", NULL))) {
370 stream->flags |= PHP_STREAM_FLAG_NO_CLOSE;
371 php_stream_to_zval(stream, zsocket);
372 retval = SUCCESS;
373 } else {
374 ZVAL_NULL(zsocket);
375 retval = FAILURE;
376 }
377 zend_get_std_object_handlers()->write_property(getThis(), &zmember, zsocket, NULL TSRMLS_CC);
378 zval_ptr_dtor(&zsocket);
379
380 return retval;
381 }
382
383 static void *php_pqconn_resource_factory_ctor(void *data, void *init_arg TSRMLS_DC)
384 {
385 php_pqconn_resource_factory_data_t *o = init_arg;
386 PGconn *conn = NULL;;
387
388 if (o->flags & PHP_PQCONN_ASYNC) {
389 conn = PQconnectStart(o->dsn);
390 } else {
391 conn = PQconnectdb(o->dsn);
392 }
393
394 if (conn) {
395 PQregisterEventProc(conn, php_pqconn_event, "ext-pq", NULL);
396 }
397
398 return conn;
399 }
400
401 static void php_pqconn_resource_factory_dtor(void *opaque, void *handle TSRMLS_DC)
402 {
403 php_pqconn_event_data_t *evdata = PQinstanceData(handle, php_pqconn_event);
404
405 /* we don't care for anything, except free'ing evdata */
406 if (evdata) {
407 PQsetInstanceData(handle, php_pqconn_event, NULL);
408 memset(evdata, 0, sizeof(*evdata));
409 efree(evdata);
410 }
411
412 PQfinish(handle);
413 }
414
415 static php_resource_factory_ops_t php_pqconn_resource_factory_ops = {
416 php_pqconn_resource_factory_ctor,
417 NULL,
418 php_pqconn_resource_factory_dtor
419 };
420
421 php_resource_factory_ops_t *php_pqconn_get_resource_factory_ops(void)
422 {
423 return &php_pqconn_resource_factory_ops;
424 }
425
426 static void php_pqconn_wakeup(php_persistent_handle_factory_t *f, void **handle TSRMLS_DC)
427 {
428 // FIXME: ping server
429 }
430
431 static int apply_unlisten(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
432 {
433 php_pqconn_object_t *obj = va_arg(argv, php_pqconn_object_t *);
434 char *quoted_channel = PQescapeIdentifier(obj->intern->conn, key->arKey, key->nKeyLength - 1);
435
436 if (quoted_channel) {
437 PGresult *res;
438 char *cmd;
439
440 spprintf(&cmd, 0, "UNLISTEN %s", quoted_channel);
441 if ((res = PQexec(obj->intern->conn, cmd))) {
442 PHP_PQclear(res);
443 }
444
445 efree(cmd);
446 PQfreemem(quoted_channel);
447 }
448
449 return ZEND_HASH_APPLY_REMOVE;
450 }
451
452 static void php_pqconn_retire(php_persistent_handle_factory_t *f, void **handle TSRMLS_DC)
453 {
454 php_pqconn_event_data_t *evdata = PQinstanceData(*handle, php_pqconn_event);
455 PGcancel *cancel;
456 PGresult *res;
457
458 /* go away */
459 PQsetInstanceData(*handle, php_pqconn_event, NULL);
460
461 /* ignore notices */
462 PQsetNoticeReceiver(*handle, php_pqconn_notice_ignore, NULL);
463
464 /* cancel async queries */
465 if (PQisBusy(*handle) && (cancel = PQgetCancel(*handle))) {
466 char err[256] = {0};
467
468 PQcancel(cancel, err, sizeof(err));
469 PQfreeCancel(cancel);
470 }
471 /* clean up async results */
472 while ((res = PQgetResult(*handle))) {
473 PHP_PQclear(res);
474 }
475
476 /* clean up transaction & session */
477 switch (PQtransactionStatus(*handle)) {
478 case PQTRANS_IDLE:
479 res = PQexec(*handle, "RESET ALL");
480 break;
481 default:
482 res = PQexec(*handle, "ROLLBACK; RESET ALL");
483 break;
484 }
485
486 if (res) {
487 PHP_PQclear(res);
488 }
489
490 if (evdata) {
491 /* clean up notify listeners */
492 zend_hash_apply_with_arguments(&evdata->obj->intern->listeners TSRMLS_CC, apply_unlisten, 1, evdata->obj);
493
494 /* release instance data */
495 //memset(evdata, 0, sizeof(*evdata));
496 efree(evdata);
497 }
498 }
499
500 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct, 0, 0, 1)
501 ZEND_ARG_INFO(0, dsn)
502 ZEND_ARG_INFO(0, async)
503 ZEND_END_ARG_INFO();
504 static PHP_METHOD(pqconn, __construct) {
505 zend_error_handling zeh;
506 char *dsn_str = "";
507 int dsn_len = 0;
508 long flags = 0;
509 STATUS rv;
510
511 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
512 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &dsn_str, &dsn_len, &flags);
513 zend_restore_error_handling(&zeh TSRMLS_CC);
514
515 if (SUCCESS == rv) {
516 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
517
518 if (obj->intern) {
519 throw_exce(EX_BAD_METHODCALL TSRMLS_CC, "pq\\Connection already initialized");
520 } else {
521 php_pqconn_event_data_t *evdata = php_pqconn_event_data_init(obj TSRMLS_CC);
522 php_pqconn_resource_factory_data_t rfdata = {dsn_str, flags};
523
524 obj->intern = ecalloc(1, sizeof(*obj->intern));
525
526 zend_hash_init(&obj->intern->listeners, 0, NULL, (dtor_func_t) zend_hash_destroy, 0);
527 zend_hash_init(&obj->intern->converters, 0, NULL, ZVAL_PTR_DTOR, 0);
528 zend_hash_init(&obj->intern->eventhandlers, 0, NULL, (dtor_func_t) zend_hash_destroy, 0);
529
530 if (flags & PHP_PQCONN_PERSISTENT) {
531 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);
532 php_resource_factory_init(&obj->intern->factory, php_persistent_handle_get_resource_factory_ops(), phf, (void (*)(void*)) php_persistent_handle_abandon);
533 } else {
534 php_resource_factory_init(&obj->intern->factory, &php_pqconn_resource_factory_ops, NULL, NULL);
535 }
536
537 if (flags & PHP_PQCONN_ASYNC) {
538 obj->intern->poller = (int (*)(PGconn*)) PQconnectPoll;
539 }
540
541 obj->intern->conn = php_resource_factory_handle_ctor(&obj->intern->factory, &rfdata TSRMLS_CC);
542
543 PQsetInstanceData(obj->intern->conn, php_pqconn_event, evdata);
544 PQsetNoticeReceiver(obj->intern->conn, php_pqconn_notice_recv, evdata);
545
546 if (SUCCESS != php_pqconn_update_socket(getThis(), obj TSRMLS_CC)) {
547 throw_exce(EX_CONNECTION_FAILED TSRMLS_CC, "Connection failed (%s)", PHP_PQerrorMessage(obj->intern->conn));
548 }
549 }
550 }
551 }
552
553 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset, 0, 0, 0)
554 ZEND_END_ARG_INFO();
555 static PHP_METHOD(pqconn, reset) {
556 zend_error_handling zeh;
557 STATUS rv;
558
559 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
560 rv = zend_parse_parameters_none();
561 zend_restore_error_handling(&zeh TSRMLS_CC);
562
563 if (SUCCESS == rv) {
564 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
565
566 if (!obj->intern) {
567 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
568 } else {
569 PQreset(obj->intern->conn);
570
571 if (CONNECTION_OK != PQstatus(obj->intern->conn)) {
572 throw_exce(EX_CONNECTION_FAILED TSRMLS_CC, "Connection reset failed: (%s)", PHP_PQerrorMessage(obj->intern->conn));
573 }
574
575 php_pqconn_notify_listeners(obj TSRMLS_CC);
576 }
577 }
578 }
579
580 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset_async, 0, 0, 0)
581 ZEND_END_ARG_INFO();
582 static PHP_METHOD(pqconn, resetAsync) {
583 zend_error_handling zeh;
584 STATUS rv;
585
586 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
587 rv = zend_parse_parameters_none();
588 zend_restore_error_handling(&zeh TSRMLS_CC);
589
590 if (SUCCESS == rv) {
591 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
592
593 if (!obj->intern) {
594 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
595 } else {
596 if (!PQresetStart(obj->intern->conn)) {
597 throw_exce(EX_IO TSRMLS_CC, "Failed to start connection reset (%s)", PHP_PQerrorMessage(obj->intern->conn));
598 } else {
599 obj->intern->poller = (int (*)(PGconn*)) PQresetPoll;
600 }
601
602 php_pqconn_notify_listeners(obj TSRMLS_CC);
603 }
604 }
605 }
606
607 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)
608 {
609 HashTable ht, *existing_listeners;
610
611 php_pq_callback_addref(listener);
612
613 if (SUCCESS == zend_hash_find(&obj->intern->listeners, channel_str, channel_len + 1, (void *) &existing_listeners)) {
614 zend_hash_next_index_insert(existing_listeners, (void *) listener, sizeof(*listener), NULL);
615 } else {
616 zend_hash_init(&ht, 1, NULL, (dtor_func_t) php_pq_callback_dtor, 0);
617 zend_hash_next_index_insert(&ht, (void *) listener, sizeof(*listener), NULL);
618 zend_hash_add(&obj->intern->listeners, channel_str, channel_len + 1, (void *) &ht, sizeof(HashTable), NULL);
619 }
620 }
621
622 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen, 0, 0, 0)
623 ZEND_ARG_INFO(0, channel)
624 ZEND_ARG_INFO(0, callable)
625 ZEND_END_ARG_INFO();
626 static PHP_METHOD(pqconn, listen) {
627 zend_error_handling zeh;
628 char *channel_str = NULL;
629 int channel_len = 0;
630 php_pq_callback_t listener = {{0}};
631 STATUS rv;
632
633 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
634 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sf", &channel_str, &channel_len, &listener.fci, &listener.fcc);
635 zend_restore_error_handling(&zeh TSRMLS_CC);
636
637 if (SUCCESS == rv) {
638 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
639
640 if (!obj->intern) {
641 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
642 } else {
643 char *quoted_channel = PQescapeIdentifier(obj->intern->conn, channel_str, channel_len);
644
645 if (!quoted_channel) {
646 throw_exce(EX_ESCAPE TSRMLS_CC, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj->intern->conn));
647 } else {
648 PGresult *res;
649 smart_str cmd = {0};
650
651 smart_str_appends(&cmd, "LISTEN ");
652 smart_str_appends(&cmd, quoted_channel);
653 smart_str_0(&cmd);
654
655 res = PQexec(obj->intern->conn, cmd.c);
656
657 smart_str_free(&cmd);
658 PQfreemem(quoted_channel);
659
660 if (!res) {
661 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to install listener (%s)", PHP_PQerrorMessage(obj->intern->conn));
662 } else {
663 if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
664 obj->intern->poller = PQconsumeInput;
665 php_pqconn_add_listener(obj, channel_str, channel_len, &listener TSRMLS_CC);
666 }
667 PHP_PQclear(res);
668 }
669
670 php_pqconn_notify_listeners(obj TSRMLS_CC);
671 }
672 }
673 }
674 }
675
676 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen_async, 0, 0, 0)
677 ZEND_ARG_INFO(0, channel)
678 ZEND_ARG_INFO(0, callable)
679 ZEND_END_ARG_INFO();
680 static PHP_METHOD(pqconn, listenAsync) {
681 zend_error_handling zeh;
682 char *channel_str = NULL;
683 int channel_len = 0;
684 php_pq_callback_t listener = {{0}};
685 STATUS rv;
686
687 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
688 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sf", &channel_str, &channel_len, &listener.fci, &listener.fcc);
689 zend_restore_error_handling(&zeh TSRMLS_CC);
690
691 if (SUCCESS == rv) {
692 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
693
694 if (!obj->intern) {
695 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
696 } else {
697 char *quoted_channel = PQescapeIdentifier(obj->intern->conn, channel_str, channel_len);
698
699 if (!quoted_channel) {
700 throw_exce(EX_ESCAPE TSRMLS_CC, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj->intern->conn));
701 } else {
702 smart_str cmd = {0};
703
704 smart_str_appends(&cmd, "LISTEN ");
705 smart_str_appends(&cmd, quoted_channel);
706 smart_str_0(&cmd);
707
708 if (!PQsendQuery(obj->intern->conn, cmd.c)) {
709 throw_exce(EX_IO TSRMLS_CC, "Failed to install listener (%s)", PHP_PQerrorMessage(obj->intern->conn));
710 } else {
711 obj->intern->poller = PQconsumeInput;
712 php_pqconn_add_listener(obj, channel_str, channel_len, &listener TSRMLS_CC);
713 }
714
715 smart_str_free(&cmd);
716 PQfreemem(quoted_channel);
717 php_pqconn_notify_listeners(obj TSRMLS_CC);
718 }
719 }
720 }
721 }
722
723 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify, 0, 0, 2)
724 ZEND_ARG_INFO(0, channel)
725 ZEND_ARG_INFO(0, message)
726 ZEND_END_ARG_INFO();
727 static PHP_METHOD(pqconn, notify) {
728 zend_error_handling zeh;
729 char *channel_str, *message_str;
730 int channel_len, message_len;
731 STATUS rv;
732
733 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
734 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &channel_str, &channel_len, &message_str, &message_len);
735 zend_restore_error_handling(&zeh TSRMLS_CC);
736
737 if (SUCCESS == rv) {
738 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
739
740 if (!obj->intern) {
741 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
742 } else {
743 PGresult *res;
744 char *params[2] = {channel_str, message_str};
745
746 res = PQexecParams(obj->intern->conn, "select pg_notify($1, $2)", 2, NULL, (const char *const*) params, NULL, NULL, 0);
747
748 if (!res) {
749 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj->intern->conn));
750 } else {
751 php_pqres_success(res TSRMLS_CC);
752 PHP_PQclear(res);
753 }
754
755 php_pqconn_notify_listeners(obj TSRMLS_CC);
756 }
757 }
758 }
759
760 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify_async, 0, 0, 2)
761 ZEND_ARG_INFO(0, channel)
762 ZEND_ARG_INFO(0, message)
763 ZEND_END_ARG_INFO();
764 static PHP_METHOD(pqconn, notifyAsync) {
765 zend_error_handling zeh;
766 char *channel_str, *message_str;
767 int channel_len, message_len;
768 STATUS rv;
769
770 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
771 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &channel_str, &channel_len, &message_str, &message_len);
772 zend_restore_error_handling(&zeh TSRMLS_CC);
773
774 if (SUCCESS == rv) {
775 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
776
777 if (!obj->intern) {
778 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
779 } else {
780 char *params[2] = {channel_str, message_str};
781
782 if (!PQsendQueryParams(obj->intern->conn, "select pg_notify($1, $2)", 2, NULL, (const char *const*) params, NULL, NULL, 0)) {
783 throw_exce(EX_IO TSRMLS_CC, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj->intern->conn));
784 } else {
785 obj->intern->poller = PQconsumeInput;
786 }
787
788 php_pqconn_notify_listeners(obj TSRMLS_CC);
789 }
790 }
791 }
792
793 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_poll, 0, 0, 0)
794 ZEND_END_ARG_INFO();
795 static PHP_METHOD(pqconn, poll) {
796 zend_error_handling zeh;
797 STATUS rv;
798
799 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
800 rv = zend_parse_parameters_none();
801 zend_restore_error_handling(&zeh TSRMLS_CC);
802
803 if (SUCCESS == rv) {
804 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
805
806 if (!obj->intern) {
807 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
808 } else if (!obj->intern->poller) {
809 throw_exce(EX_RUNTIME TSRMLS_CC, "No asynchronous operation active");
810 } else {
811 if (obj->intern->poller == PQconsumeInput) {
812 RETVAL_LONG(obj->intern->poller(obj->intern->conn) * PGRES_POLLING_OK);
813 } else {
814 RETVAL_LONG(obj->intern->poller(obj->intern->conn));
815 }
816 php_pqconn_notify_listeners(obj TSRMLS_CC);
817 }
818 }
819 }
820
821 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec, 0, 0, 1)
822 ZEND_ARG_INFO(0, query)
823 ZEND_END_ARG_INFO();
824 static PHP_METHOD(pqconn, exec) {
825 zend_error_handling zeh;
826 char *query_str;
827 int query_len;
828 STATUS rv;
829
830 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
831 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &query_str, &query_len);
832 zend_restore_error_handling(&zeh TSRMLS_CC);
833
834 if (SUCCESS == rv) {
835 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
836
837 if (!obj->intern) {
838 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
839 } else {
840 PGresult *res = PQexec(obj->intern->conn, query_str);
841
842 if (!res) {
843 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
844 } else if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
845 php_pq_object_to_zval_no_addref(PQresultInstanceData(res, php_pqconn_event), &return_value TSRMLS_CC);
846 } else {
847 PHP_PQclear(res);
848 }
849
850 php_pqconn_notify_listeners(obj TSRMLS_CC);
851 }
852 }
853 }
854
855 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_get_result, 0, 0, 0)
856 ZEND_END_ARG_INFO();
857 static PHP_METHOD(pqconn, getResult) {
858 zend_error_handling zeh;
859 STATUS rv;
860
861 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
862 rv = zend_parse_parameters_none();
863 zend_restore_error_handling(&zeh TSRMLS_CC);
864
865 if (SUCCESS == rv) {
866 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
867
868 if (!obj->intern) {
869 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
870 } else {
871 PGresult *res = PQgetResult(obj->intern->conn);
872
873 if (!res) {
874 RETVAL_NULL();
875 } else {
876 php_pq_object_to_zval_no_addref(PQresultInstanceData(res, php_pqconn_event), &return_value TSRMLS_CC);
877 }
878
879 php_pqconn_notify_listeners(obj TSRMLS_CC);
880 }
881 }
882 }
883
884 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_async, 0, 0, 1)
885 ZEND_ARG_INFO(0, query)
886 ZEND_ARG_INFO(0, callable)
887 ZEND_END_ARG_INFO();
888 static PHP_METHOD(pqconn, execAsync) {
889 zend_error_handling zeh;
890 php_pq_callback_t resolver = {{0}};
891 char *query_str;
892 int query_len;
893 STATUS rv;
894
895 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
896 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|f", &query_str, &query_len, &resolver.fci, &resolver.fcc);
897 zend_restore_error_handling(&zeh TSRMLS_CC);
898
899 if (SUCCESS == rv) {
900 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
901
902 if (!obj->intern) {
903 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
904 } else if (!PQsendQuery(obj->intern->conn, query_str)) {
905 throw_exce(EX_IO TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
906 } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
907 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
908 } else {
909 php_pq_callback_recurse(&obj->intern->onevent, &resolver TSRMLS_CC);
910 obj->intern->poller = PQconsumeInput;
911 php_pqconn_notify_listeners(obj TSRMLS_CC);
912 }
913 }
914 }
915
916 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params, 0, 0, 2)
917 ZEND_ARG_INFO(0, query)
918 ZEND_ARG_ARRAY_INFO(0, params, 0)
919 ZEND_ARG_ARRAY_INFO(0, types, 1)
920 ZEND_END_ARG_INFO();
921 static PHP_METHOD(pqconn, execParams) {
922 zend_error_handling zeh;
923 char *query_str;
924 int query_len;
925 zval *zparams;
926 zval *ztypes = NULL;
927 STATUS rv;
928
929 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
930 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|a/!", &query_str, &query_len, &zparams, &ztypes);
931 zend_restore_error_handling(&zeh TSRMLS_CC);
932
933 if (SUCCESS == rv) {
934 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
935
936 if (!obj->intern) {
937 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
938 } else {
939 PGresult *res;
940 php_pq_params_t *params;
941
942 params = php_pq_params_init(&obj->intern->converters, ztypes ? Z_ARRVAL_P(ztypes) : NULL, Z_ARRVAL_P(zparams) TSRMLS_CC);
943 res = PQexecParams(obj->intern->conn, query_str, params->param.count, params->type.oids, (const char *const*) params->param.strings, NULL, NULL, 0);
944 php_pq_params_free(&params);
945
946 if (!res) {
947 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
948 } else {
949 if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
950 php_pq_object_to_zval_no_addref(PQresultInstanceData(res, php_pqconn_event), &return_value TSRMLS_CC);
951 } else {
952 PHP_PQclear(res);
953 }
954
955 php_pqconn_notify_listeners(obj TSRMLS_CC);
956 }
957 }
958 }
959 }
960
961 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async, 0, 0, 2)
962 ZEND_ARG_INFO(0, query)
963 ZEND_ARG_ARRAY_INFO(0, params, 0)
964 ZEND_ARG_ARRAY_INFO(0, types, 1)
965 ZEND_ARG_INFO(0, callable)
966 ZEND_END_ARG_INFO();
967 static PHP_METHOD(pqconn, execParamsAsync) {
968 zend_error_handling zeh;
969 php_pq_callback_t resolver = {{0}};
970 char *query_str;
971 int query_len;
972 zval *zparams;
973 zval *ztypes = NULL;
974 STATUS rv;
975
976 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
977 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|a/!f", &query_str, &query_len, &zparams, &ztypes, &resolver.fci, &resolver.fcc);
978 zend_restore_error_handling(&zeh TSRMLS_CC);
979
980 if (SUCCESS == rv) {
981 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
982
983 if (!obj->intern) {
984 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
985 } else {
986 int rc;
987 php_pq_params_t *params;
988
989 params = php_pq_params_init(&obj->intern->converters, ztypes ? Z_ARRVAL_P(ztypes) : NULL, Z_ARRVAL_P(zparams) TSRMLS_CC);
990 rc = PQsendQueryParams(obj->intern->conn, query_str, params->param.count, params->type.oids, (const char *const*) params->param.strings, NULL, NULL, 0);
991 php_pq_params_free(&params);
992
993 if (!rc) {
994 throw_exce(EX_IO TSRMLS_CC, "Failed to execute query (%s)", PHP_PQerrorMessage(obj->intern->conn));
995 } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
996 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
997 } else {
998 php_pq_callback_recurse(&obj->intern->onevent, &resolver TSRMLS_CC);
999 obj->intern->poller = PQconsumeInput;
1000 php_pqconn_notify_listeners(obj TSRMLS_CC);
1001 }
1002 }
1003 }
1004 zend_restore_error_handling(&zeh TSRMLS_CC);
1005 }
1006
1007 STATUS php_pqconn_prepare(zval *object, php_pqconn_object_t *obj, const char *name, const char *query, php_pq_params_t *params TSRMLS_DC)
1008 {
1009 PGresult *res;
1010 STATUS rv;
1011
1012 if (!obj) {
1013 obj = zend_object_store_get_object(object TSRMLS_CC);
1014 }
1015
1016 res = PQprepare(obj->intern->conn, name, query, params->type.count, params->type.oids);
1017
1018 if (!res) {
1019 rv = FAILURE;
1020 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj->intern->conn));
1021 } else {
1022 rv = php_pqres_success(res TSRMLS_CC);
1023 PHP_PQclear(res);
1024 php_pqconn_notify_listeners(obj TSRMLS_CC);
1025 }
1026
1027 return rv;
1028 }
1029
1030 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare, 0, 0, 2)
1031 ZEND_ARG_INFO(0, name)
1032 ZEND_ARG_INFO(0, query)
1033 ZEND_ARG_ARRAY_INFO(0, types, 1)
1034 ZEND_END_ARG_INFO();
1035 static PHP_METHOD(pqconn, prepare) {
1036 zend_error_handling zeh;
1037 zval *ztypes = NULL;
1038 char *name_str, *query_str;
1039 int name_len, *query_len;
1040 STATUS rv;
1041
1042 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1043 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!", &name_str, &name_len, &query_str, &query_len, &ztypes);
1044 zend_restore_error_handling(&zeh TSRMLS_CC);
1045
1046 if (SUCCESS == rv) {
1047 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1048
1049 if (!obj->intern) {
1050 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1051 } else {
1052 php_pq_params_t *params = php_pq_params_init(&obj->intern->converters, ztypes ? Z_ARRVAL_P(ztypes) : NULL, NULL TSRMLS_CC);
1053
1054 if (SUCCESS != php_pqconn_prepare(getThis(), obj, name_str, query_str, params TSRMLS_CC)) {
1055 php_pq_params_free(&params);
1056 } else {
1057 php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
1058
1059 php_pq_object_addref(obj TSRMLS_CC);
1060 stm->conn = obj;
1061 stm->name = estrdup(name_str);
1062 stm->params = params;
1063 ZEND_INIT_SYMTABLE(&stm->bound);
1064
1065 return_value->type = IS_OBJECT;
1066 return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
1067 }
1068 }
1069 }
1070 }
1071
1072 STATUS php_pqconn_prepare_async(zval *object, php_pqconn_object_t *obj, const char *name, const char *query, php_pq_params_t *params TSRMLS_DC)
1073 {
1074 STATUS rv;
1075
1076 if (!obj) {
1077 obj = zend_object_store_get_object(object TSRMLS_CC);
1078 }
1079
1080 if (!PQsendPrepare(obj->intern->conn, name, query, params->type.count, params->type.oids)) {
1081 rv = FAILURE;
1082 throw_exce(EX_IO TSRMLS_CC, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj->intern->conn));
1083 } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
1084 rv = FAILURE;
1085 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
1086 } else {
1087 rv = SUCCESS;
1088 obj->intern->poller = PQconsumeInput;
1089 php_pqconn_notify_listeners(obj TSRMLS_CC);
1090 }
1091
1092 return rv;
1093 }
1094
1095 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare_async, 0, 0, 2)
1096 ZEND_ARG_INFO(0, name)
1097 ZEND_ARG_INFO(0, query)
1098 ZEND_ARG_ARRAY_INFO(0, types, 1)
1099 ZEND_END_ARG_INFO();
1100 static PHP_METHOD(pqconn, prepareAsync) {
1101 zend_error_handling zeh;
1102 zval *ztypes = NULL;
1103 char *name_str, *query_str;
1104 int name_len, *query_len;
1105 STATUS rv;
1106
1107 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1108 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!", &name_str, &name_len, &query_str, &query_len, &ztypes);
1109 zend_restore_error_handling(&zeh TSRMLS_CC);
1110
1111 if (SUCCESS == rv) {
1112 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1113
1114 if (!obj->intern) {
1115 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1116 } else {
1117 php_pq_params_t *params = php_pq_params_init(&obj->intern->converters, ztypes ? Z_ARRVAL_P(ztypes) : NULL, NULL TSRMLS_CC);
1118
1119 if (SUCCESS != php_pqconn_prepare_async(getThis(), obj, name_str, query_str, params TSRMLS_CC)) {
1120 php_pq_params_free(&params);
1121 } else {
1122 php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
1123
1124 php_pq_object_addref(obj TSRMLS_CC);
1125 stm->conn = obj;
1126 stm->name = estrdup(name_str);
1127 stm->params = params;
1128 ZEND_INIT_SYMTABLE(&stm->bound);
1129
1130 return_value->type = IS_OBJECT;
1131 return_value->value.obj = php_pqstm_create_object_ex(php_pqstm_class_entry, stm, NULL TSRMLS_CC);
1132 }
1133 }
1134 }
1135 }
1136
1137 static inline char *declare_str(const char *name_str, size_t name_len, unsigned flags, const char *query_str, size_t query_len)
1138 {
1139 size_t decl_len = name_len + query_len + sizeof("DECLARE BINARY INSENSITIVE NO SCROLL CURSOR WITHOUT HOLD FOR ");
1140 char *decl_str;
1141
1142 decl_str = emalloc(decl_len);
1143 decl_len = slprintf(decl_str, decl_len, "DECLARE %s %s %s %s CURSOR %s FOR %s",
1144 name_str,
1145 (flags & PHP_PQ_DECLARE_BINARY) ? "BINARY" : "",
1146 (flags & PHP_PQ_DECLARE_INSENSITIVE) ? "INSENSITIVE" : "",
1147 (flags & PHP_PQ_DECLARE_NO_SCROLL) ? "NO SCROLL" :
1148 (flags & PHP_PQ_DECLARE_SCROLL) ? "SCROLL" : "",
1149 (flags & PHP_PQ_DECLARE_WITH_HOLD) ? "WITH HOLD" : "",
1150 query_str
1151 );
1152 return decl_str;
1153 }
1154
1155 STATUS php_pqconn_declare(zval *object, php_pqconn_object_t *obj, const char *decl TSRMLS_DC)
1156 {
1157 PGresult *res;
1158 STATUS rv;
1159
1160 if (!obj) {
1161 obj = zend_object_store_get_object(object TSRMLS_CC);
1162 }
1163
1164 res = PQexec(obj->intern->conn, decl);
1165
1166 if (!res) {
1167 rv = FAILURE;
1168 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj->intern->conn));
1169 } else {
1170 rv = php_pqres_success(res TSRMLS_CC);
1171 PHP_PQclear(res);
1172 php_pqconn_notify_listeners(obj TSRMLS_CC);
1173 }
1174
1175 return rv;
1176 }
1177
1178 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare, 0, 0, 3)
1179 ZEND_ARG_INFO(0, name)
1180 ZEND_ARG_INFO(0, flags)
1181 ZEND_ARG_INFO(0, query)
1182 ZEND_END_ARG_INFO();
1183 static PHP_METHOD(pqconn, declare) {
1184 zend_error_handling zeh;
1185 char *name_str, *query_str;
1186 int name_len, query_len;
1187 long flags;
1188 STATUS rv;
1189
1190 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1191 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sls", &name_str, &name_len, &flags, &query_str, &query_len);
1192 zend_restore_error_handling(&zeh TSRMLS_CC);
1193
1194 if (SUCCESS == rv) {
1195 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1196
1197 if (!obj->intern) {
1198 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1199 } else {
1200 char *decl = declare_str(name_str, name_len, flags, query_str, query_len);
1201
1202 if (SUCCESS != php_pqconn_declare(getThis(), obj, decl TSRMLS_CC)) {
1203 efree(decl);
1204 } else {
1205 php_pqcur_t *cur = ecalloc(1, sizeof(*cur));
1206
1207 php_pq_object_addref(obj TSRMLS_CC);
1208 cur->conn = obj;
1209 cur->open = 1;
1210 cur->name = estrdup(name_str);
1211 cur->decl = decl;
1212
1213 return_value->type = IS_OBJECT;
1214 return_value->value.obj = php_pqcur_create_object_ex(php_pqcur_class_entry, cur, NULL TSRMLS_CC);
1215 }
1216 }
1217 }
1218 }
1219
1220 STATUS php_pqconn_declare_async(zval *object, php_pqconn_object_t *obj, const char *decl TSRMLS_DC)
1221 {
1222 STATUS rv;
1223
1224 if (!obj) {
1225 obj = zend_object_store_get_object(object TSRMLS_CC);
1226 }
1227
1228 if (!PQsendQuery(obj->intern->conn, decl)) {
1229 rv = FAILURE;
1230 throw_exce(EX_IO TSRMLS_CC, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj->intern->conn));
1231 } else if (obj->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn)) {
1232 rv = FAILURE;
1233 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn));
1234 } else {
1235 rv = SUCCESS;
1236 obj->intern->poller = PQconsumeInput;
1237 php_pqconn_notify_listeners(obj TSRMLS_CC);
1238 }
1239
1240 return rv;
1241 }
1242
1243 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare_async, 0, 0, 2)
1244 ZEND_ARG_INFO(0, name)
1245 ZEND_ARG_INFO(0, flags)
1246 ZEND_ARG_INFO(0, query)
1247 ZEND_END_ARG_INFO();
1248 static PHP_METHOD(pqconn, declareAsync) {
1249 zend_error_handling zeh;
1250 char *name_str, *query_str;
1251 int name_len, query_len;
1252 long flags;
1253 STATUS rv;
1254
1255 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1256 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sls", &name_str, &name_len, &flags, &query_str, &query_len);
1257 zend_restore_error_handling(&zeh TSRMLS_CC);
1258
1259 if (SUCCESS == rv) {
1260 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1261
1262 if (!obj->intern) {
1263 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1264 } else {
1265 char *decl = declare_str(name_str, name_len, flags, query_str, query_len);
1266
1267 if (SUCCESS != php_pqconn_declare_async(getThis(), obj, decl TSRMLS_CC)) {
1268 efree(decl);
1269 } else {
1270 php_pqcur_t *cur = ecalloc(1, sizeof(*cur));
1271
1272 php_pq_object_addref(obj TSRMLS_CC);
1273 cur->conn = obj;
1274 cur->open = 1;
1275 cur->name = estrdup(name_str);
1276 cur->decl = decl;
1277
1278 return_value->type = IS_OBJECT;
1279 return_value->value.obj = php_pqcur_create_object_ex(php_pqcur_class_entry, cur, NULL TSRMLS_CC);
1280 }
1281 }
1282 }
1283 }
1284
1285 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote, 0, 0, 1)
1286 ZEND_ARG_INFO(0, string)
1287 ZEND_END_ARG_INFO();
1288 static PHP_METHOD(pqconn, quote) {
1289 char *str;
1290 int len;
1291
1292 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
1293 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1294
1295 if (!obj->intern) {
1296 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1297 } else {
1298 char *quoted = PQescapeLiteral(obj->intern->conn, str, len);
1299
1300 if (!quoted) {
1301 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to quote string (%s)", PHP_PQerrorMessage(obj->intern->conn));
1302 RETVAL_FALSE;
1303 } else {
1304 RETVAL_STRING(quoted, 1);
1305 PQfreemem(quoted);
1306 }
1307 }
1308 }
1309 }
1310
1311 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote_name, 0, 0, 1)
1312 ZEND_ARG_INFO(0, type)
1313 ZEND_END_ARG_INFO();
1314 static PHP_METHOD(pqconn, quoteName) {
1315 char *str;
1316 int len;
1317
1318 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
1319 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1320
1321 if (!obj->intern) {
1322 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1323 } else {
1324 char *quoted = PQescapeIdentifier(obj->intern->conn, str, len);
1325
1326 if (!quoted) {
1327 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to quote name (%s)", PHP_PQerrorMessage(obj->intern->conn));
1328 RETVAL_FALSE;
1329 } else {
1330 RETVAL_STRING(quoted, 1);
1331 PQfreemem(quoted);
1332 }
1333 }
1334 }
1335 }
1336
1337 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_escape_bytea, 0, 0, 1)
1338 ZEND_ARG_INFO(0, bytea)
1339 ZEND_END_ARG_INFO();
1340 static PHP_METHOD(pqconn, escapeBytea) {
1341 char *str;
1342 int len;
1343
1344 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
1345 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1346
1347 if (!obj->intern) {
1348 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1349 } else {
1350 size_t escaped_len;
1351 char *escaped_str = (char *) PQescapeByteaConn(obj->intern->conn, (unsigned char *) str, len, &escaped_len);
1352
1353 if (!escaped_str) {
1354 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to escape bytea (%s)", PHP_PQerrorMessage(obj->intern->conn));
1355 RETVAL_FALSE;
1356 } else {
1357 RETVAL_STRINGL(escaped_str, escaped_len - 1, 1);
1358 PQfreemem(escaped_str);
1359 }
1360 }
1361 }
1362 }
1363
1364 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unescape_bytea, 0, 0, 1)
1365 ZEND_ARG_INFO(0, bytea)
1366 ZEND_END_ARG_INFO();
1367 static PHP_METHOD(pqconn, unescapeBytea) {
1368 char *str;
1369 int len;
1370
1371 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
1372 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1373
1374 if (!obj->intern) {
1375 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1376 } else {
1377 size_t unescaped_len;
1378 char *unescaped_str = (char *) PQunescapeBytea((unsigned char *)str, &unescaped_len);
1379
1380 if (!unescaped_str) {
1381 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to unescape bytea (%s)", PHP_PQerrorMessage(obj->intern->conn));
1382 RETVAL_FALSE;
1383 } else {
1384 RETVAL_STRINGL(unescaped_str, unescaped_len, 1);
1385 PQfreemem(unescaped_str);
1386 }
1387 }
1388 }
1389 }
1390
1391 STATUS php_pqconn_start_transaction(zval *zconn, php_pqconn_object_t *conn_obj, long isolation, zend_bool readonly, zend_bool deferrable TSRMLS_DC)
1392 {
1393 STATUS rv = FAILURE;
1394
1395 if (!conn_obj) {
1396 conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
1397 }
1398
1399 if (!conn_obj->intern) {
1400 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1401 } else {
1402 PGresult *res;
1403 smart_str cmd = {0};
1404 const char *il = isolation_level(&isolation);
1405
1406 smart_str_appends(&cmd, "START TRANSACTION ISOLATION LEVEL ");
1407 smart_str_appends(&cmd, il);
1408 smart_str_appends(&cmd, ", READ ");
1409 smart_str_appends(&cmd, readonly ? "ONLY" : "WRITE");
1410 smart_str_appends(&cmd, ",");
1411 smart_str_appends(&cmd, deferrable ? "" : " NOT");
1412 smart_str_appends(&cmd, " DEFERRABLE");
1413 smart_str_0(&cmd);
1414
1415 res = PQexec(conn_obj->intern->conn, cmd.c);
1416
1417 if (!res) {
1418 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj->intern->conn));
1419 } else {
1420 rv = php_pqres_success(res TSRMLS_CC);
1421 PHP_PQclear(res);
1422 php_pqconn_notify_listeners(conn_obj TSRMLS_CC);
1423 }
1424
1425 smart_str_free(&cmd);
1426 }
1427
1428 return rv;
1429 }
1430
1431 STATUS php_pqconn_start_transaction_async(zval *zconn, php_pqconn_object_t *conn_obj, long isolation, zend_bool readonly, zend_bool deferrable TSRMLS_DC)
1432 {
1433 STATUS rv = FAILURE;
1434
1435 if (!conn_obj) {
1436 conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
1437 }
1438
1439 if (!conn_obj->intern) {
1440 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1441 } else {
1442 smart_str cmd = {0};
1443 const char *il = isolation_level(&isolation);
1444
1445 smart_str_appends(&cmd, "START TRANSACTION ISOLATION LEVEL ");
1446 smart_str_appends(&cmd, il);
1447 smart_str_appends(&cmd, ", READ ");
1448 smart_str_appends(&cmd, readonly ? "ONLY" : "WRITE");
1449 smart_str_appends(&cmd, ",");
1450 smart_str_appends(&cmd, deferrable ? "" : "NOT ");
1451 smart_str_appends(&cmd, " DEFERRABLE");
1452 smart_str_0(&cmd);
1453
1454 if (!PQsendQuery(conn_obj->intern->conn, cmd.c)) {
1455 throw_exce(EX_IO TSRMLS_CC, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj->intern->conn));
1456 } else {
1457 rv = SUCCESS;
1458 conn_obj->intern->poller = PQconsumeInput;
1459 php_pqconn_notify_listeners(conn_obj TSRMLS_CC);
1460 }
1461
1462 smart_str_free(&cmd);
1463 }
1464
1465 return rv;
1466 }
1467
1468 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction, 0, 0, 0)
1469 ZEND_ARG_INFO(0, isolation)
1470 ZEND_ARG_INFO(0, readonly)
1471 ZEND_ARG_INFO(0, deferrable)
1472 ZEND_END_ARG_INFO();
1473 static PHP_METHOD(pqconn, startTransaction) {
1474 zend_error_handling zeh;
1475 long isolation = PHP_PQTXN_READ_COMMITTED;
1476 zend_bool readonly = 0, deferrable = 0;
1477 STATUS rv;
1478
1479 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1480 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable);
1481 zend_restore_error_handling(&zeh TSRMLS_CC);
1482
1483 if (SUCCESS == rv) {
1484 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1485
1486 rv = php_pqconn_start_transaction(getThis(), obj, isolation, readonly, deferrable TSRMLS_CC);
1487
1488 if (SUCCESS == rv) {
1489 php_pqtxn_t *txn = ecalloc(1, sizeof(*txn));
1490
1491 php_pq_object_addref(obj TSRMLS_CC);
1492 txn->conn = obj;
1493 txn->open = 1;
1494 txn->isolation = isolation;
1495 txn->readonly = readonly;
1496 txn->deferrable = deferrable;
1497
1498 return_value->type = IS_OBJECT;
1499 return_value->value.obj = php_pqtxn_create_object_ex(php_pqtxn_class_entry, txn, NULL TSRMLS_CC);
1500 }
1501 }
1502 }
1503
1504 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction_async, 0, 0, 0)
1505 ZEND_ARG_INFO(0, isolation)
1506 ZEND_ARG_INFO(0, readonly)
1507 ZEND_ARG_INFO(0, deferrable)
1508 ZEND_END_ARG_INFO();
1509 static PHP_METHOD(pqconn, startTransactionAsync) {
1510 zend_error_handling zeh;
1511 long isolation = PHP_PQTXN_READ_COMMITTED;
1512 zend_bool readonly = 0, deferrable = 0;
1513 STATUS rv;
1514
1515 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1516 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lbb", &isolation, &readonly, &deferrable);
1517 zend_restore_error_handling(&zeh TSRMLS_CC);
1518 if (SUCCESS == rv) {
1519 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1520
1521 rv = php_pqconn_start_transaction_async(getThis(), obj, isolation, readonly, deferrable TSRMLS_CC);
1522
1523 if (SUCCESS == rv) {
1524 php_pqtxn_t *txn = ecalloc(1, sizeof(*txn));
1525
1526 php_pq_object_addref(obj TSRMLS_CC);
1527 txn->conn = obj;
1528 txn->isolation = isolation;
1529 txn->readonly = readonly;
1530 txn->deferrable = deferrable;
1531
1532 return_value->type = IS_OBJECT;
1533 return_value->value.obj = php_pqtxn_create_object_ex(php_pqtxn_class_entry, txn, NULL TSRMLS_CC);
1534 }
1535 }
1536 }
1537
1538 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_trace, 0, 0, 0)
1539 ZEND_ARG_INFO(0, stdio_stream)
1540 ZEND_END_ARG_INFO();
1541 static PHP_METHOD(pqconn, trace) {
1542 zval *zstream = NULL;
1543
1544 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r!", &zstream)) {
1545 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1546
1547 if (!obj->intern) {
1548 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1549 } else {
1550 if (!zstream) {
1551 PQuntrace(obj->intern->conn);
1552 RETVAL_TRUE;
1553 } else {
1554 FILE *fp;
1555 php_stream *stream = NULL;
1556
1557 php_stream_from_zval(stream, &zstream);
1558
1559 if (SUCCESS != php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void *) &fp, REPORT_ERRORS)) {
1560 RETVAL_FALSE;
1561 } else {
1562 stream->flags |= PHP_STREAM_FLAG_NO_CLOSE;
1563 PQtrace(obj->intern->conn, fp);
1564 RETVAL_TRUE;
1565 }
1566 }
1567 }
1568 }
1569 }
1570
1571 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_on, 0, 0, 2)
1572 ZEND_ARG_INFO(0, type)
1573 ZEND_ARG_INFO(0, callable)
1574 ZEND_END_ARG_INFO();
1575 static PHP_METHOD(pqconn, on) {
1576 zend_error_handling zeh;
1577 char *type_str;
1578 int type_len;
1579 php_pq_callback_t cb = {{0}};
1580 STATUS rv;
1581
1582 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1583 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sf", &type_str, &type_len, &cb.fci, &cb.fcc);
1584 zend_restore_error_handling(&zeh TSRMLS_CC);
1585
1586 if (SUCCESS == rv) {
1587 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1588
1589 if (!obj->intern) {
1590 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1591 } else {
1592 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1593
1594 RETVAL_LONG(php_pqconn_add_eventhandler(obj, type_str, type_len, &cb TSRMLS_CC));
1595 }
1596 }
1597 }
1598
1599 static int apply_set_converter(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
1600 {
1601 zval *tmp, **zoid = p, **zcnv = va_arg(argv, zval **);
1602 HashTable *converters = va_arg(argv, HashTable *);
1603
1604 tmp = *zoid;
1605 convert_to_long_ex(&tmp);
1606 Z_ADDREF_PP(zcnv);
1607 zend_hash_index_update(converters, Z_LVAL_P(tmp), zcnv, sizeof(zval *), NULL);
1608 if (tmp != *zoid) {
1609 zval_ptr_dtor(&tmp);
1610 }
1611
1612 return ZEND_HASH_APPLY_KEEP;
1613 }
1614
1615 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_set_converter, 0, 0, 1)
1616 ZEND_ARG_OBJ_INFO(0, converter, pq\\ConverterInterface, 0)
1617 ZEND_END_ARG_INFO();
1618 static PHP_METHOD(pqconn, setConverter) {
1619 STATUS rv;
1620 zend_error_handling zeh;
1621 zval *zcnv;
1622
1623 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
1624 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zcnv, php_pqconv_class_entry);
1625 zend_restore_error_handling(&zeh TSRMLS_CC);
1626
1627 if (SUCCESS == rv) {
1628 php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1629
1630 if (!obj->intern) {
1631 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
1632 } else {
1633 zval *tmp, *zoids = NULL;
1634
1635 zend_call_method_with_0_params(&zcnv, NULL, NULL, "converttypes", &zoids);
1636 tmp = zoids;
1637 convert_to_array_ex(&zoids);
1638 zend_hash_apply_with_arguments(Z_ARRVAL_P(zoids) TSRMLS_CC, apply_set_converter, 2, &zcnv, &obj->intern->converters);
1639 if (tmp != zoids) {
1640 zval_ptr_dtor(&tmp);
1641 }
1642 zval_ptr_dtor(&zoids);
1643 }
1644 }
1645 }
1646
1647 static zend_function_entry php_pqconn_methods[] = {
1648 PHP_ME(pqconn, __construct, ai_pqconn_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
1649 PHP_ME(pqconn, reset, ai_pqconn_reset, ZEND_ACC_PUBLIC)
1650 PHP_ME(pqconn, resetAsync, ai_pqconn_reset_async, ZEND_ACC_PUBLIC)
1651 PHP_ME(pqconn, poll, ai_pqconn_poll, ZEND_ACC_PUBLIC)
1652 PHP_ME(pqconn, exec, ai_pqconn_exec, ZEND_ACC_PUBLIC)
1653 PHP_ME(pqconn, execAsync, ai_pqconn_exec_async, ZEND_ACC_PUBLIC)
1654 PHP_ME(pqconn, execParams, ai_pqconn_exec_params, ZEND_ACC_PUBLIC)
1655 PHP_ME(pqconn, execParamsAsync, ai_pqconn_exec_params_async, ZEND_ACC_PUBLIC)
1656 PHP_ME(pqconn, prepare, ai_pqconn_prepare, ZEND_ACC_PUBLIC)
1657 PHP_ME(pqconn, prepareAsync, ai_pqconn_prepare_async, ZEND_ACC_PUBLIC)
1658 PHP_ME(pqconn, declare, ai_pqconn_declare, ZEND_ACC_PUBLIC)
1659 PHP_ME(pqconn, declareAsync, ai_pqconn_declare_async, ZEND_ACC_PUBLIC)
1660 PHP_ME(pqconn, listen, ai_pqconn_listen, ZEND_ACC_PUBLIC)
1661 PHP_ME(pqconn, listenAsync, ai_pqconn_listen_async, ZEND_ACC_PUBLIC)
1662 PHP_ME(pqconn, notify, ai_pqconn_notify, ZEND_ACC_PUBLIC)
1663 PHP_ME(pqconn, notifyAsync, ai_pqconn_notify_async, ZEND_ACC_PUBLIC)
1664 PHP_ME(pqconn, getResult, ai_pqconn_get_result, ZEND_ACC_PUBLIC)
1665 PHP_ME(pqconn, quote, ai_pqconn_quote, ZEND_ACC_PUBLIC)
1666 PHP_ME(pqconn, quoteName, ai_pqconn_quote_name, ZEND_ACC_PUBLIC)
1667 PHP_ME(pqconn, escapeBytea, ai_pqconn_escape_bytea, ZEND_ACC_PUBLIC)
1668 PHP_ME(pqconn, unescapeBytea, ai_pqconn_unescape_bytea, ZEND_ACC_PUBLIC)
1669 PHP_ME(pqconn, startTransaction, ai_pqconn_start_transaction, ZEND_ACC_PUBLIC)
1670 PHP_ME(pqconn, startTransactionAsync, ai_pqconn_start_transaction_async, ZEND_ACC_PUBLIC)
1671 PHP_ME(pqconn, trace, ai_pqconn_trace, ZEND_ACC_PUBLIC)
1672 PHP_ME(pqconn, on, ai_pqconn_on, ZEND_ACC_PUBLIC)
1673 PHP_ME(pqconn, setConverter, ai_pqconn_set_converter, ZEND_ACC_PUBLIC)
1674 {0}
1675 };
1676
1677 PHP_MSHUTDOWN_FUNCTION(pqconn)
1678 {
1679 zend_hash_destroy(&php_pqconn_object_prophandlers);
1680 return SUCCESS;
1681 }
1682
1683 PHP_MINIT_FUNCTION(pqconn)
1684 {
1685 zend_class_entry ce = {0};
1686 php_pq_object_prophandler_t ph = {0};
1687
1688 INIT_NS_CLASS_ENTRY(ce, "pq", "Connection", php_pqconn_methods);
1689 php_pqconn_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
1690 php_pqconn_class_entry->create_object = php_pqconn_create_object;
1691
1692 memcpy(&php_pqconn_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1693 php_pqconn_object_handlers.read_property = php_pq_object_read_prop;
1694 php_pqconn_object_handlers.write_property = php_pq_object_write_prop;
1695 php_pqconn_object_handlers.clone_obj = NULL;
1696 php_pqconn_object_handlers.get_property_ptr_ptr = NULL;
1697 php_pqconn_object_handlers.get_gc = NULL;
1698 php_pqconn_object_handlers.get_properties = php_pq_object_properties;
1699 php_pqconn_object_handlers.get_debug_info = php_pq_object_debug_info;
1700
1701 zend_hash_init(&php_pqconn_object_prophandlers, 14, NULL, NULL, 1);
1702
1703 zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("status"), CONNECTION_BAD, ZEND_ACC_PUBLIC TSRMLS_CC);
1704 ph.read = php_pqconn_object_read_status;
1705 zend_hash_add(&php_pqconn_object_prophandlers, "status", sizeof("status"), (void *) &ph, sizeof(ph), NULL);
1706
1707 zend_declare_property_long(php_pqconn_class_entry, ZEND_STRL("transactionStatus"), PQTRANS_UNKNOWN, ZEND_ACC_PUBLIC TSRMLS_CC);
1708 ph.read = php_pqconn_object_read_transaction_status;
1709 zend_hash_add(&php_pqconn_object_prophandlers, "transactionStatus", sizeof("transactionStatus"), (void *) &ph, sizeof(ph), NULL);
1710
1711 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("socket"), ZEND_ACC_PUBLIC TSRMLS_CC);
1712 ph.read = NULL; /* forward to std prophandler */
1713 zend_hash_add(&php_pqconn_object_prophandlers, "socket", sizeof("socket"), (void *) &ph, sizeof(ph), NULL);
1714
1715 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("errorMessage"), ZEND_ACC_PUBLIC TSRMLS_CC);
1716 ph.read = php_pqconn_object_read_error_message;
1717 zend_hash_add(&php_pqconn_object_prophandlers, "errorMessage", sizeof("errorMessage"), (void *) &ph, sizeof(ph), NULL);
1718
1719 zend_declare_property_bool(php_pqconn_class_entry, ZEND_STRL("busy"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
1720 ph.read = php_pqconn_object_read_busy;
1721 zend_hash_add(&php_pqconn_object_prophandlers, "busy", sizeof("busy"), (void *) &ph, sizeof(ph), NULL);
1722
1723 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("encoding"), ZEND_ACC_PUBLIC TSRMLS_CC);
1724 ph.read = php_pqconn_object_read_encoding;
1725 ph.write = php_pqconn_object_write_encoding;
1726 zend_hash_add(&php_pqconn_object_prophandlers, "encoding", sizeof("encoding"), (void *) &ph, sizeof(ph), NULL);
1727 ph.write = NULL;
1728
1729 zend_declare_property_bool(php_pqconn_class_entry, ZEND_STRL("unbuffered"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
1730 ph.read = php_pqconn_object_read_unbuffered;
1731 ph.write = php_pqconn_object_write_unbuffered;
1732 zend_hash_add(&php_pqconn_object_prophandlers, "unbuffered", sizeof("unbuffered"), (void *) &ph, sizeof(ph), NULL);
1733 ph.write = NULL;
1734
1735 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("db"), ZEND_ACC_PUBLIC TSRMLS_CC);
1736 ph.read = php_pqconn_object_read_db;
1737 zend_hash_add(&php_pqconn_object_prophandlers, "db", sizeof("db"), (void *) &ph, sizeof(ph), NULL);
1738
1739 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("user"), ZEND_ACC_PUBLIC TSRMLS_CC);
1740 ph.read = php_pqconn_object_read_user;
1741 zend_hash_add(&php_pqconn_object_prophandlers, "user", sizeof("user"), (void *) &ph, sizeof(ph), NULL);
1742
1743 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("pass"), ZEND_ACC_PUBLIC TSRMLS_CC);
1744 ph.read = php_pqconn_object_read_pass;
1745 zend_hash_add(&php_pqconn_object_prophandlers, "pass", sizeof("pass"), (void *) &ph, sizeof(ph), NULL);
1746
1747 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("host"), ZEND_ACC_PUBLIC TSRMLS_CC);
1748 ph.read = php_pqconn_object_read_host;
1749 zend_hash_add(&php_pqconn_object_prophandlers, "host", sizeof("host"), (void *) &ph, sizeof(ph), NULL);
1750
1751 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("port"), ZEND_ACC_PUBLIC TSRMLS_CC);
1752 ph.read = php_pqconn_object_read_port;
1753 zend_hash_add(&php_pqconn_object_prophandlers, "port", sizeof("port"), (void *) &ph, sizeof(ph), NULL);
1754
1755 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("options"), ZEND_ACC_PUBLIC TSRMLS_CC);
1756 ph.read = php_pqconn_object_read_options;
1757 zend_hash_add(&php_pqconn_object_prophandlers, "options", sizeof("options"), (void *) &ph, sizeof(ph), NULL);
1758
1759 zend_declare_property_null(php_pqconn_class_entry, ZEND_STRL("eventHandlers"), ZEND_ACC_PUBLIC TSRMLS_CC);
1760 ph.read = php_pqconn_object_read_event_handlers;
1761 zend_hash_add(&php_pqconn_object_prophandlers, "eventHandlers", sizeof("eventHandlers"), (void *) &ph, sizeof(ph), NULL);
1762
1763 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("OK"), CONNECTION_OK TSRMLS_CC);
1764 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("BAD"), CONNECTION_BAD TSRMLS_CC);
1765 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("STARTED"), CONNECTION_STARTED TSRMLS_CC);
1766 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("MADE"), CONNECTION_MADE TSRMLS_CC);
1767 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("AWAITING_RESPONSE"), CONNECTION_AWAITING_RESPONSE TSRMLS_CC);
1768 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("AUTH_OK"), CONNECTION_AUTH_OK TSRMLS_CC);
1769 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("SSL_STARTUP"), CONNECTION_SSL_STARTUP TSRMLS_CC);
1770 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("SETENV"), CONNECTION_SETENV TSRMLS_CC);
1771
1772 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("TRANS_IDLE"), PQTRANS_IDLE TSRMLS_CC);
1773 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("TRANS_ACTIVE"), PQTRANS_ACTIVE TSRMLS_CC);
1774 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("TRANS_INTRANS"), PQTRANS_INTRANS TSRMLS_CC);
1775 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("TRANS_INERROR"), PQTRANS_INERROR TSRMLS_CC);
1776 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("TRANS_UNKNOWN"), PQTRANS_UNKNOWN TSRMLS_CC);
1777
1778 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_FAILED"), PGRES_POLLING_FAILED TSRMLS_CC);
1779 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_READING"), PGRES_POLLING_READING TSRMLS_CC);
1780 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_WRITING"), PGRES_POLLING_WRITING TSRMLS_CC);
1781 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("POLLING_OK"), PGRES_POLLING_OK TSRMLS_CC);
1782
1783 zend_declare_class_constant_stringl(php_pqconn_class_entry, ZEND_STRL("EVENT_NOTICE"), ZEND_STRL("notice") TSRMLS_CC);
1784 zend_declare_class_constant_stringl(php_pqconn_class_entry, ZEND_STRL("EVENT_RESULT"), ZEND_STRL("result") TSRMLS_CC);
1785 zend_declare_class_constant_stringl(php_pqconn_class_entry, ZEND_STRL("EVENT_RESET"), ZEND_STRL("reset") TSRMLS_CC);
1786
1787 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("ASYNC"), 0x1 TSRMLS_CC);
1788 zend_declare_class_constant_long(php_pqconn_class_entry, ZEND_STRL("PERSISTENT"), 0x2 TSRMLS_CC);
1789
1790 return SUCCESS;
1791 }
1792
1793 /*
1794 * Local variables:
1795 * tab-width: 4
1796 * c-basic-offset: 4
1797 * End:
1798 * vim600: noet sw=4 ts=4 fdm=marker
1799 * vim<600: noet sw=4 ts=4
1800 */