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 #ifdef HAVE_PQLIBVERSION
495 static void php_pqconn_object_read_lib_version(void *o
, zval
*return_value
)
499 php_pq_version_to_string(PQlibVersion(), ver
, sizeof(ver
));
503 #ifdef HAVE_PQPROTOCOLVERSION
504 static void php_pqconn_object_read_protocol_version(void *o
, zval
*return_value
)
506 php_pqconn_object_t
*obj
= o
;
508 RETVAL_LONG(PQprotocolVersion(obj
->intern
->conn
));
511 #ifdef HAVE_PQSERVERVERSION
512 static void php_pqconn_object_read_server_version(void *o
, zval
*return_value
)
514 php_pqconn_object_t
*obj
= o
;
517 php_pq_version_to_string(PQserverVersion(obj
->intern
->conn
), ver
, sizeof(ver
));
522 static ZEND_RESULT_CODE
php_pqconn_update_socket(zval
*zobj
, php_pqconn_object_t
*obj
)
524 zval zsocket
, zmember
;
526 ZEND_RESULT_CODE retval
;
530 obj
= PHP_PQ_OBJ(zobj
, NULL
);
533 ZVAL_STRINGL(&zmember
, "socket", sizeof("socket")-1);
535 if ((CONNECTION_BAD
!= PQstatus(obj
->intern
->conn
))
536 && (-1 < (socket
= PQsocket(obj
->intern
->conn
)))
537 && (stream
= php_stream_fopen_from_fd(socket
, "r+b", NULL
))) {
538 stream
->flags
|= PHP_STREAM_FLAG_NO_CLOSE
;
539 php_stream_to_zval(stream
, &zsocket
);
545 #if PHP_VERSION_ID >= 80000
546 zend_std_write_property(Z_OBJ_P(zobj
), Z_STR(zmember
), &zsocket
, NULL
);
548 zend_std_write_property(zobj
, &zmember
, &zsocket
, NULL
);
550 zval_ptr_dtor(&zsocket
);
551 zval_ptr_dtor(&zmember
);
556 static void *php_pqconn_resource_factory_ctor(void *data
, void *init_arg
)
558 php_pqconn_resource_factory_data_t
*o
= init_arg
;
559 PGconn
*conn
= NULL
;;
561 if (o
->flags
& PHP_PQCONN_ASYNC
) {
562 conn
= PQconnectStart(o
->dsn
);
564 conn
= PQconnectdb(o
->dsn
);
568 PQregisterEventProc(conn
, php_pqconn_event
, "ext-pq", NULL
);
574 static void php_pqconn_resource_factory_dtor(void *opaque
, void *handle
)
576 php_pqconn_event_data_t
*evdata
= PQinstanceData(handle
, php_pqconn_event
);
578 /* we don't care for anything, except free'ing evdata */
580 PQsetInstanceData(handle
, php_pqconn_event
, NULL
);
581 memset(evdata
, 0, sizeof(*evdata
));
588 static php_resource_factory_ops_t php_pqconn_resource_factory_ops
= {
589 php_pqconn_resource_factory_ctor
,
591 php_pqconn_resource_factory_dtor
594 php_resource_factory_ops_t
*php_pqconn_get_resource_factory_ops(void)
596 return &php_pqconn_resource_factory_ops
;
599 static void php_pqconn_wakeup(php_persistent_handle_factory_t
*f
, void **handle
)
601 PGresult
*res
= PQexec(*handle
, "");
602 php_pqres_clear(res
);
604 if (CONNECTION_OK
!= PQstatus(*handle
)) {
609 static inline PGresult
*unlisten(PGconn
*conn
, const char *channel_str
, size_t channel_len
)
611 char *quoted_channel
= PQescapeIdentifier(conn
, channel_str
, channel_len
);
612 PGresult
*res
= NULL
;
614 if (quoted_channel
) {
617 smart_str_appends(&cmd
, "UNLISTEN ");
618 smart_str_appends(&cmd
, quoted_channel
);
621 res
= php_pq_exec(conn
, smart_str_v(&cmd
));
623 smart_str_free(&cmd
);
624 PQfreemem(quoted_channel
);
630 static int apply_unlisten(zval
*p
, int argc
, va_list argv
, zend_hash_key
*key
)
632 php_pqconn_object_t
*obj
= va_arg(argv
, php_pqconn_object_t
*);
633 PGresult
*res
= unlisten(obj
->intern
->conn
, key
->key
->val
, key
->key
->len
);
636 php_pqres_clear(res
);
639 return ZEND_HASH_APPLY_REMOVE
;
642 static void php_pqconn_retire(php_persistent_handle_factory_t
*f
, void **handle
)
644 php_pqconn_event_data_t
*evdata
= PQinstanceData(*handle
, php_pqconn_event
);
649 PQsetInstanceData(*handle
, php_pqconn_event
, NULL
);
652 PQsetNoticeReceiver(*handle
, php_pqconn_notice_ignore
, NULL
);
654 /* cancel async queries */
655 if (PQisBusy(*handle
) && (cancel
= PQgetCancel(*handle
))) {
658 PQcancel(cancel
, err
, sizeof(err
));
659 PQfreeCancel(cancel
);
661 /* clean up async results */
662 while ((res
= PQgetResult(*handle
))) {
663 php_pqres_clear(res
);
666 /* clean up transaction & session */
667 switch (PQtransactionStatus(*handle
)) {
669 res
= PQexec(*handle
, "RESET ALL");
672 res
= PQexec(*handle
, "ROLLBACK; RESET ALL");
677 php_pqres_clear(res
);
681 /* clean up notify listeners */
682 zend_hash_apply_with_arguments(&evdata
->obj
->intern
->listeners
, apply_unlisten
, 1, evdata
->obj
);
684 /* release instance data */
689 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct
, 0, 0, 1)
690 ZEND_ARG_INFO(0, dsn
)
691 ZEND_ARG_INFO(0, flags
)
693 static PHP_METHOD(pqconn
, __construct
) {
694 zend_error_handling zeh
;
700 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
701 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "|sl", &dsn_str
, &dsn_len
, &flags
);
702 zend_restore_error_handling(&zeh
);
705 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
708 throw_exce(EX_BAD_METHODCALL
, "pq\\Connection already initialized");
710 php_pqconn_event_data_t
*evdata
= php_pqconn_event_data_init(obj
);
711 php_pqconn_resource_factory_data_t rfdata
= {dsn_str
, flags
};
713 obj
->intern
= ecalloc(1, sizeof(*obj
->intern
));
715 obj
->intern
->default_auto_convert
= PHP_PQRES_CONV_ALL
;
717 zend_hash_init(&obj
->intern
->listeners
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
718 zend_hash_init(&obj
->intern
->statements
, 0, NULL
, NULL
, 0);
719 zend_hash_init(&obj
->intern
->converters
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
720 zend_hash_init(&obj
->intern
->eventhandlers
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
722 if (flags
& PHP_PQCONN_PERSISTENT
) {
723 zend_string
*dsn
= zend_string_init(dsn_str
, dsn_len
, 0);
724 php_persistent_handle_factory_t
*phf
= php_persistent_handle_concede(NULL
, PHP_PQ_G
->connection
.name
, dsn
, php_pqconn_wakeup
, php_pqconn_retire
);
725 php_persistent_handle_resource_factory_init(&obj
->intern
->factory
, phf
);
726 zend_string_release(dsn
);
728 php_resource_factory_init(&obj
->intern
->factory
, &php_pqconn_resource_factory_ops
, NULL
, NULL
);
731 if (flags
& PHP_PQCONN_ASYNC
) {
732 obj
->intern
->poller
= (int (*)(PGconn
*)) PQconnectPoll
;
735 obj
->intern
->conn
= php_resource_factory_handle_ctor(&obj
->intern
->factory
, &rfdata
);
737 PQsetInstanceData(obj
->intern
->conn
, php_pqconn_event
, evdata
);
738 PQsetNoticeReceiver(obj
->intern
->conn
, php_pqconn_notice_recv
, evdata
);
740 if (SUCCESS
!= php_pqconn_update_socket(getThis(), obj
)) {
741 throw_exce(EX_CONNECTION_FAILED
, "Connection failed (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
747 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset
, 0, 0, 0)
749 static PHP_METHOD(pqconn
, reset
) {
750 zend_error_handling zeh
;
753 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
754 rv
= zend_parse_parameters_none();
755 zend_restore_error_handling(&zeh
);
758 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
761 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
763 PQreset(obj
->intern
->conn
);
765 if (CONNECTION_OK
!= PQstatus(obj
->intern
->conn
)) {
766 throw_exce(EX_CONNECTION_FAILED
, "Connection reset failed: (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
769 php_pqconn_notify_listeners(obj
);
774 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset_async
, 0, 0, 0)
776 static PHP_METHOD(pqconn
, resetAsync
) {
777 zend_error_handling zeh
;
780 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
781 rv
= zend_parse_parameters_none();
782 zend_restore_error_handling(&zeh
);
785 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
788 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
790 if (!PQresetStart(obj
->intern
->conn
)) {
791 throw_exce(EX_IO
, "Failed to start connection reset (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
793 obj
->intern
->poller
= (int (*)(PGconn
*)) PQresetPoll
;
796 php_pqconn_notify_listeners(obj
);
801 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten
, 0, 0, 1)
802 ZEND_ARG_INFO(0, channel
)
804 static PHP_METHOD(pqconn
, unlisten
)
806 zend_error_handling zeh
;
811 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
812 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s", &channel_str
, &channel_len
);
813 zend_restore_error_handling(&zeh
);
816 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
819 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
820 } else if (SUCCESS
== zend_hash_str_del(&obj
->intern
->listeners
, channel_str
, channel_len
)) {
821 PGresult
*res
= unlisten(obj
->intern
->conn
, channel_str
, channel_len
);
824 php_pqres_success(res
);
825 php_pqres_clear(res
);
831 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten_async
, 0, 0, 1)
832 ZEND_ARG_INFO(0, channel
)
834 static PHP_METHOD(pqconn
, unlistenAsync
) {
835 zend_error_handling zeh
;
840 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
841 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s", &channel_str
, &channel_len
);
842 zend_restore_error_handling(&zeh
);
845 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
848 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
850 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
852 if (!quoted_channel
) {
853 throw_exce(EX_ESCAPE
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
857 smart_str_appends(&cmd
, "UNLISTEN ");
858 smart_str_appends(&cmd
, quoted_channel
);
861 if (!PQsendQuery(obj
->intern
->conn
, smart_str_v(&cmd
))) {
862 throw_exce(EX_IO
, "Failed to uninstall listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
864 obj
->intern
->poller
= PQconsumeInput
;
865 zend_hash_str_del(&obj
->intern
->listeners
, channel_str
, channel_len
);
868 smart_str_free(&cmd
);
869 PQfreemem(quoted_channel
);
870 php_pqconn_notify_listeners(obj
);
876 static void php_pqconn_add_listener(php_pqconn_object_t
*obj
, const char *channel_str
, size_t channel_len
, php_pq_callback_t
*listener
)
880 php_pq_callback_addref(listener
);
882 if ((existing
= zend_hash_str_find(&obj
->intern
->listeners
, channel_str
, channel_len
))) {
883 zend_hash_next_index_insert_mem(Z_ARRVAL_P(existing
), (void *) listener
, sizeof(*listener
));
889 zend_hash_init(ht
, 0, NULL
, php_pq_callback_hash_dtor
, 0);
890 zend_hash_next_index_insert_mem(ht
, (void *) listener
, sizeof(*listener
));
893 zend_hash_str_add(&obj
->intern
->listeners
, channel_str
, channel_len
, &tmp
);
897 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen
, 0, 0, 2)
898 ZEND_ARG_INFO(0, channel
)
899 ZEND_ARG_INFO(0, callable
)
901 static PHP_METHOD(pqconn
, listen
) {
902 zend_error_handling zeh
;
903 char *channel_str
= NULL
;
904 size_t channel_len
= 0;
905 php_pq_callback_t listener
= PHP_PQ_CALLBACK_INIT
;
908 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
909 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
910 zend_restore_error_handling(&zeh
);
913 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
916 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
918 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
920 if (!quoted_channel
) {
921 throw_exce(EX_ESCAPE
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
926 smart_str_appends(&cmd
, "LISTEN ");
927 smart_str_appends(&cmd
, quoted_channel
);
930 res
= php_pq_exec(obj
->intern
->conn
, smart_str_v(&cmd
));
932 smart_str_free(&cmd
);
933 PQfreemem(quoted_channel
);
936 throw_exce(EX_RUNTIME
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
938 if (SUCCESS
== php_pqres_success(res
)) {
939 obj
->intern
->poller
= PQconsumeInput
;
940 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener
);
942 php_pqres_clear(res
);
945 php_pqconn_notify_listeners(obj
);
951 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen_async
, 0, 0, 0)
952 ZEND_ARG_INFO(0, channel
)
953 ZEND_ARG_INFO(0, callable
)
955 static PHP_METHOD(pqconn
, listenAsync
) {
956 zend_error_handling zeh
;
957 char *channel_str
= NULL
;
958 size_t channel_len
= 0;
959 php_pq_callback_t listener
= PHP_PQ_CALLBACK_INIT
;
962 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
963 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
964 zend_restore_error_handling(&zeh
);
967 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
970 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
972 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
974 if (!quoted_channel
) {
975 throw_exce(EX_ESCAPE
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
979 smart_str_appends(&cmd
, "LISTEN ");
980 smart_str_appends(&cmd
, quoted_channel
);
983 if (!PQsendQuery(obj
->intern
->conn
, smart_str_v(&cmd
))) {
984 throw_exce(EX_IO
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
986 obj
->intern
->poller
= PQconsumeInput
;
987 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener
);
990 smart_str_free(&cmd
);
991 PQfreemem(quoted_channel
);
992 php_pqconn_notify_listeners(obj
);
998 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify
, 0, 0, 2)
999 ZEND_ARG_INFO(0, channel
)
1000 ZEND_ARG_INFO(0, message
)
1001 ZEND_END_ARG_INFO();
1002 static PHP_METHOD(pqconn
, notify
) {
1003 zend_error_handling zeh
;
1004 char *channel_str
, *message_str
;
1005 size_t channel_len
, message_len
;
1006 ZEND_RESULT_CODE rv
;
1008 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1009 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
1010 zend_restore_error_handling(&zeh
);
1012 if (SUCCESS
== rv
) {
1013 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1016 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1019 char *params
[2] = {channel_str
, message_str
};
1021 res
= php_pq_exec_params(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0);
1024 throw_exce(EX_RUNTIME
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1026 php_pqres_success(res
);
1027 php_pqres_clear(res
);
1030 php_pqconn_notify_listeners(obj
);
1035 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify_async
, 0, 0, 2)
1036 ZEND_ARG_INFO(0, channel
)
1037 ZEND_ARG_INFO(0, message
)
1038 ZEND_END_ARG_INFO();
1039 static PHP_METHOD(pqconn
, notifyAsync
) {
1040 zend_error_handling zeh
;
1041 char *channel_str
, *message_str
;
1042 size_t channel_len
, message_len
;
1043 ZEND_RESULT_CODE rv
;
1045 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1046 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
1047 zend_restore_error_handling(&zeh
);
1049 if (SUCCESS
== rv
) {
1050 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1053 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1055 char *params
[2] = {channel_str
, message_str
};
1057 if (!PQsendQueryParams(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0)) {
1058 throw_exce(EX_IO
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1060 obj
->intern
->poller
= PQconsumeInput
;
1063 php_pqconn_notify_listeners(obj
);
1068 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_poll
, 0, 0, 0)
1069 ZEND_END_ARG_INFO();
1070 static PHP_METHOD(pqconn
, poll
) {
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 if (obj
->intern
->poller
== PQconsumeInput
) {
1087 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
) * PGRES_POLLING_OK
);
1089 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
));
1091 php_pqconn_notify_listeners(obj
);
1096 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_flush
, 0, 0, 0)
1097 ZEND_END_ARG_INFO();
1098 static PHP_METHOD(pqconn
, flush
) {
1099 zend_error_handling zeh
;
1100 ZEND_RESULT_CODE rv
;
1102 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1103 rv
= zend_parse_parameters_none();
1104 zend_restore_error_handling(&zeh
);
1106 if (SUCCESS
== rv
) {
1107 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1110 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1111 } else if (!obj
->intern
->poller
) {
1112 throw_exce(EX_RUNTIME
, "No asynchronous operation active");
1114 switch (PQflush(obj
->intern
->conn
)) {
1117 throw_exce(EX_RUNTIME
, "Failed to flush connection: %s", PHP_PQerrorMessage(obj
->intern
->conn
));
1130 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec
, 0, 0, 1)
1131 ZEND_ARG_INFO(0, query
)
1132 ZEND_END_ARG_INFO();
1133 static PHP_METHOD(pqconn
, exec
) {
1134 zend_error_handling zeh
;
1137 ZEND_RESULT_CODE rv
;
1139 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1140 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s", &query_str
, &query_len
);
1141 zend_restore_error_handling(&zeh
);
1143 if (SUCCESS
== rv
) {
1144 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1147 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1149 PGresult
*res
= php_pq_exec(obj
->intern
->conn
, query_str
);
1152 throw_exce(EX_RUNTIME
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1153 } else if (SUCCESS
== php_pqres_success(res
)) {
1154 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), return_value
);
1156 php_pqres_clear(res
);
1159 php_pqconn_notify_listeners(obj
);
1164 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_get_result
, 0, 0, 0)
1165 ZEND_END_ARG_INFO();
1166 static PHP_METHOD(pqconn
, getResult
) {
1167 zend_error_handling zeh
;
1168 ZEND_RESULT_CODE rv
;
1170 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1171 rv
= zend_parse_parameters_none();
1172 zend_restore_error_handling(&zeh
);
1174 if (SUCCESS
== rv
) {
1175 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1178 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1180 PGresult
*res
= PQgetResult(obj
->intern
->conn
);
1181 php_pq_object_t
*res_obj
;
1183 if (res
&& (res_obj
= PQresultInstanceData(res
, php_pqconn_event
))) {
1184 php_pq_object_to_zval_no_addref(res_obj
, return_value
);
1189 php_pqconn_notify_listeners(obj
);
1194 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_async
, 0, 0, 1)
1195 ZEND_ARG_INFO(0, query
)
1196 ZEND_ARG_INFO(0, callable
)
1197 ZEND_END_ARG_INFO();
1198 static PHP_METHOD(pqconn
, execAsync
) {
1199 zend_error_handling zeh
;
1200 php_pq_callback_t resolver
= PHP_PQ_CALLBACK_INIT
;
1203 ZEND_RESULT_CODE rv
;
1205 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1206 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s|f", &query_str
, &query_len
, &resolver
.fci
, &resolver
.fcc
);
1207 zend_restore_error_handling(&zeh
);
1209 if (SUCCESS
== rv
) {
1210 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1213 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1214 } else if (!PQsendQuery(obj
->intern
->conn
, query_str
)) {
1215 throw_exce(EX_IO
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1216 #if HAVE_PQSETSINGLEROWMODE
1217 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1218 throw_exce(EX_RUNTIME
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1221 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver
);
1222 obj
->intern
->poller
= PQconsumeInput
;
1223 php_pqconn_notify_listeners(obj
);
1228 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params
, 0, 0, 2)
1229 ZEND_ARG_INFO(0, query
)
1230 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1231 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1232 ZEND_END_ARG_INFO();
1233 static PHP_METHOD(pqconn
, execParams
) {
1234 zend_error_handling zeh
;
1238 zval
*ztypes
= NULL
;
1239 ZEND_RESULT_CODE rv
;
1241 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1242 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sa/|a/!", &query_str
, &query_len
, &zparams
, &ztypes
);
1243 zend_restore_error_handling(&zeh
);
1245 if (SUCCESS
== rv
) {
1246 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1249 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1252 php_pq_params_t
*params
;
1254 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
));
1255 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);
1256 php_pq_params_free(¶ms
);
1259 throw_exce(EX_RUNTIME
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1261 if (SUCCESS
== php_pqres_success(res
)) {
1262 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), return_value
);
1264 php_pqres_clear(res
);
1267 php_pqconn_notify_listeners(obj
);
1273 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async
, 0, 0, 2)
1274 ZEND_ARG_INFO(0, query
)
1275 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1276 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1277 ZEND_ARG_INFO(0, callable
)
1278 ZEND_END_ARG_INFO();
1279 static PHP_METHOD(pqconn
, execParamsAsync
) {
1280 zend_error_handling zeh
;
1281 php_pq_callback_t resolver
= PHP_PQ_CALLBACK_INIT
;
1285 zval
*ztypes
= NULL
;
1286 ZEND_RESULT_CODE rv
;
1288 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1289 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sa/|a/!f", &query_str
, &query_len
, &zparams
, &ztypes
, &resolver
.fci
, &resolver
.fcc
);
1290 zend_restore_error_handling(&zeh
);
1292 if (SUCCESS
== rv
) {
1293 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1296 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1299 php_pq_params_t
*params
;
1301 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
));
1302 rc
= PQsendQueryParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1303 php_pq_params_free(¶ms
);
1306 throw_exce(EX_IO
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1307 #if HAVE_PQSETSINGLEROWMODE
1308 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1309 throw_exce(EX_RUNTIME
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1312 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver
);
1313 obj
->intern
->poller
= PQconsumeInput
;
1314 php_pqconn_notify_listeners(obj
);
1318 zend_restore_error_handling(&zeh
);
1321 ZEND_RESULT_CODE
php_pqconn_prepare(zval
*object
, php_pqconn_object_t
*obj
, const char *name
, const char *query
, php_pq_params_t
*params
)
1324 ZEND_RESULT_CODE rv
;
1327 obj
= PHP_PQ_OBJ(object
, NULL
);
1330 res
= php_pq_prepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
);
1334 throw_exce(EX_RUNTIME
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1336 rv
= php_pqres_success(res
);
1337 php_pqres_clear(res
);
1338 php_pqconn_notify_listeners(obj
);
1344 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare
, 0, 0, 2)
1345 ZEND_ARG_INFO(0, name
)
1346 ZEND_ARG_INFO(0, query
)
1347 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1348 ZEND_END_ARG_INFO();
1349 static PHP_METHOD(pqconn
, prepare
) {
1350 zend_error_handling zeh
;
1351 zval
*ztypes
= NULL
;
1352 char *name_str
, *query_str
;
1353 size_t name_len
, *query_len
;
1354 ZEND_RESULT_CODE rv
;
1356 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1357 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1358 zend_restore_error_handling(&zeh
);
1360 if (SUCCESS
== rv
) {
1361 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1364 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1366 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL
);
1368 if (SUCCESS
!= php_pqconn_prepare(getThis(), obj
, name_str
, query_str
, params
)) {
1369 php_pq_params_free(¶ms
);
1371 php_pqstm_t
*stm
= php_pqstm_init(obj
, name_str
, query_str
, params
);
1373 RETVAL_OBJ(&php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
)->zo
);
1379 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
)
1381 ZEND_RESULT_CODE rv
;
1384 obj
= PHP_PQ_OBJ(object
, NULL
);
1387 if (!PQsendPrepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
)) {
1389 throw_exce(EX_IO
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1392 obj
->intern
->poller
= PQconsumeInput
;
1393 php_pqconn_notify_listeners(obj
);
1399 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare_async
, 0, 0, 2)
1400 ZEND_ARG_INFO(0, name
)
1401 ZEND_ARG_INFO(0, query
)
1402 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1403 ZEND_END_ARG_INFO();
1404 static PHP_METHOD(pqconn
, prepareAsync
) {
1405 zend_error_handling zeh
;
1406 zval
*ztypes
= NULL
;
1407 char *name_str
, *query_str
;
1408 size_t name_len
, *query_len
;
1409 ZEND_RESULT_CODE rv
;
1411 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1412 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1413 zend_restore_error_handling(&zeh
);
1415 if (SUCCESS
== rv
) {
1416 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1419 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1421 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL
);
1423 if (SUCCESS
!= php_pqconn_prepare_async(getThis(), obj
, name_str
, query_str
, params
)) {
1424 php_pq_params_free(¶ms
);
1426 php_pqstm_t
*stm
= php_pqstm_init(obj
, name_str
, query_str
, params
);
1428 RETVAL_OBJ(&php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
)->zo
);
1434 ZEND_RESULT_CODE
php_pqconn_declare(zval
*object
, php_pqconn_object_t
*obj
, const char *decl
)
1437 ZEND_RESULT_CODE rv
;
1440 obj
= PHP_PQ_OBJ(object
, NULL
);
1443 res
= php_pq_exec(obj
->intern
->conn
, decl
);
1447 throw_exce(EX_RUNTIME
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1449 rv
= php_pqres_success(res
);
1450 php_pqres_clear(res
);
1451 php_pqconn_notify_listeners(obj
);
1457 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare
, 0, 0, 3)
1458 ZEND_ARG_INFO(0, name
)
1459 ZEND_ARG_INFO(0, flags
)
1460 ZEND_ARG_INFO(0, query
)
1461 ZEND_END_ARG_INFO();
1462 static PHP_METHOD(pqconn
, declare
) {
1463 zend_error_handling zeh
;
1464 char *name_str
, *query_str
;
1465 size_t name_len
, query_len
;
1467 ZEND_RESULT_CODE rv
;
1469 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1470 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1471 zend_restore_error_handling(&zeh
);
1473 if (SUCCESS
== rv
) {
1474 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1477 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1480 char *decl
= php_pqcur_declare_str(name_str
, name_len
, flags
, query_str
, query_len
, &query_offset
);
1482 if (SUCCESS
!= php_pqconn_declare(getThis(), obj
, decl
)) {
1485 php_pqcur_t
*cur
= php_pqcur_init(obj
, name_str
, decl
, query_offset
, flags
);
1487 RETVAL_OBJ(&php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
)->zo
);
1493 ZEND_RESULT_CODE
php_pqconn_declare_async(zval
*object
, php_pqconn_object_t
*obj
, const char *decl
)
1495 ZEND_RESULT_CODE rv
;
1498 obj
= PHP_PQ_OBJ(object
, NULL
);
1501 if (!PQsendQuery(obj
->intern
->conn
, decl
)) {
1503 throw_exce(EX_IO
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1506 obj
->intern
->poller
= PQconsumeInput
;
1507 php_pqconn_notify_listeners(obj
);
1513 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare_async
, 0, 0, 2)
1514 ZEND_ARG_INFO(0, name
)
1515 ZEND_ARG_INFO(0, flags
)
1516 ZEND_ARG_INFO(0, query
)
1517 ZEND_END_ARG_INFO();
1518 static PHP_METHOD(pqconn
, declareAsync
) {
1519 zend_error_handling zeh
;
1520 char *name_str
, *query_str
;
1521 size_t name_len
, query_len
;
1523 ZEND_RESULT_CODE rv
;
1525 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1526 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1527 zend_restore_error_handling(&zeh
);
1529 if (SUCCESS
== rv
) {
1530 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1533 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1536 char *decl
= php_pqcur_declare_str(name_str
, name_len
, flags
, query_str
, query_len
, &query_offset
);
1538 if (SUCCESS
!= php_pqconn_declare_async(getThis(), obj
, decl
)) {
1541 php_pqcur_t
*cur
= php_pqcur_init(obj
, name_str
, decl
, query_offset
, flags
);
1543 RETVAL_OBJ(&php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
)->zo
);
1549 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote
, 0, 0, 1)
1550 ZEND_ARG_INFO(0, string
)
1551 ZEND_END_ARG_INFO();
1552 static PHP_METHOD(pqconn
, quote
) {
1556 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1557 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1560 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1562 char *quoted
= PQescapeLiteral(obj
->intern
->conn
, str
, len
);
1565 php_error_docref(NULL
, E_WARNING
, "Failed to quote string (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1568 RETVAL_STRING(quoted
);
1575 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote_name
, 0, 0, 1)
1576 ZEND_ARG_INFO(0, type
)
1577 ZEND_END_ARG_INFO();
1578 static PHP_METHOD(pqconn
, quoteName
) {
1582 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1583 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1586 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1588 char *quoted
= PQescapeIdentifier(obj
->intern
->conn
, str
, len
);
1591 php_error_docref(NULL
, E_WARNING
, "Failed to quote name (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1594 RETVAL_STRING(quoted
);
1601 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_escape_bytea
, 0, 0, 1)
1602 ZEND_ARG_INFO(0, bytea
)
1603 ZEND_END_ARG_INFO();
1604 static PHP_METHOD(pqconn
, escapeBytea
) {
1608 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1609 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1612 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1615 char *escaped_str
= (char *) PQescapeByteaConn(obj
->intern
->conn
, (unsigned char *) str
, len
, &escaped_len
);
1618 php_error_docref(NULL
, E_WARNING
, "Failed to escape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1621 RETVAL_STRINGL(escaped_str
, escaped_len
- 1);
1622 PQfreemem(escaped_str
);
1628 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unescape_bytea
, 0, 0, 1)
1629 ZEND_ARG_INFO(0, bytea
)
1630 ZEND_END_ARG_INFO();
1631 static PHP_METHOD(pqconn
, unescapeBytea
) {
1635 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1636 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1639 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1641 size_t unescaped_len
;
1642 char *unescaped_str
= (char *) PQunescapeBytea((unsigned char *)str
, &unescaped_len
);
1644 if (!unescaped_str
) {
1645 php_error_docref(NULL
, E_WARNING
, "Failed to unescape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1648 RETVAL_STRINGL(unescaped_str
, unescaped_len
);
1649 PQfreemem(unescaped_str
);
1655 ZEND_RESULT_CODE
php_pqconn_start_transaction(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable
)
1657 ZEND_RESULT_CODE rv
= FAILURE
;
1660 conn_obj
= PHP_PQ_OBJ(zconn
, NULL
);
1663 if (!conn_obj
->intern
) {
1664 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1667 smart_str cmd
= {0};
1668 const char *il
= php_pq_isolation_level(&isolation
);
1670 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1671 smart_str_appends(&cmd
, il
);
1672 smart_str_appends(&cmd
, ", READ ");
1673 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1674 smart_str_appends(&cmd
, ",");
1675 smart_str_appends(&cmd
, deferrable
? "" : " NOT");
1676 smart_str_appends(&cmd
, " DEFERRABLE");
1679 res
= php_pq_exec(conn_obj
->intern
->conn
, smart_str_v(&cmd
));
1682 throw_exce(EX_RUNTIME
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1684 rv
= php_pqres_success(res
);
1685 php_pqres_clear(res
);
1686 php_pqconn_notify_listeners(conn_obj
);
1689 smart_str_free(&cmd
);
1695 ZEND_RESULT_CODE
php_pqconn_start_transaction_async(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable
)
1697 ZEND_RESULT_CODE rv
= FAILURE
;
1700 conn_obj
= PHP_PQ_OBJ(zconn
, NULL
);
1703 if (!conn_obj
->intern
) {
1704 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1706 smart_str cmd
= {0};
1707 const char *il
= php_pq_isolation_level(&isolation
);
1709 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1710 smart_str_appends(&cmd
, il
);
1711 smart_str_appends(&cmd
, ", READ ");
1712 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1713 smart_str_appends(&cmd
, ",");
1714 smart_str_appends(&cmd
, deferrable
? "" : "NOT ");
1715 smart_str_appends(&cmd
, " DEFERRABLE");
1718 if (!PQsendQuery(conn_obj
->intern
->conn
, smart_str_v(&cmd
))) {
1719 throw_exce(EX_IO
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1722 conn_obj
->intern
->poller
= PQconsumeInput
;
1723 php_pqconn_notify_listeners(conn_obj
);
1726 smart_str_free(&cmd
);
1732 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction
, 0, 0, 0)
1733 ZEND_ARG_INFO(0, isolation
)
1734 ZEND_ARG_INFO(0, readonly
)
1735 ZEND_ARG_INFO(0, deferrable
)
1736 ZEND_END_ARG_INFO();
1737 static PHP_METHOD(pqconn
, startTransaction
) {
1738 zend_error_handling zeh
;
1739 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1740 zend_long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1741 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1742 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1743 ZEND_RESULT_CODE rv
;
1745 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1746 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "|lbb", &isolation
, &readonly
, &deferrable
);
1747 zend_restore_error_handling(&zeh
);
1749 if (SUCCESS
== rv
) {
1750 rv
= php_pqconn_start_transaction(getThis(), obj
, isolation
, readonly
, deferrable
);
1752 if (SUCCESS
== rv
) {
1753 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1755 php_pq_object_addref(obj
);
1758 txn
->isolation
= isolation
;
1759 txn
->readonly
= readonly
;
1760 txn
->deferrable
= deferrable
;
1762 RETVAL_OBJ(&php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
)->zo
);
1767 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction_async
, 0, 0, 0)
1768 ZEND_ARG_INFO(0, isolation
)
1769 ZEND_ARG_INFO(0, readonly
)
1770 ZEND_ARG_INFO(0, deferrable
)
1771 ZEND_END_ARG_INFO();
1772 static PHP_METHOD(pqconn
, startTransactionAsync
) {
1773 zend_error_handling zeh
;
1774 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1775 zend_long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1776 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1777 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1778 ZEND_RESULT_CODE rv
;
1780 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1781 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "|lbb", &isolation
, &readonly
, &deferrable
);
1782 zend_restore_error_handling(&zeh
);
1784 if (SUCCESS
== rv
) {
1785 rv
= php_pqconn_start_transaction_async(getThis(), obj
, isolation
, readonly
, deferrable
);
1787 if (SUCCESS
== rv
) {
1788 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1790 php_pq_object_addref(obj
);
1793 txn
->isolation
= isolation
;
1794 txn
->readonly
= readonly
;
1795 txn
->deferrable
= deferrable
;
1797 RETVAL_OBJ(&php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
)->zo
);
1802 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_trace
, 0, 0, 0)
1803 ZEND_ARG_INFO(0, stdio_stream
)
1804 ZEND_END_ARG_INFO();
1805 static PHP_METHOD(pqconn
, trace
) {
1806 zval
*zstream
= NULL
;
1808 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &zstream
)) {
1809 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1812 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1815 PQuntrace(obj
->intern
->conn
);
1819 php_stream
*stream
= NULL
;
1821 php_stream_from_zval(stream
, zstream
);
1823 if (SUCCESS
!= php_stream_cast(stream
, PHP_STREAM_AS_STDIO
, (void *) &fp
, REPORT_ERRORS
)) {
1826 stream
->flags
|= PHP_STREAM_FLAG_NO_CLOSE
;
1827 PQtrace(obj
->intern
->conn
, fp
);
1835 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_off
, 0, 0, 1)
1836 ZEND_ARG_INFO(0, type
)
1837 ZEND_END_ARG_INFO();
1838 static PHP_METHOD(pqconn
, off
) {
1839 zend_error_handling zeh
;
1841 ZEND_RESULT_CODE rv
;
1843 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1844 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "S", &type
);
1845 zend_restore_error_handling(&zeh
);
1847 if (SUCCESS
== rv
) {
1848 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1851 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1853 RETURN_BOOL(SUCCESS
== zend_hash_del(&obj
->intern
->eventhandlers
, type
));
1858 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_on
, 0, 0, 2)
1859 ZEND_ARG_INFO(0, type
)
1860 ZEND_ARG_INFO(0, callable
)
1861 ZEND_END_ARG_INFO();
1862 static PHP_METHOD(pqconn
, on
) {
1863 zend_error_handling zeh
;
1866 php_pq_callback_t cb
= PHP_PQ_CALLBACK_INIT
;
1867 ZEND_RESULT_CODE rv
;
1869 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1870 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sf", &type_str
, &type_len
, &cb
.fci
, &cb
.fcc
);
1871 zend_restore_error_handling(&zeh
);
1873 if (SUCCESS
== rv
) {
1874 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1877 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1879 RETVAL_LONG(php_pqconn_add_eventhandler(obj
, type_str
, type_len
, &cb
));
1884 struct apply_set_converter_arg
{
1890 static int apply_set_converter(zval
*zoid
, void *a
)
1892 zend_long oid
= zval_get_long(zoid
);
1893 struct apply_set_converter_arg
*arg
= a
;
1896 Z_ADDREF_P(arg
->zconv
);
1897 zend_hash_index_update(arg
->ht
, oid
, arg
->zconv
);
1899 zend_hash_index_del(arg
->ht
, oid
);
1902 return ZEND_HASH_APPLY_KEEP
;
1905 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_set_converter
, 0, 0, 1)
1906 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\Converter
, 0)
1907 ZEND_END_ARG_INFO();
1908 static PHP_METHOD(pqconn
, setConverter
) {
1909 ZEND_RESULT_CODE rv
;
1910 zend_error_handling zeh
;
1913 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1914 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zcnv
, php_pqconv_class_entry
);
1915 zend_restore_error_handling(&zeh
);
1917 if (SUCCESS
== rv
) {
1918 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1921 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1924 struct apply_set_converter_arg arg
= {NULL
};
1927 php_pq_call_method(zcnv
, "converttypes", 0, &zoids
);
1928 ZVAL_DUP(&tmp
, &zoids
);
1929 convert_to_array(&tmp
);
1931 arg
.ht
= &obj
->intern
->converters
;
1935 zend_hash_apply_with_argument(Z_ARRVAL(tmp
), apply_set_converter
, &arg
);
1937 zval_ptr_dtor(&tmp
);
1938 zval_ptr_dtor(&zoids
);
1943 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unset_converter
, 0, 0, 1)
1944 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\Converter
, 0)
1945 ZEND_END_ARG_INFO();
1946 static PHP_METHOD(pqconn
, unsetConverter
) {
1947 ZEND_RESULT_CODE rv
;
1948 zend_error_handling zeh
;
1951 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1952 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zcnv
, php_pqconv_class_entry
);
1953 zend_restore_error_handling(&zeh
);
1955 if (SUCCESS
== rv
) {
1956 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1959 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1962 struct apply_set_converter_arg arg
= {NULL
};
1965 php_pq_call_method(zcnv
, "converttypes", 0, &zoids
);
1966 ZVAL_DUP(&tmp
, &zoids
);
1967 convert_to_array(&tmp
);
1969 arg
.ht
= &obj
->intern
->converters
;
1973 zend_hash_apply_with_argument(Z_ARRVAL(tmp
), apply_set_converter
, &arg
);
1975 zval_ptr_dtor(&tmp
);
1976 zval_ptr_dtor(&zoids
);
1981 static zend_function_entry php_pqconn_methods
[] = {
1982 PHP_ME(pqconn
, __construct
, ai_pqconn_construct
, ZEND_ACC_PUBLIC
)
1983 PHP_ME(pqconn
, reset
, ai_pqconn_reset
, ZEND_ACC_PUBLIC
)
1984 PHP_ME(pqconn
, resetAsync
, ai_pqconn_reset_async
, ZEND_ACC_PUBLIC
)
1985 PHP_ME(pqconn
, poll
, ai_pqconn_poll
, ZEND_ACC_PUBLIC
)
1986 PHP_ME(pqconn
, flush
, ai_pqconn_flush
, ZEND_ACC_PUBLIC
)
1987 PHP_ME(pqconn
, exec
, ai_pqconn_exec
, ZEND_ACC_PUBLIC
)
1988 PHP_ME(pqconn
, execAsync
, ai_pqconn_exec_async
, ZEND_ACC_PUBLIC
)
1989 PHP_ME(pqconn
, execParams
, ai_pqconn_exec_params
, ZEND_ACC_PUBLIC
)
1990 PHP_ME(pqconn
, execParamsAsync
, ai_pqconn_exec_params_async
, ZEND_ACC_PUBLIC
)
1991 PHP_ME(pqconn
, prepare
, ai_pqconn_prepare
, ZEND_ACC_PUBLIC
)
1992 PHP_ME(pqconn
, prepareAsync
, ai_pqconn_prepare_async
, ZEND_ACC_PUBLIC
)
1993 PHP_ME(pqconn
, declare
, ai_pqconn_declare
, ZEND_ACC_PUBLIC
)
1994 PHP_ME(pqconn
, declareAsync
, ai_pqconn_declare_async
, ZEND_ACC_PUBLIC
)
1995 PHP_ME(pqconn
, unlisten
, ai_pqconn_unlisten
, ZEND_ACC_PUBLIC
)
1996 PHP_ME(pqconn
, unlistenAsync
, ai_pqconn_unlisten_async
, ZEND_ACC_PUBLIC
)
1997 PHP_ME(pqconn
, listen
, ai_pqconn_listen
, ZEND_ACC_PUBLIC
)
1998 PHP_ME(pqconn
, listenAsync
, ai_pqconn_listen_async
, ZEND_ACC_PUBLIC
)
1999 PHP_ME(pqconn
, notify
, ai_pqconn_notify
, ZEND_ACC_PUBLIC
)
2000 PHP_ME(pqconn
, notifyAsync
, ai_pqconn_notify_async
, ZEND_ACC_PUBLIC
)
2001 PHP_ME(pqconn
, getResult
, ai_pqconn_get_result
, ZEND_ACC_PUBLIC
)
2002 PHP_ME(pqconn
, quote
, ai_pqconn_quote
, ZEND_ACC_PUBLIC
)
2003 PHP_ME(pqconn
, quoteName
, ai_pqconn_quote_name
, ZEND_ACC_PUBLIC
)
2004 PHP_ME(pqconn
, escapeBytea
, ai_pqconn_escape_bytea
, ZEND_ACC_PUBLIC
)
2005 PHP_ME(pqconn
, unescapeBytea
, ai_pqconn_unescape_bytea
, ZEND_ACC_PUBLIC
)
2006 PHP_ME(pqconn
, startTransaction
, ai_pqconn_start_transaction
, ZEND_ACC_PUBLIC
)
2007 PHP_ME(pqconn
, startTransactionAsync
, ai_pqconn_start_transaction_async
, ZEND_ACC_PUBLIC
)
2008 PHP_ME(pqconn
, trace
, ai_pqconn_trace
, ZEND_ACC_PUBLIC
)
2009 PHP_ME(pqconn
, off
, ai_pqconn_off
, ZEND_ACC_PUBLIC
)
2010 PHP_ME(pqconn
, on
, ai_pqconn_on
, ZEND_ACC_PUBLIC
)
2011 PHP_ME(pqconn
, setConverter
, ai_pqconn_set_converter
, ZEND_ACC_PUBLIC
)
2012 PHP_ME(pqconn
, unsetConverter
, ai_pqconn_unset_converter
, ZEND_ACC_PUBLIC
)
2016 PHP_MSHUTDOWN_FUNCTION(pqconn
)
2018 php_persistent_handle_cleanup(PHP_PQ_G
->connection
.name
, NULL
);
2019 zend_string_release(PHP_PQ_G
->connection
.name
);
2020 zend_hash_destroy(&php_pqconn_object_prophandlers
);
2024 PHP_MINIT_FUNCTION(pqconn
)
2026 zend_class_entry ce
= {0};
2027 php_pq_object_prophandler_t ph
= {0};
2029 INIT_NS_CLASS_ENTRY(ce
, "pq", "Connection", php_pqconn_methods
);
2030 php_pqconn_class_entry
= zend_register_internal_class_ex(&ce
, NULL
);
2031 php_pqconn_class_entry
->create_object
= php_pqconn_create_object
;
2033 memcpy(&php_pqconn_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
2034 php_pqconn_object_handlers
.offset
= XtOffsetOf(php_pqconn_object_t
, zo
);
2035 php_pqconn_object_handlers
.free_obj
= php_pqconn_object_free
;
2036 php_pqconn_object_handlers
.read_property
= php_pq_object_read_prop
;
2037 php_pqconn_object_handlers
.write_property
= php_pq_object_write_prop
;
2038 php_pqconn_object_handlers
.clone_obj
= NULL
;
2039 php_pqconn_object_handlers
.get_property_ptr_ptr
= php_pq_object_get_prop_ptr_null
;
2040 php_pqconn_object_handlers
.get_gc
= php_pq_object_get_gc
;
2041 php_pqconn_object_handlers
.get_properties
= php_pq_object_properties
;
2042 php_pqconn_object_handlers
.get_debug_info
= php_pq_object_debug_info
;
2044 zend_hash_init(&php_pqconn_object_prophandlers
, 23, NULL
, php_pq_object_prophandler_dtor
, 1);
2046 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("status"), CONNECTION_BAD
, ZEND_ACC_PUBLIC
);
2047 ph
.read
= php_pqconn_object_read_status
;
2048 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "status", sizeof("status")-1, (void *) &ph
, sizeof(ph
));
2050 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("transactionStatus"), PQTRANS_UNKNOWN
, ZEND_ACC_PUBLIC
);
2051 ph
.read
= php_pqconn_object_read_transaction_status
;
2052 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "transactionStatus", sizeof("transactionStatus")-1, (void *) &ph
, sizeof(ph
));
2054 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("socket"), ZEND_ACC_PUBLIC
);
2055 ph
.read
= NULL
; /* forward to std prophandler */
2056 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "socket", sizeof("socket")-1, (void *) &ph
, sizeof(ph
));
2058 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("errorMessage"), ZEND_ACC_PUBLIC
);
2059 ph
.read
= php_pqconn_object_read_error_message
;
2060 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "errorMessage", sizeof("errorMessage")-1, (void *) &ph
, sizeof(ph
));
2062 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("busy"), 0, ZEND_ACC_PUBLIC
);
2063 ph
.read
= php_pqconn_object_read_busy
;
2064 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "busy", sizeof("busy")-1, (void *) &ph
, sizeof(ph
));
2066 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("encoding"), ZEND_ACC_PUBLIC
);
2067 ph
.read
= php_pqconn_object_read_encoding
;
2068 ph
.write
= php_pqconn_object_write_encoding
;
2069 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "encoding", sizeof("encoding")-1, (void *) &ph
, sizeof(ph
));
2072 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("unbuffered"), 0, ZEND_ACC_PUBLIC
);
2073 ph
.read
= php_pqconn_object_read_unbuffered
;
2074 ph
.write
= php_pqconn_object_write_unbuffered
;
2075 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "unbuffered", sizeof("unbuffered")-1, (void *) &ph
, sizeof(ph
));
2078 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("nonblocking"), 0, ZEND_ACC_PUBLIC
);
2079 ph
.read
= php_pqconn_object_read_nonblocking
;
2080 ph
.write
= php_pqconn_object_write_nonblocking
;
2081 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "nonblocking", sizeof("nonblocking")-1, (void *) &ph
, sizeof(ph
));
2084 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("db"), ZEND_ACC_PUBLIC
);
2085 ph
.read
= php_pqconn_object_read_db
;
2086 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "db", sizeof("db")-1, (void *) &ph
, sizeof(ph
));
2088 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("user"), ZEND_ACC_PUBLIC
);
2089 ph
.read
= php_pqconn_object_read_user
;
2090 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "user", sizeof("user")-1, (void *) &ph
, sizeof(ph
));
2092 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("pass"), ZEND_ACC_PUBLIC
);
2093 ph
.read
= php_pqconn_object_read_pass
;
2094 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "pass", sizeof("pass")-1, (void *) &ph
, sizeof(ph
));
2096 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("host"), ZEND_ACC_PUBLIC
);
2097 ph
.read
= php_pqconn_object_read_host
;
2098 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "host", sizeof("host")-1, (void *) &ph
, sizeof(ph
));
2100 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("port"), ZEND_ACC_PUBLIC
);
2101 ph
.read
= php_pqconn_object_read_port
;
2102 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "port", sizeof("port")-1, (void *) &ph
, sizeof(ph
));
2105 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("params"), ZEND_ACC_PUBLIC
);
2106 ph
.read
= php_pqconn_object_read_params
;
2107 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "params", sizeof("params")-1, (void *) &ph
, sizeof(ph
));
2110 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("options"), ZEND_ACC_PUBLIC
);
2111 ph
.read
= php_pqconn_object_read_options
;
2112 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "options", sizeof("options")-1, (void *) &ph
, sizeof(ph
));
2114 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("eventHandlers"), ZEND_ACC_PUBLIC
);
2115 ph
.read
= php_pqconn_object_read_event_handlers
;
2116 ph
.gc
= php_pqconn_object_gc_event_handlers
;
2117 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "eventHandlers", sizeof("eventHandlers")-1, (void *) &ph
, sizeof(ph
));
2120 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("listeners"), ZEND_ACC_PUBLIC
);
2121 ph
.read
= php_pqconn_object_read_listeners
;
2122 ph
.gc
= php_pqconn_object_gc_listeners
;
2123 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "listeners", sizeof("listeners")-1, (void *) &ph
, sizeof(ph
));
2126 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("converters"), ZEND_ACC_PUBLIC
);
2127 ph
.read
= php_pqconn_object_read_converters
;
2128 ph
.gc
= php_pqconn_object_gc_converters
;
2129 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "converters", sizeof("converters")-1, (void *) &ph
, sizeof(ph
));
2132 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultFetchType"), 0, ZEND_ACC_PUBLIC
);
2133 ph
.read
= php_pqconn_object_read_def_fetch_type
;
2134 ph
.write
= php_pqconn_object_write_def_fetch_type
;
2135 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultFetchType", sizeof("defaultFetchType")-1, (void *) &ph
, sizeof(ph
));
2138 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionIsolation"), 0, ZEND_ACC_PUBLIC
);
2139 ph
.read
= php_pqconn_object_read_def_txn_isolation
;
2140 ph
.write
= php_pqconn_object_write_def_txn_isolation
;
2141 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultTransactionIsolation", sizeof("defaultTransactionIsolation")-1, (void *) &ph
, sizeof(ph
));
2144 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionReadonly"), 0, ZEND_ACC_PUBLIC
);
2145 ph
.read
= php_pqconn_object_read_def_txn_readonly
;
2146 ph
.write
= php_pqconn_object_write_def_txn_readonly
;
2147 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultTransactionReadonly", sizeof("defaultTransactionReadonly")-1, (void *) &ph
, sizeof(ph
));
2150 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionDeferrable"), 0, ZEND_ACC_PUBLIC
);
2151 ph
.read
= php_pqconn_object_read_def_txn_deferrable
;
2152 ph
.write
= php_pqconn_object_write_def_txn_deferrable
;
2153 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultTransactionDeferrable", sizeof("defaultTransactionDeferrable")-1, (void *) &ph
, sizeof(ph
));
2156 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultAutoConvert"), PHP_PQRES_CONV_ALL
, ZEND_ACC_PUBLIC
);
2157 ph
.read
= php_pqconn_object_read_def_auto_conv
;
2158 ph
.write
= php_pqconn_object_write_def_auto_conv
;
2159 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultAutoConvert", sizeof("defaultAutoConvert")-1, (void *) &ph
, sizeof(ph
));
2162 #ifdef HAVE_PQLIBVERSION
2163 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("libraryVersion"), ZEND_ACC_PUBLIC
|ZEND_ACC_READONLY
);
2164 ph
.read
= php_pqconn_object_read_lib_version
;
2165 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, ZEND_STRL("libraryVersion"), (void *) &ph
, sizeof(ph
));
2168 #ifdef HAVE_PQPROTOCOLVERSION
2169 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("protocolVersion"), ZEND_ACC_PUBLIC
|ZEND_ACC_READONLY
);
2170 ph
.read
= php_pqconn_object_read_protocol_version
;
2171 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, ZEND_STRL("protocolVersion"), (void *) &ph
, sizeof(ph
));
2174 #ifdef HAVE_PQSERVERVERSION
2175 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("serverVersion"), ZEND_ACC_PUBLIC
|ZEND_ACC_READONLY
);
2176 ph
.read
= php_pqconn_object_read_server_version
;
2177 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, ZEND_STRL("serverVersion"), (void *) &ph
, sizeof(ph
));
2180 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("OK"), CONNECTION_OK
);
2181 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("BAD"), CONNECTION_BAD
);
2182 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("STARTED"), CONNECTION_STARTED
);
2183 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("MADE"), CONNECTION_MADE
);
2184 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AWAITING_RESPONSE"), CONNECTION_AWAITING_RESPONSE
);
2185 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AUTH_OK"), CONNECTION_AUTH_OK
);
2186 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SSL_STARTUP"), CONNECTION_SSL_STARTUP
);
2187 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SETENV"), CONNECTION_SETENV
);
2188 #ifdef HAVE_CONNECTION_CHECK_WRITABLE
2189 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("CHECK_WRITABLE"), CONNECTION_CHECK_WRITABLE
);
2191 #ifdef HAVE_CONNECTION_CONSUME
2192 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("CONSUME"), CONNECTION_CONSUME
);
2194 #ifdef HAVE_CONNECTION_GSS_STARTUP
2195 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("GSS_STARTUP"), CONNECTION_GSS_STARTUP
);
2198 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_IDLE"), PQTRANS_IDLE
);
2199 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_ACTIVE"), PQTRANS_ACTIVE
);
2200 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INTRANS"), PQTRANS_INTRANS
);
2201 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INERROR"), PQTRANS_INERROR
);
2202 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_UNKNOWN"), PQTRANS_UNKNOWN
);
2204 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_FAILED"), PGRES_POLLING_FAILED
);
2205 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_READING"), PGRES_POLLING_READING
);
2206 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_WRITING"), PGRES_POLLING_WRITING
);
2207 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_OK"), PGRES_POLLING_OK
);
2209 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_NOTICE"), ZEND_STRL("notice"));
2210 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESULT"), ZEND_STRL("result"));
2211 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESET"), ZEND_STRL("reset"));
2213 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("ASYNC"), 0x1);
2214 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("PERSISTENT"), 0x2);
2216 PHP_PQ_G
->connection
.name
= zend_string_init(ZEND_STRL("pq\\Connection"), 1);
2218 return php_persistent_handle_provide(PHP_PQ_G
->connection
.name
, php_pqconn_get_resource_factory_ops(), NULL
, NULL
);
2226 * vim600: noet sw=4 ts=4 fdm=marker
2227 * vim<600: noet sw=4 ts=4