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
->converters
);
90 zend_hash_destroy(&obj
->intern
->eventhandlers
);
94 php_pq_object_dtor(o
);
98 php_pqconn_object_t
*php_pqconn_create_object_ex(zend_class_entry
*ce
, php_pqconn_t
*intern
)
100 return php_pq_object_create(ce
, intern
, sizeof(php_pqconn_object_t
),
101 &php_pqconn_object_handlers
, &php_pqconn_object_prophandlers
);
104 static zend_object
*php_pqconn_create_object(zend_class_entry
*class_type
)
106 return &php_pqconn_create_object_ex(class_type
, NULL
)->zo
;
109 static void php_pqconn_object_read_status(zval
*object
, void *o
, zval
*return_value
)
111 php_pqconn_object_t
*obj
= o
;
113 RETVAL_LONG(PQstatus(obj
->intern
->conn
));
116 static void php_pqconn_object_read_transaction_status(zval
*object
, void *o
, zval
*return_value
)
118 php_pqconn_object_t
*obj
= o
;
120 RETVAL_LONG(PQtransactionStatus(obj
->intern
->conn
));
123 static void php_pqconn_object_read_error_message(zval
*object
, void *o
, zval
*return_value
)
125 php_pqconn_object_t
*obj
= o
;
126 char *error
= PHP_PQerrorMessage(obj
->intern
->conn
);
129 RETVAL_STRING(error
);
135 static int apply_notify_listener(zval
*p
, void *arg
)
137 php_pq_callback_t
*listener
= Z_PTR_P(p
);
139 zval zpid
, zchannel
, zmessage
;
141 ZVAL_LONG(&zpid
, nfy
->be_pid
);
142 ZVAL_STRING(&zchannel
, nfy
->relname
);
143 ZVAL_STRING(&zmessage
, nfy
->extra
);
145 zend_fcall_info_argn(&listener
->fci
, 3, &zchannel
, &zmessage
, &zpid
);
146 zend_fcall_info_call(&listener
->fci
, &listener
->fcc
, NULL
, NULL
);
147 zend_fcall_info_args_clear(&listener
->fci
, 0);
149 zval_ptr_dtor(&zchannel
);
150 zval_ptr_dtor(&zmessage
);
151 zval_ptr_dtor(&zpid
);
153 return ZEND_HASH_APPLY_KEEP
;
156 static int apply_notify_listeners(zval
*p
, int argc
, va_list argv
, zend_hash_key
*key
)
158 HashTable
*listeners
= Z_ARRVAL_P(p
);
159 PGnotify
*nfy
= va_arg(argv
, PGnotify
*);
161 if (0 == fnmatch(key
->key
->val
, nfy
->relname
, 0)) {
162 zend_hash_apply_with_argument(listeners
, apply_notify_listener
, nfy
);
165 return ZEND_HASH_APPLY_KEEP
;
168 void php_pqconn_notify_listeners(php_pqconn_object_t
*obj
)
172 while ((nfy
= PQnotifies(obj
->intern
->conn
))) {
173 zend_hash_apply_with_arguments(&obj
->intern
->listeners
, apply_notify_listeners
, 1, nfy
);
178 static void php_pqconn_object_read_busy(zval
*object
, void *o
, zval
*return_value
)
180 php_pqconn_object_t
*obj
= o
;
182 RETVAL_BOOL(PQisBusy(obj
->intern
->conn
));
185 static void php_pqconn_object_read_encoding(zval
*object
, void *o
, zval
*return_value
)
187 php_pqconn_object_t
*obj
= o
;
189 RETVAL_STRING(pg_encoding_to_char(PQclientEncoding(obj
->intern
->conn
)));
192 static void php_pqconn_object_write_encoding(zval
*object
, void *o
, zval
*value
)
194 php_pqconn_object_t
*obj
= o
;
195 zend_string
*zenc
= zval_get_string(value
);
197 if (0 > PQsetClientEncoding(obj
->intern
->conn
, zenc
->val
)) {
198 php_error(E_NOTICE
, "Unrecognized encoding '%s'", zenc
->val
);
201 zend_string_release(zenc
);
204 static void php_pqconn_object_read_unbuffered(zval
*object
, void *o
, zval
*return_value
)
206 php_pqconn_object_t
*obj
= o
;
208 RETVAL_BOOL(obj
->intern
->unbuffered
);
211 static void php_pqconn_object_write_unbuffered(zval
*object
, void *o
, zval
*value
)
213 php_pqconn_object_t
*obj
= o
;
215 obj
->intern
->unbuffered
= z_is_true(value
);
218 static void php_pqconn_object_read_db(zval
*object
, void *o
, zval
*return_value
)
220 php_pqconn_object_t
*obj
= o
;
221 char *db
= PQdb(obj
->intern
->conn
);
226 RETVAL_EMPTY_STRING();
230 static void php_pqconn_object_read_user(zval
*object
, void *o
, zval
*return_value
)
232 php_pqconn_object_t
*obj
= o
;
233 char *user
= PQuser(obj
->intern
->conn
);
238 RETVAL_EMPTY_STRING();
242 static void php_pqconn_object_read_pass(zval
*object
, void *o
, zval
*return_value
)
244 php_pqconn_object_t
*obj
= o
;
245 char *pass
= PQpass(obj
->intern
->conn
);
250 RETVAL_EMPTY_STRING();
254 static void php_pqconn_object_read_host(zval
*object
, void *o
, zval
*return_value
)
256 php_pqconn_object_t
*obj
= o
;
257 char *host
= PQhost(obj
->intern
->conn
);
262 RETVAL_EMPTY_STRING();
266 static void php_pqconn_object_read_port(zval
*object
, void *o
, zval
*return_value
)
268 php_pqconn_object_t
*obj
= o
;
269 char *port
= PQport(obj
->intern
->conn
);
274 RETVAL_EMPTY_STRING();
279 static void php_pqconn_object_read_params(zval
*object
, void *o
, zval
*return_value
)
281 php_pqconn_object_t
*obj
= o
;
282 PQconninfoOption
*ptr
, *params
= PQconninfo(obj
->intern
->conn
);
284 array_init(return_value
);
287 for (ptr
= params
; ptr
->keyword
; ++ptr
) {
289 add_assoc_string(return_value
, ptr
->keyword
, ptr
->val
);
291 add_assoc_null(return_value
, ptr
->keyword
);
294 PQconninfoFree(params
);
299 static void php_pqconn_object_read_options(zval
*object
, void *o
, zval
*return_value
)
301 php_pqconn_object_t
*obj
= o
;
302 char *options
= PQoptions(obj
->intern
->conn
);
305 RETVAL_STRING(options
);
307 RETVAL_EMPTY_STRING();
311 static int apply_read_callback_ex(zval
*p
, void *arg
)
316 zend_hash_next_index_insert(rv
, php_pq_callback_to_zval(Z_PTR_P(p
), &zcb
));
318 return ZEND_HASH_APPLY_KEEP
;
321 static int apply_read_callbacks(zval
*p
, int argc
, va_list argv
, zend_hash_key
*key
)
323 HashTable
*evhs
= Z_ARRVAL_P(p
), *rv
= va_arg(argv
, HashTable
*);
324 zval entry
, *entry_ptr
;
326 array_init_size(&entry
, zend_hash_num_elements(evhs
));
329 entry_ptr
= zend_hash_add(rv
, key
->key
, &entry
);
331 entry_ptr
= zend_hash_index_update(rv
, key
->h
, &entry
);
334 zend_hash_apply_with_argument(evhs
, apply_read_callback_ex
, Z_ARRVAL_P(entry_ptr
));
336 return ZEND_HASH_APPLY_KEEP
;
338 static void php_pqconn_object_read_event_handlers(zval
*object
, void *o
, zval
*return_value
)
340 php_pqconn_object_t
*obj
= o
;
342 array_init(return_value
);
343 zend_hash_apply_with_arguments(&obj
->intern
->eventhandlers
, apply_read_callbacks
, 1, Z_ARRVAL_P(return_value
));
346 static void php_pqconn_object_gc_event_handlers(zval
*object
, void *o
, zval
*return_value
)
348 php_pqconn_object_t
*obj
= o
;
351 ZEND_HASH_FOREACH_VAL(&obj
->intern
->eventhandlers
, evhs
)
355 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(evhs
), evh
)
359 add_next_index_zval(return_value
, php_pq_callback_to_zval_no_addref(Z_PTR_P(evh
), &zcb
));
361 ZEND_HASH_FOREACH_END();
363 ZEND_HASH_FOREACH_END();
366 static void php_pqconn_object_read_listeners(zval
*object
, void *o
, zval
*return_value
)
368 php_pqconn_object_t
*obj
= o
;
370 array_init(return_value
);
371 zend_hash_apply_with_arguments(&obj
->intern
->listeners
, apply_read_callbacks
, 1, Z_ARRVAL_P(return_value
));
374 static void php_pqconn_object_gc_listeners(zval
*object
, void *o
, zval
*return_value
)
376 php_pqconn_object_t
*obj
= o
;
379 ZEND_HASH_FOREACH_VAL(&obj
->intern
->listeners
, listeners
)
383 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(listeners
), listener
)
387 add_next_index_zval(return_value
, php_pq_callback_to_zval_no_addref(Z_PTR_P(listener
), &zcb
));
389 ZEND_HASH_FOREACH_END();
391 ZEND_HASH_FOREACH_END();
394 static void php_pqconn_object_read_converters(zval
*object
, void *o
, zval
*return_value
)
396 php_pqconn_object_t
*obj
= o
;
398 array_init(return_value
);
399 zend_hash_copy(Z_ARRVAL_P(return_value
), &obj
->intern
->converters
, zval_add_ref
);
402 static void php_pqconn_object_gc_converters(zval
*object
, void *o
, zval
*return_value
)
404 php_pqconn_object_t
*obj
= o
;
407 ZEND_HASH_FOREACH_VAL(&obj
->intern
->converters
, converter
)
409 add_next_index_zval(return_value
, converter
);
411 ZEND_HASH_FOREACH_END();
414 static void php_pqconn_object_read_def_fetch_type(zval
*object
, void *o
, zval
*return_value
)
416 php_pqconn_object_t
*obj
= o
;
418 RETVAL_LONG(obj
->intern
->default_fetch_type
);
420 static void php_pqconn_object_write_def_fetch_type(zval
*object
, void *o
, zval
*value
)
422 php_pqconn_object_t
*obj
= o
;
424 obj
->intern
->default_fetch_type
= zval_get_long(value
) & 0x3; /* two bits only */
427 static void php_pqconn_object_read_def_txn_isolation(zval
*object
, void *o
, zval
*return_value
)
429 php_pqconn_object_t
*obj
= o
;
431 RETVAL_LONG(obj
->intern
->default_txn_isolation
);
433 static void php_pqconn_object_write_def_txn_isolation(zval
*object
, void *o
, zval
*value
)
435 php_pqconn_object_t
*obj
= o
;
437 obj
->intern
->default_txn_isolation
= zval_get_long(value
) & 0x3; /* two bits only */
440 static void php_pqconn_object_read_def_txn_readonly(zval
*object
, void *o
, zval
*return_value
)
442 php_pqconn_object_t
*obj
= o
;
444 RETVAL_BOOL(obj
->intern
->default_txn_readonly
);
446 static void php_pqconn_object_write_def_txn_readonly(zval
*object
, void *o
, zval
*value
)
448 php_pqconn_object_t
*obj
= o
;
450 obj
->intern
->default_txn_readonly
= z_is_true(value
);
453 static void php_pqconn_object_read_def_txn_deferrable(zval
*object
, void *o
, zval
*return_value
)
455 php_pqconn_object_t
*obj
= o
;
457 RETVAL_BOOL(obj
->intern
->default_txn_deferrable
);
459 static void php_pqconn_object_write_def_txn_deferrable(zval
*object
, void *o
, zval
*value
)
461 php_pqconn_object_t
*obj
= o
;
463 obj
->intern
->default_txn_deferrable
= zend_is_true(value
);
466 static void php_pqconn_object_read_def_auto_conv(zval
*object
, void *o
, zval
*return_value
)
468 php_pqconn_object_t
*obj
= o
;
470 RETVAL_LONG(obj
->intern
->default_auto_convert
);
472 static void php_pqconn_object_write_def_auto_conv(zval
*object
, void *o
, zval
*value
)
474 php_pqconn_object_t
*obj
= o
;
476 obj
->intern
->default_auto_convert
= zval_get_long(value
) & PHP_PQRES_CONV_ALL
;
479 static ZEND_RESULT_CODE
php_pqconn_update_socket(zval
*zobj
, php_pqconn_object_t
*obj
)
481 zval zsocket
, zmember
;
483 ZEND_RESULT_CODE retval
;
487 obj
= PHP_PQ_OBJ(zobj
, NULL
);
490 ZVAL_STRINGL(&zmember
, "socket", sizeof("socket")-1);
492 if ((CONNECTION_BAD
!= PQstatus(obj
->intern
->conn
))
493 && (-1 < (socket
= PQsocket(obj
->intern
->conn
)))
494 && (stream
= php_stream_fopen_from_fd(socket
, "r+b", NULL
))) {
495 stream
->flags
|= PHP_STREAM_FLAG_NO_CLOSE
;
496 php_stream_to_zval(stream
, &zsocket
);
502 zend_get_std_object_handlers()->write_property(zobj
, &zmember
, &zsocket
, NULL
);
503 zval_ptr_dtor(&zsocket
);
504 zval_ptr_dtor(&zmember
);
509 static void *php_pqconn_resource_factory_ctor(void *data
, void *init_arg
)
511 php_pqconn_resource_factory_data_t
*o
= init_arg
;
512 PGconn
*conn
= NULL
;;
514 if (o
->flags
& PHP_PQCONN_ASYNC
) {
515 conn
= PQconnectStart(o
->dsn
);
517 conn
= PQconnectdb(o
->dsn
);
521 PQregisterEventProc(conn
, php_pqconn_event
, "ext-pq", NULL
);
527 static void php_pqconn_resource_factory_dtor(void *opaque
, void *handle
)
529 php_pqconn_event_data_t
*evdata
= PQinstanceData(handle
, php_pqconn_event
);
531 /* we don't care for anything, except free'ing evdata */
533 PQsetInstanceData(handle
, php_pqconn_event
, NULL
);
534 memset(evdata
, 0, sizeof(*evdata
));
541 static php_resource_factory_ops_t php_pqconn_resource_factory_ops
= {
542 php_pqconn_resource_factory_ctor
,
544 php_pqconn_resource_factory_dtor
547 php_resource_factory_ops_t
*php_pqconn_get_resource_factory_ops(void)
549 return &php_pqconn_resource_factory_ops
;
552 static void php_pqconn_wakeup(php_persistent_handle_factory_t
*f
, void **handle
)
554 PGresult
*res
= PQexec(*handle
, "");
555 php_pqres_clear(res
);
557 if (CONNECTION_OK
!= PQstatus(*handle
)) {
562 static inline PGresult
*unlisten(PGconn
*conn
, const char *channel_str
, size_t channel_len
)
564 char *quoted_channel
= PQescapeIdentifier(conn
, channel_str
, channel_len
);
565 PGresult
*res
= NULL
;
567 if (quoted_channel
) {
570 smart_str_appends(&cmd
, "UNLISTEN ");
571 smart_str_appends(&cmd
, quoted_channel
);
574 res
= PQexec(conn
, smart_str_v(&cmd
));
576 smart_str_free(&cmd
);
577 PQfreemem(quoted_channel
);
583 static int apply_unlisten(zval
*p
, int argc
, va_list argv
, zend_hash_key
*key
)
585 php_pqconn_object_t
*obj
= va_arg(argv
, php_pqconn_object_t
*);
586 PGresult
*res
= unlisten(obj
->intern
->conn
, key
->key
->val
, key
->key
->len
);
589 php_pqres_clear(res
);
592 return ZEND_HASH_APPLY_REMOVE
;
595 static void php_pqconn_retire(php_persistent_handle_factory_t
*f
, void **handle
)
597 php_pqconn_event_data_t
*evdata
= PQinstanceData(*handle
, php_pqconn_event
);
602 PQsetInstanceData(*handle
, php_pqconn_event
, NULL
);
605 PQsetNoticeReceiver(*handle
, php_pqconn_notice_ignore
, NULL
);
607 /* cancel async queries */
608 if (PQisBusy(*handle
) && (cancel
= PQgetCancel(*handle
))) {
611 PQcancel(cancel
, err
, sizeof(err
));
612 PQfreeCancel(cancel
);
614 /* clean up async results */
615 while ((res
= PQgetResult(*handle
))) {
616 php_pqres_clear(res
);
619 /* clean up transaction & session */
620 switch (PQtransactionStatus(*handle
)) {
622 res
= PQexec(*handle
, "RESET ALL");
625 res
= PQexec(*handle
, "ROLLBACK; RESET ALL");
630 php_pqres_clear(res
);
634 /* clean up notify listeners */
635 zend_hash_apply_with_arguments(&evdata
->obj
->intern
->listeners
, apply_unlisten
, 1, evdata
->obj
);
637 /* release instance data */
642 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct
, 0, 0, 1)
643 ZEND_ARG_INFO(0, dsn
)
644 ZEND_ARG_INFO(0, flags
)
646 static PHP_METHOD(pqconn
, __construct
) {
647 zend_error_handling zeh
;
653 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
654 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "|sl", &dsn_str
, &dsn_len
, &flags
);
655 zend_restore_error_handling(&zeh
);
658 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
661 throw_exce(EX_BAD_METHODCALL
, "pq\\Connection already initialized");
663 php_pqconn_event_data_t
*evdata
= php_pqconn_event_data_init(obj
);
664 php_pqconn_resource_factory_data_t rfdata
= {dsn_str
, flags
};
666 obj
->intern
= ecalloc(1, sizeof(*obj
->intern
));
668 obj
->intern
->default_auto_convert
= PHP_PQRES_CONV_ALL
;
670 zend_hash_init(&obj
->intern
->listeners
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
671 zend_hash_init(&obj
->intern
->converters
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
672 zend_hash_init(&obj
->intern
->eventhandlers
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
674 if (flags
& PHP_PQCONN_PERSISTENT
) {
675 zend_string
*dsn
= zend_string_init(dsn_str
, dsn_len
, 0);
676 php_persistent_handle_factory_t
*phf
= php_persistent_handle_concede(NULL
, PHP_PQ_G
->connection
.name
, dsn
, php_pqconn_wakeup
, php_pqconn_retire
);
677 php_persistent_handle_resource_factory_init(&obj
->intern
->factory
, phf
);
678 zend_string_release(dsn
);
680 php_resource_factory_init(&obj
->intern
->factory
, &php_pqconn_resource_factory_ops
, NULL
, NULL
);
683 if (flags
& PHP_PQCONN_ASYNC
) {
684 obj
->intern
->poller
= (int (*)(PGconn
*)) PQconnectPoll
;
687 obj
->intern
->conn
= php_resource_factory_handle_ctor(&obj
->intern
->factory
, &rfdata
);
689 PQsetInstanceData(obj
->intern
->conn
, php_pqconn_event
, evdata
);
690 PQsetNoticeReceiver(obj
->intern
->conn
, php_pqconn_notice_recv
, evdata
);
692 if (SUCCESS
!= php_pqconn_update_socket(getThis(), obj
)) {
693 throw_exce(EX_CONNECTION_FAILED
, "Connection failed (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
699 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset
, 0, 0, 0)
701 static PHP_METHOD(pqconn
, reset
) {
702 zend_error_handling zeh
;
705 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
706 rv
= zend_parse_parameters_none();
707 zend_restore_error_handling(&zeh
);
710 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
713 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
715 PQreset(obj
->intern
->conn
);
717 if (CONNECTION_OK
!= PQstatus(obj
->intern
->conn
)) {
718 throw_exce(EX_CONNECTION_FAILED
, "Connection reset failed: (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
721 php_pqconn_notify_listeners(obj
);
726 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset_async
, 0, 0, 0)
728 static PHP_METHOD(pqconn
, resetAsync
) {
729 zend_error_handling zeh
;
732 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
733 rv
= zend_parse_parameters_none();
734 zend_restore_error_handling(&zeh
);
737 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
740 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
742 if (!PQresetStart(obj
->intern
->conn
)) {
743 throw_exce(EX_IO
, "Failed to start connection reset (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
745 obj
->intern
->poller
= (int (*)(PGconn
*)) PQresetPoll
;
748 php_pqconn_notify_listeners(obj
);
753 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten
, 0, 0, 1)
754 ZEND_ARG_INFO(0, channel
)
756 static PHP_METHOD(pqconn
, unlisten
)
758 zend_error_handling zeh
;
763 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
764 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s", &channel_str
, &channel_len
);
765 zend_restore_error_handling(&zeh
);
768 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
771 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
772 } else if (SUCCESS
== zend_hash_str_del(&obj
->intern
->listeners
, channel_str
, channel_len
)) {
773 PGresult
*res
= unlisten(obj
->intern
->conn
, channel_str
, channel_len
);
776 php_pqres_success(res
);
777 php_pqres_clear(res
);
783 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten_async
, 0, 0, 1)
784 ZEND_ARG_INFO(0, channel
)
786 static PHP_METHOD(pqconn
, unlistenAsync
) {
787 zend_error_handling zeh
;
792 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
793 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s", &channel_str
, &channel_len
);
794 zend_restore_error_handling(&zeh
);
797 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
800 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
802 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
804 if (!quoted_channel
) {
805 throw_exce(EX_ESCAPE
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
809 smart_str_appends(&cmd
, "UNLISTEN ");
810 smart_str_appends(&cmd
, quoted_channel
);
813 if (!PQsendQuery(obj
->intern
->conn
, smart_str_v(&cmd
))) {
814 throw_exce(EX_IO
, "Failed to uninstall listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
816 obj
->intern
->poller
= PQconsumeInput
;
817 zend_hash_str_del(&obj
->intern
->listeners
, channel_str
, channel_len
);
820 smart_str_free(&cmd
);
821 PQfreemem(quoted_channel
);
822 php_pqconn_notify_listeners(obj
);
828 static void php_pqconn_add_listener(php_pqconn_object_t
*obj
, const char *channel_str
, size_t channel_len
, php_pq_callback_t
*listener
)
832 php_pq_callback_addref(listener
);
834 if ((existing
= zend_hash_str_find(&obj
->intern
->listeners
, channel_str
, channel_len
))) {
835 zend_hash_next_index_insert_mem(Z_ARRVAL_P(existing
), (void *) listener
, sizeof(*listener
));
841 zend_hash_init(ht
, 0, NULL
, php_pq_callback_hash_dtor
, 0);
842 zend_hash_next_index_insert_mem(ht
, (void *) listener
, sizeof(*listener
));
845 zend_hash_str_add(&obj
->intern
->listeners
, channel_str
, channel_len
, &tmp
);
849 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen
, 0, 0, 2)
850 ZEND_ARG_INFO(0, channel
)
851 ZEND_ARG_INFO(0, callable
)
853 static PHP_METHOD(pqconn
, listen
) {
854 zend_error_handling zeh
;
855 char *channel_str
= NULL
;
856 size_t channel_len
= 0;
857 php_pq_callback_t listener
= {{0}};
860 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
861 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
862 zend_restore_error_handling(&zeh
);
865 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
868 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
870 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
872 if (!quoted_channel
) {
873 throw_exce(EX_ESCAPE
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
878 smart_str_appends(&cmd
, "LISTEN ");
879 smart_str_appends(&cmd
, quoted_channel
);
882 res
= php_pq_exec(obj
->intern
->conn
, smart_str_v(&cmd
));
884 smart_str_free(&cmd
);
885 PQfreemem(quoted_channel
);
888 throw_exce(EX_RUNTIME
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
890 if (SUCCESS
== php_pqres_success(res
)) {
891 obj
->intern
->poller
= PQconsumeInput
;
892 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener
);
894 php_pqres_clear(res
);
897 php_pqconn_notify_listeners(obj
);
903 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen_async
, 0, 0, 0)
904 ZEND_ARG_INFO(0, channel
)
905 ZEND_ARG_INFO(0, callable
)
907 static PHP_METHOD(pqconn
, listenAsync
) {
908 zend_error_handling zeh
;
909 char *channel_str
= NULL
;
910 size_t channel_len
= 0;
911 php_pq_callback_t listener
= {{0}};
914 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
915 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
916 zend_restore_error_handling(&zeh
);
919 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
922 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
924 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
926 if (!quoted_channel
) {
927 throw_exce(EX_ESCAPE
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
931 smart_str_appends(&cmd
, "LISTEN ");
932 smart_str_appends(&cmd
, quoted_channel
);
935 if (!PQsendQuery(obj
->intern
->conn
, smart_str_v(&cmd
))) {
936 throw_exce(EX_IO
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
938 obj
->intern
->poller
= PQconsumeInput
;
939 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener
);
942 smart_str_free(&cmd
);
943 PQfreemem(quoted_channel
);
944 php_pqconn_notify_listeners(obj
);
950 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify
, 0, 0, 2)
951 ZEND_ARG_INFO(0, channel
)
952 ZEND_ARG_INFO(0, message
)
954 static PHP_METHOD(pqconn
, notify
) {
955 zend_error_handling zeh
;
956 char *channel_str
, *message_str
;
957 size_t channel_len
, message_len
;
960 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
961 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
962 zend_restore_error_handling(&zeh
);
965 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
968 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
971 char *params
[2] = {channel_str
, message_str
};
973 res
= PQexecParams(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0);
976 throw_exce(EX_RUNTIME
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
978 php_pqres_success(res
);
979 php_pqres_clear(res
);
982 php_pqconn_notify_listeners(obj
);
987 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify_async
, 0, 0, 2)
988 ZEND_ARG_INFO(0, channel
)
989 ZEND_ARG_INFO(0, message
)
991 static PHP_METHOD(pqconn
, notifyAsync
) {
992 zend_error_handling zeh
;
993 char *channel_str
, *message_str
;
994 size_t channel_len
, message_len
;
997 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
998 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
999 zend_restore_error_handling(&zeh
);
1001 if (SUCCESS
== rv
) {
1002 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1005 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1007 char *params
[2] = {channel_str
, message_str
};
1009 if (!PQsendQueryParams(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0)) {
1010 throw_exce(EX_IO
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1012 obj
->intern
->poller
= PQconsumeInput
;
1015 php_pqconn_notify_listeners(obj
);
1020 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_poll
, 0, 0, 0)
1021 ZEND_END_ARG_INFO();
1022 static PHP_METHOD(pqconn
, poll
) {
1023 zend_error_handling zeh
;
1024 ZEND_RESULT_CODE rv
;
1026 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1027 rv
= zend_parse_parameters_none();
1028 zend_restore_error_handling(&zeh
);
1030 if (SUCCESS
== rv
) {
1031 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1034 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1035 } else if (!obj
->intern
->poller
) {
1036 throw_exce(EX_RUNTIME
, "No asynchronous operation active");
1038 if (obj
->intern
->poller
== PQconsumeInput
) {
1039 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
) * PGRES_POLLING_OK
);
1041 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
));
1043 php_pqconn_notify_listeners(obj
);
1048 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec
, 0, 0, 1)
1049 ZEND_ARG_INFO(0, query
)
1050 ZEND_END_ARG_INFO();
1051 static PHP_METHOD(pqconn
, exec
) {
1052 zend_error_handling zeh
;
1055 ZEND_RESULT_CODE rv
;
1057 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1058 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s", &query_str
, &query_len
);
1059 zend_restore_error_handling(&zeh
);
1061 if (SUCCESS
== rv
) {
1062 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1065 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1067 PGresult
*res
= php_pq_exec(obj
->intern
->conn
, query_str
);
1070 throw_exce(EX_RUNTIME
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1071 } else if (SUCCESS
== php_pqres_success(res
)) {
1072 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), return_value
);
1074 php_pqres_clear(res
);
1077 php_pqconn_notify_listeners(obj
);
1082 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_get_result
, 0, 0, 0)
1083 ZEND_END_ARG_INFO();
1084 static PHP_METHOD(pqconn
, getResult
) {
1085 zend_error_handling zeh
;
1086 ZEND_RESULT_CODE rv
;
1088 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1089 rv
= zend_parse_parameters_none();
1090 zend_restore_error_handling(&zeh
);
1092 if (SUCCESS
== rv
) {
1093 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1096 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1098 PGresult
*res
= PQgetResult(obj
->intern
->conn
);
1103 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), return_value
);
1106 php_pqconn_notify_listeners(obj
);
1111 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_async
, 0, 0, 1)
1112 ZEND_ARG_INFO(0, query
)
1113 ZEND_ARG_INFO(0, callable
)
1114 ZEND_END_ARG_INFO();
1115 static PHP_METHOD(pqconn
, execAsync
) {
1116 zend_error_handling zeh
;
1117 php_pq_callback_t resolver
= {{0}};
1120 ZEND_RESULT_CODE rv
;
1122 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1123 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "s|f", &query_str
, &query_len
, &resolver
.fci
, &resolver
.fcc
);
1124 zend_restore_error_handling(&zeh
);
1126 if (SUCCESS
== rv
) {
1127 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1130 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1131 } else if (!PQsendQuery(obj
->intern
->conn
, query_str
)) {
1132 throw_exce(EX_IO
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1133 #if HAVE_PQSETSINGLEROWMODE
1134 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1135 throw_exce(EX_RUNTIME
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1138 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver
);
1139 obj
->intern
->poller
= PQconsumeInput
;
1140 php_pqconn_notify_listeners(obj
);
1145 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params
, 0, 0, 2)
1146 ZEND_ARG_INFO(0, query
)
1147 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1148 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1149 ZEND_END_ARG_INFO();
1150 static PHP_METHOD(pqconn
, execParams
) {
1151 zend_error_handling zeh
;
1155 zval
*ztypes
= NULL
;
1156 ZEND_RESULT_CODE rv
;
1158 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1159 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sa/|a/!", &query_str
, &query_len
, &zparams
, &ztypes
);
1160 zend_restore_error_handling(&zeh
);
1162 if (SUCCESS
== rv
) {
1163 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1166 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1169 php_pq_params_t
*params
;
1171 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
));
1172 res
= PQexecParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1173 php_pq_params_free(¶ms
);
1176 throw_exce(EX_RUNTIME
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1178 if (SUCCESS
== php_pqres_success(res
)) {
1179 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), return_value
);
1181 php_pqres_clear(res
);
1184 php_pqconn_notify_listeners(obj
);
1190 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async
, 0, 0, 2)
1191 ZEND_ARG_INFO(0, query
)
1192 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1193 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1194 ZEND_ARG_INFO(0, callable
)
1195 ZEND_END_ARG_INFO();
1196 static PHP_METHOD(pqconn
, execParamsAsync
) {
1197 zend_error_handling zeh
;
1198 php_pq_callback_t resolver
= {{0}};
1202 zval
*ztypes
= NULL
;
1203 ZEND_RESULT_CODE rv
;
1205 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1206 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sa/|a/!f", &query_str
, &query_len
, &zparams
, &ztypes
, &resolver
.fci
, &resolver
.fcc
);
1207 zend_restore_error_handling(&zeh
);
1209 if (SUCCESS
== rv
) {
1210 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1213 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1216 php_pq_params_t
*params
;
1218 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
));
1219 rc
= PQsendQueryParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1220 php_pq_params_free(¶ms
);
1223 throw_exce(EX_IO
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1224 #if HAVE_PQSETSINGLEROWMODE
1225 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1226 throw_exce(EX_RUNTIME
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1229 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver
);
1230 obj
->intern
->poller
= PQconsumeInput
;
1231 php_pqconn_notify_listeners(obj
);
1235 zend_restore_error_handling(&zeh
);
1238 ZEND_RESULT_CODE
php_pqconn_prepare(zval
*object
, php_pqconn_object_t
*obj
, const char *name
, const char *query
, php_pq_params_t
*params
)
1241 ZEND_RESULT_CODE rv
;
1244 obj
= PHP_PQ_OBJ(object
, NULL
);
1247 res
= php_pq_prepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
);
1251 throw_exce(EX_RUNTIME
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1253 rv
= php_pqres_success(res
);
1254 php_pqres_clear(res
);
1255 php_pqconn_notify_listeners(obj
);
1261 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare
, 0, 0, 2)
1262 ZEND_ARG_INFO(0, name
)
1263 ZEND_ARG_INFO(0, query
)
1264 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1265 ZEND_END_ARG_INFO();
1266 static PHP_METHOD(pqconn
, prepare
) {
1267 zend_error_handling zeh
;
1268 zval
*ztypes
= NULL
;
1269 char *name_str
, *query_str
;
1270 size_t name_len
, *query_len
;
1271 ZEND_RESULT_CODE rv
;
1273 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1274 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1275 zend_restore_error_handling(&zeh
);
1277 if (SUCCESS
== rv
) {
1278 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1281 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1283 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL
);
1285 if (SUCCESS
!= php_pqconn_prepare(getThis(), obj
, name_str
, query_str
, params
)) {
1286 php_pq_params_free(¶ms
);
1288 php_pqstm_t
*stm
= php_pqstm_init(obj
, name_str
, query_str
, params
);
1290 RETVAL_OBJ(&php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
)->zo
);
1296 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
)
1298 ZEND_RESULT_CODE rv
;
1301 obj
= PHP_PQ_OBJ(object
, NULL
);
1304 if (!PQsendPrepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
)) {
1306 throw_exce(EX_IO
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1309 obj
->intern
->poller
= PQconsumeInput
;
1310 php_pqconn_notify_listeners(obj
);
1316 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare_async
, 0, 0, 2)
1317 ZEND_ARG_INFO(0, name
)
1318 ZEND_ARG_INFO(0, query
)
1319 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1320 ZEND_END_ARG_INFO();
1321 static PHP_METHOD(pqconn
, prepareAsync
) {
1322 zend_error_handling zeh
;
1323 zval
*ztypes
= NULL
;
1324 char *name_str
, *query_str
;
1325 size_t name_len
, *query_len
;
1326 ZEND_RESULT_CODE rv
;
1328 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1329 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1330 zend_restore_error_handling(&zeh
);
1332 if (SUCCESS
== rv
) {
1333 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1336 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1338 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL
);
1340 if (SUCCESS
!= php_pqconn_prepare_async(getThis(), obj
, name_str
, query_str
, params
)) {
1341 php_pq_params_free(¶ms
);
1343 php_pqstm_t
*stm
= php_pqstm_init(obj
, name_str
, query_str
, params
);
1345 RETVAL_OBJ(&php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
)->zo
);
1351 ZEND_RESULT_CODE
php_pqconn_declare(zval
*object
, php_pqconn_object_t
*obj
, const char *decl
)
1354 ZEND_RESULT_CODE rv
;
1357 obj
= PHP_PQ_OBJ(object
, NULL
);
1360 res
= php_pq_exec(obj
->intern
->conn
, decl
);
1364 throw_exce(EX_RUNTIME
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1366 rv
= php_pqres_success(res
);
1367 php_pqres_clear(res
);
1368 php_pqconn_notify_listeners(obj
);
1374 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare
, 0, 0, 3)
1375 ZEND_ARG_INFO(0, name
)
1376 ZEND_ARG_INFO(0, flags
)
1377 ZEND_ARG_INFO(0, query
)
1378 ZEND_END_ARG_INFO();
1379 static PHP_METHOD(pqconn
, declare
) {
1380 zend_error_handling zeh
;
1381 char *name_str
, *query_str
;
1382 size_t name_len
, query_len
;
1384 ZEND_RESULT_CODE rv
;
1386 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1387 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1388 zend_restore_error_handling(&zeh
);
1390 if (SUCCESS
== rv
) {
1391 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1394 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1397 char *decl
= php_pqcur_declare_str(name_str
, name_len
, flags
, query_str
, query_len
, &query_offset
);
1399 if (SUCCESS
!= php_pqconn_declare(getThis(), obj
, decl
)) {
1402 php_pqcur_t
*cur
= php_pqcur_init(obj
, name_str
, decl
, query_offset
, flags
);
1404 RETVAL_OBJ(&php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
)->zo
);
1410 ZEND_RESULT_CODE
php_pqconn_declare_async(zval
*object
, php_pqconn_object_t
*obj
, const char *decl
)
1412 ZEND_RESULT_CODE rv
;
1415 obj
= PHP_PQ_OBJ(object
, NULL
);
1418 if (!PQsendQuery(obj
->intern
->conn
, decl
)) {
1420 throw_exce(EX_IO
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1423 obj
->intern
->poller
= PQconsumeInput
;
1424 php_pqconn_notify_listeners(obj
);
1430 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare_async
, 0, 0, 2)
1431 ZEND_ARG_INFO(0, name
)
1432 ZEND_ARG_INFO(0, flags
)
1433 ZEND_ARG_INFO(0, query
)
1434 ZEND_END_ARG_INFO();
1435 static PHP_METHOD(pqconn
, declareAsync
) {
1436 zend_error_handling zeh
;
1437 char *name_str
, *query_str
;
1438 size_t name_len
, query_len
;
1440 ZEND_RESULT_CODE rv
;
1442 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1443 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1444 zend_restore_error_handling(&zeh
);
1446 if (SUCCESS
== rv
) {
1447 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1450 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1453 char *decl
= php_pqcur_declare_str(name_str
, name_len
, flags
, query_str
, query_len
, &query_offset
);
1455 if (SUCCESS
!= php_pqconn_declare_async(getThis(), obj
, decl
)) {
1458 php_pqcur_t
*cur
= php_pqcur_init(obj
, name_str
, decl
, query_offset
, flags
);
1460 RETVAL_OBJ(&php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
)->zo
);
1466 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote
, 0, 0, 1)
1467 ZEND_ARG_INFO(0, string
)
1468 ZEND_END_ARG_INFO();
1469 static PHP_METHOD(pqconn
, quote
) {
1473 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1474 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1477 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1479 char *quoted
= PQescapeLiteral(obj
->intern
->conn
, str
, len
);
1482 php_error_docref(NULL
, E_WARNING
, "Failed to quote string (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1485 RETVAL_STRING(quoted
);
1492 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote_name
, 0, 0, 1)
1493 ZEND_ARG_INFO(0, type
)
1494 ZEND_END_ARG_INFO();
1495 static PHP_METHOD(pqconn
, quoteName
) {
1499 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1500 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1503 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1505 char *quoted
= PQescapeIdentifier(obj
->intern
->conn
, str
, len
);
1508 php_error_docref(NULL
, E_WARNING
, "Failed to quote name (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1511 RETVAL_STRING(quoted
);
1518 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_escape_bytea
, 0, 0, 1)
1519 ZEND_ARG_INFO(0, bytea
)
1520 ZEND_END_ARG_INFO();
1521 static PHP_METHOD(pqconn
, escapeBytea
) {
1525 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1526 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1529 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1532 char *escaped_str
= (char *) PQescapeByteaConn(obj
->intern
->conn
, (unsigned char *) str
, len
, &escaped_len
);
1535 php_error_docref(NULL
, E_WARNING
, "Failed to escape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1538 RETVAL_STRINGL(escaped_str
, escaped_len
- 1);
1539 PQfreemem(escaped_str
);
1545 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unescape_bytea
, 0, 0, 1)
1546 ZEND_ARG_INFO(0, bytea
)
1547 ZEND_END_ARG_INFO();
1548 static PHP_METHOD(pqconn
, unescapeBytea
) {
1552 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1553 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1556 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1558 size_t unescaped_len
;
1559 char *unescaped_str
= (char *) PQunescapeBytea((unsigned char *)str
, &unescaped_len
);
1561 if (!unescaped_str
) {
1562 php_error_docref(NULL
, E_WARNING
, "Failed to unescape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1565 RETVAL_STRINGL(unescaped_str
, unescaped_len
);
1566 PQfreemem(unescaped_str
);
1572 ZEND_RESULT_CODE
php_pqconn_start_transaction(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable
)
1574 ZEND_RESULT_CODE rv
= FAILURE
;
1577 conn_obj
= PHP_PQ_OBJ(zconn
, NULL
);
1580 if (!conn_obj
->intern
) {
1581 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1584 smart_str cmd
= {0};
1585 const char *il
= php_pq_isolation_level(&isolation
);
1587 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1588 smart_str_appends(&cmd
, il
);
1589 smart_str_appends(&cmd
, ", READ ");
1590 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1591 smart_str_appends(&cmd
, ",");
1592 smart_str_appends(&cmd
, deferrable
? "" : " NOT");
1593 smart_str_appends(&cmd
, " DEFERRABLE");
1596 res
= php_pq_exec(conn_obj
->intern
->conn
, smart_str_v(&cmd
));
1599 throw_exce(EX_RUNTIME
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1601 rv
= php_pqres_success(res
);
1602 php_pqres_clear(res
);
1603 php_pqconn_notify_listeners(conn_obj
);
1606 smart_str_free(&cmd
);
1612 ZEND_RESULT_CODE
php_pqconn_start_transaction_async(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable
)
1614 ZEND_RESULT_CODE rv
= FAILURE
;
1617 conn_obj
= PHP_PQ_OBJ(zconn
, NULL
);
1620 if (!conn_obj
->intern
) {
1621 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1623 smart_str cmd
= {0};
1624 const char *il
= php_pq_isolation_level(&isolation
);
1626 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1627 smart_str_appends(&cmd
, il
);
1628 smart_str_appends(&cmd
, ", READ ");
1629 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1630 smart_str_appends(&cmd
, ",");
1631 smart_str_appends(&cmd
, deferrable
? "" : "NOT ");
1632 smart_str_appends(&cmd
, " DEFERRABLE");
1635 if (!PQsendQuery(conn_obj
->intern
->conn
, smart_str_v(&cmd
))) {
1636 throw_exce(EX_IO
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1639 conn_obj
->intern
->poller
= PQconsumeInput
;
1640 php_pqconn_notify_listeners(conn_obj
);
1643 smart_str_free(&cmd
);
1649 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction
, 0, 0, 0)
1650 ZEND_ARG_INFO(0, isolation
)
1651 ZEND_ARG_INFO(0, readonly
)
1652 ZEND_ARG_INFO(0, deferrable
)
1653 ZEND_END_ARG_INFO();
1654 static PHP_METHOD(pqconn
, startTransaction
) {
1655 zend_error_handling zeh
;
1656 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1657 zend_long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1658 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1659 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1660 ZEND_RESULT_CODE rv
;
1662 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1663 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "|lbb", &isolation
, &readonly
, &deferrable
);
1664 zend_restore_error_handling(&zeh
);
1666 if (SUCCESS
== rv
) {
1667 rv
= php_pqconn_start_transaction(getThis(), obj
, isolation
, readonly
, deferrable
);
1669 if (SUCCESS
== rv
) {
1670 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1672 php_pq_object_addref(obj
);
1675 txn
->isolation
= isolation
;
1676 txn
->readonly
= readonly
;
1677 txn
->deferrable
= deferrable
;
1679 RETVAL_OBJ(&php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
)->zo
);
1684 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction_async
, 0, 0, 0)
1685 ZEND_ARG_INFO(0, isolation
)
1686 ZEND_ARG_INFO(0, readonly
)
1687 ZEND_ARG_INFO(0, deferrable
)
1688 ZEND_END_ARG_INFO();
1689 static PHP_METHOD(pqconn
, startTransactionAsync
) {
1690 zend_error_handling zeh
;
1691 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1692 zend_long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1693 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1694 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1695 ZEND_RESULT_CODE rv
;
1697 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1698 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "|lbb", &isolation
, &readonly
, &deferrable
);
1699 zend_restore_error_handling(&zeh
);
1701 if (SUCCESS
== rv
) {
1702 rv
= php_pqconn_start_transaction_async(getThis(), obj
, isolation
, readonly
, deferrable
);
1704 if (SUCCESS
== rv
) {
1705 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1707 php_pq_object_addref(obj
);
1710 txn
->isolation
= isolation
;
1711 txn
->readonly
= readonly
;
1712 txn
->deferrable
= deferrable
;
1714 RETVAL_OBJ(&php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
)->zo
);
1719 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_trace
, 0, 0, 0)
1720 ZEND_ARG_INFO(0, stdio_stream
)
1721 ZEND_END_ARG_INFO();
1722 static PHP_METHOD(pqconn
, trace
) {
1723 zval
*zstream
= NULL
;
1725 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &zstream
)) {
1726 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1729 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1732 PQuntrace(obj
->intern
->conn
);
1736 php_stream
*stream
= NULL
;
1738 php_stream_from_zval(stream
, zstream
);
1740 if (SUCCESS
!= php_stream_cast(stream
, PHP_STREAM_AS_STDIO
, (void *) &fp
, REPORT_ERRORS
)) {
1743 stream
->flags
|= PHP_STREAM_FLAG_NO_CLOSE
;
1744 PQtrace(obj
->intern
->conn
, fp
);
1752 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_off
, 0, 0, 1)
1753 ZEND_ARG_INFO(0, type
)
1754 ZEND_END_ARG_INFO();
1755 static PHP_METHOD(pqconn
, off
) {
1756 zend_error_handling zeh
;
1758 ZEND_RESULT_CODE rv
;
1760 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1761 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "S", &type
);
1762 zend_restore_error_handling(&zeh
);
1764 if (SUCCESS
== rv
) {
1765 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1768 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1770 RETURN_BOOL(SUCCESS
== zend_hash_del(&obj
->intern
->eventhandlers
, type
));
1775 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_on
, 0, 0, 2)
1776 ZEND_ARG_INFO(0, type
)
1777 ZEND_ARG_INFO(0, callable
)
1778 ZEND_END_ARG_INFO();
1779 static PHP_METHOD(pqconn
, on
) {
1780 zend_error_handling zeh
;
1783 php_pq_callback_t cb
= {{0}};
1784 ZEND_RESULT_CODE rv
;
1786 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1787 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "sf", &type_str
, &type_len
, &cb
.fci
, &cb
.fcc
);
1788 zend_restore_error_handling(&zeh
);
1790 if (SUCCESS
== rv
) {
1791 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1794 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1796 RETVAL_LONG(php_pqconn_add_eventhandler(obj
, type_str
, type_len
, &cb
));
1801 struct apply_set_converter_arg
{
1807 static int apply_set_converter(zval
*zoid
, void *a
)
1809 zend_long oid
= zval_get_long(zoid
);
1810 struct apply_set_converter_arg
*arg
= a
;
1813 Z_ADDREF_P(arg
->zconv
);
1814 zend_hash_index_update(arg
->ht
, oid
, arg
->zconv
);
1816 zend_hash_index_del(arg
->ht
, oid
);
1819 return ZEND_HASH_APPLY_KEEP
;
1822 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_set_converter
, 0, 0, 1)
1823 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\Converter
, 0)
1824 ZEND_END_ARG_INFO();
1825 static PHP_METHOD(pqconn
, setConverter
) {
1826 ZEND_RESULT_CODE rv
;
1827 zend_error_handling zeh
;
1830 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1831 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zcnv
, php_pqconv_class_entry
);
1832 zend_restore_error_handling(&zeh
);
1834 if (SUCCESS
== rv
) {
1835 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1838 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1841 struct apply_set_converter_arg arg
= {NULL
};
1844 zend_call_method_with_0_params(zcnv
, NULL
, NULL
, "converttypes", &zoids
);
1845 ZVAL_DUP(&tmp
, &zoids
);
1846 convert_to_array(&tmp
);
1848 arg
.ht
= &obj
->intern
->converters
;
1852 zend_hash_apply_with_argument(Z_ARRVAL(tmp
), apply_set_converter
, &arg
);
1854 zval_ptr_dtor(&tmp
);
1855 zval_ptr_dtor(&zoids
);
1860 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unset_converter
, 0, 0, 1)
1861 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\Converter
, 0)
1862 ZEND_END_ARG_INFO();
1863 static PHP_METHOD(pqconn
, unsetConverter
) {
1864 ZEND_RESULT_CODE rv
;
1865 zend_error_handling zeh
;
1868 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
1869 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zcnv
, php_pqconv_class_entry
);
1870 zend_restore_error_handling(&zeh
);
1872 if (SUCCESS
== rv
) {
1873 php_pqconn_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
1876 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
1879 struct apply_set_converter_arg arg
= {NULL
};
1882 zend_call_method_with_0_params(zcnv
, NULL
, NULL
, "converttypes", &zoids
);
1883 ZVAL_DUP(&tmp
, &zoids
);
1884 convert_to_array(&tmp
);
1886 arg
.ht
= &obj
->intern
->converters
;
1890 zend_hash_apply_with_argument(Z_ARRVAL(tmp
), apply_set_converter
, &arg
);
1892 zval_ptr_dtor(&tmp
);
1893 zval_ptr_dtor(&zoids
);
1898 static zend_function_entry php_pqconn_methods
[] = {
1899 PHP_ME(pqconn
, __construct
, ai_pqconn_construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
1900 PHP_ME(pqconn
, reset
, ai_pqconn_reset
, ZEND_ACC_PUBLIC
)
1901 PHP_ME(pqconn
, resetAsync
, ai_pqconn_reset_async
, ZEND_ACC_PUBLIC
)
1902 PHP_ME(pqconn
, poll
, ai_pqconn_poll
, ZEND_ACC_PUBLIC
)
1903 PHP_ME(pqconn
, exec
, ai_pqconn_exec
, ZEND_ACC_PUBLIC
)
1904 PHP_ME(pqconn
, execAsync
, ai_pqconn_exec_async
, ZEND_ACC_PUBLIC
)
1905 PHP_ME(pqconn
, execParams
, ai_pqconn_exec_params
, ZEND_ACC_PUBLIC
)
1906 PHP_ME(pqconn
, execParamsAsync
, ai_pqconn_exec_params_async
, ZEND_ACC_PUBLIC
)
1907 PHP_ME(pqconn
, prepare
, ai_pqconn_prepare
, ZEND_ACC_PUBLIC
)
1908 PHP_ME(pqconn
, prepareAsync
, ai_pqconn_prepare_async
, ZEND_ACC_PUBLIC
)
1909 PHP_ME(pqconn
, declare
, ai_pqconn_declare
, ZEND_ACC_PUBLIC
)
1910 PHP_ME(pqconn
, declareAsync
, ai_pqconn_declare_async
, ZEND_ACC_PUBLIC
)
1911 PHP_ME(pqconn
, unlisten
, ai_pqconn_unlisten
, ZEND_ACC_PUBLIC
)
1912 PHP_ME(pqconn
, unlistenAsync
, ai_pqconn_unlisten_async
, ZEND_ACC_PUBLIC
)
1913 PHP_ME(pqconn
, listen
, ai_pqconn_listen
, ZEND_ACC_PUBLIC
)
1914 PHP_ME(pqconn
, listenAsync
, ai_pqconn_listen_async
, ZEND_ACC_PUBLIC
)
1915 PHP_ME(pqconn
, notify
, ai_pqconn_notify
, ZEND_ACC_PUBLIC
)
1916 PHP_ME(pqconn
, notifyAsync
, ai_pqconn_notify_async
, ZEND_ACC_PUBLIC
)
1917 PHP_ME(pqconn
, getResult
, ai_pqconn_get_result
, ZEND_ACC_PUBLIC
)
1918 PHP_ME(pqconn
, quote
, ai_pqconn_quote
, ZEND_ACC_PUBLIC
)
1919 PHP_ME(pqconn
, quoteName
, ai_pqconn_quote_name
, ZEND_ACC_PUBLIC
)
1920 PHP_ME(pqconn
, escapeBytea
, ai_pqconn_escape_bytea
, ZEND_ACC_PUBLIC
)
1921 PHP_ME(pqconn
, unescapeBytea
, ai_pqconn_unescape_bytea
, ZEND_ACC_PUBLIC
)
1922 PHP_ME(pqconn
, startTransaction
, ai_pqconn_start_transaction
, ZEND_ACC_PUBLIC
)
1923 PHP_ME(pqconn
, startTransactionAsync
, ai_pqconn_start_transaction_async
, ZEND_ACC_PUBLIC
)
1924 PHP_ME(pqconn
, trace
, ai_pqconn_trace
, ZEND_ACC_PUBLIC
)
1925 PHP_ME(pqconn
, off
, ai_pqconn_off
, ZEND_ACC_PUBLIC
)
1926 PHP_ME(pqconn
, on
, ai_pqconn_on
, ZEND_ACC_PUBLIC
)
1927 PHP_ME(pqconn
, setConverter
, ai_pqconn_set_converter
, ZEND_ACC_PUBLIC
)
1928 PHP_ME(pqconn
, unsetConverter
, ai_pqconn_unset_converter
, ZEND_ACC_PUBLIC
)
1932 PHP_MSHUTDOWN_FUNCTION(pqconn
)
1934 php_persistent_handle_cleanup(PHP_PQ_G
->connection
.name
, NULL
);
1935 zend_string_release(PHP_PQ_G
->connection
.name
);
1936 zend_hash_destroy(&php_pqconn_object_prophandlers
);
1940 PHP_MINIT_FUNCTION(pqconn
)
1942 zend_class_entry ce
= {0};
1943 php_pq_object_prophandler_t ph
= {0};
1945 INIT_NS_CLASS_ENTRY(ce
, "pq", "Connection", php_pqconn_methods
);
1946 php_pqconn_class_entry
= zend_register_internal_class_ex(&ce
, NULL
);
1947 php_pqconn_class_entry
->create_object
= php_pqconn_create_object
;
1949 memcpy(&php_pqconn_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
1950 php_pqconn_object_handlers
.offset
= XtOffsetOf(php_pqconn_object_t
, zo
);
1951 php_pqconn_object_handlers
.free_obj
= php_pqconn_object_free
;
1952 php_pqconn_object_handlers
.read_property
= php_pq_object_read_prop
;
1953 php_pqconn_object_handlers
.write_property
= php_pq_object_write_prop
;
1954 php_pqconn_object_handlers
.clone_obj
= NULL
;
1955 php_pqconn_object_handlers
.get_property_ptr_ptr
= NULL
;
1956 php_pqconn_object_handlers
.get_gc
= php_pq_object_get_gc
;
1957 php_pqconn_object_handlers
.get_properties
= php_pq_object_properties
;
1958 php_pqconn_object_handlers
.get_debug_info
= php_pq_object_debug_info
;
1960 zend_hash_init(&php_pqconn_object_prophandlers
, 22, NULL
, php_pq_object_prophandler_dtor
, 1);
1962 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("status"), CONNECTION_BAD
, ZEND_ACC_PUBLIC
);
1963 ph
.read
= php_pqconn_object_read_status
;
1964 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "status", sizeof("status")-1, (void *) &ph
, sizeof(ph
));
1966 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("transactionStatus"), PQTRANS_UNKNOWN
, ZEND_ACC_PUBLIC
);
1967 ph
.read
= php_pqconn_object_read_transaction_status
;
1968 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "transactionStatus", sizeof("transactionStatus")-1, (void *) &ph
, sizeof(ph
));
1970 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("socket"), ZEND_ACC_PUBLIC
);
1971 ph
.read
= NULL
; /* forward to std prophandler */
1972 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "socket", sizeof("socket")-1, (void *) &ph
, sizeof(ph
));
1974 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("errorMessage"), ZEND_ACC_PUBLIC
);
1975 ph
.read
= php_pqconn_object_read_error_message
;
1976 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "errorMessage", sizeof("errorMessage")-1, (void *) &ph
, sizeof(ph
));
1978 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("busy"), 0, ZEND_ACC_PUBLIC
);
1979 ph
.read
= php_pqconn_object_read_busy
;
1980 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "busy", sizeof("busy")-1, (void *) &ph
, sizeof(ph
));
1982 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("encoding"), ZEND_ACC_PUBLIC
);
1983 ph
.read
= php_pqconn_object_read_encoding
;
1984 ph
.write
= php_pqconn_object_write_encoding
;
1985 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "encoding", sizeof("encoding")-1, (void *) &ph
, sizeof(ph
));
1988 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("unbuffered"), 0, ZEND_ACC_PUBLIC
);
1989 ph
.read
= php_pqconn_object_read_unbuffered
;
1990 ph
.write
= php_pqconn_object_write_unbuffered
;
1991 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "unbuffered", sizeof("unbuffered")-1, (void *) &ph
, sizeof(ph
));
1994 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("db"), ZEND_ACC_PUBLIC
);
1995 ph
.read
= php_pqconn_object_read_db
;
1996 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "db", sizeof("db")-1, (void *) &ph
, sizeof(ph
));
1998 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("user"), ZEND_ACC_PUBLIC
);
1999 ph
.read
= php_pqconn_object_read_user
;
2000 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "user", sizeof("user")-1, (void *) &ph
, sizeof(ph
));
2002 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("pass"), ZEND_ACC_PUBLIC
);
2003 ph
.read
= php_pqconn_object_read_pass
;
2004 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "pass", sizeof("pass")-1, (void *) &ph
, sizeof(ph
));
2006 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("host"), ZEND_ACC_PUBLIC
);
2007 ph
.read
= php_pqconn_object_read_host
;
2008 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "host", sizeof("host")-1, (void *) &ph
, sizeof(ph
));
2010 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("port"), ZEND_ACC_PUBLIC
);
2011 ph
.read
= php_pqconn_object_read_port
;
2012 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "port", sizeof("port")-1, (void *) &ph
, sizeof(ph
));
2015 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("params"), ZEND_ACC_PUBLIC
);
2016 ph
.read
= php_pqconn_object_read_params
;
2017 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "params", sizeof("params")-1, (void *) &ph
, sizeof(ph
));
2020 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("options"), ZEND_ACC_PUBLIC
);
2021 ph
.read
= php_pqconn_object_read_options
;
2022 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "options", sizeof("options")-1, (void *) &ph
, sizeof(ph
));
2024 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("eventHandlers"), ZEND_ACC_PUBLIC
);
2025 ph
.read
= php_pqconn_object_read_event_handlers
;
2026 ph
.gc
= php_pqconn_object_gc_event_handlers
;
2027 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "eventHandlers", sizeof("eventHandlers")-1, (void *) &ph
, sizeof(ph
));
2030 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("listeners"), ZEND_ACC_PUBLIC
);
2031 ph
.read
= php_pqconn_object_read_listeners
;
2032 ph
.gc
= php_pqconn_object_gc_listeners
;
2033 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "listeners", sizeof("listeners")-1, (void *) &ph
, sizeof(ph
));
2036 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("converters"), ZEND_ACC_PUBLIC
);
2037 ph
.read
= php_pqconn_object_read_converters
;
2038 ph
.gc
= php_pqconn_object_gc_converters
;
2039 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "converters", sizeof("converters")-1, (void *) &ph
, sizeof(ph
));
2042 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultFetchType"), 0, ZEND_ACC_PUBLIC
);
2043 ph
.read
= php_pqconn_object_read_def_fetch_type
;
2044 ph
.write
= php_pqconn_object_write_def_fetch_type
;
2045 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultFetchType", sizeof("defaultFetchType")-1, (void *) &ph
, sizeof(ph
));
2048 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionIsolation"), 0, ZEND_ACC_PUBLIC
);
2049 ph
.read
= php_pqconn_object_read_def_txn_isolation
;
2050 ph
.write
= php_pqconn_object_write_def_txn_isolation
;
2051 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultTransactionIsolation", sizeof("defaultTransactionIsolation")-1, (void *) &ph
, sizeof(ph
));
2054 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionReadonly"), 0, ZEND_ACC_PUBLIC
);
2055 ph
.read
= php_pqconn_object_read_def_txn_readonly
;
2056 ph
.write
= php_pqconn_object_write_def_txn_readonly
;
2057 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultTransactionReadonly", sizeof("defaultTransactionReadonly")-1, (void *) &ph
, sizeof(ph
));
2060 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionDeferrable"), 0, ZEND_ACC_PUBLIC
);
2061 ph
.read
= php_pqconn_object_read_def_txn_deferrable
;
2062 ph
.write
= php_pqconn_object_write_def_txn_deferrable
;
2063 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultTransactionDeferrable", sizeof("defaultTransactionDeferrable")-1, (void *) &ph
, sizeof(ph
));
2066 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultAutoConvert"), PHP_PQRES_CONV_ALL
, ZEND_ACC_PUBLIC
);
2067 ph
.read
= php_pqconn_object_read_def_auto_conv
;
2068 ph
.write
= php_pqconn_object_write_def_auto_conv
;
2069 zend_hash_str_add_mem(&php_pqconn_object_prophandlers
, "defaultAutoConvert", sizeof("defaultAutoConvert")-1, (void *) &ph
, sizeof(ph
));
2072 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("OK"), CONNECTION_OK
);
2073 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("BAD"), CONNECTION_BAD
);
2074 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("STARTED"), CONNECTION_STARTED
);
2075 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("MADE"), CONNECTION_MADE
);
2076 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AWAITING_RESPONSE"), CONNECTION_AWAITING_RESPONSE
);
2077 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AUTH_OK"), CONNECTION_AUTH_OK
);
2078 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SSL_STARTUP"), CONNECTION_SSL_STARTUP
);
2079 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SETENV"), CONNECTION_SETENV
);
2081 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_IDLE"), PQTRANS_IDLE
);
2082 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_ACTIVE"), PQTRANS_ACTIVE
);
2083 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INTRANS"), PQTRANS_INTRANS
);
2084 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INERROR"), PQTRANS_INERROR
);
2085 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_UNKNOWN"), PQTRANS_UNKNOWN
);
2087 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_FAILED"), PGRES_POLLING_FAILED
);
2088 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_READING"), PGRES_POLLING_READING
);
2089 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_WRITING"), PGRES_POLLING_WRITING
);
2090 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_OK"), PGRES_POLLING_OK
);
2092 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_NOTICE"), ZEND_STRL("notice"));
2093 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESULT"), ZEND_STRL("result"));
2094 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESET"), ZEND_STRL("reset"));
2096 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("ASYNC"), 0x1);
2097 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("PERSISTENT"), 0x2);
2099 PHP_PQ_G
->connection
.name
= zend_string_init(ZEND_STRL("pq\\Connection"), 1);
2101 return php_persistent_handle_provide(PHP_PQ_G
->connection
.name
, php_pqconn_get_resource_factory_ops(), NULL
, NULL
);
2109 * vim600: noet sw=4 ts=4 fdm=marker
2110 * vim<600: noet sw=4 ts=4