2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
19 #define SMART_STR_PREALLOC 256
20 #include <ext/standard/php_smart_str.h>
22 #include <libpq-events.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"
36 zend_class_entry
*php_pqconn_class_entry
;
37 static zend_object_handlers php_pqconn_object_handlers
;
38 static HashTable php_pqconn_object_prophandlers
;
41 static void php_pqconn_del_eventhandler(php_pqconn_object_t *obj, const char *type_str, size_t type_len, ulong id TSRMLS_DC)
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);
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
)
56 if (SUCCESS
!= zend_hash_find(&obj
->intern
->eventhandlers
, type_str
, type_len
+ 1, (void *) &evhs
)) {
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
);
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
);
70 static void php_pqconn_object_free(void *o TSRMLS_DC
)
72 php_pqconn_object_t
*obj
= o
;
74 fprintf(stderr
, "FREE conn(#%d) %p\n", obj
->zv
.handle
, obj
);
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
);
86 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
91 zend_object_value
php_pqconn_create_object_ex(zend_class_entry
*ce
, php_pqconn_t
*intern
, php_pqconn_object_t
**ptr TSRMLS_DC
)
93 php_pqconn_object_t
*o
;
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
;
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
;
114 static zend_object_value
php_pqconn_create_object(zend_class_entry
*class_type TSRMLS_DC
)
116 return php_pqconn_create_object_ex(class_type
, NULL
, NULL TSRMLS_CC
);
119 static void php_pqconn_object_read_status(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
121 php_pqconn_object_t
*obj
= o
;
123 RETVAL_LONG(PQstatus(obj
->intern
->conn
));
126 static void php_pqconn_object_read_transaction_status(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
128 php_pqconn_object_t
*obj
= o
;
130 RETVAL_LONG(PQtransactionStatus(obj
->intern
->conn
));
133 static void php_pqconn_object_read_error_message(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
135 php_pqconn_object_t
*obj
= o
;
136 char *error
= PHP_PQerrorMessage(obj
->intern
->conn
);
139 RETVAL_STRING(error
, 1);
145 static int apply_notify_listener(void *p
, void *arg TSRMLS_DC
)
147 php_pq_callback_t
*listener
= p
;
149 zval
*zpid
, *zchannel
, *zmessage
;
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);
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
);
161 zval_ptr_dtor(&zchannel
);
162 zval_ptr_dtor(&zmessage
);
163 zval_ptr_dtor(&zpid
);
165 return ZEND_HASH_APPLY_KEEP
;
168 static int apply_notify_listeners(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
170 HashTable
*listeners
= p
;
171 PGnotify
*nfy
= va_arg(argv
, PGnotify
*);
173 if (0 == fnmatch(key
->arKey
, nfy
->relname
, 0)) {
174 zend_hash_apply_with_argument(listeners
, apply_notify_listener
, nfy TSRMLS_CC
);
177 return ZEND_HASH_APPLY_KEEP
;
180 void php_pqconn_notify_listeners(php_pqconn_object_t
*obj TSRMLS_DC
)
184 while ((nfy
= PQnotifies(obj
->intern
->conn
))) {
185 zend_hash_apply_with_arguments(&obj
->intern
->listeners TSRMLS_CC
, apply_notify_listeners
, 1, nfy
);
190 static void php_pqconn_object_read_busy(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
192 php_pqconn_object_t
*obj
= o
;
194 RETVAL_BOOL(PQisBusy(obj
->intern
->conn
));
197 static void php_pqconn_object_read_encoding(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
199 php_pqconn_object_t
*obj
= o
;
201 RETVAL_STRING(pg_encoding_to_char(PQclientEncoding(obj
->intern
->conn
)), 1);
204 static void php_pqconn_object_write_encoding(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
206 php_pqconn_object_t
*obj
= o
;
209 if (Z_TYPE_P(value
) != IS_STRING
) {
210 if (Z_REFCOUNT_P(value
) > 1) {
213 ZVAL_ZVAL(tmp
, zenc
, 1, 0);
214 convert_to_string(tmp
);
217 convert_to_string_ex(&zenc
);
221 if (0 > PQsetClientEncoding(obj
->intern
->conn
, Z_STRVAL_P(zenc
))) {
222 zend_error(E_NOTICE
, "Unrecognized encoding '%s'", Z_STRVAL_P(zenc
));
226 zval_ptr_dtor(&zenc
);
230 static void php_pqconn_object_read_unbuffered(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
232 php_pqconn_object_t
*obj
= o
;
234 RETVAL_BOOL(obj
->intern
->unbuffered
);
237 static void php_pqconn_object_write_unbuffered(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
239 php_pqconn_object_t
*obj
= o
;
241 obj
->intern
->unbuffered
= z_is_true(value
);
244 static void php_pqconn_object_read_db(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
246 php_pqconn_object_t
*obj
= o
;
247 char *db
= PQdb(obj
->intern
->conn
);
250 RETVAL_STRING(db
, 1);
252 RETVAL_EMPTY_STRING();
256 static void php_pqconn_object_read_user(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
258 php_pqconn_object_t
*obj
= o
;
259 char *user
= PQuser(obj
->intern
->conn
);
262 RETVAL_STRING(user
, 1);
264 RETVAL_EMPTY_STRING();
268 static void php_pqconn_object_read_pass(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
270 php_pqconn_object_t
*obj
= o
;
271 char *pass
= PQpass(obj
->intern
->conn
);
274 RETVAL_STRING(pass
, 1);
276 RETVAL_EMPTY_STRING();
280 static void php_pqconn_object_read_host(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
282 php_pqconn_object_t
*obj
= o
;
283 char *host
= PQhost(obj
->intern
->conn
);
286 RETVAL_STRING(host
, 1);
288 RETVAL_EMPTY_STRING();
292 static void php_pqconn_object_read_port(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
294 php_pqconn_object_t
*obj
= o
;
295 char *port
= PQport(obj
->intern
->conn
);
298 RETVAL_STRING(port
, 1);
300 RETVAL_EMPTY_STRING();
304 static void php_pqconn_object_read_options(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
306 php_pqconn_object_t
*obj
= o
;
307 char *options
= PQoptions(obj
->intern
->conn
);
310 RETVAL_STRING(options
, 1);
312 RETVAL_EMPTY_STRING();
316 static int apply_read_event_handler_ex(void *p
, void *arg TSRMLS_DC
)
319 zval
*zcb
= php_pq_callback_to_zval(p
);
321 zend_hash_next_index_insert(rv
, &zcb
, sizeof(zval
*), NULL
);
323 return ZEND_HASH_APPLY_KEEP
;
326 static int apply_read_event_handlers(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
328 HashTable
*evhs
= p
, *rv
= va_arg(argv
, HashTable
*);
329 zval
*entry
, **entry_ptr
;
331 MAKE_STD_ZVAL(entry
);
332 array_init_size(entry
, zend_hash_num_elements(evhs
));
334 if (key
->nKeyLength
) {
335 zend_hash_add(rv
, key
->arKey
, key
->nKeyLength
, &entry
, sizeof(zval
*), (void *) &entry_ptr
);
337 zend_hash_index_update(rv
, key
->h
, &entry
, sizeof(zval
*), (void *) &entry_ptr
);
340 zend_hash_apply_with_argument(evhs
, apply_read_event_handler_ex
, Z_ARRVAL_PP(entry_ptr
) TSRMLS_CC
);
342 return ZEND_HASH_APPLY_KEEP
;
344 static void php_pqconn_object_read_event_handlers(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
346 php_pqconn_object_t
*obj
= o
;
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
);
352 static void php_pqconn_object_read_def_fetch_type(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
354 php_pqconn_object_t
*obj
= o
;
356 RETVAL_LONG(obj
->intern
->default_fetch_type
);
358 static void php_pqconn_object_write_def_fetch_type(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
360 php_pqconn_object_t
*obj
= o
;
363 if (Z_TYPE_P(zft
) != IS_LONG
) {
364 if (Z_REFCOUNT_P(zft
) > 1) {
367 ZVAL_ZVAL(tmp
, zft
, 1, 0);
368 convert_to_long(tmp
);
371 convert_to_long_ex(&zft
);
375 obj
->intern
->default_fetch_type
= Z_LVAL_P(zft
) & 0x2; /* two bits only */
382 static void php_pqconn_object_read_def_txn_isolation(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
384 php_pqconn_object_t
*obj
= o
;
386 RETVAL_LONG(obj
->intern
->default_txn_isolation
);
388 static void php_pqconn_object_write_def_txn_isolation(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
390 php_pqconn_object_t
*obj
= o
;
393 if (Z_TYPE_P(zti
) != IS_LONG
) {
394 if (Z_REFCOUNT_P(zti
) > 1) {
397 ZVAL_ZVAL(tmp
, zti
, 1, 0);
398 convert_to_long(tmp
);
401 convert_to_long_ex(&zti
);
405 obj
->intern
->default_txn_isolation
= Z_LVAL_P(zti
) & 0x2; /* two bits only */
412 static void php_pqconn_object_read_def_txn_readonly(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
414 php_pqconn_object_t
*obj
= o
;
416 RETVAL_BOOL(obj
->intern
->default_txn_readonly
);
418 static void php_pqconn_object_write_def_txn_readonly(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
420 php_pqconn_object_t
*obj
= o
;
422 obj
->intern
->default_txn_readonly
= zend_is_true(value
);
425 static void php_pqconn_object_read_def_txn_deferrable(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
427 php_pqconn_object_t
*obj
= o
;
429 RETVAL_BOOL(obj
->intern
->default_txn_deferrable
);
431 static void php_pqconn_object_write_def_txn_deferrable(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
433 php_pqconn_object_t
*obj
= o
;
435 obj
->intern
->default_txn_deferrable
= zend_is_true(value
);
438 static void php_pqconn_object_read_def_auto_conv(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
440 php_pqconn_object_t
*obj
= o
;
442 RETVAL_LONG(obj
->intern
->default_auto_convert
);
444 static void php_pqconn_object_write_def_auto_conv(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
446 php_pqconn_object_t
*obj
= o
;
449 if (Z_TYPE_P(zac
) != IS_LONG
) {
450 if (Z_REFCOUNT_P(zac
) > 1) {
453 ZVAL_ZVAL(tmp
, zac
, 1, 0);
454 convert_to_long(tmp
);
457 convert_to_long_ex(&zac
);
461 obj
->intern
->default_auto_convert
= Z_LVAL_P(zac
) & 0xff;
468 static STATUS
php_pqconn_update_socket(zval
*this_ptr
, php_pqconn_object_t
*obj TSRMLS_DC
)
470 zval
*zsocket
, zmember
;
476 obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
479 INIT_PZVAL(&zmember
);
480 ZVAL_STRINGL(&zmember
, "socket", sizeof("socket")-1, 0);
481 MAKE_STD_ZVAL(zsocket
);
483 if ((CONNECTION_BAD
!= PQstatus(obj
->intern
->conn
))
484 && (-1 < (socket
= PQsocket(obj
->intern
->conn
)))
485 && (stream
= php_stream_fopen_from_fd(socket
, "r+b", NULL
))) {
486 stream
->flags
|= PHP_STREAM_FLAG_NO_CLOSE
;
487 php_stream_to_zval(stream
, zsocket
);
493 zend_get_std_object_handlers()->write_property(getThis(), &zmember
, zsocket
, NULL TSRMLS_CC
);
494 zval_ptr_dtor(&zsocket
);
499 static void *php_pqconn_resource_factory_ctor(void *data
, void *init_arg TSRMLS_DC
)
501 php_pqconn_resource_factory_data_t
*o
= init_arg
;
502 PGconn
*conn
= NULL
;;
504 if (o
->flags
& PHP_PQCONN_ASYNC
) {
505 conn
= PQconnectStart(o
->dsn
);
507 conn
= PQconnectdb(o
->dsn
);
511 PQregisterEventProc(conn
, php_pqconn_event
, "ext-pq", NULL
);
517 static void php_pqconn_resource_factory_dtor(void *opaque
, void *handle TSRMLS_DC
)
519 php_pqconn_event_data_t
*evdata
= PQinstanceData(handle
, php_pqconn_event
);
521 /* we don't care for anything, except free'ing evdata */
523 PQsetInstanceData(handle
, php_pqconn_event
, NULL
);
524 memset(evdata
, 0, sizeof(*evdata
));
531 static php_resource_factory_ops_t php_pqconn_resource_factory_ops
= {
532 php_pqconn_resource_factory_ctor
,
534 php_pqconn_resource_factory_dtor
537 php_resource_factory_ops_t
*php_pqconn_get_resource_factory_ops(void)
539 return &php_pqconn_resource_factory_ops
;
542 static void php_pqconn_wakeup(php_persistent_handle_factory_t
*f
, void **handle TSRMLS_DC
)
544 PGresult
*res
= PQexec(*handle
, "");
547 if (CONNECTION_OK
!= PQstatus(*handle
)) {
552 static inline PGresult
*unlisten(PGconn
*conn
, const char *channel_str
, size_t channel_len TSRMLS_DC
)
554 char *quoted_channel
= PQescapeIdentifier(conn
, channel_str
, channel_len
);
555 PGresult
*res
= NULL
;
557 if (quoted_channel
) {
560 smart_str_appends(&cmd
, "UNLISTEN ");
561 smart_str_appends(&cmd
, quoted_channel
);
564 res
= PQexec(conn
, cmd
.c
);
566 smart_str_free(&cmd
);
567 PQfreemem(quoted_channel
);
573 static int apply_unlisten(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
575 php_pqconn_object_t
*obj
= va_arg(argv
, php_pqconn_object_t
*);
576 PGresult
*res
= unlisten(obj
->intern
->conn
, key
->arKey
, key
->nKeyLength
- 1);
582 return ZEND_HASH_APPLY_REMOVE
;
585 static void php_pqconn_retire(php_persistent_handle_factory_t
*f
, void **handle TSRMLS_DC
)
587 php_pqconn_event_data_t
*evdata
= PQinstanceData(*handle
, php_pqconn_event
);
592 PQsetInstanceData(*handle
, php_pqconn_event
, NULL
);
595 PQsetNoticeReceiver(*handle
, php_pqconn_notice_ignore
, NULL
);
597 /* cancel async queries */
598 if (PQisBusy(*handle
) && (cancel
= PQgetCancel(*handle
))) {
601 PQcancel(cancel
, err
, sizeof(err
));
602 PQfreeCancel(cancel
);
604 /* clean up async results */
605 while ((res
= PQgetResult(*handle
))) {
609 /* clean up transaction & session */
610 switch (PQtransactionStatus(*handle
)) {
612 res
= PQexec(*handle
, "RESET ALL");
615 res
= PQexec(*handle
, "ROLLBACK; RESET ALL");
624 /* clean up notify listeners */
625 zend_hash_apply_with_arguments(&evdata
->obj
->intern
->listeners TSRMLS_CC
, apply_unlisten
, 1, evdata
->obj
);
627 /* release instance data */
632 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct
, 0, 0, 1)
633 ZEND_ARG_INFO(0, dsn
)
634 ZEND_ARG_INFO(0, async
)
636 static PHP_METHOD(pqconn
, __construct
) {
637 zend_error_handling zeh
;
643 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
644 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &dsn_str
, &dsn_len
, &flags
);
645 zend_restore_error_handling(&zeh TSRMLS_CC
);
648 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
651 throw_exce(EX_BAD_METHODCALL TSRMLS_CC
, "pq\\Connection already initialized");
653 php_pqconn_event_data_t
*evdata
= php_pqconn_event_data_init(obj TSRMLS_CC
);
654 php_pqconn_resource_factory_data_t rfdata
= {dsn_str
, flags
};
656 obj
->intern
= ecalloc(1, sizeof(*obj
->intern
));
658 obj
->intern
->default_auto_convert
= PHP_PQRES_CONV_ALL
;
660 zend_hash_init(&obj
->intern
->listeners
, 0, NULL
, (dtor_func_t
) zend_hash_destroy
, 0);
661 zend_hash_init(&obj
->intern
->converters
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
662 zend_hash_init(&obj
->intern
->eventhandlers
, 0, NULL
, (dtor_func_t
) zend_hash_destroy
, 0);
664 if (flags
& PHP_PQCONN_PERSISTENT
) {
665 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
);
666 php_resource_factory_init(&obj
->intern
->factory
, php_persistent_handle_get_resource_factory_ops(), phf
, (void (*)(void*)) php_persistent_handle_abandon
);
668 php_resource_factory_init(&obj
->intern
->factory
, &php_pqconn_resource_factory_ops
, NULL
, NULL
);
671 if (flags
& PHP_PQCONN_ASYNC
) {
672 obj
->intern
->poller
= (int (*)(PGconn
*)) PQconnectPoll
;
675 obj
->intern
->conn
= php_resource_factory_handle_ctor(&obj
->intern
->factory
, &rfdata TSRMLS_CC
);
677 PQsetInstanceData(obj
->intern
->conn
, php_pqconn_event
, evdata
);
678 PQsetNoticeReceiver(obj
->intern
->conn
, php_pqconn_notice_recv
, evdata
);
680 if (SUCCESS
!= php_pqconn_update_socket(getThis(), obj TSRMLS_CC
)) {
681 throw_exce(EX_CONNECTION_FAILED TSRMLS_CC
, "Connection failed (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
687 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset
, 0, 0, 0)
689 static PHP_METHOD(pqconn
, reset
) {
690 zend_error_handling zeh
;
693 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
694 rv
= zend_parse_parameters_none();
695 zend_restore_error_handling(&zeh TSRMLS_CC
);
698 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
701 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
703 PQreset(obj
->intern
->conn
);
705 if (CONNECTION_OK
!= PQstatus(obj
->intern
->conn
)) {
706 throw_exce(EX_CONNECTION_FAILED TSRMLS_CC
, "Connection reset failed: (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
709 php_pqconn_notify_listeners(obj TSRMLS_CC
);
714 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset_async
, 0, 0, 0)
716 static PHP_METHOD(pqconn
, resetAsync
) {
717 zend_error_handling zeh
;
720 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
721 rv
= zend_parse_parameters_none();
722 zend_restore_error_handling(&zeh TSRMLS_CC
);
725 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
728 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
730 if (!PQresetStart(obj
->intern
->conn
)) {
731 throw_exce(EX_IO TSRMLS_CC
, "Failed to start connection reset (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
733 obj
->intern
->poller
= (int (*)(PGconn
*)) PQresetPoll
;
736 php_pqconn_notify_listeners(obj TSRMLS_CC
);
741 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten
, 0, 0, 1)
742 ZEND_ARG_INFO(0, channel
)
744 static PHP_METHOD(pqconn
, unlisten
)
746 zend_error_handling zeh
;
751 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
752 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &channel_str
, &channel_len
);
753 zend_restore_error_handling(&zeh TSRMLS_CC
);
756 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
759 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
760 } else if (SUCCESS
== zend_hash_del(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1)) {
761 PGresult
*res
= unlisten(obj
->intern
->conn
, channel_str
, channel_len
);
764 php_pqres_success(res TSRMLS_CC
);
771 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten_async
, 0, 0, 1)
772 ZEND_ARG_INFO(0, channel
)
774 static PHP_METHOD(pqconn
, unlistenAsync
) {
775 zend_error_handling zeh
;
780 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
781 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &channel_str
, &channel_len
);
782 zend_restore_error_handling(&zeh TSRMLS_CC
);
785 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
788 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
790 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
792 if (!quoted_channel
) {
793 throw_exce(EX_ESCAPE TSRMLS_CC
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
797 smart_str_appends(&cmd
, "UNLISTEN ");
798 smart_str_appends(&cmd
, quoted_channel
);
801 if (!PQsendQuery(obj
->intern
->conn
, cmd
.c
)) {
802 throw_exce(EX_IO TSRMLS_CC
, "Failed to uninstall listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
804 obj
->intern
->poller
= PQconsumeInput
;
805 zend_hash_del(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1);
808 smart_str_free(&cmd
);
809 PQfreemem(quoted_channel
);
810 php_pqconn_notify_listeners(obj TSRMLS_CC
);
816 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
)
818 HashTable ht
, *existing_listeners
;
820 php_pq_callback_addref(listener
);
822 if (SUCCESS
== zend_hash_find(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1, (void *) &existing_listeners
)) {
823 zend_hash_next_index_insert(existing_listeners
, (void *) listener
, sizeof(*listener
), NULL
);
825 zend_hash_init(&ht
, 1, NULL
, (dtor_func_t
) php_pq_callback_dtor
, 0);
826 zend_hash_next_index_insert(&ht
, (void *) listener
, sizeof(*listener
), NULL
);
827 zend_hash_add(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1, (void *) &ht
, sizeof(HashTable
), NULL
);
831 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen
, 0, 0, 2)
832 ZEND_ARG_INFO(0, channel
)
833 ZEND_ARG_INFO(0, callable
)
835 static PHP_METHOD(pqconn
, listen
) {
836 zend_error_handling zeh
;
837 char *channel_str
= NULL
;
839 php_pq_callback_t listener
= {{0}};
842 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
843 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
844 zend_restore_error_handling(&zeh TSRMLS_CC
);
847 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
850 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
852 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
854 if (!quoted_channel
) {
855 throw_exce(EX_ESCAPE TSRMLS_CC
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
860 smart_str_appends(&cmd
, "LISTEN ");
861 smart_str_appends(&cmd
, quoted_channel
);
864 res
= PQexec(obj
->intern
->conn
, cmd
.c
);
866 smart_str_free(&cmd
);
867 PQfreemem(quoted_channel
);
870 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
872 if (SUCCESS
== php_pqres_success(res TSRMLS_CC
)) {
873 obj
->intern
->poller
= PQconsumeInput
;
874 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener TSRMLS_CC
);
879 php_pqconn_notify_listeners(obj TSRMLS_CC
);
885 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen_async
, 0, 0, 0)
886 ZEND_ARG_INFO(0, channel
)
887 ZEND_ARG_INFO(0, callable
)
889 static PHP_METHOD(pqconn
, listenAsync
) {
890 zend_error_handling zeh
;
891 char *channel_str
= NULL
;
893 php_pq_callback_t listener
= {{0}};
896 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
897 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
898 zend_restore_error_handling(&zeh TSRMLS_CC
);
901 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
904 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
906 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
908 if (!quoted_channel
) {
909 throw_exce(EX_ESCAPE TSRMLS_CC
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
913 smart_str_appends(&cmd
, "LISTEN ");
914 smart_str_appends(&cmd
, quoted_channel
);
917 if (!PQsendQuery(obj
->intern
->conn
, cmd
.c
)) {
918 throw_exce(EX_IO TSRMLS_CC
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
920 obj
->intern
->poller
= PQconsumeInput
;
921 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener TSRMLS_CC
);
924 smart_str_free(&cmd
);
925 PQfreemem(quoted_channel
);
926 php_pqconn_notify_listeners(obj TSRMLS_CC
);
932 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify
, 0, 0, 2)
933 ZEND_ARG_INFO(0, channel
)
934 ZEND_ARG_INFO(0, message
)
936 static PHP_METHOD(pqconn
, notify
) {
937 zend_error_handling zeh
;
938 char *channel_str
, *message_str
;
939 int channel_len
, message_len
;
942 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
943 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
944 zend_restore_error_handling(&zeh TSRMLS_CC
);
947 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
950 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
953 char *params
[2] = {channel_str
, message_str
};
955 res
= PQexecParams(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0);
958 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
960 php_pqres_success(res TSRMLS_CC
);
964 php_pqconn_notify_listeners(obj TSRMLS_CC
);
969 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify_async
, 0, 0, 2)
970 ZEND_ARG_INFO(0, channel
)
971 ZEND_ARG_INFO(0, message
)
973 static PHP_METHOD(pqconn
, notifyAsync
) {
974 zend_error_handling zeh
;
975 char *channel_str
, *message_str
;
976 int channel_len
, message_len
;
979 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
980 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
981 zend_restore_error_handling(&zeh TSRMLS_CC
);
984 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
987 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
989 char *params
[2] = {channel_str
, message_str
};
991 if (!PQsendQueryParams(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0)) {
992 throw_exce(EX_IO TSRMLS_CC
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
994 obj
->intern
->poller
= PQconsumeInput
;
997 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1002 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_poll
, 0, 0, 0)
1003 ZEND_END_ARG_INFO();
1004 static PHP_METHOD(pqconn
, poll
) {
1005 zend_error_handling zeh
;
1008 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1009 rv
= zend_parse_parameters_none();
1010 zend_restore_error_handling(&zeh TSRMLS_CC
);
1012 if (SUCCESS
== rv
) {
1013 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1016 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1017 } else if (!obj
->intern
->poller
) {
1018 throw_exce(EX_RUNTIME TSRMLS_CC
, "No asynchronous operation active");
1020 if (obj
->intern
->poller
== PQconsumeInput
) {
1021 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
) * PGRES_POLLING_OK
);
1023 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
));
1025 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1030 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec
, 0, 0, 1)
1031 ZEND_ARG_INFO(0, query
)
1032 ZEND_END_ARG_INFO();
1033 static PHP_METHOD(pqconn
, exec
) {
1034 zend_error_handling zeh
;
1039 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1040 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &query_str
, &query_len
);
1041 zend_restore_error_handling(&zeh TSRMLS_CC
);
1043 if (SUCCESS
== rv
) {
1044 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1047 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1049 PGresult
*res
= PQexec(obj
->intern
->conn
, query_str
);
1052 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1053 } else if (SUCCESS
== php_pqres_success(res TSRMLS_CC
)) {
1054 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), &return_value TSRMLS_CC
);
1059 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1064 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_get_result
, 0, 0, 0)
1065 ZEND_END_ARG_INFO();
1066 static PHP_METHOD(pqconn
, getResult
) {
1067 zend_error_handling zeh
;
1070 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1071 rv
= zend_parse_parameters_none();
1072 zend_restore_error_handling(&zeh TSRMLS_CC
);
1074 if (SUCCESS
== rv
) {
1075 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1078 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1080 PGresult
*res
= PQgetResult(obj
->intern
->conn
);
1085 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), &return_value TSRMLS_CC
);
1088 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1093 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_async
, 0, 0, 1)
1094 ZEND_ARG_INFO(0, query
)
1095 ZEND_ARG_INFO(0, callable
)
1096 ZEND_END_ARG_INFO();
1097 static PHP_METHOD(pqconn
, execAsync
) {
1098 zend_error_handling zeh
;
1099 php_pq_callback_t resolver
= {{0}};
1104 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1105 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|f", &query_str
, &query_len
, &resolver
.fci
, &resolver
.fcc
);
1106 zend_restore_error_handling(&zeh TSRMLS_CC
);
1108 if (SUCCESS
== rv
) {
1109 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1112 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1113 } else if (!PQsendQuery(obj
->intern
->conn
, query_str
)) {
1114 throw_exce(EX_IO TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1115 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1116 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1118 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver TSRMLS_CC
);
1119 obj
->intern
->poller
= PQconsumeInput
;
1120 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1125 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params
, 0, 0, 2)
1126 ZEND_ARG_INFO(0, query
)
1127 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1128 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1129 ZEND_END_ARG_INFO();
1130 static PHP_METHOD(pqconn
, execParams
) {
1131 zend_error_handling zeh
;
1135 zval
*ztypes
= NULL
;
1138 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1139 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa/|a/!", &query_str
, &query_len
, &zparams
, &ztypes
);
1140 zend_restore_error_handling(&zeh TSRMLS_CC
);
1142 if (SUCCESS
== rv
) {
1143 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1146 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1149 php_pq_params_t
*params
;
1151 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
) TSRMLS_CC
);
1152 res
= PQexecParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1153 php_pq_params_free(¶ms
);
1156 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1158 if (SUCCESS
== php_pqres_success(res TSRMLS_CC
)) {
1159 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), &return_value TSRMLS_CC
);
1164 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1170 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async
, 0, 0, 2)
1171 ZEND_ARG_INFO(0, query
)
1172 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1173 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1174 ZEND_ARG_INFO(0, callable
)
1175 ZEND_END_ARG_INFO();
1176 static PHP_METHOD(pqconn
, execParamsAsync
) {
1177 zend_error_handling zeh
;
1178 php_pq_callback_t resolver
= {{0}};
1182 zval
*ztypes
= NULL
;
1185 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1186 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa/|a/!f", &query_str
, &query_len
, &zparams
, &ztypes
, &resolver
.fci
, &resolver
.fcc
);
1187 zend_restore_error_handling(&zeh TSRMLS_CC
);
1189 if (SUCCESS
== rv
) {
1190 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1193 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1196 php_pq_params_t
*params
;
1198 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
) TSRMLS_CC
);
1199 rc
= PQsendQueryParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1200 php_pq_params_free(¶ms
);
1203 throw_exce(EX_IO TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1204 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1205 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1207 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver TSRMLS_CC
);
1208 obj
->intern
->poller
= PQconsumeInput
;
1209 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1213 zend_restore_error_handling(&zeh TSRMLS_CC
);
1216 STATUS
php_pqconn_prepare(zval
*object
, php_pqconn_object_t
*obj
, const char *name
, const char *query
, php_pq_params_t
*params TSRMLS_DC
)
1222 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1225 res
= PQprepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
);
1229 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1231 rv
= php_pqres_success(res TSRMLS_CC
);
1233 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1239 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare
, 0, 0, 2)
1240 ZEND_ARG_INFO(0, name
)
1241 ZEND_ARG_INFO(0, query
)
1242 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1243 ZEND_END_ARG_INFO();
1244 static PHP_METHOD(pqconn
, prepare
) {
1245 zend_error_handling zeh
;
1246 zval
*ztypes
= NULL
;
1247 char *name_str
, *query_str
;
1248 int name_len
, *query_len
;
1251 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1252 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1253 zend_restore_error_handling(&zeh TSRMLS_CC
);
1255 if (SUCCESS
== rv
) {
1256 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1259 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1261 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL TSRMLS_CC
);
1263 if (SUCCESS
!= php_pqconn_prepare(getThis(), obj
, name_str
, query_str
, params TSRMLS_CC
)) {
1264 php_pq_params_free(¶ms
);
1266 php_pqstm_t
*stm
= ecalloc(1, sizeof(*stm
));
1268 php_pq_object_addref(obj TSRMLS_CC
);
1270 stm
->name
= estrdup(name_str
);
1271 stm
->params
= params
;
1272 ZEND_INIT_SYMTABLE(&stm
->bound
);
1274 return_value
->type
= IS_OBJECT
;
1275 return_value
->value
.obj
= php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
, NULL TSRMLS_CC
);
1281 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
)
1286 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1289 if (!PQsendPrepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
)) {
1291 throw_exce(EX_IO TSRMLS_CC
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1292 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1294 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1297 obj
->intern
->poller
= PQconsumeInput
;
1298 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1304 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare_async
, 0, 0, 2)
1305 ZEND_ARG_INFO(0, name
)
1306 ZEND_ARG_INFO(0, query
)
1307 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1308 ZEND_END_ARG_INFO();
1309 static PHP_METHOD(pqconn
, prepareAsync
) {
1310 zend_error_handling zeh
;
1311 zval
*ztypes
= NULL
;
1312 char *name_str
, *query_str
;
1313 int name_len
, *query_len
;
1316 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1317 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1318 zend_restore_error_handling(&zeh TSRMLS_CC
);
1320 if (SUCCESS
== rv
) {
1321 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1324 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1326 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL TSRMLS_CC
);
1328 if (SUCCESS
!= php_pqconn_prepare_async(getThis(), obj
, name_str
, query_str
, params TSRMLS_CC
)) {
1329 php_pq_params_free(¶ms
);
1331 php_pqstm_t
*stm
= ecalloc(1, sizeof(*stm
));
1333 php_pq_object_addref(obj TSRMLS_CC
);
1335 stm
->name
= estrdup(name_str
);
1336 stm
->params
= params
;
1337 ZEND_INIT_SYMTABLE(&stm
->bound
);
1339 return_value
->type
= IS_OBJECT
;
1340 return_value
->value
.obj
= php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
, NULL TSRMLS_CC
);
1346 static inline char *declare_str(const char *name_str
, size_t name_len
, unsigned flags
, const char *query_str
, size_t query_len
)
1348 size_t decl_len
= name_len
+ query_len
+ sizeof("DECLARE BINARY INSENSITIVE NO SCROLL CURSOR WITHOUT HOLD FOR ");
1351 decl_str
= emalloc(decl_len
);
1352 decl_len
= slprintf(decl_str
, decl_len
, "DECLARE %s %s %s %s CURSOR %s FOR %s",
1354 (flags
& PHP_PQ_DECLARE_BINARY
) ? "BINARY" : "",
1355 (flags
& PHP_PQ_DECLARE_INSENSITIVE
) ? "INSENSITIVE" : "",
1356 (flags
& PHP_PQ_DECLARE_NO_SCROLL
) ? "NO SCROLL" :
1357 (flags
& PHP_PQ_DECLARE_SCROLL
) ? "SCROLL" : "",
1358 (flags
& PHP_PQ_DECLARE_WITH_HOLD
) ? "WITH HOLD" : "",
1364 STATUS
php_pqconn_declare(zval
*object
, php_pqconn_object_t
*obj
, const char *decl TSRMLS_DC
)
1370 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1373 res
= PQexec(obj
->intern
->conn
, decl
);
1377 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1379 rv
= php_pqres_success(res TSRMLS_CC
);
1381 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1387 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare
, 0, 0, 3)
1388 ZEND_ARG_INFO(0, name
)
1389 ZEND_ARG_INFO(0, flags
)
1390 ZEND_ARG_INFO(0, query
)
1391 ZEND_END_ARG_INFO();
1392 static PHP_METHOD(pqconn
, declare
) {
1393 zend_error_handling zeh
;
1394 char *name_str
, *query_str
;
1395 int name_len
, query_len
;
1399 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1400 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1401 zend_restore_error_handling(&zeh TSRMLS_CC
);
1403 if (SUCCESS
== rv
) {
1404 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1407 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1409 char *decl
= declare_str(name_str
, name_len
, flags
, query_str
, query_len
);
1411 if (SUCCESS
!= php_pqconn_declare(getThis(), obj
, decl TSRMLS_CC
)) {
1414 php_pqcur_t
*cur
= ecalloc(1, sizeof(*cur
));
1416 php_pq_object_addref(obj TSRMLS_CC
);
1419 cur
->name
= estrdup(name_str
);
1422 return_value
->type
= IS_OBJECT
;
1423 return_value
->value
.obj
= php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
, NULL TSRMLS_CC
);
1429 STATUS
php_pqconn_declare_async(zval
*object
, php_pqconn_object_t
*obj
, const char *decl TSRMLS_DC
)
1434 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1437 if (!PQsendQuery(obj
->intern
->conn
, decl
)) {
1439 throw_exce(EX_IO TSRMLS_CC
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1440 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1442 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1445 obj
->intern
->poller
= PQconsumeInput
;
1446 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1452 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare_async
, 0, 0, 2)
1453 ZEND_ARG_INFO(0, name
)
1454 ZEND_ARG_INFO(0, flags
)
1455 ZEND_ARG_INFO(0, query
)
1456 ZEND_END_ARG_INFO();
1457 static PHP_METHOD(pqconn
, declareAsync
) {
1458 zend_error_handling zeh
;
1459 char *name_str
, *query_str
;
1460 int name_len
, query_len
;
1464 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1465 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1466 zend_restore_error_handling(&zeh TSRMLS_CC
);
1468 if (SUCCESS
== rv
) {
1469 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1472 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1474 char *decl
= declare_str(name_str
, name_len
, flags
, query_str
, query_len
);
1476 if (SUCCESS
!= php_pqconn_declare_async(getThis(), obj
, decl TSRMLS_CC
)) {
1479 php_pqcur_t
*cur
= ecalloc(1, sizeof(*cur
));
1481 php_pq_object_addref(obj TSRMLS_CC
);
1484 cur
->name
= estrdup(name_str
);
1487 return_value
->type
= IS_OBJECT
;
1488 return_value
->value
.obj
= php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
, NULL TSRMLS_CC
);
1494 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote
, 0, 0, 1)
1495 ZEND_ARG_INFO(0, string
)
1496 ZEND_END_ARG_INFO();
1497 static PHP_METHOD(pqconn
, quote
) {
1501 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1502 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1505 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1507 char *quoted
= PQescapeLiteral(obj
->intern
->conn
, str
, len
);
1510 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to quote string (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1513 RETVAL_STRING(quoted
, 1);
1520 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote_name
, 0, 0, 1)
1521 ZEND_ARG_INFO(0, type
)
1522 ZEND_END_ARG_INFO();
1523 static PHP_METHOD(pqconn
, quoteName
) {
1527 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1528 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1531 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1533 char *quoted
= PQescapeIdentifier(obj
->intern
->conn
, str
, len
);
1536 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to quote name (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1539 RETVAL_STRING(quoted
, 1);
1546 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_escape_bytea
, 0, 0, 1)
1547 ZEND_ARG_INFO(0, bytea
)
1548 ZEND_END_ARG_INFO();
1549 static PHP_METHOD(pqconn
, escapeBytea
) {
1553 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1554 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1557 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1560 char *escaped_str
= (char *) PQescapeByteaConn(obj
->intern
->conn
, (unsigned char *) str
, len
, &escaped_len
);
1563 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to escape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1566 RETVAL_STRINGL(escaped_str
, escaped_len
- 1, 1);
1567 PQfreemem(escaped_str
);
1573 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unescape_bytea
, 0, 0, 1)
1574 ZEND_ARG_INFO(0, bytea
)
1575 ZEND_END_ARG_INFO();
1576 static PHP_METHOD(pqconn
, unescapeBytea
) {
1580 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1581 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1584 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1586 size_t unescaped_len
;
1587 char *unescaped_str
= (char *) PQunescapeBytea((unsigned char *)str
, &unescaped_len
);
1589 if (!unescaped_str
) {
1590 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to unescape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1593 RETVAL_STRINGL(unescaped_str
, unescaped_len
, 1);
1594 PQfreemem(unescaped_str
);
1600 STATUS
php_pqconn_start_transaction(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable TSRMLS_DC
)
1602 STATUS rv
= FAILURE
;
1605 conn_obj
= zend_object_store_get_object(zconn TSRMLS_CC
);
1608 if (!conn_obj
->intern
) {
1609 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1612 smart_str cmd
= {0};
1613 const char *il
= isolation_level(&isolation
);
1615 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1616 smart_str_appends(&cmd
, il
);
1617 smart_str_appends(&cmd
, ", READ ");
1618 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1619 smart_str_appends(&cmd
, ",");
1620 smart_str_appends(&cmd
, deferrable
? "" : " NOT");
1621 smart_str_appends(&cmd
, " DEFERRABLE");
1624 res
= PQexec(conn_obj
->intern
->conn
, cmd
.c
);
1627 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1629 rv
= php_pqres_success(res TSRMLS_CC
);
1631 php_pqconn_notify_listeners(conn_obj TSRMLS_CC
);
1634 smart_str_free(&cmd
);
1640 STATUS
php_pqconn_start_transaction_async(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable TSRMLS_DC
)
1642 STATUS rv
= FAILURE
;
1645 conn_obj
= zend_object_store_get_object(zconn TSRMLS_CC
);
1648 if (!conn_obj
->intern
) {
1649 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1651 smart_str cmd
= {0};
1652 const char *il
= isolation_level(&isolation
);
1654 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1655 smart_str_appends(&cmd
, il
);
1656 smart_str_appends(&cmd
, ", READ ");
1657 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1658 smart_str_appends(&cmd
, ",");
1659 smart_str_appends(&cmd
, deferrable
? "" : "NOT ");
1660 smart_str_appends(&cmd
, " DEFERRABLE");
1663 if (!PQsendQuery(conn_obj
->intern
->conn
, cmd
.c
)) {
1664 throw_exce(EX_IO TSRMLS_CC
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1667 conn_obj
->intern
->poller
= PQconsumeInput
;
1668 php_pqconn_notify_listeners(conn_obj TSRMLS_CC
);
1671 smart_str_free(&cmd
);
1677 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction
, 0, 0, 0)
1678 ZEND_ARG_INFO(0, isolation
)
1679 ZEND_ARG_INFO(0, readonly
)
1680 ZEND_ARG_INFO(0, deferrable
)
1681 ZEND_END_ARG_INFO();
1682 static PHP_METHOD(pqconn
, startTransaction
) {
1683 zend_error_handling zeh
;
1684 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1685 long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1686 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1687 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1690 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1691 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lbb", &isolation
, &readonly
, &deferrable
);
1692 zend_restore_error_handling(&zeh TSRMLS_CC
);
1694 if (SUCCESS
== rv
) {
1695 rv
= php_pqconn_start_transaction(getThis(), obj
, isolation
, readonly
, deferrable TSRMLS_CC
);
1697 if (SUCCESS
== rv
) {
1698 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1700 php_pq_object_addref(obj TSRMLS_CC
);
1703 txn
->isolation
= isolation
;
1704 txn
->readonly
= readonly
;
1705 txn
->deferrable
= deferrable
;
1707 return_value
->type
= IS_OBJECT
;
1708 return_value
->value
.obj
= php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
, NULL TSRMLS_CC
);
1713 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction_async
, 0, 0, 0)
1714 ZEND_ARG_INFO(0, isolation
)
1715 ZEND_ARG_INFO(0, readonly
)
1716 ZEND_ARG_INFO(0, deferrable
)
1717 ZEND_END_ARG_INFO();
1718 static PHP_METHOD(pqconn
, startTransactionAsync
) {
1719 zend_error_handling zeh
;
1720 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1721 long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1722 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1723 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1726 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1727 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lbb", &isolation
, &readonly
, &deferrable
);
1728 zend_restore_error_handling(&zeh TSRMLS_CC
);
1730 if (SUCCESS
== rv
) {
1731 rv
= php_pqconn_start_transaction_async(getThis(), obj
, isolation
, readonly
, deferrable TSRMLS_CC
);
1733 if (SUCCESS
== rv
) {
1734 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1736 php_pq_object_addref(obj TSRMLS_CC
);
1738 txn
->isolation
= isolation
;
1739 txn
->readonly
= readonly
;
1740 txn
->deferrable
= deferrable
;
1742 return_value
->type
= IS_OBJECT
;
1743 return_value
->value
.obj
= php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
, NULL TSRMLS_CC
);
1748 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_trace
, 0, 0, 0)
1749 ZEND_ARG_INFO(0, stdio_stream
)
1750 ZEND_END_ARG_INFO();
1751 static PHP_METHOD(pqconn
, trace
) {
1752 zval
*zstream
= NULL
;
1754 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|r!", &zstream
)) {
1755 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1758 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1761 PQuntrace(obj
->intern
->conn
);
1765 php_stream
*stream
= NULL
;
1767 php_stream_from_zval(stream
, &zstream
);
1769 if (SUCCESS
!= php_stream_cast(stream
, PHP_STREAM_AS_STDIO
, (void *) &fp
, REPORT_ERRORS
)) {
1772 stream
->flags
|= PHP_STREAM_FLAG_NO_CLOSE
;
1773 PQtrace(obj
->intern
->conn
, fp
);
1781 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_off
, 0, 0, 1)
1782 ZEND_ARG_INFO(0, type
)
1783 ZEND_END_ARG_INFO();
1784 static PHP_METHOD(pqconn
, off
) {
1785 zend_error_handling zeh
;
1790 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1791 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &type_str
, &type_len
);
1792 zend_restore_error_handling(&zeh TSRMLS_CC
);
1794 if (SUCCESS
== rv
) {
1795 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1798 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1800 RETURN_BOOL(SUCCESS
== zend_hash_del(&obj
->intern
->eventhandlers
, type_str
, type_len
+ 1));
1805 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_on
, 0, 0, 2)
1806 ZEND_ARG_INFO(0, type
)
1807 ZEND_ARG_INFO(0, callable
)
1808 ZEND_END_ARG_INFO();
1809 static PHP_METHOD(pqconn
, on
) {
1810 zend_error_handling zeh
;
1813 php_pq_callback_t cb
= {{0}};
1816 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1817 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sf", &type_str
, &type_len
, &cb
.fci
, &cb
.fcc
);
1818 zend_restore_error_handling(&zeh TSRMLS_CC
);
1820 if (SUCCESS
== rv
) {
1821 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1824 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1826 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1828 RETVAL_LONG(php_pqconn_add_eventhandler(obj
, type_str
, type_len
, &cb TSRMLS_CC
));
1833 static int apply_set_converter(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
1835 zval
*tmp
, **zoid
= p
, **zcnv
= va_arg(argv
, zval
**);
1836 HashTable
*converters
= va_arg(argv
, HashTable
*);
1837 int add
= va_arg(argv
, int);
1841 convert_to_long_ex(&tmp
);
1844 zend_hash_index_update(converters
, Z_LVAL_P(tmp
), zcnv
, sizeof(zval
*), NULL
);
1846 zend_hash_index_del(converters
, Z_LVAL_P(tmp
));
1848 zval_ptr_dtor(&tmp
);
1850 return ZEND_HASH_APPLY_KEEP
;
1853 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_set_converter
, 0, 0, 1)
1854 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\ConverterInterface
, 0)
1855 ZEND_END_ARG_INFO();
1856 static PHP_METHOD(pqconn
, setConverter
) {
1858 zend_error_handling zeh
;
1861 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1862 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zcnv
, php_pqconv_class_entry
);
1863 zend_restore_error_handling(&zeh TSRMLS_CC
);
1865 if (SUCCESS
== rv
) {
1866 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1869 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1871 zval
*tmp
, *zoids
= NULL
;
1873 zend_call_method_with_0_params(&zcnv
, NULL
, NULL
, "converttypes", &zoids
);
1876 convert_to_array_ex(&tmp
);
1877 zend_hash_apply_with_arguments(Z_ARRVAL_P(tmp
) TSRMLS_CC
, apply_set_converter
, 3, &zcnv
, &obj
->intern
->converters
, 1);
1878 zval_ptr_dtor(&tmp
);
1879 zval_ptr_dtor(&zoids
);
1884 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unset_converter
, 0, 0, 1)
1885 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\ConverterInterface
, 0)
1886 ZEND_END_ARG_INFO();
1887 static PHP_METHOD(pqconn
, unsetConverter
) {
1889 zend_error_handling zeh
;
1892 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1893 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zcnv
, php_pqconv_class_entry
);
1894 zend_restore_error_handling(&zeh TSRMLS_CC
);
1896 if (SUCCESS
== rv
) {
1897 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1900 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1902 zval
*tmp
, *zoids
= NULL
;
1904 zend_call_method_with_0_params(&zcnv
, NULL
, NULL
, "converttypes", &zoids
);
1907 convert_to_array_ex(&tmp
);
1908 zend_hash_apply_with_arguments(Z_ARRVAL_P(tmp
) TSRMLS_CC
, apply_set_converter
, 3, &zcnv
, &obj
->intern
->converters
, 0);
1909 zval_ptr_dtor(&tmp
);
1910 zval_ptr_dtor(&zoids
);
1915 static zend_function_entry php_pqconn_methods
[] = {
1916 PHP_ME(pqconn
, __construct
, ai_pqconn_construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
1917 PHP_ME(pqconn
, reset
, ai_pqconn_reset
, ZEND_ACC_PUBLIC
)
1918 PHP_ME(pqconn
, resetAsync
, ai_pqconn_reset_async
, ZEND_ACC_PUBLIC
)
1919 PHP_ME(pqconn
, poll
, ai_pqconn_poll
, ZEND_ACC_PUBLIC
)
1920 PHP_ME(pqconn
, exec
, ai_pqconn_exec
, ZEND_ACC_PUBLIC
)
1921 PHP_ME(pqconn
, execAsync
, ai_pqconn_exec_async
, ZEND_ACC_PUBLIC
)
1922 PHP_ME(pqconn
, execParams
, ai_pqconn_exec_params
, ZEND_ACC_PUBLIC
)
1923 PHP_ME(pqconn
, execParamsAsync
, ai_pqconn_exec_params_async
, ZEND_ACC_PUBLIC
)
1924 PHP_ME(pqconn
, prepare
, ai_pqconn_prepare
, ZEND_ACC_PUBLIC
)
1925 PHP_ME(pqconn
, prepareAsync
, ai_pqconn_prepare_async
, ZEND_ACC_PUBLIC
)
1926 PHP_ME(pqconn
, declare
, ai_pqconn_declare
, ZEND_ACC_PUBLIC
)
1927 PHP_ME(pqconn
, declareAsync
, ai_pqconn_declare_async
, ZEND_ACC_PUBLIC
)
1928 PHP_ME(pqconn
, unlisten
, ai_pqconn_unlisten
, ZEND_ACC_PUBLIC
)
1929 PHP_ME(pqconn
, unlistenAsync
, ai_pqconn_unlisten_async
, ZEND_ACC_PUBLIC
)
1930 PHP_ME(pqconn
, listen
, ai_pqconn_listen
, ZEND_ACC_PUBLIC
)
1931 PHP_ME(pqconn
, listenAsync
, ai_pqconn_listen_async
, ZEND_ACC_PUBLIC
)
1932 PHP_ME(pqconn
, notify
, ai_pqconn_notify
, ZEND_ACC_PUBLIC
)
1933 PHP_ME(pqconn
, notifyAsync
, ai_pqconn_notify_async
, ZEND_ACC_PUBLIC
)
1934 PHP_ME(pqconn
, getResult
, ai_pqconn_get_result
, ZEND_ACC_PUBLIC
)
1935 PHP_ME(pqconn
, quote
, ai_pqconn_quote
, ZEND_ACC_PUBLIC
)
1936 PHP_ME(pqconn
, quoteName
, ai_pqconn_quote_name
, ZEND_ACC_PUBLIC
)
1937 PHP_ME(pqconn
, escapeBytea
, ai_pqconn_escape_bytea
, ZEND_ACC_PUBLIC
)
1938 PHP_ME(pqconn
, unescapeBytea
, ai_pqconn_unescape_bytea
, ZEND_ACC_PUBLIC
)
1939 PHP_ME(pqconn
, startTransaction
, ai_pqconn_start_transaction
, ZEND_ACC_PUBLIC
)
1940 PHP_ME(pqconn
, startTransactionAsync
, ai_pqconn_start_transaction_async
, ZEND_ACC_PUBLIC
)
1941 PHP_ME(pqconn
, trace
, ai_pqconn_trace
, ZEND_ACC_PUBLIC
)
1942 PHP_ME(pqconn
, off
, ai_pqconn_off
, ZEND_ACC_PUBLIC
)
1943 PHP_ME(pqconn
, on
, ai_pqconn_on
, ZEND_ACC_PUBLIC
)
1944 PHP_ME(pqconn
, setConverter
, ai_pqconn_set_converter
, ZEND_ACC_PUBLIC
)
1945 PHP_ME(pqconn
, unsetConverter
, ai_pqconn_unset_converter
, ZEND_ACC_PUBLIC
)
1949 PHP_MSHUTDOWN_FUNCTION(pqconn
)
1951 zend_hash_destroy(&php_pqconn_object_prophandlers
);
1955 PHP_MINIT_FUNCTION(pqconn
)
1957 zend_class_entry ce
= {0};
1958 php_pq_object_prophandler_t ph
= {0};
1960 INIT_NS_CLASS_ENTRY(ce
, "pq", "Connection", php_pqconn_methods
);
1961 php_pqconn_class_entry
= zend_register_internal_class_ex(&ce
, NULL
, NULL TSRMLS_CC
);
1962 php_pqconn_class_entry
->create_object
= php_pqconn_create_object
;
1964 memcpy(&php_pqconn_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
1965 php_pqconn_object_handlers
.read_property
= php_pq_object_read_prop
;
1966 php_pqconn_object_handlers
.write_property
= php_pq_object_write_prop
;
1967 php_pqconn_object_handlers
.clone_obj
= NULL
;
1968 php_pqconn_object_handlers
.get_property_ptr_ptr
= NULL
;
1969 php_pqconn_object_handlers
.get_gc
= NULL
;
1970 php_pqconn_object_handlers
.get_properties
= php_pq_object_properties
;
1971 php_pqconn_object_handlers
.get_debug_info
= php_pq_object_debug_info
;
1973 zend_hash_init(&php_pqconn_object_prophandlers
, 14, NULL
, NULL
, 1);
1975 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("status"), CONNECTION_BAD
, ZEND_ACC_PUBLIC TSRMLS_CC
);
1976 ph
.read
= php_pqconn_object_read_status
;
1977 zend_hash_add(&php_pqconn_object_prophandlers
, "status", sizeof("status"), (void *) &ph
, sizeof(ph
), NULL
);
1979 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("transactionStatus"), PQTRANS_UNKNOWN
, ZEND_ACC_PUBLIC TSRMLS_CC
);
1980 ph
.read
= php_pqconn_object_read_transaction_status
;
1981 zend_hash_add(&php_pqconn_object_prophandlers
, "transactionStatus", sizeof("transactionStatus"), (void *) &ph
, sizeof(ph
), NULL
);
1983 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("socket"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1984 ph
.read
= NULL
; /* forward to std prophandler */
1985 zend_hash_add(&php_pqconn_object_prophandlers
, "socket", sizeof("socket"), (void *) &ph
, sizeof(ph
), NULL
);
1987 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("errorMessage"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1988 ph
.read
= php_pqconn_object_read_error_message
;
1989 zend_hash_add(&php_pqconn_object_prophandlers
, "errorMessage", sizeof("errorMessage"), (void *) &ph
, sizeof(ph
), NULL
);
1991 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("busy"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
1992 ph
.read
= php_pqconn_object_read_busy
;
1993 zend_hash_add(&php_pqconn_object_prophandlers
, "busy", sizeof("busy"), (void *) &ph
, sizeof(ph
), NULL
);
1995 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("encoding"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1996 ph
.read
= php_pqconn_object_read_encoding
;
1997 ph
.write
= php_pqconn_object_write_encoding
;
1998 zend_hash_add(&php_pqconn_object_prophandlers
, "encoding", sizeof("encoding"), (void *) &ph
, sizeof(ph
), NULL
);
2001 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("unbuffered"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
2002 ph
.read
= php_pqconn_object_read_unbuffered
;
2003 ph
.write
= php_pqconn_object_write_unbuffered
;
2004 zend_hash_add(&php_pqconn_object_prophandlers
, "unbuffered", sizeof("unbuffered"), (void *) &ph
, sizeof(ph
), NULL
);
2007 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("db"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2008 ph
.read
= php_pqconn_object_read_db
;
2009 zend_hash_add(&php_pqconn_object_prophandlers
, "db", sizeof("db"), (void *) &ph
, sizeof(ph
), NULL
);
2011 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("user"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2012 ph
.read
= php_pqconn_object_read_user
;
2013 zend_hash_add(&php_pqconn_object_prophandlers
, "user", sizeof("user"), (void *) &ph
, sizeof(ph
), NULL
);
2015 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("pass"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2016 ph
.read
= php_pqconn_object_read_pass
;
2017 zend_hash_add(&php_pqconn_object_prophandlers
, "pass", sizeof("pass"), (void *) &ph
, sizeof(ph
), NULL
);
2019 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("host"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2020 ph
.read
= php_pqconn_object_read_host
;
2021 zend_hash_add(&php_pqconn_object_prophandlers
, "host", sizeof("host"), (void *) &ph
, sizeof(ph
), NULL
);
2023 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("port"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2024 ph
.read
= php_pqconn_object_read_port
;
2025 zend_hash_add(&php_pqconn_object_prophandlers
, "port", sizeof("port"), (void *) &ph
, sizeof(ph
), NULL
);
2027 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("options"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2028 ph
.read
= php_pqconn_object_read_options
;
2029 zend_hash_add(&php_pqconn_object_prophandlers
, "options", sizeof("options"), (void *) &ph
, sizeof(ph
), NULL
);
2031 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("eventHandlers"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2032 ph
.read
= php_pqconn_object_read_event_handlers
;
2033 zend_hash_add(&php_pqconn_object_prophandlers
, "eventHandlers", sizeof("eventHandlers"), (void *) &ph
, sizeof(ph
), NULL
);
2035 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultFetchType"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
2036 ph
.read
= php_pqconn_object_read_def_fetch_type
;
2037 ph
.write
= php_pqconn_object_write_def_fetch_type
;
2038 zend_hash_add(&php_pqconn_object_prophandlers
, "defaultFetchType", sizeof("defaultFetchType"), (void *) &ph
, sizeof(ph
), NULL
);
2041 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionIsolation"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
2042 ph
.read
= php_pqconn_object_read_def_txn_isolation
;
2043 ph
.write
= php_pqconn_object_write_def_txn_isolation
;
2044 zend_hash_add(&php_pqconn_object_prophandlers
, "defaultTransactionIsolation", sizeof("defaultTransactionIsolation"), (void *) &ph
, sizeof(ph
), NULL
);
2047 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionReadonly"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
2048 ph
.read
= php_pqconn_object_read_def_txn_readonly
;
2049 ph
.write
= php_pqconn_object_write_def_txn_readonly
;
2050 zend_hash_add(&php_pqconn_object_prophandlers
, "defaultTransactionReadonly", sizeof("defaultTransactionReadonly"), (void *) &ph
, sizeof(ph
), NULL
);
2053 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionDeferrable"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
2054 ph
.read
= php_pqconn_object_read_def_txn_deferrable
;
2055 ph
.write
= php_pqconn_object_write_def_txn_deferrable
;
2056 zend_hash_add(&php_pqconn_object_prophandlers
, "defaultTransactionDeferrable", sizeof("defaultTransactionDeferrable"), (void *) &ph
, sizeof(ph
), NULL
);
2059 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultAutoConvert"), PHP_PQRES_CONV_ALL
, ZEND_ACC_PUBLIC TSRMLS_CC
);
2060 ph
.read
= php_pqconn_object_read_def_auto_conv
;
2061 ph
.write
= php_pqconn_object_write_def_auto_conv
;
2062 zend_hash_add(&php_pqconn_object_prophandlers
, "defaultAutoConvert", sizeof("defaultAutoConvert"), (void *) &ph
, sizeof(ph
), NULL
);
2065 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("OK"), CONNECTION_OK TSRMLS_CC
);
2066 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("BAD"), CONNECTION_BAD TSRMLS_CC
);
2067 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("STARTED"), CONNECTION_STARTED TSRMLS_CC
);
2068 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("MADE"), CONNECTION_MADE TSRMLS_CC
);
2069 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AWAITING_RESPONSE"), CONNECTION_AWAITING_RESPONSE TSRMLS_CC
);
2070 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AUTH_OK"), CONNECTION_AUTH_OK TSRMLS_CC
);
2071 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SSL_STARTUP"), CONNECTION_SSL_STARTUP TSRMLS_CC
);
2072 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SETENV"), CONNECTION_SETENV TSRMLS_CC
);
2074 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_IDLE"), PQTRANS_IDLE TSRMLS_CC
);
2075 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_ACTIVE"), PQTRANS_ACTIVE TSRMLS_CC
);
2076 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INTRANS"), PQTRANS_INTRANS TSRMLS_CC
);
2077 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INERROR"), PQTRANS_INERROR TSRMLS_CC
);
2078 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_UNKNOWN"), PQTRANS_UNKNOWN TSRMLS_CC
);
2080 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_FAILED"), PGRES_POLLING_FAILED TSRMLS_CC
);
2081 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_READING"), PGRES_POLLING_READING TSRMLS_CC
);
2082 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_WRITING"), PGRES_POLLING_WRITING TSRMLS_CC
);
2083 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_OK"), PGRES_POLLING_OK TSRMLS_CC
);
2085 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_NOTICE"), ZEND_STRL("notice") TSRMLS_CC
);
2086 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESULT"), ZEND_STRL("result") TSRMLS_CC
);
2087 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESET"), ZEND_STRL("reset") TSRMLS_CC
);
2089 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("ASYNC"), 0x1 TSRMLS_CC
);
2090 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("PERSISTENT"), 0x2 TSRMLS_CC
);
2100 * vim600: noet sw=4 ts=4 fdm=marker
2101 * vim<600: noet sw=4 ts=4