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
);
1153 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), return_value
);
1156 php_pqconn_notify_listeners(obj
);
1161 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_async
, 0, 0, 1)
1162 ZEND_ARG_INFO(0, query
)
1163 ZEND_ARG_INFO(0, callable
)
1164 ZEND_END_ARG_INFO();
1165 static PHP_METHOD(pqconn
, execAsync
) {
1166 zend_error_handling zeh
;
1167 php_pq_callback_t resolver
= {{0}};
1170 ZEND_RESULT_CODE rv
;
1172 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1173 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s|f", &query_str
, &query_len
, &resolver
.fci
, &resolver
.fcc
);
1174 zend_restore_error_handling(&zeh
);
1176 if (SUCCESS
== rv
) {
1177 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1180 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1181 } else if (!PQsendQuery(obj
->intern
->conn
, query_str
)) {
1182 throw_exce(EX_IO
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1183 #if HAVE_PQSETSINGLEROWMODE
1184 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1185 throw_exce(EX_RUNTIME
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1188 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver
);
1189 obj
->intern
->poller
= PQconsumeInput
;
1190 php_pqconn_notify_listeners(obj
);
1195 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params
, 0, 0, 2)
1196 ZEND_ARG_INFO(0, query
)
1197 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1198 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1199 ZEND_END_ARG_INFO();
1200 static PHP_METHOD(pqconn
, execParams
) {
1201 zend_error_handling zeh
;
1205 zval
*ztypes
= NULL
;
1206 ZEND_RESULT_CODE rv
;
1208 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1209 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sa/|a/!", &query_str
, &query_len
, &zparams
, &ztypes
);
1210 zend_restore_error_handling(&zeh
);
1212 if (SUCCESS
== rv
) {
1213 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1216 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1219 php_pq_params_t
*params
;
1221 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
));
1222 res
= PQexecParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1223 php_pq_params_free(¶ms
);
1226 throw_exce(EX_RUNTIME
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1228 if (SUCCESS
== php_pqres_success(res
)) {
1229 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), return_value
);
1231 php_pqres_clear(res
);
1234 php_pqconn_notify_listeners(obj
);
1240 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async
, 0, 0, 2)
1241 ZEND_ARG_INFO(0, query
)
1242 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1243 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1244 ZEND_ARG_INFO(0, callable
)
1245 ZEND_END_ARG_INFO();
1246 static PHP_METHOD(pqconn
, execParamsAsync
) {
1247 zend_error_handling zeh
;
1248 php_pq_callback_t resolver
= {{0}};
1252 zval
*ztypes
= NULL
;
1253 ZEND_RESULT_CODE rv
;
1255 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1256 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sa/|a/!f", &query_str
, &query_len
, &zparams
, &ztypes
, &resolver
.fci
, &resolver
.fcc
);
1257 zend_restore_error_handling(&zeh
);
1259 if (SUCCESS
== rv
) {
1260 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1263 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1266 php_pq_params_t
*params
;
1268 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
));
1269 rc
= PQsendQueryParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1270 php_pq_params_free(¶ms
);
1273 throw_exce(EX_IO
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1274 #if HAVE_PQSETSINGLEROWMODE
1275 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1276 throw_exce(EX_RUNTIME
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1279 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver
);
1280 obj
->intern
->poller
= PQconsumeInput
;
1281 php_pqconn_notify_listeners(obj
);
1285 zend_restore_error_handling(&zeh
);
1288 ZEND_RESULT_CODE
php_pqconn_prepare(zval
*object
, php_pqconn_object_t
*obj
, const char *name
, const char *query
, php_pq_params_t
*params
)
1291 ZEND_RESULT_CODE rv
;
1294 obj
= PHP_PQ_OBJ(object
, NULL
);
1297 res
= php_pq_prepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
);
1301 throw_exce(EX_RUNTIME
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1303 rv
= php_pqres_success(res
);
1304 php_pqres_clear(res
);
1305 php_pqconn_notify_listeners(obj
);
1311 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare
, 0, 0, 2)
1312 ZEND_ARG_INFO(0, name
)
1313 ZEND_ARG_INFO(0, query
)
1314 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1315 ZEND_END_ARG_INFO();
1316 static PHP_METHOD(pqconn
, prepare
) {
1317 zend_error_handling zeh
;
1318 zval
*ztypes
= NULL
;
1319 char *name_str
, *query_str
;
1320 size_t name_len
, *query_len
;
1321 ZEND_RESULT_CODE rv
;
1323 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1324 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1325 zend_restore_error_handling(&zeh
);
1327 if (SUCCESS
== rv
) {
1328 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1331 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1333 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL
);
1335 if (SUCCESS
!= php_pqconn_prepare(getThis(), obj
, name_str
, query_str
, params
)) {
1336 php_pq_params_free(¶ms
);
1338 php_pqstm_t
*stm
= php_pqstm_init(obj
, name_str
, query_str
, params
);
1340 RETVAL_OBJ(&php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
)->zo
);
1346 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
)
1348 ZEND_RESULT_CODE rv
;
1351 obj
= PHP_PQ_OBJ(object
, NULL
);
1354 if (!PQsendPrepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
)) {
1356 throw_exce(EX_IO
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1359 obj
->intern
->poller
= PQconsumeInput
;
1360 php_pqconn_notify_listeners(obj
);
1366 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare_async
, 0, 0, 2)
1367 ZEND_ARG_INFO(0, name
)
1368 ZEND_ARG_INFO(0, query
)
1369 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1370 ZEND_END_ARG_INFO();
1371 static PHP_METHOD(pqconn
, prepareAsync
) {
1372 zend_error_handling zeh
;
1373 zval
*ztypes
= NULL
;
1374 char *name_str
, *query_str
;
1375 size_t name_len
, *query_len
;
1376 ZEND_RESULT_CODE rv
;
1378 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1379 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1380 zend_restore_error_handling(&zeh
);
1382 if (SUCCESS
== rv
) {
1383 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1386 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1388 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL
);
1390 if (SUCCESS
!= php_pqconn_prepare_async(getThis(), obj
, name_str
, query_str
, params
)) {
1391 php_pq_params_free(¶ms
);
1393 php_pqstm_t
*stm
= php_pqstm_init(obj
, name_str
, query_str
, params
);
1395 RETVAL_OBJ(&php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
)->zo
);
1401 ZEND_RESULT_CODE
php_pqconn_declare(zval
*object
, php_pqconn_object_t
*obj
, const char *decl
)
1404 ZEND_RESULT_CODE rv
;
1407 obj
= PHP_PQ_OBJ(object
, NULL
);
1410 res
= php_pq_exec(obj
->intern
->conn
, decl
);
1414 throw_exce(EX_RUNTIME
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1416 rv
= php_pqres_success(res
);
1417 php_pqres_clear(res
);
1418 php_pqconn_notify_listeners(obj
);
1424 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare
, 0, 0, 3)
1425 ZEND_ARG_INFO(0, name
)
1426 ZEND_ARG_INFO(0, flags
)
1427 ZEND_ARG_INFO(0, query
)
1428 ZEND_END_ARG_INFO();
1429 static PHP_METHOD(pqconn
, declare
) {
1430 zend_error_handling zeh
;
1431 char *name_str
, *query_str
;
1432 size_t name_len
, query_len
;
1434 ZEND_RESULT_CODE rv
;
1436 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1437 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1438 zend_restore_error_handling(&zeh
);
1440 if (SUCCESS
== rv
) {
1441 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1444 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1447 char *decl
= php_pqcur_declare_str(name_str
, name_len
, flags
, query_str
, query_len
, &query_offset
);
1449 if (SUCCESS
!= php_pqconn_declare(getThis(), obj
, decl
)) {
1452 php_pqcur_t
*cur
= php_pqcur_init(obj
, name_str
, decl
, query_offset
, flags
);
1454 RETVAL_OBJ(&php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
)->zo
);
1460 ZEND_RESULT_CODE
php_pqconn_declare_async(zval
*object
, php_pqconn_object_t
*obj
, const char *decl
)
1462 ZEND_RESULT_CODE rv
;
1465 obj
= PHP_PQ_OBJ(object
, NULL
);
1468 if (!PQsendQuery(obj
->intern
->conn
, decl
)) {
1470 throw_exce(EX_IO
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1473 obj
->intern
->poller
= PQconsumeInput
;
1474 php_pqconn_notify_listeners(obj
);
1480 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare_async
, 0, 0, 2)
1481 ZEND_ARG_INFO(0, name
)
1482 ZEND_ARG_INFO(0, flags
)
1483 ZEND_ARG_INFO(0, query
)
1484 ZEND_END_ARG_INFO();
1485 static PHP_METHOD(pqconn
, declareAsync
) {
1486 zend_error_handling zeh
;
1487 char *name_str
, *query_str
;
1488 size_t name_len
, query_len
;
1490 ZEND_RESULT_CODE rv
;
1492 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1493 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1494 zend_restore_error_handling(&zeh
);
1496 if (SUCCESS
== rv
) {
1497 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1500 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1503 char *decl
= php_pqcur_declare_str(name_str
, name_len
, flags
, query_str
, query_len
, &query_offset
);
1505 if (SUCCESS
!= php_pqconn_declare_async(getThis(), obj
, decl
)) {
1508 php_pqcur_t
*cur
= php_pqcur_init(obj
, name_str
, decl
, query_offset
, flags
);
1510 RETVAL_OBJ(&php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
)->zo
);
1516 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote
, 0, 0, 1)
1517 ZEND_ARG_INFO(0, string
)
1518 ZEND_END_ARG_INFO();
1519 static PHP_METHOD(pqconn
, quote
) {
1523 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1524 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1527 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1529 char *quoted
= PQescapeLiteral(obj
->intern
->conn
, str
, len
);
1532 php_error_docref(NULL
, E_WARNING
, "Failed to quote string (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1535 RETVAL_STRING(quoted
);
1542 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote_name
, 0, 0, 1)
1543 ZEND_ARG_INFO(0, type
)
1544 ZEND_END_ARG_INFO();
1545 static PHP_METHOD(pqconn
, quoteName
) {
1549 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1550 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1553 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1555 char *quoted
= PQescapeIdentifier(obj
->intern
->conn
, str
, len
);
1558 php_error_docref(NULL
, E_WARNING
, "Failed to quote name (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1561 RETVAL_STRING(quoted
);
1568 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_escape_bytea
, 0, 0, 1)
1569 ZEND_ARG_INFO(0, bytea
)
1570 ZEND_END_ARG_INFO();
1571 static PHP_METHOD(pqconn
, escapeBytea
) {
1575 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1576 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1579 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1582 char *escaped_str
= (char *) PQescapeByteaConn(obj
->intern
->conn
, (unsigned char *) str
, len
, &escaped_len
);
1585 php_error_docref(NULL
, E_WARNING
, "Failed to escape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1588 RETVAL_STRINGL(escaped_str
, escaped_len
- 1);
1589 PQfreemem(escaped_str
);
1595 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unescape_bytea
, 0, 0, 1)
1596 ZEND_ARG_INFO(0, bytea
)
1597 ZEND_END_ARG_INFO();
1598 static PHP_METHOD(pqconn
, unescapeBytea
) {
1602 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1603 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1606 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1608 size_t unescaped_len
;
1609 char *unescaped_str
= (char *) PQunescapeBytea((unsigned char *)str
, &unescaped_len
);
1611 if (!unescaped_str
) {
1612 php_error_docref(NULL
, E_WARNING
, "Failed to unescape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1615 RETVAL_STRINGL(unescaped_str
, unescaped_len
);
1616 PQfreemem(unescaped_str
);
1622 ZEND_RESULT_CODE
php_pqconn_start_transaction(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable
)
1624 ZEND_RESULT_CODE rv
= FAILURE
;
1627 conn_obj
= PHP_PQ_OBJ(zconn
, NULL
);
1630 if (!conn_obj
->intern
) {
1631 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1634 smart_str cmd
= {0};
1635 const char *il
= php_pq_isolation_level(&isolation
);
1637 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1638 smart_str_appends(&cmd
, il
);
1639 smart_str_appends(&cmd
, ", READ ");
1640 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1641 smart_str_appends(&cmd
, ",");
1642 smart_str_appends(&cmd
, deferrable
? "" : " NOT");
1643 smart_str_appends(&cmd
, " DEFERRABLE");
1646 res
= php_pq_exec(conn_obj
->intern
->conn
, smart_str_v(&cmd
));
1649 throw_exce(EX_RUNTIME
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1651 rv
= php_pqres_success(res
);
1652 php_pqres_clear(res
);
1653 php_pqconn_notify_listeners(conn_obj
);
1656 smart_str_free(&cmd
);
1662 ZEND_RESULT_CODE
php_pqconn_start_transaction_async(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable
)
1664 ZEND_RESULT_CODE rv
= FAILURE
;
1667 conn_obj
= PHP_PQ_OBJ(zconn
, NULL
);
1670 if (!conn_obj
->intern
) {
1671 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1673 smart_str cmd
= {0};
1674 const char *il
= php_pq_isolation_level(&isolation
);
1676 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1677 smart_str_appends(&cmd
, il
);
1678 smart_str_appends(&cmd
, ", READ ");
1679 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1680 smart_str_appends(&cmd
, ",");
1681 smart_str_appends(&cmd
, deferrable
? "" : "NOT ");
1682 smart_str_appends(&cmd
, " DEFERRABLE");
1685 if (!PQsendQuery(conn_obj
->intern
->conn
, smart_str_v(&cmd
))) {
1686 throw_exce(EX_IO
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1689 conn_obj
->intern
->poller
= PQconsumeInput
;
1690 php_pqconn_notify_listeners(conn_obj
);
1693 smart_str_free(&cmd
);
1699 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction
, 0, 0, 0)
1700 ZEND_ARG_INFO(0, isolation
)
1701 ZEND_ARG_INFO(0, readonly
)
1702 ZEND_ARG_INFO(0, deferrable
)
1703 ZEND_END_ARG_INFO();
1704 static PHP_METHOD(pqconn
, startTransaction
) {
1705 zend_error_handling zeh
;
1706 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1707 zend_long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1708 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1709 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1710 ZEND_RESULT_CODE rv
;
1712 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1713 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "|lbb", &isolation
, &readonly
, &deferrable
);
1714 zend_restore_error_handling(&zeh
);
1716 if (SUCCESS
== rv
) {
1717 rv
= php_pqconn_start_transaction(getThis(), obj
, isolation
, readonly
, deferrable
);
1719 if (SUCCESS
== rv
) {
1720 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1722 php_pq_object_addref(obj
);
1725 txn
->isolation
= isolation
;
1726 txn
->readonly
= readonly
;
1727 txn
->deferrable
= deferrable
;
1729 RETVAL_OBJ(&php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
)->zo
);
1734 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction_async
, 0, 0, 0)
1735 ZEND_ARG_INFO(0, isolation
)
1736 ZEND_ARG_INFO(0, readonly
)
1737 ZEND_ARG_INFO(0, deferrable
)
1738 ZEND_END_ARG_INFO();
1739 static PHP_METHOD(pqconn
, startTransactionAsync
) {
1740 zend_error_handling zeh
;
1741 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1742 zend_long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1743 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1744 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1745 ZEND_RESULT_CODE rv
;
1747 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1748 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "|lbb", &isolation
, &readonly
, &deferrable
);
1749 zend_restore_error_handling(&zeh
);
1751 if (SUCCESS
== rv
) {
1752 rv
= php_pqconn_start_transaction_async(getThis(), obj
, isolation
, readonly
, deferrable
);
1754 if (SUCCESS
== rv
) {
1755 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1757 php_pq_object_addref(obj
);
1760 txn
->isolation
= isolation
;
1761 txn
->readonly
= readonly
;
1762 txn
->deferrable
= deferrable
;
1764 RETVAL_OBJ(&php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
)->zo
);
1769 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_trace
, 0, 0, 0)
1770 ZEND_ARG_INFO(0, stdio_stream
)
1771 ZEND_END_ARG_INFO();
1772 static PHP_METHOD(pqconn
, trace
) {
1773 zval
*zstream
= NULL
;
1775 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &zstream
)) {
1776 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1779 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1782 PQuntrace(obj
->intern
->conn
);
1786 php_stream
*stream
= NULL
;
1788 php_stream_from_zval(stream
, zstream
);
1790 if (SUCCESS
!= php_stream_cast(stream
, PHP_STREAM_AS_STDIO
, (void *) &fp
, REPORT_ERRORS
)) {
1793 stream
->flags
|= PHP_STREAM_FLAG_NO_CLOSE
;
1794 PQtrace(obj
->intern
->conn
, fp
);
1802 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_off
, 0, 0, 1)
1803 ZEND_ARG_INFO(0, type
)
1804 ZEND_END_ARG_INFO();
1805 static PHP_METHOD(pqconn
, off
) {
1806 zend_error_handling zeh
;
1808 ZEND_RESULT_CODE rv
;
1810 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1811 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "S", &type
);
1812 zend_restore_error_handling(&zeh
);
1814 if (SUCCESS
== rv
) {
1815 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1818 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1820 RETURN_BOOL(SUCCESS
== zend_hash_del(&obj
->intern
->eventhandlers
, type
));
1825 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_on
, 0, 0, 2)
1826 ZEND_ARG_INFO(0, type
)
1827 ZEND_ARG_INFO(0, callable
)
1828 ZEND_END_ARG_INFO();
1829 static PHP_METHOD(pqconn
, on
) {
1830 zend_error_handling zeh
;
1833 php_pq_callback_t cb
= {{0}};
1834 ZEND_RESULT_CODE rv
;
1836 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1837 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sf", &type_str
, &type_len
, &cb
.fci
, &cb
.fcc
);
1838 zend_restore_error_handling(&zeh
);
1840 if (SUCCESS
== rv
) {
1841 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1844 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1846 RETVAL_LONG(php_pqconn_add_eventhandler(obj
, type_str
, type_len
, &cb
));
1851 struct apply_set_converter_arg
{
1857 static int apply_set_converter(zval
*zoid
, void *a
)
1859 zend_long oid
= zval_get_long(zoid
);
1860 struct apply_set_converter_arg
*arg
= a
;
1863 Z_ADDREF_P(arg
->zconv
);
1864 zend_hash_index_update(arg
->ht
, oid
, arg
->zconv
);
1866 zend_hash_index_del(arg
->ht
, oid
);
1869 return ZEND_HASH_APPLY_KEEP
;
1872 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_set_converter
, 0, 0, 1)
1873 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\Converter
, 0)
1874 ZEND_END_ARG_INFO();
1875 static PHP_METHOD(pqconn
, setConverter
) {
1876 ZEND_RESULT_CODE rv
;
1877 zend_error_handling zeh
;
1880 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1881 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zcnv
, php_pqconv_class_entry
);
1882 zend_restore_error_handling(&zeh
);
1884 if (SUCCESS
== rv
) {
1885 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1888 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1891 struct apply_set_converter_arg arg
= {NULL
};
1894 zend_call_method_with_0_params(zcnv
, NULL
, NULL
, "converttypes", &zoids
);
1895 ZVAL_DUP(&tmp
, &zoids
);
1896 convert_to_array(&tmp
);
1898 arg
.ht
= &obj
->intern
->converters
;
1902 zend_hash_apply_with_argument(Z_ARRVAL(tmp
), apply_set_converter
, &arg
);
1904 zval_ptr_dtor(&tmp
);
1905 zval_ptr_dtor(&zoids
);
1910 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unset_converter
, 0, 0, 1)
1911 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\Converter
, 0)
1912 ZEND_END_ARG_INFO();
1913 static PHP_METHOD(pqconn
, unsetConverter
) {
1914 ZEND_RESULT_CODE rv
;
1915 zend_error_handling zeh
;
1918 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1919 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zcnv
, php_pqconv_class_entry
);
1920 zend_restore_error_handling(&zeh
);
1922 if (SUCCESS
== rv
) {
1923 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1926 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1929 struct apply_set_converter_arg arg
= {NULL
};
1932 zend_call_method_with_0_params(zcnv
, NULL
, NULL
, "converttypes", &zoids
);
1933 ZVAL_DUP(&tmp
, &zoids
);
1934 convert_to_array(&tmp
);
1936 arg
.ht
= &obj
->intern
->converters
;
1940 zend_hash_apply_with_argument(Z_ARRVAL(tmp
), apply_set_converter
, &arg
);
1942 zval_ptr_dtor(&tmp
);
1943 zval_ptr_dtor(&zoids
);
1948 static zend_function_entry php_pqconn_methods
[] = {
1949 PHP_ME(pqconn
, __construct
, ai_pqconn_construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
1950 PHP_ME(pqconn
, reset
, ai_pqconn_reset
, ZEND_ACC_PUBLIC
)
1951 PHP_ME(pqconn
, resetAsync
, ai_pqconn_reset_async
, ZEND_ACC_PUBLIC
)
1952 PHP_ME(pqconn
, poll
, ai_pqconn_poll
, ZEND_ACC_PUBLIC
)
1953 PHP_ME(pqconn
, flush
, ai_pqconn_flush
, ZEND_ACC_PUBLIC
)
1954 PHP_ME(pqconn
, exec
, ai_pqconn_exec
, ZEND_ACC_PUBLIC
)
1955 PHP_ME(pqconn
, execAsync
, ai_pqconn_exec_async
, ZEND_ACC_PUBLIC
)
1956 PHP_ME(pqconn
, execParams
, ai_pqconn_exec_params
, ZEND_ACC_PUBLIC
)
1957 PHP_ME(pqconn
, execParamsAsync
, ai_pqconn_exec_params_async
, ZEND_ACC_PUBLIC
)
1958 PHP_ME(pqconn
, prepare
, ai_pqconn_prepare
, ZEND_ACC_PUBLIC
)
1959 PHP_ME(pqconn
, prepareAsync
, ai_pqconn_prepare_async
, ZEND_ACC_PUBLIC
)
1960 PHP_ME(pqconn
, declare
, ai_pqconn_declare
, ZEND_ACC_PUBLIC
)
1961 PHP_ME(pqconn
, declareAsync
, ai_pqconn_declare_async
, ZEND_ACC_PUBLIC
)
1962 PHP_ME(pqconn
, unlisten
, ai_pqconn_unlisten
, ZEND_ACC_PUBLIC
)
1963 PHP_ME(pqconn
, unlistenAsync
, ai_pqconn_unlisten_async
, ZEND_ACC_PUBLIC
)
1964 PHP_ME(pqconn
, listen
, ai_pqconn_listen
, ZEND_ACC_PUBLIC
)
1965 PHP_ME(pqconn
, listenAsync
, ai_pqconn_listen_async
, ZEND_ACC_PUBLIC
)
1966 PHP_ME(pqconn
, notify
, ai_pqconn_notify
, ZEND_ACC_PUBLIC
)
1967 PHP_ME(pqconn
, notifyAsync
, ai_pqconn_notify_async
, ZEND_ACC_PUBLIC
)
1968 PHP_ME(pqconn
, getResult
, ai_pqconn_get_result
, ZEND_ACC_PUBLIC
)
1969 PHP_ME(pqconn
, quote
, ai_pqconn_quote
, ZEND_ACC_PUBLIC
)
1970 PHP_ME(pqconn
, quoteName
, ai_pqconn_quote_name
, ZEND_ACC_PUBLIC
)
1971 PHP_ME(pqconn
, escapeBytea
, ai_pqconn_escape_bytea
, ZEND_ACC_PUBLIC
)
1972 PHP_ME(pqconn
, unescapeBytea
, ai_pqconn_unescape_bytea
, ZEND_ACC_PUBLIC
)
1973 PHP_ME(pqconn
, startTransaction
, ai_pqconn_start_transaction
, ZEND_ACC_PUBLIC
)
1974 PHP_ME(pqconn
, startTransactionAsync
, ai_pqconn_start_transaction_async
, ZEND_ACC_PUBLIC
)
1975 PHP_ME(pqconn
, trace
, ai_pqconn_trace
, ZEND_ACC_PUBLIC
)
1976 PHP_ME(pqconn
, off
, ai_pqconn_off
, ZEND_ACC_PUBLIC
)
1977 PHP_ME(pqconn
, on
, ai_pqconn_on
, ZEND_ACC_PUBLIC
)
1978 PHP_ME(pqconn
, setConverter
, ai_pqconn_set_converter
, ZEND_ACC_PUBLIC
)
1979 PHP_ME(pqconn
, unsetConverter
, ai_pqconn_unset_converter
, ZEND_ACC_PUBLIC
)
1983 PHP_MSHUTDOWN_FUNCTION(pqconn
)
1985 php_persistent_handle_cleanup(PHP_PQ_G
->connection
.name
, NULL
);
1986 zend_string_release(PHP_PQ_G
->connection
.name
);
1987 zend_hash_destroy(&php_pqconn_object_prophandlers
);
1991 PHP_MINIT_FUNCTION(pqconn
)
1993 zend_class_entry ce
= {0};
1994 php_pq_object_prophandler_t ph
= {0};
1996 INIT_NS_CLASS_ENTRY(ce
, "pq", "Connection", php_pqconn_methods
);
1997 php_pqconn_class_entry
= zend_register_internal_class_ex(&ce
, NULL
);
1998 php_pqconn_class_entry
->create_object
= php_pqconn_create_object
;
2000 memcpy(&php_pqconn_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
2001 php_pqconn_object_handlers
.offset
= XtOffsetOf(php_pqconn_object_t
, zo
);
2002 php_pqconn_object_handlers
.free_obj
= php_pqconn_object_free
;
2003 php_pqconn_object_handlers
.read_property
= php_pq_object_read_prop
;
2004 php_pqconn_object_handlers
.write_property
= php_pq_object_write_prop
;
2005 php_pqconn_object_handlers
.clone_obj
= NULL
;
2006 php_pqconn_object_handlers
.get_property_ptr_ptr
= NULL
;
2007 php_pqconn_object_handlers
.get_gc
= php_pq_object_get_gc
;
2008 php_pqconn_object_handlers
.get_properties
= php_pq_object_properties
;
2009 php_pqconn_object_handlers
.get_debug_info
= php_pq_object_debug_info
;
2011 zend_hash_init(&php_pqconn_object_prophandlers
, 23, NULL
, php_pq_object_prophandler_dtor
, 1);
2013 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("status"), CONNECTION_BAD
, ZEND_ACC_PUBLIC
);
2014 ph
.read
= php_pqconn_object_read_status
;
2015 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "status", sizeof("status")-1, (void *) &ph
, sizeof(ph
));
2017 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("transactionStatus"), PQTRANS_UNKNOWN
, ZEND_ACC_PUBLIC
);
2018 ph
.read
= php_pqconn_object_read_transaction_status
;
2019 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "transactionStatus", sizeof("transactionStatus")-1, (void *) &ph
, sizeof(ph
));
2021 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("socket"), ZEND_ACC_PUBLIC
);
2022 ph
.read
= NULL
; /* forward to std prophandler */
2023 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "socket", sizeof("socket")-1, (void *) &ph
, sizeof(ph
));
2025 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("errorMessage"), ZEND_ACC_PUBLIC
);
2026 ph
.read
= php_pqconn_object_read_error_message
;
2027 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "errorMessage", sizeof("errorMessage")-1, (void *) &ph
, sizeof(ph
));
2029 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("busy"), 0, ZEND_ACC_PUBLIC
);
2030 ph
.read
= php_pqconn_object_read_busy
;
2031 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "busy", sizeof("busy")-1, (void *) &ph
, sizeof(ph
));
2033 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("encoding"), ZEND_ACC_PUBLIC
);
2034 ph
.read
= php_pqconn_object_read_encoding
;
2035 ph
.write
= php_pqconn_object_write_encoding
;
2036 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "encoding", sizeof("encoding")-1, (void *) &ph
, sizeof(ph
));
2039 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("unbuffered"), 0, ZEND_ACC_PUBLIC
);
2040 ph
.read
= php_pqconn_object_read_unbuffered
;
2041 ph
.write
= php_pqconn_object_write_unbuffered
;
2042 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "unbuffered", sizeof("unbuffered")-1, (void *) &ph
, sizeof(ph
));
2045 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("nonblocking"), 0, ZEND_ACC_PUBLIC
);
2046 ph
.read
= php_pqconn_object_read_nonblocking
;
2047 ph
.write
= php_pqconn_object_write_nonblocking
;
2048 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "nonblocking", sizeof("nonblocking")-1, (void *) &ph
, sizeof(ph
));
2051 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("db"), ZEND_ACC_PUBLIC
);
2052 ph
.read
= php_pqconn_object_read_db
;
2053 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "db", sizeof("db")-1, (void *) &ph
, sizeof(ph
));
2055 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("user"), ZEND_ACC_PUBLIC
);
2056 ph
.read
= php_pqconn_object_read_user
;
2057 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "user", sizeof("user")-1, (void *) &ph
, sizeof(ph
));
2059 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("pass"), ZEND_ACC_PUBLIC
);
2060 ph
.read
= php_pqconn_object_read_pass
;
2061 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "pass", sizeof("pass")-1, (void *) &ph
, sizeof(ph
));
2063 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("host"), ZEND_ACC_PUBLIC
);
2064 ph
.read
= php_pqconn_object_read_host
;
2065 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "host", sizeof("host")-1, (void *) &ph
, sizeof(ph
));
2067 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("port"), ZEND_ACC_PUBLIC
);
2068 ph
.read
= php_pqconn_object_read_port
;
2069 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "port", sizeof("port")-1, (void *) &ph
, sizeof(ph
));
2072 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("params"), ZEND_ACC_PUBLIC
);
2073 ph
.read
= php_pqconn_object_read_params
;
2074 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "params", sizeof("params")-1, (void *) &ph
, sizeof(ph
));
2077 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("options"), ZEND_ACC_PUBLIC
);
2078 ph
.read
= php_pqconn_object_read_options
;
2079 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "options", sizeof("options")-1, (void *) &ph
, sizeof(ph
));
2081 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("eventHandlers"), ZEND_ACC_PUBLIC
);
2082 ph
.read
= php_pqconn_object_read_event_handlers
;
2083 ph
.gc
= php_pqconn_object_gc_event_handlers
;
2084 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "eventHandlers", sizeof("eventHandlers")-1, (void *) &ph
, sizeof(ph
));
2087 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("listeners"), ZEND_ACC_PUBLIC
);
2088 ph
.read
= php_pqconn_object_read_listeners
;
2089 ph
.gc
= php_pqconn_object_gc_listeners
;
2090 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "listeners", sizeof("listeners")-1, (void *) &ph
, sizeof(ph
));
2093 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("converters"), ZEND_ACC_PUBLIC
);
2094 ph
.read
= php_pqconn_object_read_converters
;
2095 ph
.gc
= php_pqconn_object_gc_converters
;
2096 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "converters", sizeof("converters")-1, (void *) &ph
, sizeof(ph
));
2099 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultFetchType"), 0, ZEND_ACC_PUBLIC
);
2100 ph
.read
= php_pqconn_object_read_def_fetch_type
;
2101 ph
.write
= php_pqconn_object_write_def_fetch_type
;
2102 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultFetchType", sizeof("defaultFetchType")-1, (void *) &ph
, sizeof(ph
));
2105 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionIsolation"), 0, ZEND_ACC_PUBLIC
);
2106 ph
.read
= php_pqconn_object_read_def_txn_isolation
;
2107 ph
.write
= php_pqconn_object_write_def_txn_isolation
;
2108 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultTransactionIsolation", sizeof("defaultTransactionIsolation")-1, (void *) &ph
, sizeof(ph
));
2111 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionReadonly"), 0, ZEND_ACC_PUBLIC
);
2112 ph
.read
= php_pqconn_object_read_def_txn_readonly
;
2113 ph
.write
= php_pqconn_object_write_def_txn_readonly
;
2114 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultTransactionReadonly", sizeof("defaultTransactionReadonly")-1, (void *) &ph
, sizeof(ph
));
2117 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionDeferrable"), 0, ZEND_ACC_PUBLIC
);
2118 ph
.read
= php_pqconn_object_read_def_txn_deferrable
;
2119 ph
.write
= php_pqconn_object_write_def_txn_deferrable
;
2120 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultTransactionDeferrable", sizeof("defaultTransactionDeferrable")-1, (void *) &ph
, sizeof(ph
));
2123 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultAutoConvert"), PHP_PQRES_CONV_ALL
, ZEND_ACC_PUBLIC
);
2124 ph
.read
= php_pqconn_object_read_def_auto_conv
;
2125 ph
.write
= php_pqconn_object_write_def_auto_conv
;
2126 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultAutoConvert", sizeof("defaultAutoConvert")-1, (void *) &ph
, sizeof(ph
));
2129 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("OK"), CONNECTION_OK
);
2130 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("BAD"), CONNECTION_BAD
);
2131 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("STARTED"), CONNECTION_STARTED
);
2132 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("MADE"), CONNECTION_MADE
);
2133 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AWAITING_RESPONSE"), CONNECTION_AWAITING_RESPONSE
);
2134 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AUTH_OK"), CONNECTION_AUTH_OK
);
2135 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SSL_STARTUP"), CONNECTION_SSL_STARTUP
);
2136 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SETENV"), CONNECTION_SETENV
);
2138 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_IDLE"), PQTRANS_IDLE
);
2139 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_ACTIVE"), PQTRANS_ACTIVE
);
2140 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INTRANS"), PQTRANS_INTRANS
);
2141 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INERROR"), PQTRANS_INERROR
);
2142 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_UNKNOWN"), PQTRANS_UNKNOWN
);
2144 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_FAILED"), PGRES_POLLING_FAILED
);
2145 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_READING"), PGRES_POLLING_READING
);
2146 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_WRITING"), PGRES_POLLING_WRITING
);
2147 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_OK"), PGRES_POLLING_OK
);
2149 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_NOTICE"), ZEND_STRL("notice"));
2150 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESULT"), ZEND_STRL("result"));
2151 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESET"), ZEND_STRL("reset"));
2153 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("ASYNC"), 0x1);
2154 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("PERSISTENT"), 0x2);
2156 PHP_PQ_G
->connection
.name
= zend_string_init(ZEND_STRL("pq\\Connection"), 1);
2158 return php_persistent_handle_provide(PHP_PQ_G
->connection
.name
, php_pqconn_get_resource_factory_ops(), NULL
, NULL
);
2166 * vim600: noet sw=4 ts=4 fdm=marker
2167 * vim<600: noet sw=4 ts=4