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, ulong 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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(zval
*object
, 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 zend_get_std_object_handlers()->write_property(zobj
, &zmember
, &zsocket
, NULL
);
518 zval_ptr_dtor(&zsocket
);
519 zval_ptr_dtor(&zmember
);
524 static void *php_pqconn_resource_factory_ctor(void *data
, void *init_arg
)
526 php_pqconn_resource_factory_data_t
*o
= init_arg
;
527 PGconn
*conn
= NULL
;;
529 if (o
->flags
& PHP_PQCONN_ASYNC
) {
530 conn
= PQconnectStart(o
->dsn
);
532 conn
= PQconnectdb(o
->dsn
);
536 PQregisterEventProc(conn
, php_pqconn_event
, "ext-pq", NULL
);
542 static void php_pqconn_resource_factory_dtor(void *opaque
, void *handle
)
544 php_pqconn_event_data_t
*evdata
= PQinstanceData(handle
, php_pqconn_event
);
546 /* we don't care for anything, except free'ing evdata */
548 PQsetInstanceData(handle
, php_pqconn_event
, NULL
);
549 memset(evdata
, 0, sizeof(*evdata
));
556 static php_resource_factory_ops_t php_pqconn_resource_factory_ops
= {
557 php_pqconn_resource_factory_ctor
,
559 php_pqconn_resource_factory_dtor
562 php_resource_factory_ops_t
*php_pqconn_get_resource_factory_ops(void)
564 return &php_pqconn_resource_factory_ops
;
567 static void php_pqconn_wakeup(php_persistent_handle_factory_t
*f
, void **handle
)
569 PGresult
*res
= PQexec(*handle
, "");
570 php_pqres_clear(res
);
572 if (CONNECTION_OK
!= PQstatus(*handle
)) {
577 static inline PGresult
*unlisten(PGconn
*conn
, const char *channel_str
, size_t channel_len
)
579 char *quoted_channel
= PQescapeIdentifier(conn
, channel_str
, channel_len
);
580 PGresult
*res
= NULL
;
582 if (quoted_channel
) {
585 smart_str_appends(&cmd
, "UNLISTEN ");
586 smart_str_appends(&cmd
, quoted_channel
);
589 res
= PQexec(conn
, smart_str_v(&cmd
));
591 smart_str_free(&cmd
);
592 PQfreemem(quoted_channel
);
598 static int apply_unlisten(zval
*p
, int argc
, va_list argv
, zend_hash_key
*key
)
600 php_pqconn_object_t
*obj
= va_arg(argv
, php_pqconn_object_t
*);
601 PGresult
*res
= unlisten(obj
->intern
->conn
, key
->key
->val
, key
->key
->len
);
604 php_pqres_clear(res
);
607 return ZEND_HASH_APPLY_REMOVE
;
610 static void php_pqconn_retire(php_persistent_handle_factory_t
*f
, void **handle
)
612 php_pqconn_event_data_t
*evdata
= PQinstanceData(*handle
, php_pqconn_event
);
617 PQsetInstanceData(*handle
, php_pqconn_event
, NULL
);
620 PQsetNoticeReceiver(*handle
, php_pqconn_notice_ignore
, NULL
);
622 /* cancel async queries */
623 if (PQisBusy(*handle
) && (cancel
= PQgetCancel(*handle
))) {
626 PQcancel(cancel
, err
, sizeof(err
));
627 PQfreeCancel(cancel
);
629 /* clean up async results */
630 while ((res
= PQgetResult(*handle
))) {
631 php_pqres_clear(res
);
634 /* clean up transaction & session */
635 switch (PQtransactionStatus(*handle
)) {
637 res
= PQexec(*handle
, "RESET ALL");
640 res
= PQexec(*handle
, "ROLLBACK; RESET ALL");
645 php_pqres_clear(res
);
649 /* clean up notify listeners */
650 zend_hash_apply_with_arguments(&evdata
->obj
->intern
->listeners
, apply_unlisten
, 1, evdata
->obj
);
652 /* release instance data */
657 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct
, 0, 0, 1)
658 ZEND_ARG_INFO(0, dsn
)
659 ZEND_ARG_INFO(0, flags
)
661 static PHP_METHOD(pqconn
, __construct
) {
662 zend_error_handling zeh
;
668 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
669 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "|sl", &dsn_str
, &dsn_len
, &flags
);
670 zend_restore_error_handling(&zeh
);
673 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
676 throw_exce(EX_BAD_METHODCALL
, "pq\\Connection already initialized");
678 php_pqconn_event_data_t
*evdata
= php_pqconn_event_data_init(obj
);
679 php_pqconn_resource_factory_data_t rfdata
= {dsn_str
, flags
};
681 obj
->intern
= ecalloc(1, sizeof(*obj
->intern
));
683 obj
->intern
->default_auto_convert
= PHP_PQRES_CONV_ALL
;
685 zend_hash_init(&obj
->intern
->listeners
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
686 zend_hash_init(&obj
->intern
->statements
, 0, NULL
, NULL
, 0);
687 zend_hash_init(&obj
->intern
->converters
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
688 zend_hash_init(&obj
->intern
->eventhandlers
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
690 if (flags
& PHP_PQCONN_PERSISTENT
) {
691 zend_string
*dsn
= zend_string_init(dsn_str
, dsn_len
, 0);
692 php_persistent_handle_factory_t
*phf
= php_persistent_handle_concede(NULL
, PHP_PQ_G
->connection
.name
, dsn
, php_pqconn_wakeup
, php_pqconn_retire
);
693 php_persistent_handle_resource_factory_init(&obj
->intern
->factory
, phf
);
694 zend_string_release(dsn
);
696 php_resource_factory_init(&obj
->intern
->factory
, &php_pqconn_resource_factory_ops
, NULL
, NULL
);
699 if (flags
& PHP_PQCONN_ASYNC
) {
700 obj
->intern
->poller
= (int (*)(PGconn
*)) PQconnectPoll
;
703 obj
->intern
->conn
= php_resource_factory_handle_ctor(&obj
->intern
->factory
, &rfdata
);
705 PQsetInstanceData(obj
->intern
->conn
, php_pqconn_event
, evdata
);
706 PQsetNoticeReceiver(obj
->intern
->conn
, php_pqconn_notice_recv
, evdata
);
708 if (SUCCESS
!= php_pqconn_update_socket(getThis(), obj
)) {
709 throw_exce(EX_CONNECTION_FAILED
, "Connection failed (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
715 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset
, 0, 0, 0)
717 static PHP_METHOD(pqconn
, reset
) {
718 zend_error_handling zeh
;
721 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
722 rv
= zend_parse_parameters_none();
723 zend_restore_error_handling(&zeh
);
726 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
729 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
731 PQreset(obj
->intern
->conn
);
733 if (CONNECTION_OK
!= PQstatus(obj
->intern
->conn
)) {
734 throw_exce(EX_CONNECTION_FAILED
, "Connection reset failed: (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
737 php_pqconn_notify_listeners(obj
);
742 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset_async
, 0, 0, 0)
744 static PHP_METHOD(pqconn
, resetAsync
) {
745 zend_error_handling zeh
;
748 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
749 rv
= zend_parse_parameters_none();
750 zend_restore_error_handling(&zeh
);
753 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
756 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
758 if (!PQresetStart(obj
->intern
->conn
)) {
759 throw_exce(EX_IO
, "Failed to start connection reset (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
761 obj
->intern
->poller
= (int (*)(PGconn
*)) PQresetPoll
;
764 php_pqconn_notify_listeners(obj
);
769 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten
, 0, 0, 1)
770 ZEND_ARG_INFO(0, channel
)
772 static PHP_METHOD(pqconn
, unlisten
)
774 zend_error_handling zeh
;
779 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
780 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s", &channel_str
, &channel_len
);
781 zend_restore_error_handling(&zeh
);
784 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
787 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
788 } else if (SUCCESS
== zend_hash_str_del(&obj
->intern
->listeners
, channel_str
, channel_len
)) {
789 PGresult
*res
= unlisten(obj
->intern
->conn
, channel_str
, channel_len
);
792 php_pqres_success(res
);
793 php_pqres_clear(res
);
799 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten_async
, 0, 0, 1)
800 ZEND_ARG_INFO(0, channel
)
802 static PHP_METHOD(pqconn
, unlistenAsync
) {
803 zend_error_handling zeh
;
808 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
809 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s", &channel_str
, &channel_len
);
810 zend_restore_error_handling(&zeh
);
813 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
816 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
818 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
820 if (!quoted_channel
) {
821 throw_exce(EX_ESCAPE
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
825 smart_str_appends(&cmd
, "UNLISTEN ");
826 smart_str_appends(&cmd
, quoted_channel
);
829 if (!PQsendQuery(obj
->intern
->conn
, smart_str_v(&cmd
))) {
830 throw_exce(EX_IO
, "Failed to uninstall listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
832 obj
->intern
->poller
= PQconsumeInput
;
833 zend_hash_str_del(&obj
->intern
->listeners
, channel_str
, channel_len
);
836 smart_str_free(&cmd
);
837 PQfreemem(quoted_channel
);
838 php_pqconn_notify_listeners(obj
);
844 static void php_pqconn_add_listener(php_pqconn_object_t
*obj
, const char *channel_str
, size_t channel_len
, php_pq_callback_t
*listener
)
848 php_pq_callback_addref(listener
);
850 if ((existing
= zend_hash_str_find(&obj
->intern
->listeners
, channel_str
, channel_len
))) {
851 zend_hash_next_index_insert_mem(Z_ARRVAL_P(existing
), (void *) listener
, sizeof(*listener
));
857 zend_hash_init(ht
, 0, NULL
, php_pq_callback_hash_dtor
, 0);
858 zend_hash_next_index_insert_mem(ht
, (void *) listener
, sizeof(*listener
));
861 zend_hash_str_add(&obj
->intern
->listeners
, channel_str
, channel_len
, &tmp
);
865 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen
, 0, 0, 2)
866 ZEND_ARG_INFO(0, channel
)
867 ZEND_ARG_INFO(0, callable
)
869 static PHP_METHOD(pqconn
, listen
) {
870 zend_error_handling zeh
;
871 char *channel_str
= NULL
;
872 size_t channel_len
= 0;
873 php_pq_callback_t listener
= {{0}};
876 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
877 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
878 zend_restore_error_handling(&zeh
);
881 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
884 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
886 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
888 if (!quoted_channel
) {
889 throw_exce(EX_ESCAPE
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
894 smart_str_appends(&cmd
, "LISTEN ");
895 smart_str_appends(&cmd
, quoted_channel
);
898 res
= php_pq_exec(obj
->intern
->conn
, smart_str_v(&cmd
));
900 smart_str_free(&cmd
);
901 PQfreemem(quoted_channel
);
904 throw_exce(EX_RUNTIME
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
906 if (SUCCESS
== php_pqres_success(res
)) {
907 obj
->intern
->poller
= PQconsumeInput
;
908 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener
);
910 php_pqres_clear(res
);
913 php_pqconn_notify_listeners(obj
);
919 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen_async
, 0, 0, 0)
920 ZEND_ARG_INFO(0, channel
)
921 ZEND_ARG_INFO(0, callable
)
923 static PHP_METHOD(pqconn
, listenAsync
) {
924 zend_error_handling zeh
;
925 char *channel_str
= NULL
;
926 size_t channel_len
= 0;
927 php_pq_callback_t listener
= {{0}};
930 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
931 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
932 zend_restore_error_handling(&zeh
);
935 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
938 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
940 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
942 if (!quoted_channel
) {
943 throw_exce(EX_ESCAPE
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
947 smart_str_appends(&cmd
, "LISTEN ");
948 smart_str_appends(&cmd
, quoted_channel
);
951 if (!PQsendQuery(obj
->intern
->conn
, smart_str_v(&cmd
))) {
952 throw_exce(EX_IO
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
954 obj
->intern
->poller
= PQconsumeInput
;
955 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener
);
958 smart_str_free(&cmd
);
959 PQfreemem(quoted_channel
);
960 php_pqconn_notify_listeners(obj
);
966 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify
, 0, 0, 2)
967 ZEND_ARG_INFO(0, channel
)
968 ZEND_ARG_INFO(0, message
)
970 static PHP_METHOD(pqconn
, notify
) {
971 zend_error_handling zeh
;
972 char *channel_str
, *message_str
;
973 size_t channel_len
, message_len
;
976 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
977 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
978 zend_restore_error_handling(&zeh
);
981 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
984 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
987 char *params
[2] = {channel_str
, message_str
};
989 res
= PQexecParams(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0);
992 throw_exce(EX_RUNTIME
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
994 php_pqres_success(res
);
995 php_pqres_clear(res
);
998 php_pqconn_notify_listeners(obj
);
1003 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify_async
, 0, 0, 2)
1004 ZEND_ARG_INFO(0, channel
)
1005 ZEND_ARG_INFO(0, message
)
1006 ZEND_END_ARG_INFO();
1007 static PHP_METHOD(pqconn
, notifyAsync
) {
1008 zend_error_handling zeh
;
1009 char *channel_str
, *message_str
;
1010 size_t channel_len
, message_len
;
1011 ZEND_RESULT_CODE rv
;
1013 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1014 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
1015 zend_restore_error_handling(&zeh
);
1017 if (SUCCESS
== rv
) {
1018 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1021 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1023 char *params
[2] = {channel_str
, message_str
};
1025 if (!PQsendQueryParams(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0)) {
1026 throw_exce(EX_IO
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1028 obj
->intern
->poller
= PQconsumeInput
;
1031 php_pqconn_notify_listeners(obj
);
1036 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_poll
, 0, 0, 0)
1037 ZEND_END_ARG_INFO();
1038 static PHP_METHOD(pqconn
, poll
) {
1039 zend_error_handling zeh
;
1040 ZEND_RESULT_CODE rv
;
1042 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1043 rv
= zend_parse_parameters_none();
1044 zend_restore_error_handling(&zeh
);
1046 if (SUCCESS
== rv
) {
1047 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1050 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1051 } else if (!obj
->intern
->poller
) {
1052 throw_exce(EX_RUNTIME
, "No asynchronous operation active");
1054 if (obj
->intern
->poller
== PQconsumeInput
) {
1055 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
) * PGRES_POLLING_OK
);
1057 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
));
1059 php_pqconn_notify_listeners(obj
);
1064 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_flush
, 0, 0, 0)
1065 ZEND_END_ARG_INFO();
1066 static PHP_METHOD(pqconn
, flush
) {
1067 zend_error_handling zeh
;
1068 ZEND_RESULT_CODE rv
;
1070 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1071 rv
= zend_parse_parameters_none();
1072 zend_restore_error_handling(&zeh
);
1074 if (SUCCESS
== rv
) {
1075 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1078 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1079 } else if (!obj
->intern
->poller
) {
1080 throw_exce(EX_RUNTIME
, "No asynchronous operation active");
1082 switch (PQflush(obj
->intern
->conn
)) {
1085 throw_exce(EX_RUNTIME
, "Failed to flush connection: %s", PHP_PQerrorMessage(obj
->intern
->conn
));
1098 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec
, 0, 0, 1)
1099 ZEND_ARG_INFO(0, query
)
1100 ZEND_END_ARG_INFO();
1101 static PHP_METHOD(pqconn
, exec
) {
1102 zend_error_handling zeh
;
1105 ZEND_RESULT_CODE rv
;
1107 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1108 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s", &query_str
, &query_len
);
1109 zend_restore_error_handling(&zeh
);
1111 if (SUCCESS
== rv
) {
1112 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1115 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1117 PGresult
*res
= php_pq_exec(obj
->intern
->conn
, query_str
);
1120 throw_exce(EX_RUNTIME
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1121 } else if (SUCCESS
== php_pqres_success(res
)) {
1122 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), return_value
);
1124 php_pqres_clear(res
);
1127 php_pqconn_notify_listeners(obj
);
1132 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_get_result
, 0, 0, 0)
1133 ZEND_END_ARG_INFO();
1134 static PHP_METHOD(pqconn
, getResult
) {
1135 zend_error_handling zeh
;
1136 ZEND_RESULT_CODE rv
;
1138 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1139 rv
= zend_parse_parameters_none();
1140 zend_restore_error_handling(&zeh
);
1142 if (SUCCESS
== rv
) {
1143 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1146 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1148 PGresult
*res
= PQgetResult(obj
->intern
->conn
);
1149 php_pq_object_t
*res_obj
;
1151 if (res
&& (res_obj
= PQresultInstanceData(res
, php_pqconn_event
))) {
1152 php_pq_object_to_zval_no_addref(res_obj
, return_value
);
1157 php_pqconn_notify_listeners(obj
);
1162 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_async
, 0, 0, 1)
1163 ZEND_ARG_INFO(0, query
)
1164 ZEND_ARG_INFO(0, callable
)
1165 ZEND_END_ARG_INFO();
1166 static PHP_METHOD(pqconn
, execAsync
) {
1167 zend_error_handling zeh
;
1168 php_pq_callback_t resolver
= {{0}};
1171 ZEND_RESULT_CODE rv
;
1173 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1174 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s|f", &query_str
, &query_len
, &resolver
.fci
, &resolver
.fcc
);
1175 zend_restore_error_handling(&zeh
);
1177 if (SUCCESS
== rv
) {
1178 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1181 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1182 } else if (!PQsendQuery(obj
->intern
->conn
, query_str
)) {
1183 throw_exce(EX_IO
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1184 #if HAVE_PQSETSINGLEROWMODE
1185 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1186 throw_exce(EX_RUNTIME
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1189 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver
);
1190 obj
->intern
->poller
= PQconsumeInput
;
1191 php_pqconn_notify_listeners(obj
);
1196 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params
, 0, 0, 2)
1197 ZEND_ARG_INFO(0, query
)
1198 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1199 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1200 ZEND_END_ARG_INFO();
1201 static PHP_METHOD(pqconn
, execParams
) {
1202 zend_error_handling zeh
;
1206 zval
*ztypes
= NULL
;
1207 ZEND_RESULT_CODE rv
;
1209 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1210 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sa/|a/!", &query_str
, &query_len
, &zparams
, &ztypes
);
1211 zend_restore_error_handling(&zeh
);
1213 if (SUCCESS
== rv
) {
1214 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1217 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1220 php_pq_params_t
*params
;
1222 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
));
1223 res
= PQexecParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1224 php_pq_params_free(¶ms
);
1227 throw_exce(EX_RUNTIME
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1229 if (SUCCESS
== php_pqres_success(res
)) {
1230 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), return_value
);
1232 php_pqres_clear(res
);
1235 php_pqconn_notify_listeners(obj
);
1241 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async
, 0, 0, 2)
1242 ZEND_ARG_INFO(0, query
)
1243 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1244 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1245 ZEND_ARG_INFO(0, callable
)
1246 ZEND_END_ARG_INFO();
1247 static PHP_METHOD(pqconn
, execParamsAsync
) {
1248 zend_error_handling zeh
;
1249 php_pq_callback_t resolver
= {{0}};
1253 zval
*ztypes
= NULL
;
1254 ZEND_RESULT_CODE rv
;
1256 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1257 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sa/|a/!f", &query_str
, &query_len
, &zparams
, &ztypes
, &resolver
.fci
, &resolver
.fcc
);
1258 zend_restore_error_handling(&zeh
);
1260 if (SUCCESS
== rv
) {
1261 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1264 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1267 php_pq_params_t
*params
;
1269 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
));
1270 rc
= PQsendQueryParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1271 php_pq_params_free(¶ms
);
1274 throw_exce(EX_IO
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1275 #if HAVE_PQSETSINGLEROWMODE
1276 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1277 throw_exce(EX_RUNTIME
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1280 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver
);
1281 obj
->intern
->poller
= PQconsumeInput
;
1282 php_pqconn_notify_listeners(obj
);
1286 zend_restore_error_handling(&zeh
);
1289 ZEND_RESULT_CODE
php_pqconn_prepare(zval
*object
, php_pqconn_object_t
*obj
, const char *name
, const char *query
, php_pq_params_t
*params
)
1292 ZEND_RESULT_CODE rv
;
1295 obj
= PHP_PQ_OBJ(object
, NULL
);
1298 res
= php_pq_prepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
);
1302 throw_exce(EX_RUNTIME
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1304 rv
= php_pqres_success(res
);
1305 php_pqres_clear(res
);
1306 php_pqconn_notify_listeners(obj
);
1312 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare
, 0, 0, 2)
1313 ZEND_ARG_INFO(0, name
)
1314 ZEND_ARG_INFO(0, query
)
1315 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1316 ZEND_END_ARG_INFO();
1317 static PHP_METHOD(pqconn
, prepare
) {
1318 zend_error_handling zeh
;
1319 zval
*ztypes
= NULL
;
1320 char *name_str
, *query_str
;
1321 size_t name_len
, *query_len
;
1322 ZEND_RESULT_CODE rv
;
1324 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1325 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1326 zend_restore_error_handling(&zeh
);
1328 if (SUCCESS
== rv
) {
1329 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1332 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1334 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL
);
1336 if (SUCCESS
!= php_pqconn_prepare(getThis(), obj
, name_str
, query_str
, params
)) {
1337 php_pq_params_free(¶ms
);
1339 php_pqstm_t
*stm
= php_pqstm_init(obj
, name_str
, query_str
, params
);
1341 RETVAL_OBJ(&php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
)->zo
);
1347 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
)
1349 ZEND_RESULT_CODE rv
;
1352 obj
= PHP_PQ_OBJ(object
, NULL
);
1355 if (!PQsendPrepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
)) {
1357 throw_exce(EX_IO
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1360 obj
->intern
->poller
= PQconsumeInput
;
1361 php_pqconn_notify_listeners(obj
);
1367 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare_async
, 0, 0, 2)
1368 ZEND_ARG_INFO(0, name
)
1369 ZEND_ARG_INFO(0, query
)
1370 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1371 ZEND_END_ARG_INFO();
1372 static PHP_METHOD(pqconn
, prepareAsync
) {
1373 zend_error_handling zeh
;
1374 zval
*ztypes
= NULL
;
1375 char *name_str
, *query_str
;
1376 size_t name_len
, *query_len
;
1377 ZEND_RESULT_CODE rv
;
1379 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1380 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1381 zend_restore_error_handling(&zeh
);
1383 if (SUCCESS
== rv
) {
1384 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1387 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1389 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL
);
1391 if (SUCCESS
!= php_pqconn_prepare_async(getThis(), obj
, name_str
, query_str
, params
)) {
1392 php_pq_params_free(¶ms
);
1394 php_pqstm_t
*stm
= php_pqstm_init(obj
, name_str
, query_str
, params
);
1396 RETVAL_OBJ(&php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
)->zo
);
1402 ZEND_RESULT_CODE
php_pqconn_declare(zval
*object
, php_pqconn_object_t
*obj
, const char *decl
)
1405 ZEND_RESULT_CODE rv
;
1408 obj
= PHP_PQ_OBJ(object
, NULL
);
1411 res
= php_pq_exec(obj
->intern
->conn
, decl
);
1415 throw_exce(EX_RUNTIME
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1417 rv
= php_pqres_success(res
);
1418 php_pqres_clear(res
);
1419 php_pqconn_notify_listeners(obj
);
1425 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare
, 0, 0, 3)
1426 ZEND_ARG_INFO(0, name
)
1427 ZEND_ARG_INFO(0, flags
)
1428 ZEND_ARG_INFO(0, query
)
1429 ZEND_END_ARG_INFO();
1430 static PHP_METHOD(pqconn
, declare
) {
1431 zend_error_handling zeh
;
1432 char *name_str
, *query_str
;
1433 size_t name_len
, query_len
;
1435 ZEND_RESULT_CODE rv
;
1437 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1438 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1439 zend_restore_error_handling(&zeh
);
1441 if (SUCCESS
== rv
) {
1442 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1445 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1448 char *decl
= php_pqcur_declare_str(name_str
, name_len
, flags
, query_str
, query_len
, &query_offset
);
1450 if (SUCCESS
!= php_pqconn_declare(getThis(), obj
, decl
)) {
1453 php_pqcur_t
*cur
= php_pqcur_init(obj
, name_str
, decl
, query_offset
, flags
);
1455 RETVAL_OBJ(&php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
)->zo
);
1461 ZEND_RESULT_CODE
php_pqconn_declare_async(zval
*object
, php_pqconn_object_t
*obj
, const char *decl
)
1463 ZEND_RESULT_CODE rv
;
1466 obj
= PHP_PQ_OBJ(object
, NULL
);
1469 if (!PQsendQuery(obj
->intern
->conn
, decl
)) {
1471 throw_exce(EX_IO
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1474 obj
->intern
->poller
= PQconsumeInput
;
1475 php_pqconn_notify_listeners(obj
);
1481 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare_async
, 0, 0, 2)
1482 ZEND_ARG_INFO(0, name
)
1483 ZEND_ARG_INFO(0, flags
)
1484 ZEND_ARG_INFO(0, query
)
1485 ZEND_END_ARG_INFO();
1486 static PHP_METHOD(pqconn
, declareAsync
) {
1487 zend_error_handling zeh
;
1488 char *name_str
, *query_str
;
1489 size_t name_len
, query_len
;
1491 ZEND_RESULT_CODE rv
;
1493 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1494 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1495 zend_restore_error_handling(&zeh
);
1497 if (SUCCESS
== rv
) {
1498 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1501 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1504 char *decl
= php_pqcur_declare_str(name_str
, name_len
, flags
, query_str
, query_len
, &query_offset
);
1506 if (SUCCESS
!= php_pqconn_declare_async(getThis(), obj
, decl
)) {
1509 php_pqcur_t
*cur
= php_pqcur_init(obj
, name_str
, decl
, query_offset
, flags
);
1511 RETVAL_OBJ(&php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
)->zo
);
1517 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote
, 0, 0, 1)
1518 ZEND_ARG_INFO(0, string
)
1519 ZEND_END_ARG_INFO();
1520 static PHP_METHOD(pqconn
, quote
) {
1524 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1525 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1528 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1530 char *quoted
= PQescapeLiteral(obj
->intern
->conn
, str
, len
);
1533 php_error_docref(NULL
, E_WARNING
, "Failed to quote string (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1536 RETVAL_STRING(quoted
);
1543 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote_name
, 0, 0, 1)
1544 ZEND_ARG_INFO(0, type
)
1545 ZEND_END_ARG_INFO();
1546 static PHP_METHOD(pqconn
, quoteName
) {
1550 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1551 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1554 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1556 char *quoted
= PQescapeIdentifier(obj
->intern
->conn
, str
, len
);
1559 php_error_docref(NULL
, E_WARNING
, "Failed to quote name (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1562 RETVAL_STRING(quoted
);
1569 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_escape_bytea
, 0, 0, 1)
1570 ZEND_ARG_INFO(0, bytea
)
1571 ZEND_END_ARG_INFO();
1572 static PHP_METHOD(pqconn
, escapeBytea
) {
1576 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1577 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1580 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1583 char *escaped_str
= (char *) PQescapeByteaConn(obj
->intern
->conn
, (unsigned char *) str
, len
, &escaped_len
);
1586 php_error_docref(NULL
, E_WARNING
, "Failed to escape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1589 RETVAL_STRINGL(escaped_str
, escaped_len
- 1);
1590 PQfreemem(escaped_str
);
1596 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unescape_bytea
, 0, 0, 1)
1597 ZEND_ARG_INFO(0, bytea
)
1598 ZEND_END_ARG_INFO();
1599 static PHP_METHOD(pqconn
, unescapeBytea
) {
1603 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1604 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1607 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1609 size_t unescaped_len
;
1610 char *unescaped_str
= (char *) PQunescapeBytea((unsigned char *)str
, &unescaped_len
);
1612 if (!unescaped_str
) {
1613 php_error_docref(NULL
, E_WARNING
, "Failed to unescape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1616 RETVAL_STRINGL(unescaped_str
, unescaped_len
);
1617 PQfreemem(unescaped_str
);
1623 ZEND_RESULT_CODE
php_pqconn_start_transaction(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable
)
1625 ZEND_RESULT_CODE rv
= FAILURE
;
1628 conn_obj
= PHP_PQ_OBJ(zconn
, NULL
);
1631 if (!conn_obj
->intern
) {
1632 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1635 smart_str cmd
= {0};
1636 const char *il
= php_pq_isolation_level(&isolation
);
1638 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1639 smart_str_appends(&cmd
, il
);
1640 smart_str_appends(&cmd
, ", READ ");
1641 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1642 smart_str_appends(&cmd
, ",");
1643 smart_str_appends(&cmd
, deferrable
? "" : " NOT");
1644 smart_str_appends(&cmd
, " DEFERRABLE");
1647 res
= php_pq_exec(conn_obj
->intern
->conn
, smart_str_v(&cmd
));
1650 throw_exce(EX_RUNTIME
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1652 rv
= php_pqres_success(res
);
1653 php_pqres_clear(res
);
1654 php_pqconn_notify_listeners(conn_obj
);
1657 smart_str_free(&cmd
);
1663 ZEND_RESULT_CODE
php_pqconn_start_transaction_async(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable
)
1665 ZEND_RESULT_CODE rv
= FAILURE
;
1668 conn_obj
= PHP_PQ_OBJ(zconn
, NULL
);
1671 if (!conn_obj
->intern
) {
1672 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1674 smart_str cmd
= {0};
1675 const char *il
= php_pq_isolation_level(&isolation
);
1677 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1678 smart_str_appends(&cmd
, il
);
1679 smart_str_appends(&cmd
, ", READ ");
1680 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1681 smart_str_appends(&cmd
, ",");
1682 smart_str_appends(&cmd
, deferrable
? "" : "NOT ");
1683 smart_str_appends(&cmd
, " DEFERRABLE");
1686 if (!PQsendQuery(conn_obj
->intern
->conn
, smart_str_v(&cmd
))) {
1687 throw_exce(EX_IO
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1690 conn_obj
->intern
->poller
= PQconsumeInput
;
1691 php_pqconn_notify_listeners(conn_obj
);
1694 smart_str_free(&cmd
);
1700 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction
, 0, 0, 0)
1701 ZEND_ARG_INFO(0, isolation
)
1702 ZEND_ARG_INFO(0, readonly
)
1703 ZEND_ARG_INFO(0, deferrable
)
1704 ZEND_END_ARG_INFO();
1705 static PHP_METHOD(pqconn
, startTransaction
) {
1706 zend_error_handling zeh
;
1707 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1708 zend_long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1709 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1710 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1711 ZEND_RESULT_CODE rv
;
1713 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1714 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "|lbb", &isolation
, &readonly
, &deferrable
);
1715 zend_restore_error_handling(&zeh
);
1717 if (SUCCESS
== rv
) {
1718 rv
= php_pqconn_start_transaction(getThis(), obj
, isolation
, readonly
, deferrable
);
1720 if (SUCCESS
== rv
) {
1721 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1723 php_pq_object_addref(obj
);
1726 txn
->isolation
= isolation
;
1727 txn
->readonly
= readonly
;
1728 txn
->deferrable
= deferrable
;
1730 RETVAL_OBJ(&php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
)->zo
);
1735 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction_async
, 0, 0, 0)
1736 ZEND_ARG_INFO(0, isolation
)
1737 ZEND_ARG_INFO(0, readonly
)
1738 ZEND_ARG_INFO(0, deferrable
)
1739 ZEND_END_ARG_INFO();
1740 static PHP_METHOD(pqconn
, startTransactionAsync
) {
1741 zend_error_handling zeh
;
1742 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1743 zend_long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1744 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1745 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1746 ZEND_RESULT_CODE rv
;
1748 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1749 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "|lbb", &isolation
, &readonly
, &deferrable
);
1750 zend_restore_error_handling(&zeh
);
1752 if (SUCCESS
== rv
) {
1753 rv
= php_pqconn_start_transaction_async(getThis(), obj
, isolation
, readonly
, deferrable
);
1755 if (SUCCESS
== rv
) {
1756 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1758 php_pq_object_addref(obj
);
1761 txn
->isolation
= isolation
;
1762 txn
->readonly
= readonly
;
1763 txn
->deferrable
= deferrable
;
1765 RETVAL_OBJ(&php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
)->zo
);
1770 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_trace
, 0, 0, 0)
1771 ZEND_ARG_INFO(0, stdio_stream
)
1772 ZEND_END_ARG_INFO();
1773 static PHP_METHOD(pqconn
, trace
) {
1774 zval
*zstream
= NULL
;
1776 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &zstream
)) {
1777 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1780 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1783 PQuntrace(obj
->intern
->conn
);
1787 php_stream
*stream
= NULL
;
1789 php_stream_from_zval(stream
, zstream
);
1791 if (SUCCESS
!= php_stream_cast(stream
, PHP_STREAM_AS_STDIO
, (void *) &fp
, REPORT_ERRORS
)) {
1794 stream
->flags
|= PHP_STREAM_FLAG_NO_CLOSE
;
1795 PQtrace(obj
->intern
->conn
, fp
);
1803 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_off
, 0, 0, 1)
1804 ZEND_ARG_INFO(0, type
)
1805 ZEND_END_ARG_INFO();
1806 static PHP_METHOD(pqconn
, off
) {
1807 zend_error_handling zeh
;
1809 ZEND_RESULT_CODE rv
;
1811 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1812 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "S", &type
);
1813 zend_restore_error_handling(&zeh
);
1815 if (SUCCESS
== rv
) {
1816 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1819 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1821 RETURN_BOOL(SUCCESS
== zend_hash_del(&obj
->intern
->eventhandlers
, type
));
1826 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_on
, 0, 0, 2)
1827 ZEND_ARG_INFO(0, type
)
1828 ZEND_ARG_INFO(0, callable
)
1829 ZEND_END_ARG_INFO();
1830 static PHP_METHOD(pqconn
, on
) {
1831 zend_error_handling zeh
;
1834 php_pq_callback_t cb
= {{0}};
1835 ZEND_RESULT_CODE rv
;
1837 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1838 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sf", &type_str
, &type_len
, &cb
.fci
, &cb
.fcc
);
1839 zend_restore_error_handling(&zeh
);
1841 if (SUCCESS
== rv
) {
1842 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1845 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1847 RETVAL_LONG(php_pqconn_add_eventhandler(obj
, type_str
, type_len
, &cb
));
1852 struct apply_set_converter_arg
{
1858 static int apply_set_converter(zval
*zoid
, void *a
)
1860 zend_long oid
= zval_get_long(zoid
);
1861 struct apply_set_converter_arg
*arg
= a
;
1864 Z_ADDREF_P(arg
->zconv
);
1865 zend_hash_index_update(arg
->ht
, oid
, arg
->zconv
);
1867 zend_hash_index_del(arg
->ht
, oid
);
1870 return ZEND_HASH_APPLY_KEEP
;
1873 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_set_converter
, 0, 0, 1)
1874 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\Converter
, 0)
1875 ZEND_END_ARG_INFO();
1876 static PHP_METHOD(pqconn
, setConverter
) {
1877 ZEND_RESULT_CODE rv
;
1878 zend_error_handling zeh
;
1881 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1882 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zcnv
, php_pqconv_class_entry
);
1883 zend_restore_error_handling(&zeh
);
1885 if (SUCCESS
== rv
) {
1886 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1889 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1892 struct apply_set_converter_arg arg
= {NULL
};
1895 zend_call_method_with_0_params(zcnv
, NULL
, NULL
, "converttypes", &zoids
);
1896 ZVAL_DUP(&tmp
, &zoids
);
1897 convert_to_array(&tmp
);
1899 arg
.ht
= &obj
->intern
->converters
;
1903 zend_hash_apply_with_argument(Z_ARRVAL(tmp
), apply_set_converter
, &arg
);
1905 zval_ptr_dtor(&tmp
);
1906 zval_ptr_dtor(&zoids
);
1911 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unset_converter
, 0, 0, 1)
1912 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\Converter
, 0)
1913 ZEND_END_ARG_INFO();
1914 static PHP_METHOD(pqconn
, unsetConverter
) {
1915 ZEND_RESULT_CODE rv
;
1916 zend_error_handling zeh
;
1919 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1920 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zcnv
, php_pqconv_class_entry
);
1921 zend_restore_error_handling(&zeh
);
1923 if (SUCCESS
== rv
) {
1924 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1927 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1930 struct apply_set_converter_arg arg
= {NULL
};
1933 zend_call_method_with_0_params(zcnv
, NULL
, NULL
, "converttypes", &zoids
);
1934 ZVAL_DUP(&tmp
, &zoids
);
1935 convert_to_array(&tmp
);
1937 arg
.ht
= &obj
->intern
->converters
;
1941 zend_hash_apply_with_argument(Z_ARRVAL(tmp
), apply_set_converter
, &arg
);
1943 zval_ptr_dtor(&tmp
);
1944 zval_ptr_dtor(&zoids
);
1949 static zend_function_entry php_pqconn_methods
[] = {
1950 PHP_ME(pqconn
, __construct
, ai_pqconn_construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
1951 PHP_ME(pqconn
, reset
, ai_pqconn_reset
, ZEND_ACC_PUBLIC
)
1952 PHP_ME(pqconn
, resetAsync
, ai_pqconn_reset_async
, ZEND_ACC_PUBLIC
)
1953 PHP_ME(pqconn
, poll
, ai_pqconn_poll
, ZEND_ACC_PUBLIC
)
1954 PHP_ME(pqconn
, flush
, ai_pqconn_flush
, ZEND_ACC_PUBLIC
)
1955 PHP_ME(pqconn
, exec
, ai_pqconn_exec
, ZEND_ACC_PUBLIC
)
1956 PHP_ME(pqconn
, execAsync
, ai_pqconn_exec_async
, ZEND_ACC_PUBLIC
)
1957 PHP_ME(pqconn
, execParams
, ai_pqconn_exec_params
, ZEND_ACC_PUBLIC
)
1958 PHP_ME(pqconn
, execParamsAsync
, ai_pqconn_exec_params_async
, ZEND_ACC_PUBLIC
)
1959 PHP_ME(pqconn
, prepare
, ai_pqconn_prepare
, ZEND_ACC_PUBLIC
)
1960 PHP_ME(pqconn
, prepareAsync
, ai_pqconn_prepare_async
, ZEND_ACC_PUBLIC
)
1961 PHP_ME(pqconn
, declare
, ai_pqconn_declare
, ZEND_ACC_PUBLIC
)
1962 PHP_ME(pqconn
, declareAsync
, ai_pqconn_declare_async
, ZEND_ACC_PUBLIC
)
1963 PHP_ME(pqconn
, unlisten
, ai_pqconn_unlisten
, ZEND_ACC_PUBLIC
)
1964 PHP_ME(pqconn
, unlistenAsync
, ai_pqconn_unlisten_async
, ZEND_ACC_PUBLIC
)
1965 PHP_ME(pqconn
, listen
, ai_pqconn_listen
, ZEND_ACC_PUBLIC
)
1966 PHP_ME(pqconn
, listenAsync
, ai_pqconn_listen_async
, ZEND_ACC_PUBLIC
)
1967 PHP_ME(pqconn
, notify
, ai_pqconn_notify
, ZEND_ACC_PUBLIC
)
1968 PHP_ME(pqconn
, notifyAsync
, ai_pqconn_notify_async
, ZEND_ACC_PUBLIC
)
1969 PHP_ME(pqconn
, getResult
, ai_pqconn_get_result
, ZEND_ACC_PUBLIC
)
1970 PHP_ME(pqconn
, quote
, ai_pqconn_quote
, ZEND_ACC_PUBLIC
)
1971 PHP_ME(pqconn
, quoteName
, ai_pqconn_quote_name
, ZEND_ACC_PUBLIC
)
1972 PHP_ME(pqconn
, escapeBytea
, ai_pqconn_escape_bytea
, ZEND_ACC_PUBLIC
)
1973 PHP_ME(pqconn
, unescapeBytea
, ai_pqconn_unescape_bytea
, ZEND_ACC_PUBLIC
)
1974 PHP_ME(pqconn
, startTransaction
, ai_pqconn_start_transaction
, ZEND_ACC_PUBLIC
)
1975 PHP_ME(pqconn
, startTransactionAsync
, ai_pqconn_start_transaction_async
, ZEND_ACC_PUBLIC
)
1976 PHP_ME(pqconn
, trace
, ai_pqconn_trace
, ZEND_ACC_PUBLIC
)
1977 PHP_ME(pqconn
, off
, ai_pqconn_off
, ZEND_ACC_PUBLIC
)
1978 PHP_ME(pqconn
, on
, ai_pqconn_on
, ZEND_ACC_PUBLIC
)
1979 PHP_ME(pqconn
, setConverter
, ai_pqconn_set_converter
, ZEND_ACC_PUBLIC
)
1980 PHP_ME(pqconn
, unsetConverter
, ai_pqconn_unset_converter
, ZEND_ACC_PUBLIC
)
1984 PHP_MSHUTDOWN_FUNCTION(pqconn
)
1986 php_persistent_handle_cleanup(PHP_PQ_G
->connection
.name
, NULL
);
1987 zend_string_release(PHP_PQ_G
->connection
.name
);
1988 zend_hash_destroy(&php_pqconn_object_prophandlers
);
1992 PHP_MINIT_FUNCTION(pqconn
)
1994 zend_class_entry ce
= {0};
1995 php_pq_object_prophandler_t ph
= {0};
1997 INIT_NS_CLASS_ENTRY(ce
, "pq", "Connection", php_pqconn_methods
);
1998 php_pqconn_class_entry
= zend_register_internal_class_ex(&ce
, NULL
);
1999 php_pqconn_class_entry
->create_object
= php_pqconn_create_object
;
2001 memcpy(&php_pqconn_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
2002 php_pqconn_object_handlers
.offset
= XtOffsetOf(php_pqconn_object_t
, zo
);
2003 php_pqconn_object_handlers
.free_obj
= php_pqconn_object_free
;
2004 php_pqconn_object_handlers
.read_property
= php_pq_object_read_prop
;
2005 php_pqconn_object_handlers
.write_property
= php_pq_object_write_prop
;
2006 php_pqconn_object_handlers
.clone_obj
= NULL
;
2007 php_pqconn_object_handlers
.get_property_ptr_ptr
= NULL
;
2008 php_pqconn_object_handlers
.get_gc
= php_pq_object_get_gc
;
2009 php_pqconn_object_handlers
.get_properties
= php_pq_object_properties
;
2010 php_pqconn_object_handlers
.get_debug_info
= php_pq_object_debug_info
;
2012 zend_hash_init(&php_pqconn_object_prophandlers
, 23, NULL
, php_pq_object_prophandler_dtor
, 1);
2014 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("status"), CONNECTION_BAD
, ZEND_ACC_PUBLIC
);
2015 ph
.read
= php_pqconn_object_read_status
;
2016 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "status", sizeof("status")-1, (void *) &ph
, sizeof(ph
));
2018 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("transactionStatus"), PQTRANS_UNKNOWN
, ZEND_ACC_PUBLIC
);
2019 ph
.read
= php_pqconn_object_read_transaction_status
;
2020 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "transactionStatus", sizeof("transactionStatus")-1, (void *) &ph
, sizeof(ph
));
2022 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("socket"), ZEND_ACC_PUBLIC
);
2023 ph
.read
= NULL
; /* forward to std prophandler */
2024 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "socket", sizeof("socket")-1, (void *) &ph
, sizeof(ph
));
2026 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("errorMessage"), ZEND_ACC_PUBLIC
);
2027 ph
.read
= php_pqconn_object_read_error_message
;
2028 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "errorMessage", sizeof("errorMessage")-1, (void *) &ph
, sizeof(ph
));
2030 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("busy"), 0, ZEND_ACC_PUBLIC
);
2031 ph
.read
= php_pqconn_object_read_busy
;
2032 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "busy", sizeof("busy")-1, (void *) &ph
, sizeof(ph
));
2034 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("encoding"), ZEND_ACC_PUBLIC
);
2035 ph
.read
= php_pqconn_object_read_encoding
;
2036 ph
.write
= php_pqconn_object_write_encoding
;
2037 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "encoding", sizeof("encoding")-1, (void *) &ph
, sizeof(ph
));
2040 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("unbuffered"), 0, ZEND_ACC_PUBLIC
);
2041 ph
.read
= php_pqconn_object_read_unbuffered
;
2042 ph
.write
= php_pqconn_object_write_unbuffered
;
2043 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "unbuffered", sizeof("unbuffered")-1, (void *) &ph
, sizeof(ph
));
2046 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("nonblocking"), 0, ZEND_ACC_PUBLIC
);
2047 ph
.read
= php_pqconn_object_read_nonblocking
;
2048 ph
.write
= php_pqconn_object_write_nonblocking
;
2049 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "nonblocking", sizeof("nonblocking")-1, (void *) &ph
, sizeof(ph
));
2052 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("db"), ZEND_ACC_PUBLIC
);
2053 ph
.read
= php_pqconn_object_read_db
;
2054 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "db", sizeof("db")-1, (void *) &ph
, sizeof(ph
));
2056 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("user"), ZEND_ACC_PUBLIC
);
2057 ph
.read
= php_pqconn_object_read_user
;
2058 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "user", sizeof("user")-1, (void *) &ph
, sizeof(ph
));
2060 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("pass"), ZEND_ACC_PUBLIC
);
2061 ph
.read
= php_pqconn_object_read_pass
;
2062 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "pass", sizeof("pass")-1, (void *) &ph
, sizeof(ph
));
2064 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("host"), ZEND_ACC_PUBLIC
);
2065 ph
.read
= php_pqconn_object_read_host
;
2066 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "host", sizeof("host")-1, (void *) &ph
, sizeof(ph
));
2068 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("port"), ZEND_ACC_PUBLIC
);
2069 ph
.read
= php_pqconn_object_read_port
;
2070 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "port", sizeof("port")-1, (void *) &ph
, sizeof(ph
));
2073 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("params"), ZEND_ACC_PUBLIC
);
2074 ph
.read
= php_pqconn_object_read_params
;
2075 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "params", sizeof("params")-1, (void *) &ph
, sizeof(ph
));
2078 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("options"), ZEND_ACC_PUBLIC
);
2079 ph
.read
= php_pqconn_object_read_options
;
2080 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "options", sizeof("options")-1, (void *) &ph
, sizeof(ph
));
2082 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("eventHandlers"), ZEND_ACC_PUBLIC
);
2083 ph
.read
= php_pqconn_object_read_event_handlers
;
2084 ph
.gc
= php_pqconn_object_gc_event_handlers
;
2085 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "eventHandlers", sizeof("eventHandlers")-1, (void *) &ph
, sizeof(ph
));
2088 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("listeners"), ZEND_ACC_PUBLIC
);
2089 ph
.read
= php_pqconn_object_read_listeners
;
2090 ph
.gc
= php_pqconn_object_gc_listeners
;
2091 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "listeners", sizeof("listeners")-1, (void *) &ph
, sizeof(ph
));
2094 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("converters"), ZEND_ACC_PUBLIC
);
2095 ph
.read
= php_pqconn_object_read_converters
;
2096 ph
.gc
= php_pqconn_object_gc_converters
;
2097 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "converters", sizeof("converters")-1, (void *) &ph
, sizeof(ph
));
2100 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultFetchType"), 0, ZEND_ACC_PUBLIC
);
2101 ph
.read
= php_pqconn_object_read_def_fetch_type
;
2102 ph
.write
= php_pqconn_object_write_def_fetch_type
;
2103 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultFetchType", sizeof("defaultFetchType")-1, (void *) &ph
, sizeof(ph
));
2106 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionIsolation"), 0, ZEND_ACC_PUBLIC
);
2107 ph
.read
= php_pqconn_object_read_def_txn_isolation
;
2108 ph
.write
= php_pqconn_object_write_def_txn_isolation
;
2109 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultTransactionIsolation", sizeof("defaultTransactionIsolation")-1, (void *) &ph
, sizeof(ph
));
2112 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionReadonly"), 0, ZEND_ACC_PUBLIC
);
2113 ph
.read
= php_pqconn_object_read_def_txn_readonly
;
2114 ph
.write
= php_pqconn_object_write_def_txn_readonly
;
2115 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultTransactionReadonly", sizeof("defaultTransactionReadonly")-1, (void *) &ph
, sizeof(ph
));
2118 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionDeferrable"), 0, ZEND_ACC_PUBLIC
);
2119 ph
.read
= php_pqconn_object_read_def_txn_deferrable
;
2120 ph
.write
= php_pqconn_object_write_def_txn_deferrable
;
2121 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultTransactionDeferrable", sizeof("defaultTransactionDeferrable")-1, (void *) &ph
, sizeof(ph
));
2124 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultAutoConvert"), PHP_PQRES_CONV_ALL
, ZEND_ACC_PUBLIC
);
2125 ph
.read
= php_pqconn_object_read_def_auto_conv
;
2126 ph
.write
= php_pqconn_object_write_def_auto_conv
;
2127 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultAutoConvert", sizeof("defaultAutoConvert")-1, (void *) &ph
, sizeof(ph
));
2130 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("OK"), CONNECTION_OK
);
2131 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("BAD"), CONNECTION_BAD
);
2132 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("STARTED"), CONNECTION_STARTED
);
2133 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("MADE"), CONNECTION_MADE
);
2134 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AWAITING_RESPONSE"), CONNECTION_AWAITING_RESPONSE
);
2135 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AUTH_OK"), CONNECTION_AUTH_OK
);
2136 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SSL_STARTUP"), CONNECTION_SSL_STARTUP
);
2137 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SETENV"), CONNECTION_SETENV
);
2139 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_IDLE"), PQTRANS_IDLE
);
2140 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_ACTIVE"), PQTRANS_ACTIVE
);
2141 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INTRANS"), PQTRANS_INTRANS
);
2142 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INERROR"), PQTRANS_INERROR
);
2143 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_UNKNOWN"), PQTRANS_UNKNOWN
);
2145 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_FAILED"), PGRES_POLLING_FAILED
);
2146 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_READING"), PGRES_POLLING_READING
);
2147 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_WRITING"), PGRES_POLLING_WRITING
);
2148 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_OK"), PGRES_POLLING_OK
);
2150 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_NOTICE"), ZEND_STRL("notice"));
2151 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESULT"), ZEND_STRL("result"));
2152 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESET"), ZEND_STRL("reset"));
2154 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("ASYNC"), 0x1);
2155 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("PERSISTENT"), 0x2);
2157 PHP_PQ_G
->connection
.name
= zend_string_init(ZEND_STRL("pq\\Connection"), 1);
2159 return php_persistent_handle_provide(PHP_PQ_G
->connection
.name
, php_pqconn_get_resource_factory_ops(), NULL
, NULL
);
2167 * vim600: noet sw=4 ts=4 fdm=marker
2168 * vim<600: noet sw=4 ts=4