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 +--------------------------------------------------------------------+
19 #define SMART_STR_PREALLOC 256
20 #include <ext/standard/php_smart_str.h>
22 #include <libpq-events.h>
26 #include "php_pq_misc.h"
27 #include "php_pq_object.h"
28 #include "php_pqexc.h"
29 #include "php_pqconn.h"
30 #include "php_pqconn_event.h"
31 #include "php_pqres.h"
32 #include "php_pqstm.h"
33 #include "php_pqtxn.h"
34 #include "php_pqcur.h"
36 zend_class_entry
*php_pqconn_class_entry
;
37 static zend_object_handlers php_pqconn_object_handlers
;
38 static HashTable php_pqconn_object_prophandlers
;
41 static void php_pqconn_del_eventhandler(php_pqconn_object_t *obj, const char *type_str, size_t type_len, ulong id TSRMLS_DC)
45 if (SUCCESS == zend_hash_find(&obj->intern->eventhandlers, type_str, type_len + 1, (void *) &evhs)) {
46 zend_hash_index_del(Z_ARRVAL_PP(evhs), id);
51 static ulong
php_pqconn_add_eventhandler(php_pqconn_object_t
*obj
, const char *type_str
, size_t type_len
, php_pq_callback_t
*cb TSRMLS_DC
)
56 if (SUCCESS
!= zend_hash_find(&obj
->intern
->eventhandlers
, type_str
, type_len
+ 1, (void *) &evhs
)) {
59 zend_hash_init(&evh
, 1, NULL
, (dtor_func_t
) php_pq_callback_dtor
, 0);
60 zend_hash_add(&obj
->intern
->eventhandlers
, type_str
, type_len
+ 1, (void *) &evh
, sizeof(evh
), (void *) &evhs
);
63 php_pq_callback_addref(cb
);
64 h
= zend_hash_next_free_element(evhs
);
65 zend_hash_index_update(evhs
, h
, (void *) cb
, sizeof(*cb
), NULL
);
70 static void php_pqconn_object_free(void *o TSRMLS_DC
)
72 php_pqconn_object_t
*obj
= o
;
74 fprintf(stderr
, "FREE conn(#%d) %p\n", obj
->zv
.handle
, obj
);
77 php_pq_callback_dtor(&obj
->intern
->onevent
);
78 php_resource_factory_handle_dtor(&obj
->intern
->factory
, obj
->intern
->conn TSRMLS_CC
);
79 php_resource_factory_dtor(&obj
->intern
->factory
);
80 zend_hash_destroy(&obj
->intern
->listeners
);
81 zend_hash_destroy(&obj
->intern
->statements
);
82 zend_hash_destroy(&obj
->intern
->converters
);
83 zend_hash_destroy(&obj
->intern
->eventhandlers
);
87 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
92 zend_object_value
php_pqconn_create_object_ex(zend_class_entry
*ce
, php_pqconn_t
*intern
, php_pqconn_object_t
**ptr TSRMLS_DC
)
94 php_pqconn_object_t
*o
;
96 o
= ecalloc(1, sizeof(*o
));
97 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
98 object_properties_init((zend_object
*) o
, ce
);
99 o
->prophandler
= &php_pqconn_object_prophandlers
;
109 o
->zv
.handle
= zend_objects_store_put((zend_object
*) o
, NULL
, php_pqconn_object_free
, NULL TSRMLS_CC
);
110 o
->zv
.handlers
= &php_pqconn_object_handlers
;
115 static zend_object_value
php_pqconn_create_object(zend_class_entry
*class_type TSRMLS_DC
)
117 return php_pqconn_create_object_ex(class_type
, NULL
, NULL TSRMLS_CC
);
120 static void php_pqconn_object_read_status(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
122 php_pqconn_object_t
*obj
= o
;
124 RETVAL_LONG(PQstatus(obj
->intern
->conn
));
127 static void php_pqconn_object_read_transaction_status(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
129 php_pqconn_object_t
*obj
= o
;
131 RETVAL_LONG(PQtransactionStatus(obj
->intern
->conn
));
134 static void php_pqconn_object_read_error_message(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
136 php_pqconn_object_t
*obj
= o
;
137 char *error
= PHP_PQerrorMessage(obj
->intern
->conn
);
140 RETVAL_STRING(error
, 1);
146 static int apply_notify_listener(void *p
, void *arg TSRMLS_DC
)
148 php_pq_callback_t
*listener
= p
;
150 zval
*zpid
, *zchannel
, *zmessage
;
153 ZVAL_LONG(zpid
, nfy
->be_pid
);
154 MAKE_STD_ZVAL(zchannel
);
155 ZVAL_STRING(zchannel
, nfy
->relname
, 1);
156 MAKE_STD_ZVAL(zmessage
);
157 ZVAL_STRING(zmessage
, nfy
->extra
, 1);
159 zend_fcall_info_argn(&listener
->fci TSRMLS_CC
, 3, &zchannel
, &zmessage
, &zpid
);
160 zend_fcall_info_call(&listener
->fci
, &listener
->fcc
, NULL
, NULL TSRMLS_CC
);
162 zval_ptr_dtor(&zchannel
);
163 zval_ptr_dtor(&zmessage
);
164 zval_ptr_dtor(&zpid
);
166 return ZEND_HASH_APPLY_KEEP
;
169 static int apply_notify_listeners(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
171 HashTable
*listeners
= p
;
172 PGnotify
*nfy
= va_arg(argv
, PGnotify
*);
174 if (0 == fnmatch(key
->arKey
, nfy
->relname
, 0)) {
175 zend_hash_apply_with_argument(listeners
, apply_notify_listener
, nfy TSRMLS_CC
);
178 return ZEND_HASH_APPLY_KEEP
;
181 void php_pqconn_notify_listeners(php_pqconn_object_t
*obj TSRMLS_DC
)
185 while ((nfy
= PQnotifies(obj
->intern
->conn
))) {
186 zend_hash_apply_with_arguments(&obj
->intern
->listeners TSRMLS_CC
, apply_notify_listeners
, 1, nfy
);
191 static void php_pqconn_object_read_busy(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
193 php_pqconn_object_t
*obj
= o
;
195 RETVAL_BOOL(PQisBusy(obj
->intern
->conn
));
198 static void php_pqconn_object_read_encoding(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
200 php_pqconn_object_t
*obj
= o
;
202 RETVAL_STRING(pg_encoding_to_char(PQclientEncoding(obj
->intern
->conn
)), 1);
205 static void php_pqconn_object_write_encoding(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
207 php_pqconn_object_t
*obj
= o
;
210 if (Z_TYPE_P(value
) != IS_STRING
) {
211 if (Z_REFCOUNT_P(value
) > 1) {
214 ZVAL_ZVAL(tmp
, zenc
, 1, 0);
215 convert_to_string(tmp
);
218 convert_to_string_ex(&zenc
);
222 if (0 > PQsetClientEncoding(obj
->intern
->conn
, Z_STRVAL_P(zenc
))) {
223 php_error(E_NOTICE
, "Unrecognized encoding '%s'", Z_STRVAL_P(zenc
));
227 zval_ptr_dtor(&zenc
);
231 static void php_pqconn_object_read_unbuffered(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
233 php_pqconn_object_t
*obj
= o
;
235 RETVAL_BOOL(obj
->intern
->unbuffered
);
238 static void php_pqconn_object_write_unbuffered(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
240 php_pqconn_object_t
*obj
= o
;
242 obj
->intern
->unbuffered
= z_is_true(value
);
245 static void php_pqconn_object_read_db(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
247 php_pqconn_object_t
*obj
= o
;
248 char *db
= PQdb(obj
->intern
->conn
);
251 RETVAL_STRING(db
, 1);
253 RETVAL_EMPTY_STRING();
257 static void php_pqconn_object_read_user(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
259 php_pqconn_object_t
*obj
= o
;
260 char *user
= PQuser(obj
->intern
->conn
);
263 RETVAL_STRING(user
, 1);
265 RETVAL_EMPTY_STRING();
269 static void php_pqconn_object_read_pass(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
271 php_pqconn_object_t
*obj
= o
;
272 char *pass
= PQpass(obj
->intern
->conn
);
275 RETVAL_STRING(pass
, 1);
277 RETVAL_EMPTY_STRING();
281 static void php_pqconn_object_read_host(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
283 php_pqconn_object_t
*obj
= o
;
284 char *host
= PQhost(obj
->intern
->conn
);
287 RETVAL_STRING(host
, 1);
289 RETVAL_EMPTY_STRING();
293 static void php_pqconn_object_read_port(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
295 php_pqconn_object_t
*obj
= o
;
296 char *port
= PQport(obj
->intern
->conn
);
299 RETVAL_STRING(port
, 1);
301 RETVAL_EMPTY_STRING();
306 static void php_pqconn_object_read_params(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
308 php_pqconn_object_t
*obj
= o
;
309 PQconninfoOption
*ptr
, *params
= PQconninfo(obj
->intern
->conn
);
311 array_init(return_value
);
314 for (ptr
= params
; ptr
->keyword
; ++ptr
) {
316 add_assoc_string(return_value
, ptr
->keyword
, ptr
->val
, 1);
318 add_assoc_null(return_value
, ptr
->keyword
);
321 PQconninfoFree(params
);
326 static void php_pqconn_object_read_options(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
328 php_pqconn_object_t
*obj
= o
;
329 char *options
= PQoptions(obj
->intern
->conn
);
332 RETVAL_STRING(options
, 1);
334 RETVAL_EMPTY_STRING();
338 static int apply_read_event_handler_ex(void *p
, void *arg TSRMLS_DC
)
341 zval
*zcb
= php_pq_callback_to_zval(p
);
343 zend_hash_next_index_insert(rv
, &zcb
, sizeof(zval
*), NULL
);
345 return ZEND_HASH_APPLY_KEEP
;
348 static int apply_read_event_handlers(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
350 HashTable
*evhs
= p
, *rv
= va_arg(argv
, HashTable
*);
351 zval
*entry
, **entry_ptr
;
353 MAKE_STD_ZVAL(entry
);
354 array_init_size(entry
, zend_hash_num_elements(evhs
));
356 if (key
->nKeyLength
) {
357 zend_hash_add(rv
, key
->arKey
, key
->nKeyLength
, &entry
, sizeof(zval
*), (void *) &entry_ptr
);
359 zend_hash_index_update(rv
, key
->h
, &entry
, sizeof(zval
*), (void *) &entry_ptr
);
362 zend_hash_apply_with_argument(evhs
, apply_read_event_handler_ex
, Z_ARRVAL_PP(entry_ptr
) TSRMLS_CC
);
364 return ZEND_HASH_APPLY_KEEP
;
366 static void php_pqconn_object_read_event_handlers(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
368 php_pqconn_object_t
*obj
= o
;
370 array_init(return_value
);
371 zend_hash_apply_with_arguments(&obj
->intern
->eventhandlers TSRMLS_CC
, apply_read_event_handlers
, 1, Z_ARRVAL_P(return_value
) TSRMLS_CC
);
374 static void php_pqconn_object_read_def_fetch_type(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
376 php_pqconn_object_t
*obj
= o
;
378 RETVAL_LONG(obj
->intern
->default_fetch_type
);
380 static void php_pqconn_object_write_def_fetch_type(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
382 php_pqconn_object_t
*obj
= o
;
385 if (Z_TYPE_P(zft
) != IS_LONG
) {
386 if (Z_REFCOUNT_P(zft
) > 1) {
389 ZVAL_ZVAL(tmp
, zft
, 1, 0);
390 convert_to_long(tmp
);
393 convert_to_long_ex(&zft
);
397 obj
->intern
->default_fetch_type
= Z_LVAL_P(zft
) & 0x3; /* two bits only */
404 static void php_pqconn_object_read_def_txn_isolation(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
406 php_pqconn_object_t
*obj
= o
;
408 RETVAL_LONG(obj
->intern
->default_txn_isolation
);
410 static void php_pqconn_object_write_def_txn_isolation(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
412 php_pqconn_object_t
*obj
= o
;
415 if (Z_TYPE_P(zti
) != IS_LONG
) {
416 if (Z_REFCOUNT_P(zti
) > 1) {
419 ZVAL_ZVAL(tmp
, zti
, 1, 0);
420 convert_to_long(tmp
);
423 convert_to_long_ex(&zti
);
427 obj
->intern
->default_txn_isolation
= Z_LVAL_P(zti
) & 0x3; /* two bits only */
434 static void php_pqconn_object_read_def_txn_readonly(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
436 php_pqconn_object_t
*obj
= o
;
438 RETVAL_BOOL(obj
->intern
->default_txn_readonly
);
440 static void php_pqconn_object_write_def_txn_readonly(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
442 php_pqconn_object_t
*obj
= o
;
444 obj
->intern
->default_txn_readonly
= zend_is_true(value
);
447 static void php_pqconn_object_read_def_txn_deferrable(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
449 php_pqconn_object_t
*obj
= o
;
451 RETVAL_BOOL(obj
->intern
->default_txn_deferrable
);
453 static void php_pqconn_object_write_def_txn_deferrable(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
455 php_pqconn_object_t
*obj
= o
;
457 obj
->intern
->default_txn_deferrable
= zend_is_true(value
);
460 static void php_pqconn_object_read_def_auto_conv(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
462 php_pqconn_object_t
*obj
= o
;
464 RETVAL_LONG(obj
->intern
->default_auto_convert
);
466 static void php_pqconn_object_write_def_auto_conv(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
468 php_pqconn_object_t
*obj
= o
;
471 if (Z_TYPE_P(zac
) != IS_LONG
) {
472 if (Z_REFCOUNT_P(zac
) > 1) {
475 ZVAL_ZVAL(tmp
, zac
, 1, 0);
476 convert_to_long(tmp
);
479 convert_to_long_ex(&zac
);
483 obj
->intern
->default_auto_convert
= Z_LVAL_P(zac
) & PHP_PQRES_CONV_ALL
;
490 static ZEND_RESULT_CODE
php_pqconn_update_socket(zval
*this_ptr
, php_pqconn_object_t
*obj TSRMLS_DC
)
492 zval
*zsocket
, zmember
;
494 ZEND_RESULT_CODE retval
;
498 obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
501 INIT_PZVAL(&zmember
);
502 ZVAL_STRINGL(&zmember
, "socket", sizeof("socket")-1, 0);
503 MAKE_STD_ZVAL(zsocket
);
505 if ((CONNECTION_BAD
!= PQstatus(obj
->intern
->conn
))
506 && (-1 < (socket
= PQsocket(obj
->intern
->conn
)))
507 && (stream
= php_stream_fopen_from_fd(socket
, "r+b", NULL
))) {
508 stream
->flags
|= PHP_STREAM_FLAG_NO_CLOSE
;
509 php_stream_to_zval(stream
, zsocket
);
515 zend_get_std_object_handlers()->write_property(getThis(), &zmember
, zsocket
, NULL TSRMLS_CC
);
516 zval_ptr_dtor(&zsocket
);
521 static void *php_pqconn_resource_factory_ctor(void *data
, void *init_arg TSRMLS_DC
)
523 php_pqconn_resource_factory_data_t
*o
= init_arg
;
524 PGconn
*conn
= NULL
;;
526 if (o
->flags
& PHP_PQCONN_ASYNC
) {
527 conn
= PQconnectStart(o
->dsn
);
529 conn
= PQconnectdb(o
->dsn
);
533 PQregisterEventProc(conn
, php_pqconn_event
, "ext-pq", NULL
);
539 static void php_pqconn_resource_factory_dtor(void *opaque
, void *handle TSRMLS_DC
)
541 php_pqconn_event_data_t
*evdata
= PQinstanceData(handle
, php_pqconn_event
);
543 /* we don't care for anything, except free'ing evdata */
545 PQsetInstanceData(handle
, php_pqconn_event
, NULL
);
546 memset(evdata
, 0, sizeof(*evdata
));
553 static php_resource_factory_ops_t php_pqconn_resource_factory_ops
= {
554 php_pqconn_resource_factory_ctor
,
556 php_pqconn_resource_factory_dtor
559 php_resource_factory_ops_t
*php_pqconn_get_resource_factory_ops(void)
561 return &php_pqconn_resource_factory_ops
;
564 static void php_pqconn_wakeup(php_persistent_handle_factory_t
*f
, void **handle TSRMLS_DC
)
566 PGresult
*res
= PQexec(*handle
, "");
567 php_pqres_clear(res
);
569 if (CONNECTION_OK
!= PQstatus(*handle
)) {
574 static inline PGresult
*unlisten(PGconn
*conn
, const char *channel_str
, size_t channel_len TSRMLS_DC
)
576 char *quoted_channel
= PQescapeIdentifier(conn
, channel_str
, channel_len
);
577 PGresult
*res
= NULL
;
579 if (quoted_channel
) {
582 smart_str_appends(&cmd
, "UNLISTEN ");
583 smart_str_appends(&cmd
, quoted_channel
);
586 res
= PQexec(conn
, cmd
.c
);
588 smart_str_free(&cmd
);
589 PQfreemem(quoted_channel
);
595 static int apply_unlisten(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
597 php_pqconn_object_t
*obj
= va_arg(argv
, php_pqconn_object_t
*);
598 PGresult
*res
= unlisten(obj
->intern
->conn
, key
->arKey
, key
->nKeyLength
- 1 TSRMLS_CC
);
601 php_pqres_clear(res
);
604 return ZEND_HASH_APPLY_REMOVE
;
607 static void php_pqconn_retire(php_persistent_handle_factory_t
*f
, void **handle TSRMLS_DC
)
609 php_pqconn_event_data_t
*evdata
= PQinstanceData(*handle
, php_pqconn_event
);
614 PQsetInstanceData(*handle
, php_pqconn_event
, NULL
);
617 PQsetNoticeReceiver(*handle
, php_pqconn_notice_ignore
, NULL
);
619 /* cancel async queries */
620 if (PQisBusy(*handle
) && (cancel
= PQgetCancel(*handle
))) {
623 PQcancel(cancel
, err
, sizeof(err
));
624 PQfreeCancel(cancel
);
626 /* clean up async results */
627 while ((res
= PQgetResult(*handle
))) {
628 php_pqres_clear(res
);
631 /* clean up transaction & session */
632 switch (PQtransactionStatus(*handle
)) {
634 res
= PQexec(*handle
, "RESET ALL");
637 res
= PQexec(*handle
, "ROLLBACK; RESET ALL");
642 php_pqres_clear(res
);
646 /* clean up notify listeners */
647 zend_hash_apply_with_arguments(&evdata
->obj
->intern
->listeners TSRMLS_CC
, apply_unlisten
, 1, evdata
->obj
);
649 /* release instance data */
654 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct
, 0, 0, 1)
655 ZEND_ARG_INFO(0, dsn
)
656 ZEND_ARG_INFO(0, flags
)
658 static PHP_METHOD(pqconn
, __construct
) {
659 zend_error_handling zeh
;
665 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
666 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &dsn_str
, &dsn_len
, &flags
);
667 zend_restore_error_handling(&zeh TSRMLS_CC
);
670 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
673 throw_exce(EX_BAD_METHODCALL TSRMLS_CC
, "pq\\Connection already initialized");
675 php_pqconn_event_data_t
*evdata
= php_pqconn_event_data_init(obj TSRMLS_CC
);
676 php_pqconn_resource_factory_data_t rfdata
= {dsn_str
, flags
};
678 obj
->intern
= ecalloc(1, sizeof(*obj
->intern
));
680 obj
->intern
->default_auto_convert
= PHP_PQRES_CONV_ALL
;
682 zend_hash_init(&obj
->intern
->listeners
, 0, NULL
, (dtor_func_t
) zend_hash_destroy
, 0);
683 zend_hash_init(&obj
->intern
->statements
, 0, NULL
, NULL
, 0);
684 zend_hash_init(&obj
->intern
->converters
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
685 zend_hash_init(&obj
->intern
->eventhandlers
, 0, NULL
, (dtor_func_t
) zend_hash_destroy
, 0);
687 if (flags
& PHP_PQCONN_PERSISTENT
) {
688 php_persistent_handle_factory_t
*phf
= php_persistent_handle_concede(NULL
, ZEND_STRL("pq\\Connection"), dsn_str
, dsn_len
, php_pqconn_wakeup
, php_pqconn_retire TSRMLS_CC
);
689 php_persistent_handle_resource_factory_init(&obj
->intern
->factory
, phf
);
691 php_resource_factory_init(&obj
->intern
->factory
, &php_pqconn_resource_factory_ops
, NULL
, NULL
);
694 if (flags
& PHP_PQCONN_ASYNC
) {
695 obj
->intern
->poller
= (int (*)(PGconn
*)) PQconnectPoll
;
698 obj
->intern
->conn
= php_resource_factory_handle_ctor(&obj
->intern
->factory
, &rfdata TSRMLS_CC
);
700 PQsetInstanceData(obj
->intern
->conn
, php_pqconn_event
, evdata
);
701 PQsetNoticeReceiver(obj
->intern
->conn
, php_pqconn_notice_recv
, evdata
);
703 if (SUCCESS
!= php_pqconn_update_socket(getThis(), obj TSRMLS_CC
)) {
704 throw_exce(EX_CONNECTION_FAILED TSRMLS_CC
, "Connection failed (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
710 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset
, 0, 0, 0)
712 static PHP_METHOD(pqconn
, reset
) {
713 zend_error_handling zeh
;
716 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
717 rv
= zend_parse_parameters_none();
718 zend_restore_error_handling(&zeh TSRMLS_CC
);
721 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
724 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
726 PQreset(obj
->intern
->conn
);
728 if (CONNECTION_OK
!= PQstatus(obj
->intern
->conn
)) {
729 throw_exce(EX_CONNECTION_FAILED TSRMLS_CC
, "Connection reset failed: (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
732 php_pqconn_notify_listeners(obj TSRMLS_CC
);
737 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset_async
, 0, 0, 0)
739 static PHP_METHOD(pqconn
, resetAsync
) {
740 zend_error_handling zeh
;
743 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
744 rv
= zend_parse_parameters_none();
745 zend_restore_error_handling(&zeh TSRMLS_CC
);
748 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
751 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
753 if (!PQresetStart(obj
->intern
->conn
)) {
754 throw_exce(EX_IO TSRMLS_CC
, "Failed to start connection reset (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
756 obj
->intern
->poller
= (int (*)(PGconn
*)) PQresetPoll
;
759 php_pqconn_notify_listeners(obj TSRMLS_CC
);
764 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten
, 0, 0, 1)
765 ZEND_ARG_INFO(0, channel
)
767 static PHP_METHOD(pqconn
, unlisten
)
769 zend_error_handling zeh
;
774 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
775 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &channel_str
, &channel_len
);
776 zend_restore_error_handling(&zeh TSRMLS_CC
);
779 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
782 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
783 } else if (SUCCESS
== zend_hash_del(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1)) {
784 PGresult
*res
= unlisten(obj
->intern
->conn
, channel_str
, channel_len TSRMLS_CC
);
787 php_pqres_success(res TSRMLS_CC
);
788 php_pqres_clear(res
);
794 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten_async
, 0, 0, 1)
795 ZEND_ARG_INFO(0, channel
)
797 static PHP_METHOD(pqconn
, unlistenAsync
) {
798 zend_error_handling zeh
;
803 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
804 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &channel_str
, &channel_len
);
805 zend_restore_error_handling(&zeh TSRMLS_CC
);
808 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
811 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
813 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
815 if (!quoted_channel
) {
816 throw_exce(EX_ESCAPE TSRMLS_CC
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
820 smart_str_appends(&cmd
, "UNLISTEN ");
821 smart_str_appends(&cmd
, quoted_channel
);
824 if (!PQsendQuery(obj
->intern
->conn
, cmd
.c
)) {
825 throw_exce(EX_IO TSRMLS_CC
, "Failed to uninstall listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
827 obj
->intern
->poller
= PQconsumeInput
;
828 zend_hash_del(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1);
831 smart_str_free(&cmd
);
832 PQfreemem(quoted_channel
);
833 php_pqconn_notify_listeners(obj TSRMLS_CC
);
839 static void php_pqconn_add_listener(php_pqconn_object_t
*obj
, const char *channel_str
, size_t channel_len
, php_pq_callback_t
*listener TSRMLS_DC
)
841 HashTable ht
, *existing_listeners
;
843 php_pq_callback_addref(listener
);
845 if (SUCCESS
== zend_hash_find(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1, (void *) &existing_listeners
)) {
846 zend_hash_next_index_insert(existing_listeners
, (void *) listener
, sizeof(*listener
), NULL
);
848 zend_hash_init(&ht
, 1, NULL
, (dtor_func_t
) php_pq_callback_dtor
, 0);
849 zend_hash_next_index_insert(&ht
, (void *) listener
, sizeof(*listener
), NULL
);
850 zend_hash_add(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1, (void *) &ht
, sizeof(HashTable
), NULL
);
854 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen
, 0, 0, 2)
855 ZEND_ARG_INFO(0, channel
)
856 ZEND_ARG_INFO(0, callable
)
858 static PHP_METHOD(pqconn
, listen
) {
859 zend_error_handling zeh
;
860 char *channel_str
= NULL
;
862 php_pq_callback_t listener
= {{0}};
865 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
866 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
867 zend_restore_error_handling(&zeh TSRMLS_CC
);
870 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
873 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
875 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
877 if (!quoted_channel
) {
878 throw_exce(EX_ESCAPE TSRMLS_CC
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
883 smart_str_appends(&cmd
, "LISTEN ");
884 smart_str_appends(&cmd
, quoted_channel
);
887 res
= php_pq_exec(obj
->intern
->conn
, cmd
.c
);
889 smart_str_free(&cmd
);
890 PQfreemem(quoted_channel
);
893 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
895 if (SUCCESS
== php_pqres_success(res TSRMLS_CC
)) {
896 obj
->intern
->poller
= PQconsumeInput
;
897 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener TSRMLS_CC
);
899 php_pqres_clear(res
);
902 php_pqconn_notify_listeners(obj TSRMLS_CC
);
908 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen_async
, 0, 0, 0)
909 ZEND_ARG_INFO(0, channel
)
910 ZEND_ARG_INFO(0, callable
)
912 static PHP_METHOD(pqconn
, listenAsync
) {
913 zend_error_handling zeh
;
914 char *channel_str
= NULL
;
916 php_pq_callback_t listener
= {{0}};
919 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
920 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
921 zend_restore_error_handling(&zeh TSRMLS_CC
);
924 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
927 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
929 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
931 if (!quoted_channel
) {
932 throw_exce(EX_ESCAPE TSRMLS_CC
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
936 smart_str_appends(&cmd
, "LISTEN ");
937 smart_str_appends(&cmd
, quoted_channel
);
940 if (!PQsendQuery(obj
->intern
->conn
, cmd
.c
)) {
941 throw_exce(EX_IO TSRMLS_CC
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
943 obj
->intern
->poller
= PQconsumeInput
;
944 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener TSRMLS_CC
);
947 smart_str_free(&cmd
);
948 PQfreemem(quoted_channel
);
949 php_pqconn_notify_listeners(obj TSRMLS_CC
);
955 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify
, 0, 0, 2)
956 ZEND_ARG_INFO(0, channel
)
957 ZEND_ARG_INFO(0, message
)
959 static PHP_METHOD(pqconn
, notify
) {
960 zend_error_handling zeh
;
961 char *channel_str
, *message_str
;
962 int channel_len
, message_len
;
965 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
966 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
967 zend_restore_error_handling(&zeh TSRMLS_CC
);
970 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
973 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
976 char *params
[2] = {channel_str
, message_str
};
978 res
= PQexecParams(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0);
981 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
983 php_pqres_success(res TSRMLS_CC
);
984 php_pqres_clear(res
);
987 php_pqconn_notify_listeners(obj TSRMLS_CC
);
992 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify_async
, 0, 0, 2)
993 ZEND_ARG_INFO(0, channel
)
994 ZEND_ARG_INFO(0, message
)
996 static PHP_METHOD(pqconn
, notifyAsync
) {
997 zend_error_handling zeh
;
998 char *channel_str
, *message_str
;
999 int channel_len
, message_len
;
1000 ZEND_RESULT_CODE rv
;
1002 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1003 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
1004 zend_restore_error_handling(&zeh TSRMLS_CC
);
1006 if (SUCCESS
== rv
) {
1007 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1010 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1012 char *params
[2] = {channel_str
, message_str
};
1014 if (!PQsendQueryParams(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0)) {
1015 throw_exce(EX_IO TSRMLS_CC
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1017 obj
->intern
->poller
= PQconsumeInput
;
1020 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1025 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_poll
, 0, 0, 0)
1026 ZEND_END_ARG_INFO();
1027 static PHP_METHOD(pqconn
, poll
) {
1028 zend_error_handling zeh
;
1029 ZEND_RESULT_CODE rv
;
1031 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1032 rv
= zend_parse_parameters_none();
1033 zend_restore_error_handling(&zeh TSRMLS_CC
);
1035 if (SUCCESS
== rv
) {
1036 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1039 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1040 } else if (!obj
->intern
->poller
) {
1041 throw_exce(EX_RUNTIME TSRMLS_CC
, "No asynchronous operation active");
1043 if (obj
->intern
->poller
== PQconsumeInput
) {
1044 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
) * PGRES_POLLING_OK
);
1046 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
));
1048 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1053 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec
, 0, 0, 1)
1054 ZEND_ARG_INFO(0, query
)
1055 ZEND_END_ARG_INFO();
1056 static PHP_METHOD(pqconn
, exec
) {
1057 zend_error_handling zeh
;
1060 ZEND_RESULT_CODE rv
;
1062 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1063 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &query_str
, &query_len
);
1064 zend_restore_error_handling(&zeh TSRMLS_CC
);
1066 if (SUCCESS
== rv
) {
1067 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1070 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1072 PGresult
*res
= php_pq_exec(obj
->intern
->conn
, query_str
);
1075 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1076 } else if (SUCCESS
== php_pqres_success(res TSRMLS_CC
)) {
1077 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), &return_value TSRMLS_CC
);
1079 php_pqres_clear(res
);
1082 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1087 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_get_result
, 0, 0, 0)
1088 ZEND_END_ARG_INFO();
1089 static PHP_METHOD(pqconn
, getResult
) {
1090 zend_error_handling zeh
;
1091 ZEND_RESULT_CODE rv
;
1093 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1094 rv
= zend_parse_parameters_none();
1095 zend_restore_error_handling(&zeh TSRMLS_CC
);
1097 if (SUCCESS
== rv
) {
1098 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1101 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1103 PGresult
*res
= PQgetResult(obj
->intern
->conn
);
1108 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), &return_value TSRMLS_CC
);
1111 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1116 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_async
, 0, 0, 1)
1117 ZEND_ARG_INFO(0, query
)
1118 ZEND_ARG_INFO(0, callable
)
1119 ZEND_END_ARG_INFO();
1120 static PHP_METHOD(pqconn
, execAsync
) {
1121 zend_error_handling zeh
;
1122 php_pq_callback_t resolver
= {{0}};
1125 ZEND_RESULT_CODE rv
;
1127 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1128 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|f", &query_str
, &query_len
, &resolver
.fci
, &resolver
.fcc
);
1129 zend_restore_error_handling(&zeh TSRMLS_CC
);
1131 if (SUCCESS
== rv
) {
1132 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1135 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1136 } else if (!PQsendQuery(obj
->intern
->conn
, query_str
)) {
1137 throw_exce(EX_IO TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1138 #if HAVE_PQSETSINGLEROWMODE
1139 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1140 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1143 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver TSRMLS_CC
);
1144 obj
->intern
->poller
= PQconsumeInput
;
1145 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1150 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params
, 0, 0, 2)
1151 ZEND_ARG_INFO(0, query
)
1152 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1153 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1154 ZEND_END_ARG_INFO();
1155 static PHP_METHOD(pqconn
, execParams
) {
1156 zend_error_handling zeh
;
1160 zval
*ztypes
= NULL
;
1161 ZEND_RESULT_CODE rv
;
1163 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1164 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa/|a/!", &query_str
, &query_len
, &zparams
, &ztypes
);
1165 zend_restore_error_handling(&zeh TSRMLS_CC
);
1167 if (SUCCESS
== rv
) {
1168 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1171 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1174 php_pq_params_t
*params
;
1176 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
) TSRMLS_CC
);
1177 res
= PQexecParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1178 php_pq_params_free(¶ms
);
1181 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1183 if (SUCCESS
== php_pqres_success(res TSRMLS_CC
)) {
1184 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), &return_value TSRMLS_CC
);
1186 php_pqres_clear(res
);
1189 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1195 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async
, 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_ARG_INFO(0, callable
)
1200 ZEND_END_ARG_INFO();
1201 static PHP_METHOD(pqconn
, execParamsAsync
) {
1202 zend_error_handling zeh
;
1203 php_pq_callback_t resolver
= {{0}};
1207 zval
*ztypes
= NULL
;
1208 ZEND_RESULT_CODE rv
;
1210 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1211 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa/|a/!f", &query_str
, &query_len
, &zparams
, &ztypes
, &resolver
.fci
, &resolver
.fcc
);
1212 zend_restore_error_handling(&zeh TSRMLS_CC
);
1214 if (SUCCESS
== rv
) {
1215 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1218 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1221 php_pq_params_t
*params
;
1223 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
) TSRMLS_CC
);
1224 rc
= PQsendQueryParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1225 php_pq_params_free(¶ms
);
1228 throw_exce(EX_IO TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1229 #if HAVE_PQSETSINGLEROWMODE
1230 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1231 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1234 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver TSRMLS_CC
);
1235 obj
->intern
->poller
= PQconsumeInput
;
1236 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1240 zend_restore_error_handling(&zeh TSRMLS_CC
);
1243 ZEND_RESULT_CODE
php_pqconn_prepare(zval
*object
, php_pqconn_object_t
*obj
, const char *name
, const char *query
, php_pq_params_t
*params TSRMLS_DC
)
1246 ZEND_RESULT_CODE rv
;
1249 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1252 res
= php_pq_prepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
);
1256 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1258 rv
= php_pqres_success(res TSRMLS_CC
);
1259 php_pqres_clear(res
);
1260 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1266 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare
, 0, 0, 2)
1267 ZEND_ARG_INFO(0, name
)
1268 ZEND_ARG_INFO(0, query
)
1269 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1270 ZEND_END_ARG_INFO();
1271 static PHP_METHOD(pqconn
, prepare
) {
1272 zend_error_handling zeh
;
1273 zval
*ztypes
= NULL
;
1274 char *name_str
, *query_str
;
1275 int name_len
, *query_len
;
1276 ZEND_RESULT_CODE rv
;
1278 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1279 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1280 zend_restore_error_handling(&zeh TSRMLS_CC
);
1282 if (SUCCESS
== rv
) {
1283 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1286 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1288 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL TSRMLS_CC
);
1290 if (SUCCESS
!= php_pqconn_prepare(getThis(), obj
, name_str
, query_str
, params TSRMLS_CC
)) {
1291 php_pq_params_free(¶ms
);
1293 php_pqstm_t
*stm
= php_pqstm_init(obj
, name_str
, query_str
, params TSRMLS_CC
);
1295 return_value
->type
= IS_OBJECT
;
1296 return_value
->value
.obj
= php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
, NULL TSRMLS_CC
);
1302 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 TSRMLS_DC
)
1304 ZEND_RESULT_CODE rv
;
1307 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1310 if (!PQsendPrepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
)) {
1312 throw_exce(EX_IO TSRMLS_CC
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1315 obj
->intern
->poller
= PQconsumeInput
;
1316 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1322 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare_async
, 0, 0, 2)
1323 ZEND_ARG_INFO(0, name
)
1324 ZEND_ARG_INFO(0, query
)
1325 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1326 ZEND_END_ARG_INFO();
1327 static PHP_METHOD(pqconn
, prepareAsync
) {
1328 zend_error_handling zeh
;
1329 zval
*ztypes
= NULL
;
1330 char *name_str
, *query_str
;
1331 int name_len
, *query_len
;
1332 ZEND_RESULT_CODE rv
;
1334 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1335 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1336 zend_restore_error_handling(&zeh TSRMLS_CC
);
1338 if (SUCCESS
== rv
) {
1339 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1342 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1344 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL TSRMLS_CC
);
1346 if (SUCCESS
!= php_pqconn_prepare_async(getThis(), obj
, name_str
, query_str
, params TSRMLS_CC
)) {
1347 php_pq_params_free(¶ms
);
1349 php_pqstm_t
*stm
= php_pqstm_init(obj
, name_str
, query_str
, params TSRMLS_CC
);
1351 return_value
->type
= IS_OBJECT
;
1352 return_value
->value
.obj
= php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
, NULL TSRMLS_CC
);
1358 ZEND_RESULT_CODE
php_pqconn_declare(zval
*object
, php_pqconn_object_t
*obj
, const char *decl TSRMLS_DC
)
1361 ZEND_RESULT_CODE rv
;
1364 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1367 res
= php_pq_exec(obj
->intern
->conn
, decl
);
1371 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1373 rv
= php_pqres_success(res TSRMLS_CC
);
1374 php_pqres_clear(res
);
1375 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1381 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare
, 0, 0, 3)
1382 ZEND_ARG_INFO(0, name
)
1383 ZEND_ARG_INFO(0, flags
)
1384 ZEND_ARG_INFO(0, query
)
1385 ZEND_END_ARG_INFO();
1386 static PHP_METHOD(pqconn
, declare
) {
1387 zend_error_handling zeh
;
1388 char *name_str
, *query_str
;
1389 int name_len
, query_len
;
1391 ZEND_RESULT_CODE rv
;
1393 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1394 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1395 zend_restore_error_handling(&zeh TSRMLS_CC
);
1397 if (SUCCESS
== rv
) {
1398 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1401 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1404 char *decl
= php_pqcur_declare_str(name_str
, name_len
, flags
, query_str
, query_len
, &query_offset
);
1406 if (SUCCESS
!= php_pqconn_declare(getThis(), obj
, decl TSRMLS_CC
)) {
1409 php_pqcur_t
*cur
= php_pqcur_init(obj
, name_str
, decl
, query_offset
, flags TSRMLS_CC
);
1411 return_value
->type
= IS_OBJECT
;
1412 return_value
->value
.obj
= php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
, NULL TSRMLS_CC
);
1418 ZEND_RESULT_CODE
php_pqconn_declare_async(zval
*object
, php_pqconn_object_t
*obj
, const char *decl TSRMLS_DC
)
1420 ZEND_RESULT_CODE rv
;
1423 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1426 if (!PQsendQuery(obj
->intern
->conn
, decl
)) {
1428 throw_exce(EX_IO TSRMLS_CC
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1431 obj
->intern
->poller
= PQconsumeInput
;
1432 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1438 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare_async
, 0, 0, 2)
1439 ZEND_ARG_INFO(0, name
)
1440 ZEND_ARG_INFO(0, flags
)
1441 ZEND_ARG_INFO(0, query
)
1442 ZEND_END_ARG_INFO();
1443 static PHP_METHOD(pqconn
, declareAsync
) {
1444 zend_error_handling zeh
;
1445 char *name_str
, *query_str
;
1446 int name_len
, query_len
;
1448 ZEND_RESULT_CODE rv
;
1450 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1451 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1452 zend_restore_error_handling(&zeh TSRMLS_CC
);
1454 if (SUCCESS
== rv
) {
1455 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1458 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1461 char *decl
= php_pqcur_declare_str(name_str
, name_len
, flags
, query_str
, query_len
, &query_offset
);
1463 if (SUCCESS
!= php_pqconn_declare_async(getThis(), obj
, decl TSRMLS_CC
)) {
1466 php_pqcur_t
*cur
= php_pqcur_init(obj
, name_str
, decl
, query_offset
, flags TSRMLS_CC
);
1468 return_value
->type
= IS_OBJECT
;
1469 return_value
->value
.obj
= php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
, NULL TSRMLS_CC
);
1475 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote
, 0, 0, 1)
1476 ZEND_ARG_INFO(0, string
)
1477 ZEND_END_ARG_INFO();
1478 static PHP_METHOD(pqconn
, quote
) {
1482 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1483 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1486 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1488 char *quoted
= PQescapeLiteral(obj
->intern
->conn
, str
, len
);
1491 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to quote string (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1494 RETVAL_STRING(quoted
, 1);
1501 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote_name
, 0, 0, 1)
1502 ZEND_ARG_INFO(0, type
)
1503 ZEND_END_ARG_INFO();
1504 static PHP_METHOD(pqconn
, quoteName
) {
1508 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1509 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1512 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1514 char *quoted
= PQescapeIdentifier(obj
->intern
->conn
, str
, len
);
1517 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to quote name (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1520 RETVAL_STRING(quoted
, 1);
1527 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_escape_bytea
, 0, 0, 1)
1528 ZEND_ARG_INFO(0, bytea
)
1529 ZEND_END_ARG_INFO();
1530 static PHP_METHOD(pqconn
, escapeBytea
) {
1534 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1535 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1538 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1541 char *escaped_str
= (char *) PQescapeByteaConn(obj
->intern
->conn
, (unsigned char *) str
, len
, &escaped_len
);
1544 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to escape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1547 RETVAL_STRINGL(escaped_str
, escaped_len
- 1, 1);
1548 PQfreemem(escaped_str
);
1554 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unescape_bytea
, 0, 0, 1)
1555 ZEND_ARG_INFO(0, bytea
)
1556 ZEND_END_ARG_INFO();
1557 static PHP_METHOD(pqconn
, unescapeBytea
) {
1561 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1562 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1565 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1567 size_t unescaped_len
;
1568 char *unescaped_str
= (char *) PQunescapeBytea((unsigned char *)str
, &unescaped_len
);
1570 if (!unescaped_str
) {
1571 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to unescape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1574 RETVAL_STRINGL(unescaped_str
, unescaped_len
, 1);
1575 PQfreemem(unescaped_str
);
1581 ZEND_RESULT_CODE
php_pqconn_start_transaction(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable TSRMLS_DC
)
1583 ZEND_RESULT_CODE rv
= FAILURE
;
1586 conn_obj
= zend_object_store_get_object(zconn TSRMLS_CC
);
1589 if (!conn_obj
->intern
) {
1590 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1593 smart_str cmd
= {0};
1594 const char *il
= php_pq_isolation_level(&isolation
);
1596 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1597 smart_str_appends(&cmd
, il
);
1598 smart_str_appends(&cmd
, ", READ ");
1599 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1600 smart_str_appends(&cmd
, ",");
1601 smart_str_appends(&cmd
, deferrable
? "" : " NOT");
1602 smart_str_appends(&cmd
, " DEFERRABLE");
1605 res
= php_pq_exec(conn_obj
->intern
->conn
, cmd
.c
);
1608 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1610 rv
= php_pqres_success(res TSRMLS_CC
);
1611 php_pqres_clear(res
);
1612 php_pqconn_notify_listeners(conn_obj TSRMLS_CC
);
1615 smart_str_free(&cmd
);
1621 ZEND_RESULT_CODE
php_pqconn_start_transaction_async(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable TSRMLS_DC
)
1623 ZEND_RESULT_CODE rv
= FAILURE
;
1626 conn_obj
= zend_object_store_get_object(zconn TSRMLS_CC
);
1629 if (!conn_obj
->intern
) {
1630 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1632 smart_str cmd
= {0};
1633 const char *il
= php_pq_isolation_level(&isolation
);
1635 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1636 smart_str_appends(&cmd
, il
);
1637 smart_str_appends(&cmd
, ", READ ");
1638 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1639 smart_str_appends(&cmd
, ",");
1640 smart_str_appends(&cmd
, deferrable
? "" : "NOT ");
1641 smart_str_appends(&cmd
, " DEFERRABLE");
1644 if (!PQsendQuery(conn_obj
->intern
->conn
, cmd
.c
)) {
1645 throw_exce(EX_IO TSRMLS_CC
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1648 conn_obj
->intern
->poller
= PQconsumeInput
;
1649 php_pqconn_notify_listeners(conn_obj TSRMLS_CC
);
1652 smart_str_free(&cmd
);
1658 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction
, 0, 0, 0)
1659 ZEND_ARG_INFO(0, isolation
)
1660 ZEND_ARG_INFO(0, readonly
)
1661 ZEND_ARG_INFO(0, deferrable
)
1662 ZEND_END_ARG_INFO();
1663 static PHP_METHOD(pqconn
, startTransaction
) {
1664 zend_error_handling zeh
;
1665 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1666 long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1667 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1668 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1669 ZEND_RESULT_CODE rv
;
1671 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1672 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lbb", &isolation
, &readonly
, &deferrable
);
1673 zend_restore_error_handling(&zeh TSRMLS_CC
);
1675 if (SUCCESS
== rv
) {
1676 rv
= php_pqconn_start_transaction(getThis(), obj
, isolation
, readonly
, deferrable TSRMLS_CC
);
1678 if (SUCCESS
== rv
) {
1679 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1681 php_pq_object_addref(obj TSRMLS_CC
);
1684 txn
->isolation
= isolation
;
1685 txn
->readonly
= readonly
;
1686 txn
->deferrable
= deferrable
;
1688 return_value
->type
= IS_OBJECT
;
1689 return_value
->value
.obj
= php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
, NULL TSRMLS_CC
);
1694 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction_async
, 0, 0, 0)
1695 ZEND_ARG_INFO(0, isolation
)
1696 ZEND_ARG_INFO(0, readonly
)
1697 ZEND_ARG_INFO(0, deferrable
)
1698 ZEND_END_ARG_INFO();
1699 static PHP_METHOD(pqconn
, startTransactionAsync
) {
1700 zend_error_handling zeh
;
1701 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1702 long isolation
= obj
->intern
? obj
->intern
->default_txn_isolation
: PHP_PQTXN_READ_COMMITTED
;
1703 zend_bool readonly
= obj
->intern
? obj
->intern
->default_txn_readonly
: 0;
1704 zend_bool deferrable
= obj
->intern
? obj
->intern
->default_txn_deferrable
: 0;
1705 ZEND_RESULT_CODE rv
;
1707 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1708 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lbb", &isolation
, &readonly
, &deferrable
);
1709 zend_restore_error_handling(&zeh TSRMLS_CC
);
1711 if (SUCCESS
== rv
) {
1712 rv
= php_pqconn_start_transaction_async(getThis(), obj
, isolation
, readonly
, deferrable TSRMLS_CC
);
1714 if (SUCCESS
== rv
) {
1715 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1717 php_pq_object_addref(obj TSRMLS_CC
);
1720 txn
->isolation
= isolation
;
1721 txn
->readonly
= readonly
;
1722 txn
->deferrable
= deferrable
;
1724 return_value
->type
= IS_OBJECT
;
1725 return_value
->value
.obj
= php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
, NULL TSRMLS_CC
);
1730 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_trace
, 0, 0, 0)
1731 ZEND_ARG_INFO(0, stdio_stream
)
1732 ZEND_END_ARG_INFO();
1733 static PHP_METHOD(pqconn
, trace
) {
1734 zval
*zstream
= NULL
;
1736 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|r!", &zstream
)) {
1737 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1740 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1743 PQuntrace(obj
->intern
->conn
);
1747 php_stream
*stream
= NULL
;
1749 php_stream_from_zval(stream
, &zstream
);
1751 if (SUCCESS
!= php_stream_cast(stream
, PHP_STREAM_AS_STDIO
, (void *) &fp
, REPORT_ERRORS
)) {
1754 stream
->flags
|= PHP_STREAM_FLAG_NO_CLOSE
;
1755 PQtrace(obj
->intern
->conn
, fp
);
1763 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_off
, 0, 0, 1)
1764 ZEND_ARG_INFO(0, type
)
1765 ZEND_END_ARG_INFO();
1766 static PHP_METHOD(pqconn
, off
) {
1767 zend_error_handling zeh
;
1770 ZEND_RESULT_CODE rv
;
1772 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1773 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &type_str
, &type_len
);
1774 zend_restore_error_handling(&zeh TSRMLS_CC
);
1776 if (SUCCESS
== rv
) {
1777 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1780 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1782 RETURN_BOOL(SUCCESS
== zend_hash_del(&obj
->intern
->eventhandlers
, type_str
, type_len
+ 1));
1787 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_on
, 0, 0, 2)
1788 ZEND_ARG_INFO(0, type
)
1789 ZEND_ARG_INFO(0, callable
)
1790 ZEND_END_ARG_INFO();
1791 static PHP_METHOD(pqconn
, on
) {
1792 zend_error_handling zeh
;
1795 php_pq_callback_t cb
= {{0}};
1796 ZEND_RESULT_CODE rv
;
1798 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1799 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sf", &type_str
, &type_len
, &cb
.fci
, &cb
.fcc
);
1800 zend_restore_error_handling(&zeh TSRMLS_CC
);
1802 if (SUCCESS
== rv
) {
1803 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1806 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1808 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1810 RETVAL_LONG(php_pqconn_add_eventhandler(obj
, type_str
, type_len
, &cb TSRMLS_CC
));
1815 struct apply_set_converter_arg
{
1821 static int apply_set_converter(void *p
, void *a TSRMLS_DC
)
1823 zval
*tmp
, **zoid
= p
;
1824 struct apply_set_converter_arg
*arg
= a
;
1828 convert_to_long_ex(&tmp
);
1830 Z_ADDREF_PP(arg
->zconv
);
1831 zend_hash_index_update(arg
->ht
, Z_LVAL_P(tmp
), arg
->zconv
, sizeof(zval
*), NULL
);
1833 zend_hash_index_del(arg
->ht
, Z_LVAL_P(tmp
));
1835 zval_ptr_dtor(&tmp
);
1837 return ZEND_HASH_APPLY_KEEP
;
1840 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_set_converter
, 0, 0, 1)
1841 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\Converter
, 0)
1842 ZEND_END_ARG_INFO();
1843 static PHP_METHOD(pqconn
, setConverter
) {
1844 ZEND_RESULT_CODE rv
;
1845 zend_error_handling zeh
;
1848 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1849 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zcnv
, php_pqconv_class_entry
);
1850 zend_restore_error_handling(&zeh TSRMLS_CC
);
1852 if (SUCCESS
== rv
) {
1853 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1856 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1858 zval
*tmp
, *zoids
= NULL
;
1859 struct apply_set_converter_arg arg
= {NULL
};
1861 zend_call_method_with_0_params(&zcnv
, NULL
, NULL
, "converttypes", &zoids
);
1864 convert_to_array_ex(&tmp
);
1866 arg
.ht
= &obj
->intern
->converters
;
1870 zend_hash_apply_with_argument(Z_ARRVAL_P(tmp
), apply_set_converter
, &arg TSRMLS_CC
);
1872 zval_ptr_dtor(&tmp
);
1873 zval_ptr_dtor(&zoids
);
1878 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unset_converter
, 0, 0, 1)
1879 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\Converter
, 0)
1880 ZEND_END_ARG_INFO();
1881 static PHP_METHOD(pqconn
, unsetConverter
) {
1882 ZEND_RESULT_CODE rv
;
1883 zend_error_handling zeh
;
1886 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1887 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zcnv
, php_pqconv_class_entry
);
1888 zend_restore_error_handling(&zeh TSRMLS_CC
);
1890 if (SUCCESS
== rv
) {
1891 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1894 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1896 zval
*tmp
, *zoids
= NULL
;
1897 struct apply_set_converter_arg arg
= {NULL
};
1899 zend_call_method_with_0_params(&zcnv
, NULL
, NULL
, "converttypes", &zoids
);
1902 convert_to_array_ex(&tmp
);
1904 arg
.ht
= &obj
->intern
->converters
;
1908 zend_hash_apply_with_argument(Z_ARRVAL_P(tmp
), apply_set_converter
, &arg TSRMLS_CC
);
1910 zval_ptr_dtor(&tmp
);
1911 zval_ptr_dtor(&zoids
);
1916 static zend_function_entry php_pqconn_methods
[] = {
1917 PHP_ME(pqconn
, __construct
, ai_pqconn_construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
1918 PHP_ME(pqconn
, reset
, ai_pqconn_reset
, ZEND_ACC_PUBLIC
)
1919 PHP_ME(pqconn
, resetAsync
, ai_pqconn_reset_async
, ZEND_ACC_PUBLIC
)
1920 PHP_ME(pqconn
, poll
, ai_pqconn_poll
, ZEND_ACC_PUBLIC
)
1921 PHP_ME(pqconn
, exec
, ai_pqconn_exec
, ZEND_ACC_PUBLIC
)
1922 PHP_ME(pqconn
, execAsync
, ai_pqconn_exec_async
, ZEND_ACC_PUBLIC
)
1923 PHP_ME(pqconn
, execParams
, ai_pqconn_exec_params
, ZEND_ACC_PUBLIC
)
1924 PHP_ME(pqconn
, execParamsAsync
, ai_pqconn_exec_params_async
, ZEND_ACC_PUBLIC
)
1925 PHP_ME(pqconn
, prepare
, ai_pqconn_prepare
, ZEND_ACC_PUBLIC
)
1926 PHP_ME(pqconn
, prepareAsync
, ai_pqconn_prepare_async
, ZEND_ACC_PUBLIC
)
1927 PHP_ME(pqconn
, declare
, ai_pqconn_declare
, ZEND_ACC_PUBLIC
)
1928 PHP_ME(pqconn
, declareAsync
, ai_pqconn_declare_async
, ZEND_ACC_PUBLIC
)
1929 PHP_ME(pqconn
, unlisten
, ai_pqconn_unlisten
, ZEND_ACC_PUBLIC
)
1930 PHP_ME(pqconn
, unlistenAsync
, ai_pqconn_unlisten_async
, ZEND_ACC_PUBLIC
)
1931 PHP_ME(pqconn
, listen
, ai_pqconn_listen
, ZEND_ACC_PUBLIC
)
1932 PHP_ME(pqconn
, listenAsync
, ai_pqconn_listen_async
, ZEND_ACC_PUBLIC
)
1933 PHP_ME(pqconn
, notify
, ai_pqconn_notify
, ZEND_ACC_PUBLIC
)
1934 PHP_ME(pqconn
, notifyAsync
, ai_pqconn_notify_async
, ZEND_ACC_PUBLIC
)
1935 PHP_ME(pqconn
, getResult
, ai_pqconn_get_result
, ZEND_ACC_PUBLIC
)
1936 PHP_ME(pqconn
, quote
, ai_pqconn_quote
, ZEND_ACC_PUBLIC
)
1937 PHP_ME(pqconn
, quoteName
, ai_pqconn_quote_name
, ZEND_ACC_PUBLIC
)
1938 PHP_ME(pqconn
, escapeBytea
, ai_pqconn_escape_bytea
, ZEND_ACC_PUBLIC
)
1939 PHP_ME(pqconn
, unescapeBytea
, ai_pqconn_unescape_bytea
, ZEND_ACC_PUBLIC
)
1940 PHP_ME(pqconn
, startTransaction
, ai_pqconn_start_transaction
, ZEND_ACC_PUBLIC
)
1941 PHP_ME(pqconn
, startTransactionAsync
, ai_pqconn_start_transaction_async
, ZEND_ACC_PUBLIC
)
1942 PHP_ME(pqconn
, trace
, ai_pqconn_trace
, ZEND_ACC_PUBLIC
)
1943 PHP_ME(pqconn
, off
, ai_pqconn_off
, ZEND_ACC_PUBLIC
)
1944 PHP_ME(pqconn
, on
, ai_pqconn_on
, ZEND_ACC_PUBLIC
)
1945 PHP_ME(pqconn
, setConverter
, ai_pqconn_set_converter
, ZEND_ACC_PUBLIC
)
1946 PHP_ME(pqconn
, unsetConverter
, ai_pqconn_unset_converter
, ZEND_ACC_PUBLIC
)
1950 PHP_MSHUTDOWN_FUNCTION(pqconn
)
1952 zend_hash_destroy(&php_pqconn_object_prophandlers
);
1956 PHP_MINIT_FUNCTION(pqconn
)
1958 zend_class_entry ce
= {0};
1959 php_pq_object_prophandler_t ph
= {0};
1961 INIT_NS_CLASS_ENTRY(ce
, "pq", "Connection", php_pqconn_methods
);
1962 php_pqconn_class_entry
= zend_register_internal_class_ex(&ce
, NULL
, NULL TSRMLS_CC
);
1963 php_pqconn_class_entry
->create_object
= php_pqconn_create_object
;
1965 memcpy(&php_pqconn_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
1966 php_pqconn_object_handlers
.read_property
= php_pq_object_read_prop
;
1967 php_pqconn_object_handlers
.write_property
= php_pq_object_write_prop
;
1968 php_pqconn_object_handlers
.clone_obj
= NULL
;
1969 php_pqconn_object_handlers
.get_property_ptr_ptr
= NULL
;
1970 php_pqconn_object_handlers
.get_gc
= NULL
;
1971 php_pqconn_object_handlers
.get_properties
= php_pq_object_properties
;
1972 php_pqconn_object_handlers
.get_debug_info
= php_pq_object_debug_info
;
1974 zend_hash_init(&php_pqconn_object_prophandlers
, 20, NULL
, NULL
, 1);
1976 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("status"), CONNECTION_BAD
, ZEND_ACC_PUBLIC TSRMLS_CC
);
1977 ph
.read
= php_pqconn_object_read_status
;
1978 zend_hash_add(&php_pqconn_object_prophandlers
, "status", sizeof("status"), (void *) &ph
, sizeof(ph
), NULL
);
1980 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("transactionStatus"), PQTRANS_UNKNOWN
, ZEND_ACC_PUBLIC TSRMLS_CC
);
1981 ph
.read
= php_pqconn_object_read_transaction_status
;
1982 zend_hash_add(&php_pqconn_object_prophandlers
, "transactionStatus", sizeof("transactionStatus"), (void *) &ph
, sizeof(ph
), NULL
);
1984 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("socket"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1985 ph
.read
= NULL
; /* forward to std prophandler */
1986 zend_hash_add(&php_pqconn_object_prophandlers
, "socket", sizeof("socket"), (void *) &ph
, sizeof(ph
), NULL
);
1988 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("errorMessage"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1989 ph
.read
= php_pqconn_object_read_error_message
;
1990 zend_hash_add(&php_pqconn_object_prophandlers
, "errorMessage", sizeof("errorMessage"), (void *) &ph
, sizeof(ph
), NULL
);
1992 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("busy"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
1993 ph
.read
= php_pqconn_object_read_busy
;
1994 zend_hash_add(&php_pqconn_object_prophandlers
, "busy", sizeof("busy"), (void *) &ph
, sizeof(ph
), NULL
);
1996 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("encoding"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1997 ph
.read
= php_pqconn_object_read_encoding
;
1998 ph
.write
= php_pqconn_object_write_encoding
;
1999 zend_hash_add(&php_pqconn_object_prophandlers
, "encoding", sizeof("encoding"), (void *) &ph
, sizeof(ph
), NULL
);
2002 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("unbuffered"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
2003 ph
.read
= php_pqconn_object_read_unbuffered
;
2004 ph
.write
= php_pqconn_object_write_unbuffered
;
2005 zend_hash_add(&php_pqconn_object_prophandlers
, "unbuffered", sizeof("unbuffered"), (void *) &ph
, sizeof(ph
), NULL
);
2008 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("db"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2009 ph
.read
= php_pqconn_object_read_db
;
2010 zend_hash_add(&php_pqconn_object_prophandlers
, "db", sizeof("db"), (void *) &ph
, sizeof(ph
), NULL
);
2012 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("user"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2013 ph
.read
= php_pqconn_object_read_user
;
2014 zend_hash_add(&php_pqconn_object_prophandlers
, "user", sizeof("user"), (void *) &ph
, sizeof(ph
), NULL
);
2016 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("pass"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2017 ph
.read
= php_pqconn_object_read_pass
;
2018 zend_hash_add(&php_pqconn_object_prophandlers
, "pass", sizeof("pass"), (void *) &ph
, sizeof(ph
), NULL
);
2020 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("host"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2021 ph
.read
= php_pqconn_object_read_host
;
2022 zend_hash_add(&php_pqconn_object_prophandlers
, "host", sizeof("host"), (void *) &ph
, sizeof(ph
), NULL
);
2024 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("port"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2025 ph
.read
= php_pqconn_object_read_port
;
2026 zend_hash_add(&php_pqconn_object_prophandlers
, "port", sizeof("port"), (void *) &ph
, sizeof(ph
), NULL
);
2029 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("params"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2030 ph
.read
= php_pqconn_object_read_params
;
2031 zend_hash_add(&php_pqconn_object_prophandlers
, "params", sizeof("params"), (void *) &ph
, sizeof(ph
), NULL
);
2034 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("options"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2035 ph
.read
= php_pqconn_object_read_options
;
2036 zend_hash_add(&php_pqconn_object_prophandlers
, "options", sizeof("options"), (void *) &ph
, sizeof(ph
), NULL
);
2038 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("eventHandlers"), ZEND_ACC_PUBLIC TSRMLS_CC
);
2039 ph
.read
= php_pqconn_object_read_event_handlers
;
2040 zend_hash_add(&php_pqconn_object_prophandlers
, "eventHandlers", sizeof("eventHandlers"), (void *) &ph
, sizeof(ph
), NULL
);
2042 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultFetchType"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
2043 ph
.read
= php_pqconn_object_read_def_fetch_type
;
2044 ph
.write
= php_pqconn_object_write_def_fetch_type
;
2045 zend_hash_add(&php_pqconn_object_prophandlers
, "defaultFetchType", sizeof("defaultFetchType"), (void *) &ph
, sizeof(ph
), NULL
);
2048 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionIsolation"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
2049 ph
.read
= php_pqconn_object_read_def_txn_isolation
;
2050 ph
.write
= php_pqconn_object_write_def_txn_isolation
;
2051 zend_hash_add(&php_pqconn_object_prophandlers
, "defaultTransactionIsolation", sizeof("defaultTransactionIsolation"), (void *) &ph
, sizeof(ph
), NULL
);
2054 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionReadonly"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
2055 ph
.read
= php_pqconn_object_read_def_txn_readonly
;
2056 ph
.write
= php_pqconn_object_write_def_txn_readonly
;
2057 zend_hash_add(&php_pqconn_object_prophandlers
, "defaultTransactionReadonly", sizeof("defaultTransactionReadonly"), (void *) &ph
, sizeof(ph
), NULL
);
2060 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("defaultTransactionDeferrable"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
2061 ph
.read
= php_pqconn_object_read_def_txn_deferrable
;
2062 ph
.write
= php_pqconn_object_write_def_txn_deferrable
;
2063 zend_hash_add(&php_pqconn_object_prophandlers
, "defaultTransactionDeferrable", sizeof("defaultTransactionDeferrable"), (void *) &ph
, sizeof(ph
), NULL
);
2066 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("defaultAutoConvert"), PHP_PQRES_CONV_ALL
, ZEND_ACC_PUBLIC TSRMLS_CC
);
2067 ph
.read
= php_pqconn_object_read_def_auto_conv
;
2068 ph
.write
= php_pqconn_object_write_def_auto_conv
;
2069 zend_hash_add(&php_pqconn_object_prophandlers
, "defaultAutoConvert", sizeof("defaultAutoConvert"), (void *) &ph
, sizeof(ph
), NULL
);
2072 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("OK"), CONNECTION_OK TSRMLS_CC
);
2073 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("BAD"), CONNECTION_BAD TSRMLS_CC
);
2074 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("STARTED"), CONNECTION_STARTED TSRMLS_CC
);
2075 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("MADE"), CONNECTION_MADE TSRMLS_CC
);
2076 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AWAITING_RESPONSE"), CONNECTION_AWAITING_RESPONSE TSRMLS_CC
);
2077 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AUTH_OK"), CONNECTION_AUTH_OK TSRMLS_CC
);
2078 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SSL_STARTUP"), CONNECTION_SSL_STARTUP TSRMLS_CC
);
2079 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SETENV"), CONNECTION_SETENV TSRMLS_CC
);
2081 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_IDLE"), PQTRANS_IDLE TSRMLS_CC
);
2082 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_ACTIVE"), PQTRANS_ACTIVE TSRMLS_CC
);
2083 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INTRANS"), PQTRANS_INTRANS TSRMLS_CC
);
2084 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INERROR"), PQTRANS_INERROR TSRMLS_CC
);
2085 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_UNKNOWN"), PQTRANS_UNKNOWN TSRMLS_CC
);
2087 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_FAILED"), PGRES_POLLING_FAILED TSRMLS_CC
);
2088 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_READING"), PGRES_POLLING_READING TSRMLS_CC
);
2089 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_WRITING"), PGRES_POLLING_WRITING TSRMLS_CC
);
2090 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_OK"), PGRES_POLLING_OK TSRMLS_CC
);
2092 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_NOTICE"), ZEND_STRL("notice") TSRMLS_CC
);
2093 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESULT"), ZEND_STRL("result") TSRMLS_CC
);
2094 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESET"), ZEND_STRL("reset") TSRMLS_CC
);
2096 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("ASYNC"), 0x1 TSRMLS_CC
);
2097 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("PERSISTENT"), 0x2 TSRMLS_CC
);
2107 * vim600: noet sw=4 ts=4 fdm=marker
2108 * vim<600: noet sw=4 ts=4