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 // FIXME: ping server
547 static inline PGresult
*unlisten(PGconn
*conn
, const char *channel_str
, size_t channel_len TSRMLS_DC
)
549 char *quoted_channel
= PQescapeIdentifier(conn
, channel_str
, channel_len
);
550 PGresult
*res
= NULL
;
552 if (quoted_channel
) {
555 smart_str_appends(&cmd
, "UNLISTEN ");
556 smart_str_appends(&cmd
, quoted_channel
);
559 res
= PQexec(conn
, cmd
.c
);
561 smart_str_free(&cmd
);
562 PQfreemem(quoted_channel
);
568 static int apply_unlisten(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
570 php_pqconn_object_t
*obj
= va_arg(argv
, php_pqconn_object_t
*);
571 PGresult
*res
= unlisten(obj
->intern
->conn
, key
->arKey
, key
->nKeyLength
- 1);
577 return ZEND_HASH_APPLY_REMOVE
;
580 static void php_pqconn_retire(php_persistent_handle_factory_t
*f
, void **handle TSRMLS_DC
)
582 php_pqconn_event_data_t
*evdata
= PQinstanceData(*handle
, php_pqconn_event
);
587 PQsetInstanceData(*handle
, php_pqconn_event
, NULL
);
590 PQsetNoticeReceiver(*handle
, php_pqconn_notice_ignore
, NULL
);
592 /* cancel async queries */
593 if (PQisBusy(*handle
) && (cancel
= PQgetCancel(*handle
))) {
596 PQcancel(cancel
, err
, sizeof(err
));
597 PQfreeCancel(cancel
);
599 /* clean up async results */
600 while ((res
= PQgetResult(*handle
))) {
604 /* clean up transaction & session */
605 switch (PQtransactionStatus(*handle
)) {
607 res
= PQexec(*handle
, "RESET ALL");
610 res
= PQexec(*handle
, "ROLLBACK; RESET ALL");
619 /* clean up notify listeners */
620 zend_hash_apply_with_arguments(&evdata
->obj
->intern
->listeners TSRMLS_CC
, apply_unlisten
, 1, evdata
->obj
);
622 /* release instance data */
627 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct
, 0, 0, 1)
628 ZEND_ARG_INFO(0, dsn
)
629 ZEND_ARG_INFO(0, async
)
631 static PHP_METHOD(pqconn
, __construct
) {
632 zend_error_handling zeh
;
638 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
639 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &dsn_str
, &dsn_len
, &flags
);
640 zend_restore_error_handling(&zeh TSRMLS_CC
);
643 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
646 throw_exce(EX_BAD_METHODCALL TSRMLS_CC
, "pq\\Connection already initialized");
648 php_pqconn_event_data_t
*evdata
= php_pqconn_event_data_init(obj TSRMLS_CC
);
649 php_pqconn_resource_factory_data_t rfdata
= {dsn_str
, flags
};
651 obj
->intern
= ecalloc(1, sizeof(*obj
->intern
));
653 obj
->intern
->default_auto_convert
= PHP_PQRES_CONV_ALL
;
655 zend_hash_init(&obj
->intern
->listeners
, 0, NULL
, (dtor_func_t
) zend_hash_destroy
, 0);
656 zend_hash_init(&obj
->intern
->converters
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
657 zend_hash_init(&obj
->intern
->eventhandlers
, 0, NULL
, (dtor_func_t
) zend_hash_destroy
, 0);
659 if (flags
& PHP_PQCONN_PERSISTENT
) {
660 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
);
661 php_resource_factory_init(&obj
->intern
->factory
, php_persistent_handle_get_resource_factory_ops(), phf
, (void (*)(void*)) php_persistent_handle_abandon
);
663 php_resource_factory_init(&obj
->intern
->factory
, &php_pqconn_resource_factory_ops
, NULL
, NULL
);
666 if (flags
& PHP_PQCONN_ASYNC
) {
667 obj
->intern
->poller
= (int (*)(PGconn
*)) PQconnectPoll
;
670 obj
->intern
->conn
= php_resource_factory_handle_ctor(&obj
->intern
->factory
, &rfdata TSRMLS_CC
);
672 PQsetInstanceData(obj
->intern
->conn
, php_pqconn_event
, evdata
);
673 PQsetNoticeReceiver(obj
->intern
->conn
, php_pqconn_notice_recv
, evdata
);
675 if (SUCCESS
!= php_pqconn_update_socket(getThis(), obj TSRMLS_CC
)) {
676 throw_exce(EX_CONNECTION_FAILED TSRMLS_CC
, "Connection failed (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
682 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset
, 0, 0, 0)
684 static PHP_METHOD(pqconn
, reset
) {
685 zend_error_handling zeh
;
688 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
689 rv
= zend_parse_parameters_none();
690 zend_restore_error_handling(&zeh TSRMLS_CC
);
693 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
696 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
698 PQreset(obj
->intern
->conn
);
700 if (CONNECTION_OK
!= PQstatus(obj
->intern
->conn
)) {
701 throw_exce(EX_CONNECTION_FAILED TSRMLS_CC
, "Connection reset failed: (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
704 php_pqconn_notify_listeners(obj TSRMLS_CC
);
709 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset_async
, 0, 0, 0)
711 static PHP_METHOD(pqconn
, resetAsync
) {
712 zend_error_handling zeh
;
715 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
716 rv
= zend_parse_parameters_none();
717 zend_restore_error_handling(&zeh TSRMLS_CC
);
720 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
723 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
725 if (!PQresetStart(obj
->intern
->conn
)) {
726 throw_exce(EX_IO TSRMLS_CC
, "Failed to start connection reset (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
728 obj
->intern
->poller
= (int (*)(PGconn
*)) PQresetPoll
;
731 php_pqconn_notify_listeners(obj TSRMLS_CC
);
736 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten
, 0, 0, 1)
737 ZEND_ARG_INFO(0, channel
)
739 static PHP_METHOD(pqconn
, unlisten
)
741 zend_error_handling zeh
;
746 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
747 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &channel_str
, &channel_len
);
748 zend_restore_error_handling(&zeh TSRMLS_CC
);
751 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
754 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
755 } else if (SUCCESS
== zend_hash_del(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1)) {
756 PGresult
*res
= unlisten(obj
->intern
->conn
, channel_str
, channel_len
);
759 php_pqres_success(res TSRMLS_CC
);
766 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten_async
, 0, 0, 1)
767 ZEND_ARG_INFO(0, channel
)
769 static PHP_METHOD(pqconn
, unlistenAsync
) {
770 zend_error_handling zeh
;
775 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
776 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &channel_str
, &channel_len
);
777 zend_restore_error_handling(&zeh TSRMLS_CC
);
780 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
783 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
785 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
787 if (!quoted_channel
) {
788 throw_exce(EX_ESCAPE TSRMLS_CC
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
792 smart_str_appends(&cmd
, "UNLISTEN ");
793 smart_str_appends(&cmd
, quoted_channel
);
796 if (!PQsendQuery(obj
->intern
->conn
, cmd
.c
)) {
797 throw_exce(EX_IO TSRMLS_CC
, "Failed to uninstall listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
799 obj
->intern
->poller
= PQconsumeInput
;
800 zend_hash_del(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1);
803 smart_str_free(&cmd
);
804 PQfreemem(quoted_channel
);
805 php_pqconn_notify_listeners(obj TSRMLS_CC
);
811 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
)
813 HashTable ht
, *existing_listeners
;
815 php_pq_callback_addref(listener
);
817 if (SUCCESS
== zend_hash_find(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1, (void *) &existing_listeners
)) {
818 zend_hash_next_index_insert(existing_listeners
, (void *) listener
, sizeof(*listener
), NULL
);
820 zend_hash_init(&ht
, 1, NULL
, (dtor_func_t
) php_pq_callback_dtor
, 0);
821 zend_hash_next_index_insert(&ht
, (void *) listener
, sizeof(*listener
), NULL
);
822 zend_hash_add(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1, (void *) &ht
, sizeof(HashTable
), NULL
);
826 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen
, 0, 0, 2)
827 ZEND_ARG_INFO(0, channel
)
828 ZEND_ARG_INFO(0, callable
)
830 static PHP_METHOD(pqconn
, listen
) {
831 zend_error_handling zeh
;
832 char *channel_str
= NULL
;
834 php_pq_callback_t listener
= {{0}};
837 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
838 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
839 zend_restore_error_handling(&zeh TSRMLS_CC
);
842 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
845 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
847 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
849 if (!quoted_channel
) {
850 throw_exce(EX_ESCAPE TSRMLS_CC
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
855 smart_str_appends(&cmd
, "LISTEN ");
856 smart_str_appends(&cmd
, quoted_channel
);
859 res
= PQexec(obj
->intern
->conn
, cmd
.c
);
861 smart_str_free(&cmd
);
862 PQfreemem(quoted_channel
);
865 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
867 if (SUCCESS
== php_pqres_success(res TSRMLS_CC
)) {
868 obj
->intern
->poller
= PQconsumeInput
;
869 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener TSRMLS_CC
);
874 php_pqconn_notify_listeners(obj TSRMLS_CC
);
880 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen_async
, 0, 0, 0)
881 ZEND_ARG_INFO(0, channel
)
882 ZEND_ARG_INFO(0, callable
)
884 static PHP_METHOD(pqconn
, listenAsync
) {
885 zend_error_handling zeh
;
886 char *channel_str
= NULL
;
888 php_pq_callback_t listener
= {{0}};
891 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
892 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
893 zend_restore_error_handling(&zeh TSRMLS_CC
);
896 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
899 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
901 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
903 if (!quoted_channel
) {
904 throw_exce(EX_ESCAPE TSRMLS_CC
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
908 smart_str_appends(&cmd
, "LISTEN ");
909 smart_str_appends(&cmd
, quoted_channel
);
912 if (!PQsendQuery(obj
->intern
->conn
, cmd
.c
)) {
913 throw_exce(EX_IO TSRMLS_CC
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
915 obj
->intern
->poller
= PQconsumeInput
;
916 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener TSRMLS_CC
);
919 smart_str_free(&cmd
);
920 PQfreemem(quoted_channel
);
921 php_pqconn_notify_listeners(obj TSRMLS_CC
);
927 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify
, 0, 0, 2)
928 ZEND_ARG_INFO(0, channel
)
929 ZEND_ARG_INFO(0, message
)
931 static PHP_METHOD(pqconn
, notify
) {
932 zend_error_handling zeh
;
933 char *channel_str
, *message_str
;
934 int channel_len
, message_len
;
937 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
938 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
939 zend_restore_error_handling(&zeh TSRMLS_CC
);
942 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
945 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
948 char *params
[2] = {channel_str
, message_str
};
950 res
= PQexecParams(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0);
953 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
955 php_pqres_success(res TSRMLS_CC
);
959 php_pqconn_notify_listeners(obj TSRMLS_CC
);
964 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify_async
, 0, 0, 2)
965 ZEND_ARG_INFO(0, channel
)
966 ZEND_ARG_INFO(0, message
)
968 static PHP_METHOD(pqconn
, notifyAsync
) {
969 zend_error_handling zeh
;
970 char *channel_str
, *message_str
;
971 int channel_len
, message_len
;
974 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
975 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
976 zend_restore_error_handling(&zeh TSRMLS_CC
);
979 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
982 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
984 char *params
[2] = {channel_str
, message_str
};
986 if (!PQsendQueryParams(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0)) {
987 throw_exce(EX_IO TSRMLS_CC
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
989 obj
->intern
->poller
= PQconsumeInput
;
992 php_pqconn_notify_listeners(obj TSRMLS_CC
);
997 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_poll
, 0, 0, 0)
999 static PHP_METHOD(pqconn
, poll
) {
1000 zend_error_handling zeh
;
1003 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1004 rv
= zend_parse_parameters_none();
1005 zend_restore_error_handling(&zeh TSRMLS_CC
);
1007 if (SUCCESS
== rv
) {
1008 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1011 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1012 } else if (!obj
->intern
->poller
) {
1013 throw_exce(EX_RUNTIME TSRMLS_CC
, "No asynchronous operation active");
1015 if (obj
->intern
->poller
== PQconsumeInput
) {
1016 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
) * PGRES_POLLING_OK
);
1018 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
));
1020 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1025 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec
, 0, 0, 1)
1026 ZEND_ARG_INFO(0, query
)
1027 ZEND_END_ARG_INFO();
1028 static PHP_METHOD(pqconn
, exec
) {
1029 zend_error_handling zeh
;
1034 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1035 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &query_str
, &query_len
);
1036 zend_restore_error_handling(&zeh TSRMLS_CC
);
1038 if (SUCCESS
== rv
) {
1039 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1042 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1044 PGresult
*res
= PQexec(obj
->intern
->conn
, query_str
);
1047 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1048 } else if (SUCCESS
== php_pqres_success(res TSRMLS_CC
)) {
1049 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), &return_value TSRMLS_CC
);
1054 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1059 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_get_result
, 0, 0, 0)
1060 ZEND_END_ARG_INFO();
1061 static PHP_METHOD(pqconn
, getResult
) {
1062 zend_error_handling zeh
;
1065 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1066 rv
= zend_parse_parameters_none();
1067 zend_restore_error_handling(&zeh TSRMLS_CC
);
1069 if (SUCCESS
== rv
) {
1070 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1073 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1075 PGresult
*res
= PQgetResult(obj
->intern
->conn
);
1080 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), &return_value TSRMLS_CC
);
1083 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1088 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_async
, 0, 0, 1)
1089 ZEND_ARG_INFO(0, query
)
1090 ZEND_ARG_INFO(0, callable
)
1091 ZEND_END_ARG_INFO();
1092 static PHP_METHOD(pqconn
, execAsync
) {
1093 zend_error_handling zeh
;
1094 php_pq_callback_t resolver
= {{0}};
1099 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1100 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|f", &query_str
, &query_len
, &resolver
.fci
, &resolver
.fcc
);
1101 zend_restore_error_handling(&zeh TSRMLS_CC
);
1103 if (SUCCESS
== rv
) {
1104 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1107 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1108 } else if (!PQsendQuery(obj
->intern
->conn
, query_str
)) {
1109 throw_exce(EX_IO TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1110 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1111 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1113 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver TSRMLS_CC
);
1114 obj
->intern
->poller
= PQconsumeInput
;
1115 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1120 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params
, 0, 0, 2)
1121 ZEND_ARG_INFO(0, query
)
1122 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1123 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1124 ZEND_END_ARG_INFO();
1125 static PHP_METHOD(pqconn
, execParams
) {
1126 zend_error_handling zeh
;
1130 zval
*ztypes
= NULL
;
1133 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1134 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa/|a/!", &query_str
, &query_len
, &zparams
, &ztypes
);
1135 zend_restore_error_handling(&zeh TSRMLS_CC
);
1137 if (SUCCESS
== rv
) {
1138 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1141 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1144 php_pq_params_t
*params
;
1146 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
) TSRMLS_CC
);
1147 res
= PQexecParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1148 php_pq_params_free(¶ms
);
1151 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1153 if (SUCCESS
== php_pqres_success(res TSRMLS_CC
)) {
1154 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), &return_value TSRMLS_CC
);
1159 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1165 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async
, 0, 0, 2)
1166 ZEND_ARG_INFO(0, query
)
1167 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1168 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1169 ZEND_ARG_INFO(0, callable
)
1170 ZEND_END_ARG_INFO();
1171 static PHP_METHOD(pqconn
, execParamsAsync
) {
1172 zend_error_handling zeh
;
1173 php_pq_callback_t resolver
= {{0}};
1177 zval
*ztypes
= NULL
;
1180 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1181 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa/|a/!f", &query_str
, &query_len
, &zparams
, &ztypes
, &resolver
.fci
, &resolver
.fcc
);
1182 zend_restore_error_handling(&zeh TSRMLS_CC
);
1184 if (SUCCESS
== rv
) {
1185 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1188 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1191 php_pq_params_t
*params
;
1193 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
) TSRMLS_CC
);
1194 rc
= PQsendQueryParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1195 php_pq_params_free(¶ms
);
1198 throw_exce(EX_IO TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1199 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1200 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1202 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver TSRMLS_CC
);
1203 obj
->intern
->poller
= PQconsumeInput
;
1204 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1208 zend_restore_error_handling(&zeh TSRMLS_CC
);
1211 STATUS
php_pqconn_prepare(zval
*object
, php_pqconn_object_t
*obj
, const char *name
, const char *query
, php_pq_params_t
*params TSRMLS_DC
)
1217 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1220 res
= PQprepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
);
1224 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1226 rv
= php_pqres_success(res TSRMLS_CC
);
1228 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1234 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare
, 0, 0, 2)
1235 ZEND_ARG_INFO(0, name
)
1236 ZEND_ARG_INFO(0, query
)
1237 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1238 ZEND_END_ARG_INFO();
1239 static PHP_METHOD(pqconn
, prepare
) {
1240 zend_error_handling zeh
;
1241 zval
*ztypes
= NULL
;
1242 char *name_str
, *query_str
;
1243 int name_len
, *query_len
;
1246 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1247 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1248 zend_restore_error_handling(&zeh TSRMLS_CC
);
1250 if (SUCCESS
== rv
) {
1251 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1254 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1256 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL TSRMLS_CC
);
1258 if (SUCCESS
!= php_pqconn_prepare(getThis(), obj
, name_str
, query_str
, params TSRMLS_CC
)) {
1259 php_pq_params_free(¶ms
);
1261 php_pqstm_t
*stm
= ecalloc(1, sizeof(*stm
));
1263 php_pq_object_addref(obj TSRMLS_CC
);
1265 stm
->name
= estrdup(name_str
);
1266 stm
->params
= params
;
1267 ZEND_INIT_SYMTABLE(&stm
->bound
);
1269 return_value
->type
= IS_OBJECT
;
1270 return_value
->value
.obj
= php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
, NULL TSRMLS_CC
);
1276 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
)
1281 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1284 if (!PQsendPrepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
)) {
1286 throw_exce(EX_IO TSRMLS_CC
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1287 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1289 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1292 obj
->intern
->poller
= PQconsumeInput
;
1293 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1299 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare_async
, 0, 0, 2)
1300 ZEND_ARG_INFO(0, name
)
1301 ZEND_ARG_INFO(0, query
)
1302 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1303 ZEND_END_ARG_INFO();
1304 static PHP_METHOD(pqconn
, prepareAsync
) {
1305 zend_error_handling zeh
;
1306 zval
*ztypes
= NULL
;
1307 char *name_str
, *query_str
;
1308 int name_len
, *query_len
;
1311 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1312 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1313 zend_restore_error_handling(&zeh TSRMLS_CC
);
1315 if (SUCCESS
== rv
) {
1316 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1319 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1321 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL TSRMLS_CC
);
1323 if (SUCCESS
!= php_pqconn_prepare_async(getThis(), obj
, name_str
, query_str
, params TSRMLS_CC
)) {
1324 php_pq_params_free(¶ms
);
1326 php_pqstm_t
*stm
= ecalloc(1, sizeof(*stm
));
1328 php_pq_object_addref(obj TSRMLS_CC
);
1330 stm
->name
= estrdup(name_str
);
1331 stm
->params
= params
;
1332 ZEND_INIT_SYMTABLE(&stm
->bound
);
1334 return_value
->type
= IS_OBJECT
;
1335 return_value
->value
.obj
= php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
, NULL TSRMLS_CC
);
1341 static inline char *declare_str(const char *name_str
, size_t name_len
, unsigned flags
, const char *query_str
, size_t query_len
)
1343 size_t decl_len
= name_len
+ query_len
+ sizeof("DECLARE BINARY INSENSITIVE NO SCROLL CURSOR WITHOUT HOLD FOR ");
1346 decl_str
= emalloc(decl_len
);
1347 decl_len
= slprintf(decl_str
, decl_len
, "DECLARE %s %s %s %s CURSOR %s FOR %s",
1349 (flags
& PHP_PQ_DECLARE_BINARY
) ? "BINARY" : "",
1350 (flags
& PHP_PQ_DECLARE_INSENSITIVE
) ? "INSENSITIVE" : "",
1351 (flags
& PHP_PQ_DECLARE_NO_SCROLL
) ? "NO SCROLL" :
1352 (flags
& PHP_PQ_DECLARE_SCROLL
) ? "SCROLL" : "",
1353 (flags
& PHP_PQ_DECLARE_WITH_HOLD
) ? "WITH HOLD" : "",
1359 STATUS
php_pqconn_declare(zval
*object
, php_pqconn_object_t
*obj
, const char *decl TSRMLS_DC
)
1365 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1368 res
= PQexec(obj
->intern
->conn
, decl
);
1372 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1374 rv
= php_pqres_success(res TSRMLS_CC
);
1376 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1382 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare
, 0, 0, 3)
1383 ZEND_ARG_INFO(0, name
)
1384 ZEND_ARG_INFO(0, flags
)
1385 ZEND_ARG_INFO(0, query
)
1386 ZEND_END_ARG_INFO();
1387 static PHP_METHOD(pqconn
, declare
) {
1388 zend_error_handling zeh
;
1389 char *name_str
, *query_str
;
1390 int name_len
, query_len
;
1394 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1395 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1396 zend_restore_error_handling(&zeh TSRMLS_CC
);
1398 if (SUCCESS
== rv
) {
1399 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1402 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1404 char *decl
= declare_str(name_str
, name_len
, flags
, query_str
, query_len
);
1406 if (SUCCESS
!= php_pqconn_declare(getThis(), obj
, decl TSRMLS_CC
)) {
1409 php_pqcur_t
*cur
= ecalloc(1, sizeof(*cur
));
1411 php_pq_object_addref(obj TSRMLS_CC
);
1414 cur
->name
= estrdup(name_str
);
1417 return_value
->type
= IS_OBJECT
;
1418 return_value
->value
.obj
= php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
, NULL TSRMLS_CC
);
1424 STATUS
php_pqconn_declare_async(zval
*object
, php_pqconn_object_t
*obj
, const char *decl TSRMLS_DC
)
1429 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1432 if (!PQsendQuery(obj
->intern
->conn
, decl
)) {
1434 throw_exce(EX_IO TSRMLS_CC
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1435 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1437 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1440 obj
->intern
->poller
= PQconsumeInput
;
1441 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1447 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare_async
, 0, 0, 2)
1448 ZEND_ARG_INFO(0, name
)
1449 ZEND_ARG_INFO(0, flags
)
1450 ZEND_ARG_INFO(0, query
)
1451 ZEND_END_ARG_INFO();
1452 static PHP_METHOD(pqconn
, declareAsync
) {
1453 zend_error_handling zeh
;
1454 char *name_str
, *query_str
;
1455 int name_len
, query_len
;
1459 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1460 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1461 zend_restore_error_handling(&zeh TSRMLS_CC
);
1463 if (SUCCESS
== rv
) {
1464 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1467 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1469 char *decl
= declare_str(name_str
, name_len
, flags
, query_str
, query_len
);
1471 if (SUCCESS
!= php_pqconn_declare_async(getThis(), obj
, decl TSRMLS_CC
)) {
1474 php_pqcur_t
*cur
= ecalloc(1, sizeof(*cur
));
1476 php_pq_object_addref(obj TSRMLS_CC
);
1479 cur
->name
= estrdup(name_str
);
1482 return_value
->type
= IS_OBJECT
;
1483 return_value
->value
.obj
= php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
, NULL TSRMLS_CC
);
1489 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote
, 0, 0, 1)
1490 ZEND_ARG_INFO(0, string
)
1491 ZEND_END_ARG_INFO();
1492 static PHP_METHOD(pqconn
, quote
) {
1496 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1497 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1500 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1502 char *quoted
= PQescapeLiteral(obj
->intern
->conn
, str
, len
);
1505 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to quote string (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1508 RETVAL_STRING(quoted
, 1);
1515 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote_name
, 0, 0, 1)
1516 ZEND_ARG_INFO(0, type
)
1517 ZEND_END_ARG_INFO();
1518 static PHP_METHOD(pqconn
, quoteName
) {
1522 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1523 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1526 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1528 char *quoted
= PQescapeIdentifier(obj
->intern
->conn
, str
, len
);
1531 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to quote name (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1534 RETVAL_STRING(quoted
, 1);
1541 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_escape_bytea
, 0, 0, 1)
1542 ZEND_ARG_INFO(0, bytea
)
1543 ZEND_END_ARG_INFO();
1544 static PHP_METHOD(pqconn
, escapeBytea
) {
1548 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1549 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1552 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1555 char *escaped_str
= (char *) PQescapeByteaConn(obj
->intern
->conn
, (unsigned char *) str
, len
, &escaped_len
);
1558 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to escape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1561 RETVAL_STRINGL(escaped_str
, escaped_len
- 1, 1);
1562 PQfreemem(escaped_str
);
1568 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unescape_bytea
, 0, 0, 1)
1569 ZEND_ARG_INFO(0, bytea
)
1570 ZEND_END_ARG_INFO();
1571 static PHP_METHOD(pqconn
, unescapeBytea
) {
1575 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1576 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1579 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1581 size_t unescaped_len
;
1582 char *unescaped_str
= (char *) PQunescapeBytea((unsigned char *)str
, &unescaped_len
);
1584 if (!unescaped_str
) {
1585 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to unescape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1588 RETVAL_STRINGL(unescaped_str
, unescaped_len
, 1);
1589 PQfreemem(unescaped_str
);
1595 STATUS
php_pqconn_start_transaction(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable TSRMLS_DC
)
1597 STATUS rv
= FAILURE
;
1600 conn_obj
= zend_object_store_get_object(zconn TSRMLS_CC
);
1603 if (!conn_obj
->intern
) {
1604 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1607 smart_str cmd
= {0};
1608 const char *il
= isolation_level(&isolation
);
1610 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1611 smart_str_appends(&cmd
, il
);
1612 smart_str_appends(&cmd
, ", READ ");
1613 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1614 smart_str_appends(&cmd
, ",");
1615 smart_str_appends(&cmd
, deferrable
? "" : " NOT");
1616 smart_str_appends(&cmd
, " DEFERRABLE");
1619 res
= PQexec(conn_obj
->intern
->conn
, cmd
.c
);
1622 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1624 rv
= php_pqres_success(res TSRMLS_CC
);
1626 php_pqconn_notify_listeners(conn_obj TSRMLS_CC
);
1629 smart_str_free(&cmd
);
1635 STATUS
php_pqconn_start_transaction_async(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable TSRMLS_DC
)
1637 STATUS rv
= FAILURE
;
1640 conn_obj
= zend_object_store_get_object(zconn TSRMLS_CC
);
1643 if (!conn_obj
->intern
) {
1644 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1646 smart_str cmd
= {0};
1647 const char *il
= isolation_level(&isolation
);
1649 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1650 smart_str_appends(&cmd
, il
);
1651 smart_str_appends(&cmd
, ", READ ");
1652 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1653 smart_str_appends(&cmd
, ",");
1654 smart_str_appends(&cmd
, deferrable
? "" : "NOT ");
1655 smart_str_appends(&cmd
, " DEFERRABLE");
1658 if (!PQsendQuery(conn_obj
->intern
->conn
, cmd
.c
)) {
1659 throw_exce(EX_IO TSRMLS_CC
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1662 conn_obj
->intern
->poller
= PQconsumeInput
;
1663 php_pqconn_notify_listeners(conn_obj TSRMLS_CC
);
1666 smart_str_free(&cmd
);
1672 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction
, 0, 0, 0)
1673 ZEND_ARG_INFO(0, isolation
)
1674 ZEND_ARG_INFO(0, readonly
)
1675 ZEND_ARG_INFO(0, deferrable
)
1676 ZEND_END_ARG_INFO();
1677 static PHP_METHOD(pqconn
, startTransaction
) {
1678 zend_error_handling zeh
;
1679 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1680 long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1681 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1682 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1685 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1686 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lbb", &isolation
, &readonly
, &deferrable
);
1687 zend_restore_error_handling(&zeh TSRMLS_CC
);
1689 if (SUCCESS
== rv
) {
1690 rv
= php_pqconn_start_transaction(getThis(), obj
, isolation
, readonly
, deferrable TSRMLS_CC
);
1692 if (SUCCESS
== rv
) {
1693 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1695 php_pq_object_addref(obj TSRMLS_CC
);
1698 txn
->isolation
= isolation
;
1699 txn
->readonly
= readonly
;
1700 txn
->deferrable
= deferrable
;
1702 return_value
->type
= IS_OBJECT
;
1703 return_value
->value
.obj
= php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
, NULL TSRMLS_CC
);
1708 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction_async
, 0, 0, 0)
1709 ZEND_ARG_INFO(0, isolation
)
1710 ZEND_ARG_INFO(0, readonly
)
1711 ZEND_ARG_INFO(0, deferrable
)
1712 ZEND_END_ARG_INFO();
1713 static PHP_METHOD(pqconn
, startTransactionAsync
) {
1714 zend_error_handling zeh
;
1715 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1716 long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1717 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1718 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1721 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1722 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lbb", &isolation
, &readonly
, &deferrable
);
1723 zend_restore_error_handling(&zeh TSRMLS_CC
);
1725 if (SUCCESS
== rv
) {
1726 rv
= php_pqconn_start_transaction_async(getThis(), obj
, isolation
, readonly
, deferrable TSRMLS_CC
);
1728 if (SUCCESS
== rv
) {
1729 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1731 php_pq_object_addref(obj TSRMLS_CC
);
1733 txn
->isolation
= isolation
;
1734 txn
->readonly
= readonly
;
1735 txn
->deferrable
= deferrable
;
1737 return_value
->type
= IS_OBJECT
;
1738 return_value
->value
.obj
= php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
, NULL TSRMLS_CC
);
1743 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_trace
, 0, 0, 0)
1744 ZEND_ARG_INFO(0, stdio_stream
)
1745 ZEND_END_ARG_INFO();
1746 static PHP_METHOD(pqconn
, trace
) {
1747 zval
*zstream
= NULL
;
1749 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|r!", &zstream
)) {
1750 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1753 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1756 PQuntrace(obj
->intern
->conn
);
1760 php_stream
*stream
= NULL
;
1762 php_stream_from_zval(stream
, &zstream
);
1764 if (SUCCESS
!= php_stream_cast(stream
, PHP_STREAM_AS_STDIO
, (void *) &fp
, REPORT_ERRORS
)) {
1767 stream
->flags
|= PHP_STREAM_FLAG_NO_CLOSE
;
1768 PQtrace(obj
->intern
->conn
, fp
);
1776 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_off
, 0, 0, 1)
1777 ZEND_ARG_INFO(0, type
)
1778 ZEND_END_ARG_INFO();
1779 static PHP_METHOD(pqconn
, off
) {
1780 zend_error_handling zeh
;
1785 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1786 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &type_str
, &type_len
);
1787 zend_restore_error_handling(&zeh TSRMLS_CC
);
1789 if (SUCCESS
== rv
) {
1790 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1793 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1795 RETURN_BOOL(SUCCESS
== zend_hash_del(&obj
->intern
->eventhandlers
, type_str
, type_len
+ 1));
1800 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_on
, 0, 0, 2)
1801 ZEND_ARG_INFO(0, type
)
1802 ZEND_ARG_INFO(0, callable
)
1803 ZEND_END_ARG_INFO();
1804 static PHP_METHOD(pqconn
, on
) {
1805 zend_error_handling zeh
;
1808 php_pq_callback_t cb
= {{0}};
1811 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1812 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sf", &type_str
, &type_len
, &cb
.fci
, &cb
.fcc
);
1813 zend_restore_error_handling(&zeh TSRMLS_CC
);
1815 if (SUCCESS
== rv
) {
1816 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1819 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1821 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1823 RETVAL_LONG(php_pqconn_add_eventhandler(obj
, type_str
, type_len
, &cb TSRMLS_CC
));
1828 static int apply_set_converter(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
1830 zval
*tmp
, **zoid
= p
, **zcnv
= va_arg(argv
, zval
**);
1831 HashTable
*converters
= va_arg(argv
, HashTable
*);
1832 int add
= va_arg(argv
, int);
1836 convert_to_long_ex(&tmp
);
1839 zend_hash_index_update(converters
, Z_LVAL_P(tmp
), zcnv
, sizeof(zval
*), NULL
);
1841 zend_hash_index_del(converters
, Z_LVAL_P(tmp
));
1843 zval_ptr_dtor(&tmp
);
1845 return ZEND_HASH_APPLY_KEEP
;
1848 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_set_converter
, 0, 0, 1)
1849 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\ConverterInterface
, 0)
1850 ZEND_END_ARG_INFO();
1851 static PHP_METHOD(pqconn
, setConverter
) {
1853 zend_error_handling zeh
;
1856 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1857 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zcnv
, php_pqconv_class_entry
);
1858 zend_restore_error_handling(&zeh TSRMLS_CC
);
1860 if (SUCCESS
== rv
) {
1861 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1864 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1866 zval
*tmp
, *zoids
= NULL
;
1868 zend_call_method_with_0_params(&zcnv
, NULL
, NULL
, "converttypes", &zoids
);
1871 convert_to_array_ex(&tmp
);
1872 zend_hash_apply_with_arguments(Z_ARRVAL_P(tmp
) TSRMLS_CC
, apply_set_converter
, 3, &zcnv
, &obj
->intern
->converters
, 1);
1873 zval_ptr_dtor(&tmp
);
1874 zval_ptr_dtor(&zoids
);
1879 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unset_converter
, 0, 0, 1)
1880 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\ConverterInterface
, 0)
1881 ZEND_END_ARG_INFO();
1882 static PHP_METHOD(pqconn
, unsetConverter
) {
1884 zend_error_handling zeh
;
1887 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1888 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zcnv
, php_pqconv_class_entry
);
1889 zend_restore_error_handling(&zeh TSRMLS_CC
);
1891 if (SUCCESS
== rv
) {
1892 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1895 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1897 zval
*tmp
, *zoids
= NULL
;
1899 zend_call_method_with_0_params(&zcnv
, NULL
, NULL
, "converttypes", &zoids
);
1902 convert_to_array_ex(&tmp
);
1903 zend_hash_apply_with_arguments(Z_ARRVAL_P(tmp
) TSRMLS_CC
, apply_set_converter
, 3, &zcnv
, &obj
->intern
->converters
, 0);
1904 zval_ptr_dtor(&tmp
);
1905 zval_ptr_dtor(&zoids
);
1910 static zend_function_entry php_pqconn_methods
[] = {
1911 PHP_ME(pqconn
, __construct
, ai_pqconn_construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
1912 PHP_ME(pqconn
, reset
, ai_pqconn_reset
, ZEND_ACC_PUBLIC
)
1913 PHP_ME(pqconn
, resetAsync
, ai_pqconn_reset_async
, ZEND_ACC_PUBLIC
)
1914 PHP_ME(pqconn
, poll
, ai_pqconn_poll
, ZEND_ACC_PUBLIC
)
1915 PHP_ME(pqconn
, exec
, ai_pqconn_exec
, ZEND_ACC_PUBLIC
)
1916 PHP_ME(pqconn
, execAsync
, ai_pqconn_exec_async
, ZEND_ACC_PUBLIC
)
1917 PHP_ME(pqconn
, execParams
, ai_pqconn_exec_params
, ZEND_ACC_PUBLIC
)
1918 PHP_ME(pqconn
, execParamsAsync
, ai_pqconn_exec_params_async
, ZEND_ACC_PUBLIC
)
1919 PHP_ME(pqconn
, prepare
, ai_pqconn_prepare
, ZEND_ACC_PUBLIC
)
1920 PHP_ME(pqconn
, prepareAsync
, ai_pqconn_prepare_async
, ZEND_ACC_PUBLIC
)
1921 PHP_ME(pqconn
, declare
, ai_pqconn_declare
, ZEND_ACC_PUBLIC
)
1922 PHP_ME(pqconn
, declareAsync
, ai_pqconn_declare_async
, ZEND_ACC_PUBLIC
)
1923 PHP_ME(pqconn
, unlisten
, ai_pqconn_unlisten
, ZEND_ACC_PUBLIC
)
1924 PHP_ME(pqconn
, unlistenAsync
, ai_pqconn_unlisten_async
, ZEND_ACC_PUBLIC
)
1925 PHP_ME(pqconn
, listen
, ai_pqconn_listen
, ZEND_ACC_PUBLIC
)
1926 PHP_ME(pqconn
, listenAsync
, ai_pqconn_listen_async
, ZEND_ACC_PUBLIC
)
1927 PHP_ME(pqconn
, notify
, ai_pqconn_notify
, ZEND_ACC_PUBLIC
)
1928 PHP_ME(pqconn
, notifyAsync
, ai_pqconn_notify_async
, ZEND_ACC_PUBLIC
)
1929 PHP_ME(pqconn
, getResult
, ai_pqconn_get_result
, ZEND_ACC_PUBLIC
)
1930 PHP_ME(pqconn
, quote
, ai_pqconn_quote
, ZEND_ACC_PUBLIC
)
1931 PHP_ME(pqconn
, quoteName
, ai_pqconn_quote_name
, ZEND_ACC_PUBLIC
)
1932 PHP_ME(pqconn
, escapeBytea
, ai_pqconn_escape_bytea
, ZEND_ACC_PUBLIC
)
1933 PHP_ME(pqconn
, unescapeBytea
, ai_pqconn_unescape_bytea
, ZEND_ACC_PUBLIC
)
1934 PHP_ME(pqconn
, startTransaction
, ai_pqconn_start_transaction
, ZEND_ACC_PUBLIC
)
1935 PHP_ME(pqconn
, startTransactionAsync
, ai_pqconn_start_transaction_async
, ZEND_ACC_PUBLIC
)
1936 PHP_ME(pqconn
, trace
, ai_pqconn_trace
, ZEND_ACC_PUBLIC
)
1937 PHP_ME(pqconn
, off
, ai_pqconn_off
, ZEND_ACC_PUBLIC
)
1938 PHP_ME(pqconn
, on
, ai_pqconn_on
, ZEND_ACC_PUBLIC
)
1939 PHP_ME(pqconn
, setConverter
, ai_pqconn_set_converter
, ZEND_ACC_PUBLIC
)
1940 PHP_ME(pqconn
, unsetConverter
, ai_pqconn_unset_converter
, ZEND_ACC_PUBLIC
)
1944 PHP_MSHUTDOWN_FUNCTION(pqconn
)
1946 zend_hash_destroy(&php_pqconn_object_prophandlers
);
1950 PHP_MINIT_FUNCTION(pqconn
)
1952 zend_class_entry ce
= {0};
1953 php_pq_object_prophandler_t ph
= {0};
1955 INIT_NS_CLASS_ENTRY(ce
, "pq", "Connection", php_pqconn_methods
);
1956 php_pqconn_class_entry
= zend_register_internal_class_ex(&ce
, NULL
, NULL TSRMLS_CC
);
1957 php_pqconn_class_entry
->create_object
= php_pqconn_create_object
;
1959 memcpy(&php_pqconn_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
1960 php_pqconn_object_handlers
.read_property
= php_pq_object_read_prop
;
1961 php_pqconn_object_handlers
.write_property
= php_pq_object_write_prop
;
1962 php_pqconn_object_handlers
.clone_obj
= NULL
;
1963 php_pqconn_object_handlers
.get_property_ptr_ptr
= NULL
;
1964 php_pqconn_object_handlers
.get_gc
= NULL
;
1965 php_pqconn_object_handlers
.get_properties
= php_pq_object_properties
;
1966 php_pqconn_object_handlers
.get_debug_info
= php_pq_object_debug_info
;
1968 zend_hash_init(&php_pqconn_object_prophandlers
, 14, NULL
, NULL
, 1);
1970 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("status"), CONNECTION_BAD
, ZEND_ACC_PUBLIC TSRMLS_CC
);
1971 ph
.read
= php_pqconn_object_read_status
;
1972 zend_hash_add(&php_pqconn_object_prophandlers
, "status", sizeof("status"), (void *) &ph
, sizeof(ph
), NULL
);
1974 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("transactionStatus"), PQTRANS_UNKNOWN
, ZEND_ACC_PUBLIC TSRMLS_CC
);
1975 ph
.read
= php_pqconn_object_read_transaction_status
;
1976 zend_hash_add(&php_pqconn_object_prophandlers
, "transactionStatus", sizeof("transactionStatus"), (void *) &ph
, sizeof(ph
), NULL
);
1978 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("socket"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1979 ph
.read
= NULL
; /* forward to std prophandler */
1980 zend_hash_add(&php_pqconn_object_prophandlers
, "socket", sizeof("socket"), (void *) &ph
, sizeof(ph
), NULL
);
1982 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("errorMessage"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1983 ph
.read
= php_pqconn_object_read_error_message
;
1984 zend_hash_add(&php_pqconn_object_prophandlers
, "errorMessage", sizeof("errorMessage"), (void *) &ph
, sizeof(ph
), NULL
);
1986 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("busy"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
1987 ph
.read
= php_pqconn_object_read_busy
;
1988 zend_hash_add(&php_pqconn_object_prophandlers
, "busy", sizeof("busy"), (void *) &ph
, sizeof(ph
), NULL
);
1990 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("encoding"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1991 ph
.read
= php_pqconn_object_read_encoding
;
1992 ph
.write
= php_pqconn_object_write_encoding
;
1993 zend_hash_add(&php_pqconn_object_prophandlers
, "encoding", sizeof("encoding"), (void *) &ph
, sizeof(ph
), NULL
);
1996 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("unbuffered"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
1997 ph
.read
= php_pqconn_object_read_unbuffered
;
1998 ph
.write
= php_pqconn_object_write_unbuffered
;
1999 zend_hash_add(&php_pqconn_object_prophandlers
, "unbuffered", sizeof("unbuffered"), (void *) &ph
, sizeof(ph
), NULL
);
2002 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("db"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2003 ph
.read
= php_pqconn_object_read_db
;
2004 zend_hash_add(&php_pqconn_object_prophandlers
, "db", sizeof("db"), (void *) &ph
, sizeof(ph
), NULL
);
2006 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("user"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2007 ph
.read
= php_pqconn_object_read_user
;
2008 zend_hash_add(&php_pqconn_object_prophandlers
, "user", sizeof("user"), (void *) &ph
, sizeof(ph
), NULL
);
2010 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("pass"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2011 ph
.read
= php_pqconn_object_read_pass
;
2012 zend_hash_add(&php_pqconn_object_prophandlers
, "pass", sizeof("pass"), (void *) &ph
, sizeof(ph
), NULL
);
2014 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("host"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2015 ph
.read
= php_pqconn_object_read_host
;
2016 zend_hash_add(&php_pqconn_object_prophandlers
, "host", sizeof("host"), (void *) &ph
, sizeof(ph
), NULL
);
2018 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("port"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2019 ph
.read
= php_pqconn_object_read_port
;
2020 zend_hash_add(&php_pqconn_object_prophandlers
, "port", sizeof("port"), (void *) &ph
, sizeof(ph
), NULL
);
2022 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("options"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2023 ph
.read
= php_pqconn_object_read_options
;
2024 zend_hash_add(&php_pqconn_object_prophandlers
, "options", sizeof("options"), (void *) &ph
, sizeof(ph
), NULL
);
2026 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("eventHandlers"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2027 ph
.read
= php_pqconn_object_read_event_handlers
;
2028 zend_hash_add(&php_pqconn_object_prophandlers
, "eventHandlers", sizeof("eventHandlers"), (void *) &ph
, sizeof(ph
), NULL
);
2030 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultFetchType"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
2031 ph
.read
= php_pqconn_object_read_def_fetch_type
;
2032 ph
.write
= php_pqconn_object_write_def_fetch_type
;
2033 zend_hash_add(&php_pqconn_object_prophandlers
, "defaultFetchType", sizeof("defaultFetchType"), (void *) &ph
, sizeof(ph
), NULL
);
2036 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionIsolation"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
2037 ph
.read
= php_pqconn_object_read_def_txn_isolation
;
2038 ph
.write
= php_pqconn_object_write_def_txn_isolation
;
2039 zend_hash_add(&php_pqconn_object_prophandlers
, "defaultTransactionIsolation", sizeof("defaultTransactionIsolation"), (void *) &ph
, sizeof(ph
), NULL
);
2042 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionReadonly"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
2043 ph
.read
= php_pqconn_object_read_def_txn_readonly
;
2044 ph
.write
= php_pqconn_object_write_def_txn_readonly
;
2045 zend_hash_add(&php_pqconn_object_prophandlers
, "defaultTransactionReadonly", sizeof("defaultTransactionReadonly"), (void *) &ph
, sizeof(ph
), NULL
);
2048 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionDeferrable"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
2049 ph
.read
= php_pqconn_object_read_def_txn_deferrable
;
2050 ph
.write
= php_pqconn_object_write_def_txn_deferrable
;
2051 zend_hash_add(&php_pqconn_object_prophandlers
, "defaultTransactionDeferrable", sizeof("defaultTransactionDeferrable"), (void *) &ph
, sizeof(ph
), NULL
);
2054 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultAutoConvert"), PHP_PQRES_CONV_ALL
, ZEND_ACC_PUBLIC TSRMLS_CC
);
2055 ph
.read
= php_pqconn_object_read_def_auto_conv
;
2056 ph
.write
= php_pqconn_object_write_def_auto_conv
;
2057 zend_hash_add(&php_pqconn_object_prophandlers
, "defaultAutoConvert", sizeof("defaultAutoConvert"), (void *) &ph
, sizeof(ph
), NULL
);
2060 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("OK"), CONNECTION_OK TSRMLS_CC
);
2061 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("BAD"), CONNECTION_BAD TSRMLS_CC
);
2062 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("STARTED"), CONNECTION_STARTED TSRMLS_CC
);
2063 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("MADE"), CONNECTION_MADE TSRMLS_CC
);
2064 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AWAITING_RESPONSE"), CONNECTION_AWAITING_RESPONSE TSRMLS_CC
);
2065 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AUTH_OK"), CONNECTION_AUTH_OK TSRMLS_CC
);
2066 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SSL_STARTUP"), CONNECTION_SSL_STARTUP TSRMLS_CC
);
2067 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SETENV"), CONNECTION_SETENV TSRMLS_CC
);
2069 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_IDLE"), PQTRANS_IDLE TSRMLS_CC
);
2070 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_ACTIVE"), PQTRANS_ACTIVE TSRMLS_CC
);
2071 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INTRANS"), PQTRANS_INTRANS TSRMLS_CC
);
2072 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INERROR"), PQTRANS_INERROR TSRMLS_CC
);
2073 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_UNKNOWN"), PQTRANS_UNKNOWN TSRMLS_CC
);
2075 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_FAILED"), PGRES_POLLING_FAILED TSRMLS_CC
);
2076 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_READING"), PGRES_POLLING_READING TSRMLS_CC
);
2077 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_WRITING"), PGRES_POLLING_WRITING TSRMLS_CC
);
2078 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_OK"), PGRES_POLLING_OK TSRMLS_CC
);
2080 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_NOTICE"), ZEND_STRL("notice") TSRMLS_CC
);
2081 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESULT"), ZEND_STRL("result") TSRMLS_CC
);
2082 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESET"), ZEND_STRL("reset") TSRMLS_CC
);
2084 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("ASYNC"), 0x1 TSRMLS_CC
);
2085 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("PERSISTENT"), 0x2 TSRMLS_CC
);
2095 * vim600: noet sw=4 ts=4 fdm=marker
2096 * vim<600: noet sw=4 ts=4