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 +--------------------------------------------------------------------+
18 #include <Zend/zend_smart_str.h>
20 #include <libpq-events.h>
24 #include "php_pq_misc.h"
25 #include "php_pq_object.h"
26 #include "php_pqexc.h"
27 #include "php_pqconn.h"
28 #include "php_pqconn_event.h"
29 #include "php_pqres.h"
30 #include "php_pqstm.h"
31 #include "php_pqtxn.h"
32 #include "php_pqcur.h"
34 zend_class_entry
*php_pqconn_class_entry
;
35 static zend_object_handlers php_pqconn_object_handlers
;
36 static HashTable php_pqconn_object_prophandlers
;
38 static void php_pq_callback_hash_dtor(zval
*p
)
40 php_pq_callback_dtor(Z_PTR_P(p
));
45 static void php_pqconn_del_eventhandler(php_pqconn_object_t *obj, const char *type_str, size_t type_len, unsigned long id)
49 if (SUCCESS == zend_hash_find(&obj->intern->eventhandlers, type_str, type_len + 1, (void *) &evhs)) {
50 zend_hash_index_del(Z_ARRVAL_PP(evhs), id);
55 static zend_long
php_pqconn_add_eventhandler(php_pqconn_object_t
*obj
, const char *type_str
, size_t type_len
, php_pq_callback_t
*cb
)
60 if (!(zevhs
= zend_hash_str_find(&obj
->intern
->eventhandlers
, type_str
, type_len
))) {
64 ALLOC_HASHTABLE(evhs
);
65 zend_hash_init(evhs
, 1, NULL
, php_pq_callback_hash_dtor
, 0);
68 zevhs
= zend_hash_str_add(&obj
->intern
->eventhandlers
, type_str
, type_len
, &tmp
);
71 php_pq_callback_addref(cb
);
72 h
= zend_hash_next_free_element(Z_ARRVAL_P(zevhs
));
73 zend_hash_index_update_mem(Z_ARRVAL_P(zevhs
), h
, (void *) cb
, sizeof(*cb
));
78 static void php_pqconn_object_free(zend_object
*o
)
80 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(NULL
, o
);
82 fprintf(stderr
, "FREE conn(#%d) %p\n", obj
->zo
.handle
, obj
);
85 php_pq_callback_dtor(&obj
->intern
->onevent
);
86 php_resource_factory_handle_dtor(&obj
->intern
->factory
, obj
->intern
->conn
);
87 php_resource_factory_dtor(&obj
->intern
->factory
);
88 zend_hash_destroy(&obj
->intern
->listeners
);
89 zend_hash_destroy(&obj
->intern
->statements
);
90 zend_hash_destroy(&obj
->intern
->converters
);
91 zend_hash_destroy(&obj
->intern
->eventhandlers
);
95 php_pq_object_dtor(o
);
99 php_pqconn_object_t
*php_pqconn_create_object_ex(zend_class_entry
*ce
, php_pqconn_t
*intern
)
101 return php_pq_object_create(ce
, intern
, sizeof(php_pqconn_object_t
),
102 &php_pqconn_object_handlers
, &php_pqconn_object_prophandlers
);
105 static zend_object
*php_pqconn_create_object(zend_class_entry
*class_type
)
107 return &php_pqconn_create_object_ex(class_type
, NULL
)->zo
;
110 static void php_pqconn_object_read_status(void *o
, zval
*return_value
)
112 php_pqconn_object_t
*obj
= o
;
114 RETVAL_LONG(PQstatus(obj
->intern
->conn
));
117 static void php_pqconn_object_read_transaction_status(void *o
, zval
*return_value
)
119 php_pqconn_object_t
*obj
= o
;
121 RETVAL_LONG(PQtransactionStatus(obj
->intern
->conn
));
124 static void php_pqconn_object_read_error_message(void *o
, zval
*return_value
)
126 php_pqconn_object_t
*obj
= o
;
127 char *error
= PHP_PQerrorMessage(obj
->intern
->conn
);
130 RETVAL_STRING(error
);
136 static int apply_notify_listener(zval
*p
, void *arg
)
138 php_pq_callback_t
*listener
= Z_PTR_P(p
);
140 zval zpid
, zchannel
, zmessage
;
142 ZVAL_LONG(&zpid
, nfy
->be_pid
);
143 ZVAL_STRING(&zchannel
, nfy
->relname
);
144 ZVAL_STRING(&zmessage
, nfy
->extra
);
146 zend_fcall_info_argn(&listener
->fci
, 3, &zchannel
, &zmessage
, &zpid
);
147 zend_fcall_info_call(&listener
->fci
, &listener
->fcc
, NULL
, NULL
);
148 zend_fcall_info_args_clear(&listener
->fci
, 0);
150 zval_ptr_dtor(&zchannel
);
151 zval_ptr_dtor(&zmessage
);
152 zval_ptr_dtor(&zpid
);
154 return ZEND_HASH_APPLY_KEEP
;
157 static int apply_notify_listeners(zval
*p
, int argc
, va_list argv
, zend_hash_key
*key
)
159 HashTable
*listeners
= Z_ARRVAL_P(p
);
160 PGnotify
*nfy
= va_arg(argv
, PGnotify
*);
162 if (0 == fnmatch(key
->key
->val
, nfy
->relname
, 0)) {
163 zend_hash_apply_with_argument(listeners
, apply_notify_listener
, nfy
);
166 return ZEND_HASH_APPLY_KEEP
;
169 void php_pqconn_notify_listeners(php_pqconn_object_t
*obj
)
173 while ((nfy
= PQnotifies(obj
->intern
->conn
))) {
174 zend_hash_apply_with_arguments(&obj
->intern
->listeners
, apply_notify_listeners
, 1, nfy
);
179 static void php_pqconn_object_read_busy(void *o
, zval
*return_value
)
181 php_pqconn_object_t
*obj
= o
;
183 RETVAL_BOOL(PQisBusy(obj
->intern
->conn
));
186 static void php_pqconn_object_read_encoding(void *o
, zval
*return_value
)
188 php_pqconn_object_t
*obj
= o
;
190 RETVAL_STRING(pg_encoding_to_char(PQclientEncoding(obj
->intern
->conn
)));
193 static void php_pqconn_object_write_encoding(void *o
, zval
*value
)
195 php_pqconn_object_t
*obj
= o
;
196 zend_string
*zenc
= zval_get_string(value
);
198 if (0 > PQsetClientEncoding(obj
->intern
->conn
, zenc
->val
)) {
199 php_error(E_NOTICE
, "Unrecognized encoding '%s'", zenc
->val
);
202 zend_string_release(zenc
);
205 static void php_pqconn_object_read_unbuffered(void *o
, zval
*return_value
)
207 php_pqconn_object_t
*obj
= o
;
209 RETVAL_BOOL(obj
->intern
->unbuffered
);
212 static void php_pqconn_object_write_unbuffered(void *o
, zval
*value
)
214 php_pqconn_object_t
*obj
= o
;
216 obj
->intern
->unbuffered
= z_is_true(value
);
219 static void php_pqconn_object_read_nonblocking(void *o
, zval
*return_value
)
221 php_pqconn_object_t
*obj
= o
;
223 RETVAL_BOOL(PQisnonblocking(obj
->intern
->conn
));
226 static void php_pqconn_object_write_nonblocking(void *o
, zval
*value
)
228 php_pqconn_object_t
*obj
= o
;
230 PQsetnonblocking(obj
->intern
->conn
, z_is_true(value
));
233 static void php_pqconn_object_read_db(void *o
, zval
*return_value
)
235 php_pqconn_object_t
*obj
= o
;
236 char *db
= PQdb(obj
->intern
->conn
);
241 RETVAL_EMPTY_STRING();
245 static void php_pqconn_object_read_user(void *o
, zval
*return_value
)
247 php_pqconn_object_t
*obj
= o
;
248 char *user
= PQuser(obj
->intern
->conn
);
253 RETVAL_EMPTY_STRING();
257 static void php_pqconn_object_read_pass(void *o
, zval
*return_value
)
259 php_pqconn_object_t
*obj
= o
;
260 char *pass
= PQpass(obj
->intern
->conn
);
265 RETVAL_EMPTY_STRING();
269 static void php_pqconn_object_read_host(void *o
, zval
*return_value
)
271 php_pqconn_object_t
*obj
= o
;
272 char *host
= PQhost(obj
->intern
->conn
);
277 RETVAL_EMPTY_STRING();
281 static void php_pqconn_object_read_port(void *o
, zval
*return_value
)
283 php_pqconn_object_t
*obj
= o
;
284 char *port
= PQport(obj
->intern
->conn
);
289 RETVAL_EMPTY_STRING();
294 static void php_pqconn_object_read_params(void *o
, zval
*return_value
)
296 php_pqconn_object_t
*obj
= o
;
297 PQconninfoOption
*ptr
, *params
= PQconninfo(obj
->intern
->conn
);
299 array_init(return_value
);
302 for (ptr
= params
; ptr
->keyword
; ++ptr
) {
304 add_assoc_string(return_value
, ptr
->keyword
, ptr
->val
);
306 add_assoc_null(return_value
, ptr
->keyword
);
309 PQconninfoFree(params
);
314 static void php_pqconn_object_read_options(void *o
, zval
*return_value
)
316 php_pqconn_object_t
*obj
= o
;
317 char *options
= PQoptions(obj
->intern
->conn
);
320 RETVAL_STRING(options
);
322 RETVAL_EMPTY_STRING();
326 static int apply_read_callback_ex(zval
*p
, void *arg
)
331 zend_hash_next_index_insert(rv
, php_pq_callback_to_zval(Z_PTR_P(p
), &zcb
));
333 return ZEND_HASH_APPLY_KEEP
;
336 static int apply_read_callbacks(zval
*p
, int argc
, va_list argv
, zend_hash_key
*key
)
338 HashTable
*evhs
= Z_ARRVAL_P(p
), *rv
= va_arg(argv
, HashTable
*);
339 zval entry
, *entry_ptr
;
341 array_init_size(&entry
, zend_hash_num_elements(evhs
));
344 entry_ptr
= zend_hash_add(rv
, key
->key
, &entry
);
346 entry_ptr
= zend_hash_index_update(rv
, key
->h
, &entry
);
349 zend_hash_apply_with_argument(evhs
, apply_read_callback_ex
, Z_ARRVAL_P(entry_ptr
));
351 return ZEND_HASH_APPLY_KEEP
;
353 static void php_pqconn_object_read_event_handlers(void *o
, zval
*return_value
)
355 php_pqconn_object_t
*obj
= o
;
357 array_init(return_value
);
358 zend_hash_apply_with_arguments(&obj
->intern
->eventhandlers
, apply_read_callbacks
, 1, Z_ARRVAL_P(return_value
));
361 static void php_pqconn_object_gc_event_handlers(void *o
, zval
*return_value
)
363 php_pqconn_object_t
*obj
= o
;
366 ZEND_HASH_FOREACH_VAL(&obj
->intern
->eventhandlers
, evhs
)
370 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(evhs
), evh
)
374 add_next_index_zval(return_value
, php_pq_callback_to_zval_no_addref(Z_PTR_P(evh
), &zcb
));
376 ZEND_HASH_FOREACH_END();
378 ZEND_HASH_FOREACH_END();
381 static void php_pqconn_object_read_listeners(void *o
, zval
*return_value
)
383 php_pqconn_object_t
*obj
= o
;
385 array_init(return_value
);
386 zend_hash_apply_with_arguments(&obj
->intern
->listeners
, apply_read_callbacks
, 1, Z_ARRVAL_P(return_value
));
389 static void php_pqconn_object_gc_listeners(void *o
, zval
*return_value
)
391 php_pqconn_object_t
*obj
= o
;
394 ZEND_HASH_FOREACH_VAL(&obj
->intern
->listeners
, listeners
)
398 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(listeners
), listener
)
402 add_next_index_zval(return_value
, php_pq_callback_to_zval_no_addref(Z_PTR_P(listener
), &zcb
));
404 ZEND_HASH_FOREACH_END();
406 ZEND_HASH_FOREACH_END();
409 static void php_pqconn_object_read_converters(void *o
, zval
*return_value
)
411 php_pqconn_object_t
*obj
= o
;
413 array_init(return_value
);
414 zend_hash_copy(Z_ARRVAL_P(return_value
), &obj
->intern
->converters
, zval_add_ref
);
417 static void php_pqconn_object_gc_converters(void *o
, zval
*return_value
)
419 php_pqconn_object_t
*obj
= o
;
422 ZEND_HASH_FOREACH_VAL(&obj
->intern
->converters
, converter
)
424 add_next_index_zval(return_value
, converter
);
426 ZEND_HASH_FOREACH_END();
429 static void php_pqconn_object_read_def_fetch_type(void *o
, zval
*return_value
)
431 php_pqconn_object_t
*obj
= o
;
433 RETVAL_LONG(obj
->intern
->default_fetch_type
);
435 static void php_pqconn_object_write_def_fetch_type(void *o
, zval
*value
)
437 php_pqconn_object_t
*obj
= o
;
439 obj
->intern
->default_fetch_type
= zval_get_long(value
) & 0x3; /* two bits only */
442 static void php_pqconn_object_read_def_txn_isolation(void *o
, zval
*return_value
)
444 php_pqconn_object_t
*obj
= o
;
446 RETVAL_LONG(obj
->intern
->default_txn_isolation
);
448 static void php_pqconn_object_write_def_txn_isolation(void *o
, zval
*value
)
450 php_pqconn_object_t
*obj
= o
;
452 obj
->intern
->default_txn_isolation
= zval_get_long(value
) & 0x3; /* two bits only */
455 static void php_pqconn_object_read_def_txn_readonly(void *o
, zval
*return_value
)
457 php_pqconn_object_t
*obj
= o
;
459 RETVAL_BOOL(obj
->intern
->default_txn_readonly
);
461 static void php_pqconn_object_write_def_txn_readonly(void *o
, zval
*value
)
463 php_pqconn_object_t
*obj
= o
;
465 obj
->intern
->default_txn_readonly
= z_is_true(value
);
468 static void php_pqconn_object_read_def_txn_deferrable(void *o
, zval
*return_value
)
470 php_pqconn_object_t
*obj
= o
;
472 RETVAL_BOOL(obj
->intern
->default_txn_deferrable
);
474 static void php_pqconn_object_write_def_txn_deferrable(void *o
, zval
*value
)
476 php_pqconn_object_t
*obj
= o
;
478 obj
->intern
->default_txn_deferrable
= zend_is_true(value
);
481 static void php_pqconn_object_read_def_auto_conv(void *o
, zval
*return_value
)
483 php_pqconn_object_t
*obj
= o
;
485 RETVAL_LONG(obj
->intern
->default_auto_convert
);
487 static void php_pqconn_object_write_def_auto_conv(void *o
, zval
*value
)
489 php_pqconn_object_t
*obj
= o
;
491 obj
->intern
->default_auto_convert
= zval_get_long(value
) & PHP_PQRES_CONV_ALL
;
494 static ZEND_RESULT_CODE
php_pqconn_update_socket(zval
*zobj
, php_pqconn_object_t
*obj
)
496 zval zsocket
, zmember
;
498 ZEND_RESULT_CODE retval
;
502 obj
= PHP_PQ_OBJ(zobj
, NULL
);
505 ZVAL_STRINGL(&zmember
, "socket", sizeof("socket")-1);
507 if ((CONNECTION_BAD
!= PQstatus(obj
->intern
->conn
))
508 && (-1 < (socket
= PQsocket(obj
->intern
->conn
)))
509 && (stream
= php_stream_fopen_from_fd(socket
, "r+b", NULL
))) {
510 stream
->flags
|= PHP_STREAM_FLAG_NO_CLOSE
;
511 php_stream_to_zval(stream
, &zsocket
);
517 #if PHP_VERSION_ID >= 80000
518 zend_std_write_property(Z_OBJ_P(zobj
), Z_STR(zmember
), &zsocket
, NULL
);
520 zend_std_write_property(zobj
, &zmember
, &zsocket
, NULL
);
522 zval_ptr_dtor(&zsocket
);
523 zval_ptr_dtor(&zmember
);
528 static void *php_pqconn_resource_factory_ctor(void *data
, void *init_arg
)
530 php_pqconn_resource_factory_data_t
*o
= init_arg
;
531 PGconn
*conn
= NULL
;;
533 if (o
->flags
& PHP_PQCONN_ASYNC
) {
534 conn
= PQconnectStart(o
->dsn
);
536 conn
= PQconnectdb(o
->dsn
);
540 PQregisterEventProc(conn
, php_pqconn_event
, "ext-pq", NULL
);
546 static void php_pqconn_resource_factory_dtor(void *opaque
, void *handle
)
548 php_pqconn_event_data_t
*evdata
= PQinstanceData(handle
, php_pqconn_event
);
550 /* we don't care for anything, except free'ing evdata */
552 PQsetInstanceData(handle
, php_pqconn_event
, NULL
);
553 memset(evdata
, 0, sizeof(*evdata
));
560 static php_resource_factory_ops_t php_pqconn_resource_factory_ops
= {
561 php_pqconn_resource_factory_ctor
,
563 php_pqconn_resource_factory_dtor
566 php_resource_factory_ops_t
*php_pqconn_get_resource_factory_ops(void)
568 return &php_pqconn_resource_factory_ops
;
571 static void php_pqconn_wakeup(php_persistent_handle_factory_t
*f
, void **handle
)
573 PGresult
*res
= PQexec(*handle
, "");
574 php_pqres_clear(res
);
576 if (CONNECTION_OK
!= PQstatus(*handle
)) {
581 static inline PGresult
*unlisten(PGconn
*conn
, const char *channel_str
, size_t channel_len
)
583 char *quoted_channel
= PQescapeIdentifier(conn
, channel_str
, channel_len
);
584 PGresult
*res
= NULL
;
586 if (quoted_channel
) {
589 smart_str_appends(&cmd
, "UNLISTEN ");
590 smart_str_appends(&cmd
, quoted_channel
);
593 res
= php_pq_exec(conn
, smart_str_v(&cmd
));
595 smart_str_free(&cmd
);
596 PQfreemem(quoted_channel
);
602 static int apply_unlisten(zval
*p
, int argc
, va_list argv
, zend_hash_key
*key
)
604 php_pqconn_object_t
*obj
= va_arg(argv
, php_pqconn_object_t
*);
605 PGresult
*res
= unlisten(obj
->intern
->conn
, key
->key
->val
, key
->key
->len
);
608 php_pqres_clear(res
);
611 return ZEND_HASH_APPLY_REMOVE
;
614 static void php_pqconn_retire(php_persistent_handle_factory_t
*f
, void **handle
)
616 php_pqconn_event_data_t
*evdata
= PQinstanceData(*handle
, php_pqconn_event
);
621 PQsetInstanceData(*handle
, php_pqconn_event
, NULL
);
624 PQsetNoticeReceiver(*handle
, php_pqconn_notice_ignore
, NULL
);
626 /* cancel async queries */
627 if (PQisBusy(*handle
) && (cancel
= PQgetCancel(*handle
))) {
630 PQcancel(cancel
, err
, sizeof(err
));
631 PQfreeCancel(cancel
);
633 /* clean up async results */
634 while ((res
= PQgetResult(*handle
))) {
635 php_pqres_clear(res
);
638 /* clean up transaction & session */
639 switch (PQtransactionStatus(*handle
)) {
641 res
= PQexec(*handle
, "RESET ALL");
644 res
= PQexec(*handle
, "ROLLBACK; RESET ALL");
649 php_pqres_clear(res
);
653 /* clean up notify listeners */
654 zend_hash_apply_with_arguments(&evdata
->obj
->intern
->listeners
, apply_unlisten
, 1, evdata
->obj
);
656 /* release instance data */
661 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct
, 0, 0, 1)
662 ZEND_ARG_INFO(0, dsn
)
663 ZEND_ARG_INFO(0, flags
)
665 static PHP_METHOD(pqconn
, __construct
) {
666 zend_error_handling zeh
;
672 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
673 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "|sl", &dsn_str
, &dsn_len
, &flags
);
674 zend_restore_error_handling(&zeh
);
677 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
680 throw_exce(EX_BAD_METHODCALL
, "pq\\Connection already initialized");
682 php_pqconn_event_data_t
*evdata
= php_pqconn_event_data_init(obj
);
683 php_pqconn_resource_factory_data_t rfdata
= {dsn_str
, flags
};
685 obj
->intern
= ecalloc(1, sizeof(*obj
->intern
));
687 obj
->intern
->default_auto_convert
= PHP_PQRES_CONV_ALL
;
689 zend_hash_init(&obj
->intern
->listeners
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
690 zend_hash_init(&obj
->intern
->statements
, 0, NULL
, NULL
, 0);
691 zend_hash_init(&obj
->intern
->converters
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
692 zend_hash_init(&obj
->intern
->eventhandlers
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
694 if (flags
& PHP_PQCONN_PERSISTENT
) {
695 zend_string
*dsn
= zend_string_init(dsn_str
, dsn_len
, 0);
696 php_persistent_handle_factory_t
*phf
= php_persistent_handle_concede(NULL
, PHP_PQ_G
->connection
.name
, dsn
, php_pqconn_wakeup
, php_pqconn_retire
);
697 php_persistent_handle_resource_factory_init(&obj
->intern
->factory
, phf
);
698 zend_string_release(dsn
);
700 php_resource_factory_init(&obj
->intern
->factory
, &php_pqconn_resource_factory_ops
, NULL
, NULL
);
703 if (flags
& PHP_PQCONN_ASYNC
) {
704 obj
->intern
->poller
= (int (*)(PGconn
*)) PQconnectPoll
;
707 obj
->intern
->conn
= php_resource_factory_handle_ctor(&obj
->intern
->factory
, &rfdata
);
709 PQsetInstanceData(obj
->intern
->conn
, php_pqconn_event
, evdata
);
710 PQsetNoticeReceiver(obj
->intern
->conn
, php_pqconn_notice_recv
, evdata
);
712 if (SUCCESS
!= php_pqconn_update_socket(getThis(), obj
)) {
713 throw_exce(EX_CONNECTION_FAILED
, "Connection failed (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
719 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset
, 0, 0, 0)
721 static PHP_METHOD(pqconn
, reset
) {
722 zend_error_handling zeh
;
725 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
726 rv
= zend_parse_parameters_none();
727 zend_restore_error_handling(&zeh
);
730 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
733 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
735 PQreset(obj
->intern
->conn
);
737 if (CONNECTION_OK
!= PQstatus(obj
->intern
->conn
)) {
738 throw_exce(EX_CONNECTION_FAILED
, "Connection reset failed: (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
741 php_pqconn_notify_listeners(obj
);
746 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset_async
, 0, 0, 0)
748 static PHP_METHOD(pqconn
, resetAsync
) {
749 zend_error_handling zeh
;
752 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
753 rv
= zend_parse_parameters_none();
754 zend_restore_error_handling(&zeh
);
757 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
760 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
762 if (!PQresetStart(obj
->intern
->conn
)) {
763 throw_exce(EX_IO
, "Failed to start connection reset (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
765 obj
->intern
->poller
= (int (*)(PGconn
*)) PQresetPoll
;
768 php_pqconn_notify_listeners(obj
);
773 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten
, 0, 0, 1)
774 ZEND_ARG_INFO(0, channel
)
776 static PHP_METHOD(pqconn
, unlisten
)
778 zend_error_handling zeh
;
783 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
784 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s", &channel_str
, &channel_len
);
785 zend_restore_error_handling(&zeh
);
788 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
791 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
792 } else if (SUCCESS
== zend_hash_str_del(&obj
->intern
->listeners
, channel_str
, channel_len
)) {
793 PGresult
*res
= unlisten(obj
->intern
->conn
, channel_str
, channel_len
);
796 php_pqres_success(res
);
797 php_pqres_clear(res
);
803 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten_async
, 0, 0, 1)
804 ZEND_ARG_INFO(0, channel
)
806 static PHP_METHOD(pqconn
, unlistenAsync
) {
807 zend_error_handling zeh
;
812 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
813 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s", &channel_str
, &channel_len
);
814 zend_restore_error_handling(&zeh
);
817 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
820 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
822 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
824 if (!quoted_channel
) {
825 throw_exce(EX_ESCAPE
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
829 smart_str_appends(&cmd
, "UNLISTEN ");
830 smart_str_appends(&cmd
, quoted_channel
);
833 if (!PQsendQuery(obj
->intern
->conn
, smart_str_v(&cmd
))) {
834 throw_exce(EX_IO
, "Failed to uninstall listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
836 obj
->intern
->poller
= PQconsumeInput
;
837 zend_hash_str_del(&obj
->intern
->listeners
, channel_str
, channel_len
);
840 smart_str_free(&cmd
);
841 PQfreemem(quoted_channel
);
842 php_pqconn_notify_listeners(obj
);
848 static void php_pqconn_add_listener(php_pqconn_object_t
*obj
, const char *channel_str
, size_t channel_len
, php_pq_callback_t
*listener
)
852 php_pq_callback_addref(listener
);
854 if ((existing
= zend_hash_str_find(&obj
->intern
->listeners
, channel_str
, channel_len
))) {
855 zend_hash_next_index_insert_mem(Z_ARRVAL_P(existing
), (void *) listener
, sizeof(*listener
));
861 zend_hash_init(ht
, 0, NULL
, php_pq_callback_hash_dtor
, 0);
862 zend_hash_next_index_insert_mem(ht
, (void *) listener
, sizeof(*listener
));
865 zend_hash_str_add(&obj
->intern
->listeners
, channel_str
, channel_len
, &tmp
);
869 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen
, 0, 0, 2)
870 ZEND_ARG_INFO(0, channel
)
871 ZEND_ARG_INFO(0, callable
)
873 static PHP_METHOD(pqconn
, listen
) {
874 zend_error_handling zeh
;
875 char *channel_str
= NULL
;
876 size_t channel_len
= 0;
877 php_pq_callback_t listener
= PHP_PQ_CALLBACK_INIT
;
880 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
881 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
882 zend_restore_error_handling(&zeh
);
885 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
888 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
890 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
892 if (!quoted_channel
) {
893 throw_exce(EX_ESCAPE
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
898 smart_str_appends(&cmd
, "LISTEN ");
899 smart_str_appends(&cmd
, quoted_channel
);
902 res
= php_pq_exec(obj
->intern
->conn
, smart_str_v(&cmd
));
904 smart_str_free(&cmd
);
905 PQfreemem(quoted_channel
);
908 throw_exce(EX_RUNTIME
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
910 if (SUCCESS
== php_pqres_success(res
)) {
911 obj
->intern
->poller
= PQconsumeInput
;
912 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener
);
914 php_pqres_clear(res
);
917 php_pqconn_notify_listeners(obj
);
923 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen_async
, 0, 0, 0)
924 ZEND_ARG_INFO(0, channel
)
925 ZEND_ARG_INFO(0, callable
)
927 static PHP_METHOD(pqconn
, listenAsync
) {
928 zend_error_handling zeh
;
929 char *channel_str
= NULL
;
930 size_t channel_len
= 0;
931 php_pq_callback_t listener
= PHP_PQ_CALLBACK_INIT
;
934 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
935 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
936 zend_restore_error_handling(&zeh
);
939 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
942 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
944 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
946 if (!quoted_channel
) {
947 throw_exce(EX_ESCAPE
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
951 smart_str_appends(&cmd
, "LISTEN ");
952 smart_str_appends(&cmd
, quoted_channel
);
955 if (!PQsendQuery(obj
->intern
->conn
, smart_str_v(&cmd
))) {
956 throw_exce(EX_IO
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
958 obj
->intern
->poller
= PQconsumeInput
;
959 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener
);
962 smart_str_free(&cmd
);
963 PQfreemem(quoted_channel
);
964 php_pqconn_notify_listeners(obj
);
970 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify
, 0, 0, 2)
971 ZEND_ARG_INFO(0, channel
)
972 ZEND_ARG_INFO(0, message
)
974 static PHP_METHOD(pqconn
, notify
) {
975 zend_error_handling zeh
;
976 char *channel_str
, *message_str
;
977 size_t channel_len
, message_len
;
980 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
981 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
982 zend_restore_error_handling(&zeh
);
985 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
988 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
991 char *params
[2] = {channel_str
, message_str
};
993 res
= php_pq_exec_params(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0);
996 throw_exce(EX_RUNTIME
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
998 php_pqres_success(res
);
999 php_pqres_clear(res
);
1002 php_pqconn_notify_listeners(obj
);
1007 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify_async
, 0, 0, 2)
1008 ZEND_ARG_INFO(0, channel
)
1009 ZEND_ARG_INFO(0, message
)
1010 ZEND_END_ARG_INFO();
1011 static PHP_METHOD(pqconn
, notifyAsync
) {
1012 zend_error_handling zeh
;
1013 char *channel_str
, *message_str
;
1014 size_t channel_len
, message_len
;
1015 ZEND_RESULT_CODE rv
;
1017 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1018 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
1019 zend_restore_error_handling(&zeh
);
1021 if (SUCCESS
== rv
) {
1022 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1025 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1027 char *params
[2] = {channel_str
, message_str
};
1029 if (!PQsendQueryParams(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0)) {
1030 throw_exce(EX_IO
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1032 obj
->intern
->poller
= PQconsumeInput
;
1035 php_pqconn_notify_listeners(obj
);
1040 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_poll
, 0, 0, 0)
1041 ZEND_END_ARG_INFO();
1042 static PHP_METHOD(pqconn
, poll
) {
1043 zend_error_handling zeh
;
1044 ZEND_RESULT_CODE rv
;
1046 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1047 rv
= zend_parse_parameters_none();
1048 zend_restore_error_handling(&zeh
);
1050 if (SUCCESS
== rv
) {
1051 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1054 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1055 } else if (!obj
->intern
->poller
) {
1056 throw_exce(EX_RUNTIME
, "No asynchronous operation active");
1058 if (obj
->intern
->poller
== PQconsumeInput
) {
1059 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
) * PGRES_POLLING_OK
);
1061 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
));
1063 php_pqconn_notify_listeners(obj
);
1068 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_flush
, 0, 0, 0)
1069 ZEND_END_ARG_INFO();
1070 static PHP_METHOD(pqconn
, flush
) {
1071 zend_error_handling zeh
;
1072 ZEND_RESULT_CODE rv
;
1074 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1075 rv
= zend_parse_parameters_none();
1076 zend_restore_error_handling(&zeh
);
1078 if (SUCCESS
== rv
) {
1079 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1082 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1083 } else if (!obj
->intern
->poller
) {
1084 throw_exce(EX_RUNTIME
, "No asynchronous operation active");
1086 switch (PQflush(obj
->intern
->conn
)) {
1089 throw_exce(EX_RUNTIME
, "Failed to flush connection: %s", PHP_PQerrorMessage(obj
->intern
->conn
));
1102 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec
, 0, 0, 1)
1103 ZEND_ARG_INFO(0, query
)
1104 ZEND_END_ARG_INFO();
1105 static PHP_METHOD(pqconn
, exec
) {
1106 zend_error_handling zeh
;
1109 ZEND_RESULT_CODE rv
;
1111 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1112 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s", &query_str
, &query_len
);
1113 zend_restore_error_handling(&zeh
);
1115 if (SUCCESS
== rv
) {
1116 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1119 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1121 PGresult
*res
= php_pq_exec(obj
->intern
->conn
, query_str
);
1124 throw_exce(EX_RUNTIME
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1125 } else if (SUCCESS
== php_pqres_success(res
)) {
1126 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), return_value
);
1128 php_pqres_clear(res
);
1131 php_pqconn_notify_listeners(obj
);
1136 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_get_result
, 0, 0, 0)
1137 ZEND_END_ARG_INFO();
1138 static PHP_METHOD(pqconn
, getResult
) {
1139 zend_error_handling zeh
;
1140 ZEND_RESULT_CODE rv
;
1142 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1143 rv
= zend_parse_parameters_none();
1144 zend_restore_error_handling(&zeh
);
1146 if (SUCCESS
== rv
) {
1147 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1150 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1152 PGresult
*res
= PQgetResult(obj
->intern
->conn
);
1153 php_pq_object_t
*res_obj
;
1155 if (res
&& (res_obj
= PQresultInstanceData(res
, php_pqconn_event
))) {
1156 php_pq_object_to_zval_no_addref(res_obj
, return_value
);
1161 php_pqconn_notify_listeners(obj
);
1166 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_async
, 0, 0, 1)
1167 ZEND_ARG_INFO(0, query
)
1168 ZEND_ARG_INFO(0, callable
)
1169 ZEND_END_ARG_INFO();
1170 static PHP_METHOD(pqconn
, execAsync
) {
1171 zend_error_handling zeh
;
1172 php_pq_callback_t resolver
= PHP_PQ_CALLBACK_INIT
;
1175 ZEND_RESULT_CODE rv
;
1177 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1178 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s|f", &query_str
, &query_len
, &resolver
.fci
, &resolver
.fcc
);
1179 zend_restore_error_handling(&zeh
);
1181 if (SUCCESS
== rv
) {
1182 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1185 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1186 } else if (!PQsendQuery(obj
->intern
->conn
, query_str
)) {
1187 throw_exce(EX_IO
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1188 #if HAVE_PQSETSINGLEROWMODE
1189 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1190 throw_exce(EX_RUNTIME
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1193 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver
);
1194 obj
->intern
->poller
= PQconsumeInput
;
1195 php_pqconn_notify_listeners(obj
);
1200 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params
, 0, 0, 2)
1201 ZEND_ARG_INFO(0, query
)
1202 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1203 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1204 ZEND_END_ARG_INFO();
1205 static PHP_METHOD(pqconn
, execParams
) {
1206 zend_error_handling zeh
;
1210 zval
*ztypes
= NULL
;
1211 ZEND_RESULT_CODE rv
;
1213 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1214 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sa/|a/!", &query_str
, &query_len
, &zparams
, &ztypes
);
1215 zend_restore_error_handling(&zeh
);
1217 if (SUCCESS
== rv
) {
1218 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1221 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1224 php_pq_params_t
*params
;
1226 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
));
1227 res
= php_pq_exec_params(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1228 php_pq_params_free(¶ms
);
1231 throw_exce(EX_RUNTIME
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1233 if (SUCCESS
== php_pqres_success(res
)) {
1234 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), return_value
);
1236 php_pqres_clear(res
);
1239 php_pqconn_notify_listeners(obj
);
1245 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async
, 0, 0, 2)
1246 ZEND_ARG_INFO(0, query
)
1247 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1248 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1249 ZEND_ARG_INFO(0, callable
)
1250 ZEND_END_ARG_INFO();
1251 static PHP_METHOD(pqconn
, execParamsAsync
) {
1252 zend_error_handling zeh
;
1253 php_pq_callback_t resolver
= PHP_PQ_CALLBACK_INIT
;
1257 zval
*ztypes
= NULL
;
1258 ZEND_RESULT_CODE rv
;
1260 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1261 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sa/|a/!f", &query_str
, &query_len
, &zparams
, &ztypes
, &resolver
.fci
, &resolver
.fcc
);
1262 zend_restore_error_handling(&zeh
);
1264 if (SUCCESS
== rv
) {
1265 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1268 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1271 php_pq_params_t
*params
;
1273 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
));
1274 rc
= PQsendQueryParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1275 php_pq_params_free(¶ms
);
1278 throw_exce(EX_IO
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1279 #if HAVE_PQSETSINGLEROWMODE
1280 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1281 throw_exce(EX_RUNTIME
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1284 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver
);
1285 obj
->intern
->poller
= PQconsumeInput
;
1286 php_pqconn_notify_listeners(obj
);
1290 zend_restore_error_handling(&zeh
);
1293 ZEND_RESULT_CODE
php_pqconn_prepare(zval
*object
, php_pqconn_object_t
*obj
, const char *name
, const char *query
, php_pq_params_t
*params
)
1296 ZEND_RESULT_CODE rv
;
1299 obj
= PHP_PQ_OBJ(object
, NULL
);
1302 res
= php_pq_prepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
);
1306 throw_exce(EX_RUNTIME
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1308 rv
= php_pqres_success(res
);
1309 php_pqres_clear(res
);
1310 php_pqconn_notify_listeners(obj
);
1316 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare
, 0, 0, 2)
1317 ZEND_ARG_INFO(0, name
)
1318 ZEND_ARG_INFO(0, query
)
1319 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1320 ZEND_END_ARG_INFO();
1321 static PHP_METHOD(pqconn
, prepare
) {
1322 zend_error_handling zeh
;
1323 zval
*ztypes
= NULL
;
1324 char *name_str
, *query_str
;
1325 size_t name_len
, *query_len
;
1326 ZEND_RESULT_CODE rv
;
1328 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1329 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1330 zend_restore_error_handling(&zeh
);
1332 if (SUCCESS
== rv
) {
1333 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1336 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1338 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL
);
1340 if (SUCCESS
!= php_pqconn_prepare(getThis(), obj
, name_str
, query_str
, params
)) {
1341 php_pq_params_free(¶ms
);
1343 php_pqstm_t
*stm
= php_pqstm_init(obj
, name_str
, query_str
, params
);
1345 RETVAL_OBJ(&php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
)->zo
);
1351 ZEND_RESULT_CODE
php_pqconn_prepare_async(zval
*object
, php_pqconn_object_t
*obj
, const char *name
, const char *query
, php_pq_params_t
*params
)
1353 ZEND_RESULT_CODE rv
;
1356 obj
= PHP_PQ_OBJ(object
, NULL
);
1359 if (!PQsendPrepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
)) {
1361 throw_exce(EX_IO
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1364 obj
->intern
->poller
= PQconsumeInput
;
1365 php_pqconn_notify_listeners(obj
);
1371 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare_async
, 0, 0, 2)
1372 ZEND_ARG_INFO(0, name
)
1373 ZEND_ARG_INFO(0, query
)
1374 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1375 ZEND_END_ARG_INFO();
1376 static PHP_METHOD(pqconn
, prepareAsync
) {
1377 zend_error_handling zeh
;
1378 zval
*ztypes
= NULL
;
1379 char *name_str
, *query_str
;
1380 size_t name_len
, *query_len
;
1381 ZEND_RESULT_CODE rv
;
1383 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1384 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1385 zend_restore_error_handling(&zeh
);
1387 if (SUCCESS
== rv
) {
1388 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1391 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1393 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL
);
1395 if (SUCCESS
!= php_pqconn_prepare_async(getThis(), obj
, name_str
, query_str
, params
)) {
1396 php_pq_params_free(¶ms
);
1398 php_pqstm_t
*stm
= php_pqstm_init(obj
, name_str
, query_str
, params
);
1400 RETVAL_OBJ(&php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
)->zo
);
1406 ZEND_RESULT_CODE
php_pqconn_declare(zval
*object
, php_pqconn_object_t
*obj
, const char *decl
)
1409 ZEND_RESULT_CODE rv
;
1412 obj
= PHP_PQ_OBJ(object
, NULL
);
1415 res
= php_pq_exec(obj
->intern
->conn
, decl
);
1419 throw_exce(EX_RUNTIME
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1421 rv
= php_pqres_success(res
);
1422 php_pqres_clear(res
);
1423 php_pqconn_notify_listeners(obj
);
1429 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare
, 0, 0, 3)
1430 ZEND_ARG_INFO(0, name
)
1431 ZEND_ARG_INFO(0, flags
)
1432 ZEND_ARG_INFO(0, query
)
1433 ZEND_END_ARG_INFO();
1434 static PHP_METHOD(pqconn
, declare
) {
1435 zend_error_handling zeh
;
1436 char *name_str
, *query_str
;
1437 size_t name_len
, query_len
;
1439 ZEND_RESULT_CODE rv
;
1441 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1442 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1443 zend_restore_error_handling(&zeh
);
1445 if (SUCCESS
== rv
) {
1446 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1449 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1452 char *decl
= php_pqcur_declare_str(name_str
, name_len
, flags
, query_str
, query_len
, &query_offset
);
1454 if (SUCCESS
!= php_pqconn_declare(getThis(), obj
, decl
)) {
1457 php_pqcur_t
*cur
= php_pqcur_init(obj
, name_str
, decl
, query_offset
, flags
);
1459 RETVAL_OBJ(&php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
)->zo
);
1465 ZEND_RESULT_CODE
php_pqconn_declare_async(zval
*object
, php_pqconn_object_t
*obj
, const char *decl
)
1467 ZEND_RESULT_CODE rv
;
1470 obj
= PHP_PQ_OBJ(object
, NULL
);
1473 if (!PQsendQuery(obj
->intern
->conn
, decl
)) {
1475 throw_exce(EX_IO
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1478 obj
->intern
->poller
= PQconsumeInput
;
1479 php_pqconn_notify_listeners(obj
);
1485 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare_async
, 0, 0, 2)
1486 ZEND_ARG_INFO(0, name
)
1487 ZEND_ARG_INFO(0, flags
)
1488 ZEND_ARG_INFO(0, query
)
1489 ZEND_END_ARG_INFO();
1490 static PHP_METHOD(pqconn
, declareAsync
) {
1491 zend_error_handling zeh
;
1492 char *name_str
, *query_str
;
1493 size_t name_len
, query_len
;
1495 ZEND_RESULT_CODE rv
;
1497 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1498 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1499 zend_restore_error_handling(&zeh
);
1501 if (SUCCESS
== rv
) {
1502 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1505 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1508 char *decl
= php_pqcur_declare_str(name_str
, name_len
, flags
, query_str
, query_len
, &query_offset
);
1510 if (SUCCESS
!= php_pqconn_declare_async(getThis(), obj
, decl
)) {
1513 php_pqcur_t
*cur
= php_pqcur_init(obj
, name_str
, decl
, query_offset
, flags
);
1515 RETVAL_OBJ(&php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
)->zo
);
1521 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote
, 0, 0, 1)
1522 ZEND_ARG_INFO(0, string
)
1523 ZEND_END_ARG_INFO();
1524 static PHP_METHOD(pqconn
, quote
) {
1528 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1529 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1532 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1534 char *quoted
= PQescapeLiteral(obj
->intern
->conn
, str
, len
);
1537 php_error_docref(NULL
, E_WARNING
, "Failed to quote string (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1540 RETVAL_STRING(quoted
);
1547 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote_name
, 0, 0, 1)
1548 ZEND_ARG_INFO(0, type
)
1549 ZEND_END_ARG_INFO();
1550 static PHP_METHOD(pqconn
, quoteName
) {
1554 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1555 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1558 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1560 char *quoted
= PQescapeIdentifier(obj
->intern
->conn
, str
, len
);
1563 php_error_docref(NULL
, E_WARNING
, "Failed to quote name (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1566 RETVAL_STRING(quoted
);
1573 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_escape_bytea
, 0, 0, 1)
1574 ZEND_ARG_INFO(0, bytea
)
1575 ZEND_END_ARG_INFO();
1576 static PHP_METHOD(pqconn
, escapeBytea
) {
1580 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1581 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1584 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1587 char *escaped_str
= (char *) PQescapeByteaConn(obj
->intern
->conn
, (unsigned char *) str
, len
, &escaped_len
);
1590 php_error_docref(NULL
, E_WARNING
, "Failed to escape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1593 RETVAL_STRINGL(escaped_str
, escaped_len
- 1);
1594 PQfreemem(escaped_str
);
1600 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unescape_bytea
, 0, 0, 1)
1601 ZEND_ARG_INFO(0, bytea
)
1602 ZEND_END_ARG_INFO();
1603 static PHP_METHOD(pqconn
, unescapeBytea
) {
1607 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1608 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1611 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1613 size_t unescaped_len
;
1614 char *unescaped_str
= (char *) PQunescapeBytea((unsigned char *)str
, &unescaped_len
);
1616 if (!unescaped_str
) {
1617 php_error_docref(NULL
, E_WARNING
, "Failed to unescape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1620 RETVAL_STRINGL(unescaped_str
, unescaped_len
);
1621 PQfreemem(unescaped_str
);
1627 ZEND_RESULT_CODE
php_pqconn_start_transaction(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable
)
1629 ZEND_RESULT_CODE rv
= FAILURE
;
1632 conn_obj
= PHP_PQ_OBJ(zconn
, NULL
);
1635 if (!conn_obj
->intern
) {
1636 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1639 smart_str cmd
= {0};
1640 const char *il
= php_pq_isolation_level(&isolation
);
1642 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1643 smart_str_appends(&cmd
, il
);
1644 smart_str_appends(&cmd
, ", READ ");
1645 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1646 smart_str_appends(&cmd
, ",");
1647 smart_str_appends(&cmd
, deferrable
? "" : " NOT");
1648 smart_str_appends(&cmd
, " DEFERRABLE");
1651 res
= php_pq_exec(conn_obj
->intern
->conn
, smart_str_v(&cmd
));
1654 throw_exce(EX_RUNTIME
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1656 rv
= php_pqres_success(res
);
1657 php_pqres_clear(res
);
1658 php_pqconn_notify_listeners(conn_obj
);
1661 smart_str_free(&cmd
);
1667 ZEND_RESULT_CODE
php_pqconn_start_transaction_async(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable
)
1669 ZEND_RESULT_CODE rv
= FAILURE
;
1672 conn_obj
= PHP_PQ_OBJ(zconn
, NULL
);
1675 if (!conn_obj
->intern
) {
1676 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1678 smart_str cmd
= {0};
1679 const char *il
= php_pq_isolation_level(&isolation
);
1681 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1682 smart_str_appends(&cmd
, il
);
1683 smart_str_appends(&cmd
, ", READ ");
1684 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1685 smart_str_appends(&cmd
, ",");
1686 smart_str_appends(&cmd
, deferrable
? "" : "NOT ");
1687 smart_str_appends(&cmd
, " DEFERRABLE");
1690 if (!PQsendQuery(conn_obj
->intern
->conn
, smart_str_v(&cmd
))) {
1691 throw_exce(EX_IO
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1694 conn_obj
->intern
->poller
= PQconsumeInput
;
1695 php_pqconn_notify_listeners(conn_obj
);
1698 smart_str_free(&cmd
);
1704 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction
, 0, 0, 0)
1705 ZEND_ARG_INFO(0, isolation
)
1706 ZEND_ARG_INFO(0, readonly
)
1707 ZEND_ARG_INFO(0, deferrable
)
1708 ZEND_END_ARG_INFO();
1709 static PHP_METHOD(pqconn
, startTransaction
) {
1710 zend_error_handling zeh
;
1711 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1712 zend_long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1713 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1714 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1715 ZEND_RESULT_CODE rv
;
1717 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1718 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "|lbb", &isolation
, &readonly
, &deferrable
);
1719 zend_restore_error_handling(&zeh
);
1721 if (SUCCESS
== rv
) {
1722 rv
= php_pqconn_start_transaction(getThis(), obj
, isolation
, readonly
, deferrable
);
1724 if (SUCCESS
== rv
) {
1725 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1727 php_pq_object_addref(obj
);
1730 txn
->isolation
= isolation
;
1731 txn
->readonly
= readonly
;
1732 txn
->deferrable
= deferrable
;
1734 RETVAL_OBJ(&php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
)->zo
);
1739 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction_async
, 0, 0, 0)
1740 ZEND_ARG_INFO(0, isolation
)
1741 ZEND_ARG_INFO(0, readonly
)
1742 ZEND_ARG_INFO(0, deferrable
)
1743 ZEND_END_ARG_INFO();
1744 static PHP_METHOD(pqconn
, startTransactionAsync
) {
1745 zend_error_handling zeh
;
1746 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1747 zend_long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1748 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1749 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1750 ZEND_RESULT_CODE rv
;
1752 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1753 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "|lbb", &isolation
, &readonly
, &deferrable
);
1754 zend_restore_error_handling(&zeh
);
1756 if (SUCCESS
== rv
) {
1757 rv
= php_pqconn_start_transaction_async(getThis(), obj
, isolation
, readonly
, deferrable
);
1759 if (SUCCESS
== rv
) {
1760 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1762 php_pq_object_addref(obj
);
1765 txn
->isolation
= isolation
;
1766 txn
->readonly
= readonly
;
1767 txn
->deferrable
= deferrable
;
1769 RETVAL_OBJ(&php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
)->zo
);
1774 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_trace
, 0, 0, 0)
1775 ZEND_ARG_INFO(0, stdio_stream
)
1776 ZEND_END_ARG_INFO();
1777 static PHP_METHOD(pqconn
, trace
) {
1778 zval
*zstream
= NULL
;
1780 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &zstream
)) {
1781 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1784 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1787 PQuntrace(obj
->intern
->conn
);
1791 php_stream
*stream
= NULL
;
1793 php_stream_from_zval(stream
, zstream
);
1795 if (SUCCESS
!= php_stream_cast(stream
, PHP_STREAM_AS_STDIO
, (void *) &fp
, REPORT_ERRORS
)) {
1798 stream
->flags
|= PHP_STREAM_FLAG_NO_CLOSE
;
1799 PQtrace(obj
->intern
->conn
, fp
);
1807 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_off
, 0, 0, 1)
1808 ZEND_ARG_INFO(0, type
)
1809 ZEND_END_ARG_INFO();
1810 static PHP_METHOD(pqconn
, off
) {
1811 zend_error_handling zeh
;
1813 ZEND_RESULT_CODE rv
;
1815 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1816 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "S", &type
);
1817 zend_restore_error_handling(&zeh
);
1819 if (SUCCESS
== rv
) {
1820 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1823 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1825 RETURN_BOOL(SUCCESS
== zend_hash_del(&obj
->intern
->eventhandlers
, type
));
1830 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_on
, 0, 0, 2)
1831 ZEND_ARG_INFO(0, type
)
1832 ZEND_ARG_INFO(0, callable
)
1833 ZEND_END_ARG_INFO();
1834 static PHP_METHOD(pqconn
, on
) {
1835 zend_error_handling zeh
;
1838 php_pq_callback_t cb
= PHP_PQ_CALLBACK_INIT
;
1839 ZEND_RESULT_CODE rv
;
1841 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1842 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sf", &type_str
, &type_len
, &cb
.fci
, &cb
.fcc
);
1843 zend_restore_error_handling(&zeh
);
1845 if (SUCCESS
== rv
) {
1846 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1849 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1851 RETVAL_LONG(php_pqconn_add_eventhandler(obj
, type_str
, type_len
, &cb
));
1856 struct apply_set_converter_arg
{
1862 static int apply_set_converter(zval
*zoid
, void *a
)
1864 zend_long oid
= zval_get_long(zoid
);
1865 struct apply_set_converter_arg
*arg
= a
;
1868 Z_ADDREF_P(arg
->zconv
);
1869 zend_hash_index_update(arg
->ht
, oid
, arg
->zconv
);
1871 zend_hash_index_del(arg
->ht
, oid
);
1874 return ZEND_HASH_APPLY_KEEP
;
1877 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_set_converter
, 0, 0, 1)
1878 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\Converter
, 0)
1879 ZEND_END_ARG_INFO();
1880 static PHP_METHOD(pqconn
, setConverter
) {
1881 ZEND_RESULT_CODE rv
;
1882 zend_error_handling zeh
;
1885 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1886 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zcnv
, php_pqconv_class_entry
);
1887 zend_restore_error_handling(&zeh
);
1889 if (SUCCESS
== rv
) {
1890 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1893 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1896 struct apply_set_converter_arg arg
= {NULL
};
1899 php_pq_call_method(zcnv
, "converttypes", 0, &zoids
);
1900 ZVAL_DUP(&tmp
, &zoids
);
1901 convert_to_array(&tmp
);
1903 arg
.ht
= &obj
->intern
->converters
;
1907 zend_hash_apply_with_argument(Z_ARRVAL(tmp
), apply_set_converter
, &arg
);
1909 zval_ptr_dtor(&tmp
);
1910 zval_ptr_dtor(&zoids
);
1915 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unset_converter
, 0, 0, 1)
1916 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\Converter
, 0)
1917 ZEND_END_ARG_INFO();
1918 static PHP_METHOD(pqconn
, unsetConverter
) {
1919 ZEND_RESULT_CODE rv
;
1920 zend_error_handling zeh
;
1923 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1924 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zcnv
, php_pqconv_class_entry
);
1925 zend_restore_error_handling(&zeh
);
1927 if (SUCCESS
== rv
) {
1928 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1931 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1934 struct apply_set_converter_arg arg
= {NULL
};
1937 php_pq_call_method(zcnv
, "converttypes", 0, &zoids
);
1938 ZVAL_DUP(&tmp
, &zoids
);
1939 convert_to_array(&tmp
);
1941 arg
.ht
= &obj
->intern
->converters
;
1945 zend_hash_apply_with_argument(Z_ARRVAL(tmp
), apply_set_converter
, &arg
);
1947 zval_ptr_dtor(&tmp
);
1948 zval_ptr_dtor(&zoids
);
1953 static zend_function_entry php_pqconn_methods
[] = {
1954 PHP_ME(pqconn
, __construct
, ai_pqconn_construct
, ZEND_ACC_PUBLIC
)
1955 PHP_ME(pqconn
, reset
, ai_pqconn_reset
, ZEND_ACC_PUBLIC
)
1956 PHP_ME(pqconn
, resetAsync
, ai_pqconn_reset_async
, ZEND_ACC_PUBLIC
)
1957 PHP_ME(pqconn
, poll
, ai_pqconn_poll
, ZEND_ACC_PUBLIC
)
1958 PHP_ME(pqconn
, flush
, ai_pqconn_flush
, ZEND_ACC_PUBLIC
)
1959 PHP_ME(pqconn
, exec
, ai_pqconn_exec
, ZEND_ACC_PUBLIC
)
1960 PHP_ME(pqconn
, execAsync
, ai_pqconn_exec_async
, ZEND_ACC_PUBLIC
)
1961 PHP_ME(pqconn
, execParams
, ai_pqconn_exec_params
, ZEND_ACC_PUBLIC
)
1962 PHP_ME(pqconn
, execParamsAsync
, ai_pqconn_exec_params_async
, ZEND_ACC_PUBLIC
)
1963 PHP_ME(pqconn
, prepare
, ai_pqconn_prepare
, ZEND_ACC_PUBLIC
)
1964 PHP_ME(pqconn
, prepareAsync
, ai_pqconn_prepare_async
, ZEND_ACC_PUBLIC
)
1965 PHP_ME(pqconn
, declare
, ai_pqconn_declare
, ZEND_ACC_PUBLIC
)
1966 PHP_ME(pqconn
, declareAsync
, ai_pqconn_declare_async
, ZEND_ACC_PUBLIC
)
1967 PHP_ME(pqconn
, unlisten
, ai_pqconn_unlisten
, ZEND_ACC_PUBLIC
)
1968 PHP_ME(pqconn
, unlistenAsync
, ai_pqconn_unlisten_async
, ZEND_ACC_PUBLIC
)
1969 PHP_ME(pqconn
, listen
, ai_pqconn_listen
, ZEND_ACC_PUBLIC
)
1970 PHP_ME(pqconn
, listenAsync
, ai_pqconn_listen_async
, ZEND_ACC_PUBLIC
)
1971 PHP_ME(pqconn
, notify
, ai_pqconn_notify
, ZEND_ACC_PUBLIC
)
1972 PHP_ME(pqconn
, notifyAsync
, ai_pqconn_notify_async
, ZEND_ACC_PUBLIC
)
1973 PHP_ME(pqconn
, getResult
, ai_pqconn_get_result
, ZEND_ACC_PUBLIC
)
1974 PHP_ME(pqconn
, quote
, ai_pqconn_quote
, ZEND_ACC_PUBLIC
)
1975 PHP_ME(pqconn
, quoteName
, ai_pqconn_quote_name
, ZEND_ACC_PUBLIC
)
1976 PHP_ME(pqconn
, escapeBytea
, ai_pqconn_escape_bytea
, ZEND_ACC_PUBLIC
)
1977 PHP_ME(pqconn
, unescapeBytea
, ai_pqconn_unescape_bytea
, ZEND_ACC_PUBLIC
)
1978 PHP_ME(pqconn
, startTransaction
, ai_pqconn_start_transaction
, ZEND_ACC_PUBLIC
)
1979 PHP_ME(pqconn
, startTransactionAsync
, ai_pqconn_start_transaction_async
, ZEND_ACC_PUBLIC
)
1980 PHP_ME(pqconn
, trace
, ai_pqconn_trace
, ZEND_ACC_PUBLIC
)
1981 PHP_ME(pqconn
, off
, ai_pqconn_off
, ZEND_ACC_PUBLIC
)
1982 PHP_ME(pqconn
, on
, ai_pqconn_on
, ZEND_ACC_PUBLIC
)
1983 PHP_ME(pqconn
, setConverter
, ai_pqconn_set_converter
, ZEND_ACC_PUBLIC
)
1984 PHP_ME(pqconn
, unsetConverter
, ai_pqconn_unset_converter
, ZEND_ACC_PUBLIC
)
1988 PHP_MSHUTDOWN_FUNCTION(pqconn
)
1990 php_persistent_handle_cleanup(PHP_PQ_G
->connection
.name
, NULL
);
1991 zend_string_release(PHP_PQ_G
->connection
.name
);
1992 zend_hash_destroy(&php_pqconn_object_prophandlers
);
1996 PHP_MINIT_FUNCTION(pqconn
)
1998 zend_class_entry ce
= {0};
1999 php_pq_object_prophandler_t ph
= {0};
2001 INIT_NS_CLASS_ENTRY(ce
, "pq", "Connection", php_pqconn_methods
);
2002 php_pqconn_class_entry
= zend_register_internal_class_ex(&ce
, NULL
);
2003 php_pqconn_class_entry
->create_object
= php_pqconn_create_object
;
2005 memcpy(&php_pqconn_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
2006 php_pqconn_object_handlers
.offset
= XtOffsetOf(php_pqconn_object_t
, zo
);
2007 php_pqconn_object_handlers
.free_obj
= php_pqconn_object_free
;
2008 php_pqconn_object_handlers
.read_property
= php_pq_object_read_prop
;
2009 php_pqconn_object_handlers
.write_property
= php_pq_object_write_prop
;
2010 php_pqconn_object_handlers
.clone_obj
= NULL
;
2011 php_pqconn_object_handlers
.get_property_ptr_ptr
= php_pq_object_get_prop_ptr_null
;
2012 php_pqconn_object_handlers
.get_gc
= php_pq_object_get_gc
;
2013 php_pqconn_object_handlers
.get_properties
= php_pq_object_properties
;
2014 php_pqconn_object_handlers
.get_debug_info
= php_pq_object_debug_info
;
2016 zend_hash_init(&php_pqconn_object_prophandlers
, 23, NULL
, php_pq_object_prophandler_dtor
, 1);
2018 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("status"), CONNECTION_BAD
, ZEND_ACC_PUBLIC
);
2019 ph
.read
= php_pqconn_object_read_status
;
2020 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "status", sizeof("status")-1, (void *) &ph
, sizeof(ph
));
2022 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("transactionStatus"), PQTRANS_UNKNOWN
, ZEND_ACC_PUBLIC
);
2023 ph
.read
= php_pqconn_object_read_transaction_status
;
2024 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "transactionStatus", sizeof("transactionStatus")-1, (void *) &ph
, sizeof(ph
));
2026 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("socket"), ZEND_ACC_PUBLIC
);
2027 ph
.read
= NULL
; /* forward to std prophandler */
2028 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "socket", sizeof("socket")-1, (void *) &ph
, sizeof(ph
));
2030 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("errorMessage"), ZEND_ACC_PUBLIC
);
2031 ph
.read
= php_pqconn_object_read_error_message
;
2032 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "errorMessage", sizeof("errorMessage")-1, (void *) &ph
, sizeof(ph
));
2034 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("busy"), 0, ZEND_ACC_PUBLIC
);
2035 ph
.read
= php_pqconn_object_read_busy
;
2036 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "busy", sizeof("busy")-1, (void *) &ph
, sizeof(ph
));
2038 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("encoding"), ZEND_ACC_PUBLIC
);
2039 ph
.read
= php_pqconn_object_read_encoding
;
2040 ph
.write
= php_pqconn_object_write_encoding
;
2041 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "encoding", sizeof("encoding")-1, (void *) &ph
, sizeof(ph
));
2044 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("unbuffered"), 0, ZEND_ACC_PUBLIC
);
2045 ph
.read
= php_pqconn_object_read_unbuffered
;
2046 ph
.write
= php_pqconn_object_write_unbuffered
;
2047 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "unbuffered", sizeof("unbuffered")-1, (void *) &ph
, sizeof(ph
));
2050 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("nonblocking"), 0, ZEND_ACC_PUBLIC
);
2051 ph
.read
= php_pqconn_object_read_nonblocking
;
2052 ph
.write
= php_pqconn_object_write_nonblocking
;
2053 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "nonblocking", sizeof("nonblocking")-1, (void *) &ph
, sizeof(ph
));
2056 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("db"), ZEND_ACC_PUBLIC
);
2057 ph
.read
= php_pqconn_object_read_db
;
2058 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "db", sizeof("db")-1, (void *) &ph
, sizeof(ph
));
2060 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("user"), ZEND_ACC_PUBLIC
);
2061 ph
.read
= php_pqconn_object_read_user
;
2062 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "user", sizeof("user")-1, (void *) &ph
, sizeof(ph
));
2064 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("pass"), ZEND_ACC_PUBLIC
);
2065 ph
.read
= php_pqconn_object_read_pass
;
2066 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "pass", sizeof("pass")-1, (void *) &ph
, sizeof(ph
));
2068 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("host"), ZEND_ACC_PUBLIC
);
2069 ph
.read
= php_pqconn_object_read_host
;
2070 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "host", sizeof("host")-1, (void *) &ph
, sizeof(ph
));
2072 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("port"), ZEND_ACC_PUBLIC
);
2073 ph
.read
= php_pqconn_object_read_port
;
2074 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "port", sizeof("port")-1, (void *) &ph
, sizeof(ph
));
2077 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("params"), ZEND_ACC_PUBLIC
);
2078 ph
.read
= php_pqconn_object_read_params
;
2079 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "params", sizeof("params")-1, (void *) &ph
, sizeof(ph
));
2082 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("options"), ZEND_ACC_PUBLIC
);
2083 ph
.read
= php_pqconn_object_read_options
;
2084 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "options", sizeof("options")-1, (void *) &ph
, sizeof(ph
));
2086 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("eventHandlers"), ZEND_ACC_PUBLIC
);
2087 ph
.read
= php_pqconn_object_read_event_handlers
;
2088 ph
.gc
= php_pqconn_object_gc_event_handlers
;
2089 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "eventHandlers", sizeof("eventHandlers")-1, (void *) &ph
, sizeof(ph
));
2092 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("listeners"), ZEND_ACC_PUBLIC
);
2093 ph
.read
= php_pqconn_object_read_listeners
;
2094 ph
.gc
= php_pqconn_object_gc_listeners
;
2095 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "listeners", sizeof("listeners")-1, (void *) &ph
, sizeof(ph
));
2098 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("converters"), ZEND_ACC_PUBLIC
);
2099 ph
.read
= php_pqconn_object_read_converters
;
2100 ph
.gc
= php_pqconn_object_gc_converters
;
2101 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "converters", sizeof("converters")-1, (void *) &ph
, sizeof(ph
));
2104 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultFetchType"), 0, ZEND_ACC_PUBLIC
);
2105 ph
.read
= php_pqconn_object_read_def_fetch_type
;
2106 ph
.write
= php_pqconn_object_write_def_fetch_type
;
2107 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultFetchType", sizeof("defaultFetchType")-1, (void *) &ph
, sizeof(ph
));
2110 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionIsolation"), 0, ZEND_ACC_PUBLIC
);
2111 ph
.read
= php_pqconn_object_read_def_txn_isolation
;
2112 ph
.write
= php_pqconn_object_write_def_txn_isolation
;
2113 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultTransactionIsolation", sizeof("defaultTransactionIsolation")-1, (void *) &ph
, sizeof(ph
));
2116 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionReadonly"), 0, ZEND_ACC_PUBLIC
);
2117 ph
.read
= php_pqconn_object_read_def_txn_readonly
;
2118 ph
.write
= php_pqconn_object_write_def_txn_readonly
;
2119 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultTransactionReadonly", sizeof("defaultTransactionReadonly")-1, (void *) &ph
, sizeof(ph
));
2122 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionDeferrable"), 0, ZEND_ACC_PUBLIC
);
2123 ph
.read
= php_pqconn_object_read_def_txn_deferrable
;
2124 ph
.write
= php_pqconn_object_write_def_txn_deferrable
;
2125 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultTransactionDeferrable", sizeof("defaultTransactionDeferrable")-1, (void *) &ph
, sizeof(ph
));
2128 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultAutoConvert"), PHP_PQRES_CONV_ALL
, ZEND_ACC_PUBLIC
);
2129 ph
.read
= php_pqconn_object_read_def_auto_conv
;
2130 ph
.write
= php_pqconn_object_write_def_auto_conv
;
2131 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultAutoConvert", sizeof("defaultAutoConvert")-1, (void *) &ph
, sizeof(ph
));
2134 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("OK"), CONNECTION_OK
);
2135 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("BAD"), CONNECTION_BAD
);
2136 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("STARTED"), CONNECTION_STARTED
);
2137 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("MADE"), CONNECTION_MADE
);
2138 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AWAITING_RESPONSE"), CONNECTION_AWAITING_RESPONSE
);
2139 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AUTH_OK"), CONNECTION_AUTH_OK
);
2140 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SSL_STARTUP"), CONNECTION_SSL_STARTUP
);
2141 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SETENV"), CONNECTION_SETENV
);
2142 #ifdef HAVE_CONNECTION_CHECK_WRITABLE
2143 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("CHECK_WRITABLE"), CONNECTION_CHECK_WRITABLE
);
2145 #ifdef HAVE_CONNECTION_CONSUME
2146 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("CONSUME"), CONNECTION_CONSUME
);
2148 #ifdef HAVE_CONNECTION_GSS_STARTUP
2149 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("GSS_STARTUP"), CONNECTION_GSS_STARTUP
);
2152 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_IDLE"), PQTRANS_IDLE
);
2153 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_ACTIVE"), PQTRANS_ACTIVE
);
2154 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INTRANS"), PQTRANS_INTRANS
);
2155 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INERROR"), PQTRANS_INERROR
);
2156 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_UNKNOWN"), PQTRANS_UNKNOWN
);
2158 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_FAILED"), PGRES_POLLING_FAILED
);
2159 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_READING"), PGRES_POLLING_READING
);
2160 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_WRITING"), PGRES_POLLING_WRITING
);
2161 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_OK"), PGRES_POLLING_OK
);
2163 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_NOTICE"), ZEND_STRL("notice"));
2164 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESULT"), ZEND_STRL("result"));
2165 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESET"), ZEND_STRL("reset"));
2167 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("ASYNC"), 0x1);
2168 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("PERSISTENT"), 0x2);
2170 PHP_PQ_G
->connection
.name
= zend_string_init(ZEND_STRL("pq\\Connection"), 1);
2172 return php_persistent_handle_provide(PHP_PQ_G
->connection
.name
, php_pqconn_get_resource_factory_ops(), NULL
, NULL
);
2180 * vim600: noet sw=4 ts=4 fdm=marker
2181 * vim<600: noet sw=4 ts=4