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
->converters
);
82 zend_hash_destroy(&obj
->intern
->eventhandlers
);
86 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
91 zend_object_value
php_pqconn_create_object_ex(zend_class_entry
*ce
, php_pqconn_t
*intern
, php_pqconn_object_t
**ptr TSRMLS_DC
)
93 php_pqconn_object_t
*o
;
95 o
= ecalloc(1, sizeof(*o
));
96 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
97 object_properties_init((zend_object
*) o
, ce
);
98 o
->prophandler
= &php_pqconn_object_prophandlers
;
108 o
->zv
.handle
= zend_objects_store_put((zend_object
*) o
, NULL
, php_pqconn_object_free
, NULL TSRMLS_CC
);
109 o
->zv
.handlers
= &php_pqconn_object_handlers
;
114 static zend_object_value
php_pqconn_create_object(zend_class_entry
*class_type TSRMLS_DC
)
116 return php_pqconn_create_object_ex(class_type
, NULL
, NULL TSRMLS_CC
);
119 static void php_pqconn_object_read_status(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
121 php_pqconn_object_t
*obj
= o
;
123 RETVAL_LONG(PQstatus(obj
->intern
->conn
));
126 static void php_pqconn_object_read_transaction_status(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
128 php_pqconn_object_t
*obj
= o
;
130 RETVAL_LONG(PQtransactionStatus(obj
->intern
->conn
));
133 static void php_pqconn_object_read_error_message(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
135 php_pqconn_object_t
*obj
= o
;
136 char *error
= PHP_PQerrorMessage(obj
->intern
->conn
);
139 RETVAL_STRING(error
, 1);
145 static int apply_notify_listener(void *p
, void *arg TSRMLS_DC
)
147 php_pq_callback_t
*listener
= p
;
149 zval
*zpid
, *zchannel
, *zmessage
;
152 ZVAL_LONG(zpid
, nfy
->be_pid
);
153 MAKE_STD_ZVAL(zchannel
);
154 ZVAL_STRING(zchannel
, nfy
->relname
, 1);
155 MAKE_STD_ZVAL(zmessage
);
156 ZVAL_STRING(zmessage
, nfy
->extra
, 1);
158 zend_fcall_info_argn(&listener
->fci TSRMLS_CC
, 3, &zchannel
, &zmessage
, &zpid
);
159 zend_fcall_info_call(&listener
->fci
, &listener
->fcc
, NULL
, NULL TSRMLS_CC
);
161 zval_ptr_dtor(&zchannel
);
162 zval_ptr_dtor(&zmessage
);
163 zval_ptr_dtor(&zpid
);
165 return ZEND_HASH_APPLY_KEEP
;
168 static int apply_notify_listeners(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
170 HashTable
*listeners
= p
;
171 PGnotify
*nfy
= va_arg(argv
, PGnotify
*);
173 if (0 == fnmatch(key
->arKey
, nfy
->relname
, 0)) {
174 zend_hash_apply_with_argument(listeners
, apply_notify_listener
, nfy TSRMLS_CC
);
177 return ZEND_HASH_APPLY_KEEP
;
180 void php_pqconn_notify_listeners(php_pqconn_object_t
*obj TSRMLS_DC
)
184 while ((nfy
= PQnotifies(obj
->intern
->conn
))) {
185 zend_hash_apply_with_arguments(&obj
->intern
->listeners TSRMLS_CC
, apply_notify_listeners
, 1, nfy
);
190 static void php_pqconn_object_read_busy(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
192 php_pqconn_object_t
*obj
= o
;
194 RETVAL_BOOL(PQisBusy(obj
->intern
->conn
));
197 static void php_pqconn_object_read_encoding(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
199 php_pqconn_object_t
*obj
= o
;
201 RETVAL_STRING(pg_encoding_to_char(PQclientEncoding(obj
->intern
->conn
)), 1);
204 static void php_pqconn_object_write_encoding(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
206 php_pqconn_object_t
*obj
= o
;
209 if (Z_TYPE_P(value
) != IS_STRING
) {
210 if (Z_REFCOUNT_P(value
) > 1) {
213 ZVAL_ZVAL(tmp
, zenc
, 1, 0);
214 convert_to_string(tmp
);
217 convert_to_string_ex(&zenc
);
221 if (0 > PQsetClientEncoding(obj
->intern
->conn
, Z_STRVAL_P(zenc
))) {
222 zend_error(E_NOTICE
, "Unrecognized encoding '%s'", Z_STRVAL_P(zenc
));
226 zval_ptr_dtor(&zenc
);
230 static void php_pqconn_object_read_unbuffered(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
232 php_pqconn_object_t
*obj
= o
;
234 RETVAL_BOOL(obj
->intern
->unbuffered
);
237 static void php_pqconn_object_write_unbuffered(zval
*object
, void *o
, zval
*value TSRMLS_DC
)
239 php_pqconn_object_t
*obj
= o
;
241 obj
->intern
->unbuffered
= z_is_true(value
);
244 static void php_pqconn_object_read_db(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
246 php_pqconn_object_t
*obj
= o
;
247 char *db
= PQdb(obj
->intern
->conn
);
250 RETVAL_STRING(db
, 1);
252 RETVAL_EMPTY_STRING();
256 static void php_pqconn_object_read_user(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
258 php_pqconn_object_t
*obj
= o
;
259 char *user
= PQuser(obj
->intern
->conn
);
262 RETVAL_STRING(user
, 1);
264 RETVAL_EMPTY_STRING();
268 static void php_pqconn_object_read_pass(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
270 php_pqconn_object_t
*obj
= o
;
271 char *pass
= PQpass(obj
->intern
->conn
);
274 RETVAL_STRING(pass
, 1);
276 RETVAL_EMPTY_STRING();
280 static void php_pqconn_object_read_host(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
282 php_pqconn_object_t
*obj
= o
;
283 char *host
= PQhost(obj
->intern
->conn
);
286 RETVAL_STRING(host
, 1);
288 RETVAL_EMPTY_STRING();
292 static void php_pqconn_object_read_port(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
294 php_pqconn_object_t
*obj
= o
;
295 char *port
= PQport(obj
->intern
->conn
);
298 RETVAL_STRING(port
, 1);
300 RETVAL_EMPTY_STRING();
304 static void php_pqconn_object_read_options(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
306 php_pqconn_object_t
*obj
= o
;
307 char *options
= PQoptions(obj
->intern
->conn
);
310 RETVAL_STRING(options
, 1);
312 RETVAL_EMPTY_STRING();
316 static int apply_read_event_handler_ex(void *p
, void *arg TSRMLS_DC
)
319 zval
*zcb
= php_pq_callback_to_zval(p
);
321 zend_hash_next_index_insert(rv
, &zcb
, sizeof(zval
*), NULL
);
323 return ZEND_HASH_APPLY_KEEP
;
326 static int apply_read_event_handlers(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
328 HashTable
*evhs
= p
, *rv
= va_arg(argv
, HashTable
*);
329 zval
*entry
, **entry_ptr
;
331 MAKE_STD_ZVAL(entry
);
332 array_init_size(entry
, zend_hash_num_elements(evhs
));
334 if (key
->nKeyLength
) {
335 zend_hash_add(rv
, key
->arKey
, key
->nKeyLength
, &entry
, sizeof(zval
*), (void *) &entry_ptr
);
337 zend_hash_index_update(rv
, key
->h
, &entry
, sizeof(zval
*), (void *) &entry_ptr
);
340 zend_hash_apply_with_argument(evhs
, apply_read_event_handler_ex
, Z_ARRVAL_PP(entry_ptr
) TSRMLS_CC
);
342 return ZEND_HASH_APPLY_KEEP
;
344 static void php_pqconn_object_read_event_handlers(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
346 php_pqconn_object_t
*obj
= o
;
348 array_init(return_value
);
349 zend_hash_apply_with_arguments(&obj
->intern
->eventhandlers TSRMLS_CC
, apply_read_event_handlers
, 1, Z_ARRVAL_P(return_value
) TSRMLS_CC
);
352 static STATUS
php_pqconn_update_socket(zval
*this_ptr
, php_pqconn_object_t
*obj TSRMLS_DC
)
354 zval
*zsocket
, zmember
;
360 obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
363 INIT_PZVAL(&zmember
);
364 ZVAL_STRINGL(&zmember
, "socket", sizeof("socket")-1, 0);
365 MAKE_STD_ZVAL(zsocket
);
367 if ((CONNECTION_BAD
!= PQstatus(obj
->intern
->conn
))
368 && (-1 < (socket
= PQsocket(obj
->intern
->conn
)))
369 && (stream
= php_stream_fopen_from_fd(socket
, "r+b", NULL
))) {
370 stream
->flags
|= PHP_STREAM_FLAG_NO_CLOSE
;
371 php_stream_to_zval(stream
, zsocket
);
377 zend_get_std_object_handlers()->write_property(getThis(), &zmember
, zsocket
, NULL TSRMLS_CC
);
378 zval_ptr_dtor(&zsocket
);
383 static void *php_pqconn_resource_factory_ctor(void *data
, void *init_arg TSRMLS_DC
)
385 php_pqconn_resource_factory_data_t
*o
= init_arg
;
386 PGconn
*conn
= NULL
;;
388 if (o
->flags
& PHP_PQCONN_ASYNC
) {
389 conn
= PQconnectStart(o
->dsn
);
391 conn
= PQconnectdb(o
->dsn
);
395 PQregisterEventProc(conn
, php_pqconn_event
, "ext-pq", NULL
);
401 static void php_pqconn_resource_factory_dtor(void *opaque
, void *handle TSRMLS_DC
)
403 php_pqconn_event_data_t
*evdata
= PQinstanceData(handle
, php_pqconn_event
);
405 /* we don't care for anything, except free'ing evdata */
407 PQsetInstanceData(handle
, php_pqconn_event
, NULL
);
408 memset(evdata
, 0, sizeof(*evdata
));
415 static php_resource_factory_ops_t php_pqconn_resource_factory_ops
= {
416 php_pqconn_resource_factory_ctor
,
418 php_pqconn_resource_factory_dtor
421 php_resource_factory_ops_t
*php_pqconn_get_resource_factory_ops(void)
423 return &php_pqconn_resource_factory_ops
;
426 static void php_pqconn_wakeup(php_persistent_handle_factory_t
*f
, void **handle TSRMLS_DC
)
428 // FIXME: ping server
431 static inline PGresult
*unlisten(PGconn
*conn
, const char *channel_str
, size_t channel_len TSRMLS_DC
)
433 char *quoted_channel
= PQescapeIdentifier(conn
, channel_str
, channel_len
);
434 PGresult
*res
= NULL
;
436 if (quoted_channel
) {
439 smart_str_appends(&cmd
, "UNLISTEN ");
440 smart_str_appends(&cmd
, quoted_channel
);
443 res
= PQexec(conn
, cmd
.c
);
445 smart_str_free(&cmd
);
446 PQfreemem(quoted_channel
);
452 static int apply_unlisten(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
454 php_pqconn_object_t
*obj
= va_arg(argv
, php_pqconn_object_t
*);
455 PGresult
*res
= unlisten(obj
->intern
->conn
, key
->arKey
, key
->nKeyLength
- 1);
461 return ZEND_HASH_APPLY_REMOVE
;
464 static void php_pqconn_retire(php_persistent_handle_factory_t
*f
, void **handle TSRMLS_DC
)
466 php_pqconn_event_data_t
*evdata
= PQinstanceData(*handle
, php_pqconn_event
);
471 PQsetInstanceData(*handle
, php_pqconn_event
, NULL
);
474 PQsetNoticeReceiver(*handle
, php_pqconn_notice_ignore
, NULL
);
476 /* cancel async queries */
477 if (PQisBusy(*handle
) && (cancel
= PQgetCancel(*handle
))) {
480 PQcancel(cancel
, err
, sizeof(err
));
481 PQfreeCancel(cancel
);
483 /* clean up async results */
484 while ((res
= PQgetResult(*handle
))) {
488 /* clean up transaction & session */
489 switch (PQtransactionStatus(*handle
)) {
491 res
= PQexec(*handle
, "RESET ALL");
494 res
= PQexec(*handle
, "ROLLBACK; RESET ALL");
503 /* clean up notify listeners */
504 zend_hash_apply_with_arguments(&evdata
->obj
->intern
->listeners TSRMLS_CC
, apply_unlisten
, 1, evdata
->obj
);
506 /* release instance data */
507 //memset(evdata, 0, sizeof(*evdata));
512 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_construct
, 0, 0, 1)
513 ZEND_ARG_INFO(0, dsn
)
514 ZEND_ARG_INFO(0, async
)
516 static PHP_METHOD(pqconn
, __construct
) {
517 zend_error_handling zeh
;
523 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
524 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sl", &dsn_str
, &dsn_len
, &flags
);
525 zend_restore_error_handling(&zeh TSRMLS_CC
);
528 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
531 throw_exce(EX_BAD_METHODCALL TSRMLS_CC
, "pq\\Connection already initialized");
533 php_pqconn_event_data_t
*evdata
= php_pqconn_event_data_init(obj TSRMLS_CC
);
534 php_pqconn_resource_factory_data_t rfdata
= {dsn_str
, flags
};
536 obj
->intern
= ecalloc(1, sizeof(*obj
->intern
));
538 zend_hash_init(&obj
->intern
->listeners
, 0, NULL
, (dtor_func_t
) zend_hash_destroy
, 0);
539 zend_hash_init(&obj
->intern
->converters
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
540 zend_hash_init(&obj
->intern
->eventhandlers
, 0, NULL
, (dtor_func_t
) zend_hash_destroy
, 0);
542 if (flags
& PHP_PQCONN_PERSISTENT
) {
543 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
);
544 php_resource_factory_init(&obj
->intern
->factory
, php_persistent_handle_get_resource_factory_ops(), phf
, (void (*)(void*)) php_persistent_handle_abandon
);
546 php_resource_factory_init(&obj
->intern
->factory
, &php_pqconn_resource_factory_ops
, NULL
, NULL
);
549 if (flags
& PHP_PQCONN_ASYNC
) {
550 obj
->intern
->poller
= (int (*)(PGconn
*)) PQconnectPoll
;
553 obj
->intern
->conn
= php_resource_factory_handle_ctor(&obj
->intern
->factory
, &rfdata TSRMLS_CC
);
555 PQsetInstanceData(obj
->intern
->conn
, php_pqconn_event
, evdata
);
556 PQsetNoticeReceiver(obj
->intern
->conn
, php_pqconn_notice_recv
, evdata
);
558 if (SUCCESS
!= php_pqconn_update_socket(getThis(), obj TSRMLS_CC
)) {
559 throw_exce(EX_CONNECTION_FAILED TSRMLS_CC
, "Connection failed (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
565 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset
, 0, 0, 0)
567 static PHP_METHOD(pqconn
, reset
) {
568 zend_error_handling zeh
;
571 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
572 rv
= zend_parse_parameters_none();
573 zend_restore_error_handling(&zeh TSRMLS_CC
);
576 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
579 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
581 PQreset(obj
->intern
->conn
);
583 if (CONNECTION_OK
!= PQstatus(obj
->intern
->conn
)) {
584 throw_exce(EX_CONNECTION_FAILED TSRMLS_CC
, "Connection reset failed: (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
587 php_pqconn_notify_listeners(obj TSRMLS_CC
);
592 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_reset_async
, 0, 0, 0)
594 static PHP_METHOD(pqconn
, resetAsync
) {
595 zend_error_handling zeh
;
598 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
599 rv
= zend_parse_parameters_none();
600 zend_restore_error_handling(&zeh TSRMLS_CC
);
603 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
606 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
608 if (!PQresetStart(obj
->intern
->conn
)) {
609 throw_exce(EX_IO TSRMLS_CC
, "Failed to start connection reset (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
611 obj
->intern
->poller
= (int (*)(PGconn
*)) PQresetPoll
;
614 php_pqconn_notify_listeners(obj TSRMLS_CC
);
619 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten
, 0, 0, 1)
620 ZEND_ARG_INFO(0, channel
)
622 static PHP_METHOD(pqconn
, unlisten
)
624 zend_error_handling zeh
;
629 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
630 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &channel_str
, &channel_len
);
631 zend_restore_error_handling(&zeh TSRMLS_CC
);
634 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
637 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
638 } else if (SUCCESS
== zend_hash_del(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1)) {
639 PGresult
*res
= unlisten(obj
->intern
->conn
, channel_str
, channel_len
);
642 php_pqres_success(res TSRMLS_CC
);
649 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unlisten_async
, 0, 0, 1)
650 ZEND_ARG_INFO(0, channel
)
652 static PHP_METHOD(pqconn
, unlistenAsync
) {
653 zend_error_handling zeh
;
658 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
659 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &channel_str
, &channel_len
);
660 zend_restore_error_handling(&zeh TSRMLS_CC
);
663 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
666 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
668 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
670 if (!quoted_channel
) {
671 throw_exce(EX_ESCAPE TSRMLS_CC
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
675 smart_str_appends(&cmd
, "UNLISTEN ");
676 smart_str_appends(&cmd
, quoted_channel
);
679 if (!PQsendQuery(obj
->intern
->conn
, cmd
.c
)) {
680 throw_exce(EX_IO TSRMLS_CC
, "Failed to uninstall listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
682 obj
->intern
->poller
= PQconsumeInput
;
683 zend_hash_del(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1);
686 smart_str_free(&cmd
);
687 PQfreemem(quoted_channel
);
688 php_pqconn_notify_listeners(obj TSRMLS_CC
);
694 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
)
696 HashTable ht
, *existing_listeners
;
698 php_pq_callback_addref(listener
);
700 if (SUCCESS
== zend_hash_find(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1, (void *) &existing_listeners
)) {
701 zend_hash_next_index_insert(existing_listeners
, (void *) listener
, sizeof(*listener
), NULL
);
703 zend_hash_init(&ht
, 1, NULL
, (dtor_func_t
) php_pq_callback_dtor
, 0);
704 zend_hash_next_index_insert(&ht
, (void *) listener
, sizeof(*listener
), NULL
);
705 zend_hash_add(&obj
->intern
->listeners
, channel_str
, channel_len
+ 1, (void *) &ht
, sizeof(HashTable
), NULL
);
709 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen
, 0, 0, 2)
710 ZEND_ARG_INFO(0, channel
)
711 ZEND_ARG_INFO(0, callable
)
713 static PHP_METHOD(pqconn
, listen
) {
714 zend_error_handling zeh
;
715 char *channel_str
= NULL
;
717 php_pq_callback_t listener
= {{0}};
720 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
721 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
722 zend_restore_error_handling(&zeh TSRMLS_CC
);
725 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
728 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
730 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
732 if (!quoted_channel
) {
733 throw_exce(EX_ESCAPE TSRMLS_CC
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
738 smart_str_appends(&cmd
, "LISTEN ");
739 smart_str_appends(&cmd
, quoted_channel
);
742 res
= PQexec(obj
->intern
->conn
, cmd
.c
);
744 smart_str_free(&cmd
);
745 PQfreemem(quoted_channel
);
748 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
750 if (SUCCESS
== php_pqres_success(res TSRMLS_CC
)) {
751 obj
->intern
->poller
= PQconsumeInput
;
752 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener TSRMLS_CC
);
757 php_pqconn_notify_listeners(obj TSRMLS_CC
);
763 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_listen_async
, 0, 0, 0)
764 ZEND_ARG_INFO(0, channel
)
765 ZEND_ARG_INFO(0, callable
)
767 static PHP_METHOD(pqconn
, listenAsync
) {
768 zend_error_handling zeh
;
769 char *channel_str
= NULL
;
771 php_pq_callback_t listener
= {{0}};
774 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
775 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sf", &channel_str
, &channel_len
, &listener
.fci
, &listener
.fcc
);
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");
784 char *quoted_channel
= PQescapeIdentifier(obj
->intern
->conn
, channel_str
, channel_len
);
786 if (!quoted_channel
) {
787 throw_exce(EX_ESCAPE TSRMLS_CC
, "Failed to escape channel identifier (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
791 smart_str_appends(&cmd
, "LISTEN ");
792 smart_str_appends(&cmd
, quoted_channel
);
795 if (!PQsendQuery(obj
->intern
->conn
, cmd
.c
)) {
796 throw_exce(EX_IO TSRMLS_CC
, "Failed to install listener (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
798 obj
->intern
->poller
= PQconsumeInput
;
799 php_pqconn_add_listener(obj
, channel_str
, channel_len
, &listener TSRMLS_CC
);
802 smart_str_free(&cmd
);
803 PQfreemem(quoted_channel
);
804 php_pqconn_notify_listeners(obj TSRMLS_CC
);
810 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify
, 0, 0, 2)
811 ZEND_ARG_INFO(0, channel
)
812 ZEND_ARG_INFO(0, message
)
814 static PHP_METHOD(pqconn
, notify
) {
815 zend_error_handling zeh
;
816 char *channel_str
, *message_str
;
817 int channel_len
, message_len
;
820 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
821 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
822 zend_restore_error_handling(&zeh TSRMLS_CC
);
825 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
828 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
831 char *params
[2] = {channel_str
, message_str
};
833 res
= PQexecParams(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0);
836 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
838 php_pqres_success(res TSRMLS_CC
);
842 php_pqconn_notify_listeners(obj TSRMLS_CC
);
847 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_notify_async
, 0, 0, 2)
848 ZEND_ARG_INFO(0, channel
)
849 ZEND_ARG_INFO(0, message
)
851 static PHP_METHOD(pqconn
, notifyAsync
) {
852 zend_error_handling zeh
;
853 char *channel_str
, *message_str
;
854 int channel_len
, message_len
;
857 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
858 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss", &channel_str
, &channel_len
, &message_str
, &message_len
);
859 zend_restore_error_handling(&zeh TSRMLS_CC
);
862 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
865 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
867 char *params
[2] = {channel_str
, message_str
};
869 if (!PQsendQueryParams(obj
->intern
->conn
, "select pg_notify($1, $2)", 2, NULL
, (const char *const*) params
, NULL
, NULL
, 0)) {
870 throw_exce(EX_IO TSRMLS_CC
, "Failed to notify listeners (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
872 obj
->intern
->poller
= PQconsumeInput
;
875 php_pqconn_notify_listeners(obj TSRMLS_CC
);
880 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_poll
, 0, 0, 0)
882 static PHP_METHOD(pqconn
, poll
) {
883 zend_error_handling zeh
;
886 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
887 rv
= zend_parse_parameters_none();
888 zend_restore_error_handling(&zeh TSRMLS_CC
);
891 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
894 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
895 } else if (!obj
->intern
->poller
) {
896 throw_exce(EX_RUNTIME TSRMLS_CC
, "No asynchronous operation active");
898 if (obj
->intern
->poller
== PQconsumeInput
) {
899 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
) * PGRES_POLLING_OK
);
901 RETVAL_LONG(obj
->intern
->poller(obj
->intern
->conn
));
903 php_pqconn_notify_listeners(obj TSRMLS_CC
);
908 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec
, 0, 0, 1)
909 ZEND_ARG_INFO(0, query
)
911 static PHP_METHOD(pqconn
, exec
) {
912 zend_error_handling zeh
;
917 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
918 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &query_str
, &query_len
);
919 zend_restore_error_handling(&zeh TSRMLS_CC
);
922 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
925 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
927 PGresult
*res
= PQexec(obj
->intern
->conn
, query_str
);
930 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
931 } else if (SUCCESS
== php_pqres_success(res TSRMLS_CC
)) {
932 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), &return_value TSRMLS_CC
);
937 php_pqconn_notify_listeners(obj TSRMLS_CC
);
942 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_get_result
, 0, 0, 0)
944 static PHP_METHOD(pqconn
, getResult
) {
945 zend_error_handling zeh
;
948 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
949 rv
= zend_parse_parameters_none();
950 zend_restore_error_handling(&zeh TSRMLS_CC
);
953 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
956 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
958 PGresult
*res
= PQgetResult(obj
->intern
->conn
);
963 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), &return_value TSRMLS_CC
);
966 php_pqconn_notify_listeners(obj TSRMLS_CC
);
971 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_async
, 0, 0, 1)
972 ZEND_ARG_INFO(0, query
)
973 ZEND_ARG_INFO(0, callable
)
975 static PHP_METHOD(pqconn
, execAsync
) {
976 zend_error_handling zeh
;
977 php_pq_callback_t resolver
= {{0}};
982 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
983 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|f", &query_str
, &query_len
, &resolver
.fci
, &resolver
.fcc
);
984 zend_restore_error_handling(&zeh TSRMLS_CC
);
987 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
990 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
991 } else if (!PQsendQuery(obj
->intern
->conn
, query_str
)) {
992 throw_exce(EX_IO TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
993 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
994 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
996 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver TSRMLS_CC
);
997 obj
->intern
->poller
= PQconsumeInput
;
998 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1003 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params
, 0, 0, 2)
1004 ZEND_ARG_INFO(0, query
)
1005 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1006 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1007 ZEND_END_ARG_INFO();
1008 static PHP_METHOD(pqconn
, execParams
) {
1009 zend_error_handling zeh
;
1013 zval
*ztypes
= NULL
;
1016 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1017 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa/|a/!", &query_str
, &query_len
, &zparams
, &ztypes
);
1018 zend_restore_error_handling(&zeh TSRMLS_CC
);
1020 if (SUCCESS
== rv
) {
1021 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1024 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1027 php_pq_params_t
*params
;
1029 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
) TSRMLS_CC
);
1030 res
= PQexecParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1031 php_pq_params_free(¶ms
);
1034 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1036 if (SUCCESS
== php_pqres_success(res TSRMLS_CC
)) {
1037 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), &return_value TSRMLS_CC
);
1042 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1048 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_exec_params_async
, 0, 0, 2)
1049 ZEND_ARG_INFO(0, query
)
1050 ZEND_ARG_ARRAY_INFO(0, params
, 0)
1051 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1052 ZEND_ARG_INFO(0, callable
)
1053 ZEND_END_ARG_INFO();
1054 static PHP_METHOD(pqconn
, execParamsAsync
) {
1055 zend_error_handling zeh
;
1056 php_pq_callback_t resolver
= {{0}};
1060 zval
*ztypes
= NULL
;
1063 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1064 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa/|a/!f", &query_str
, &query_len
, &zparams
, &ztypes
, &resolver
.fci
, &resolver
.fcc
);
1065 zend_restore_error_handling(&zeh TSRMLS_CC
);
1067 if (SUCCESS
== rv
) {
1068 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1071 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1074 php_pq_params_t
*params
;
1076 params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, Z_ARRVAL_P(zparams
) TSRMLS_CC
);
1077 rc
= PQsendQueryParams(obj
->intern
->conn
, query_str
, params
->param
.count
, params
->type
.oids
, (const char *const*) params
->param
.strings
, NULL
, NULL
, 0);
1078 php_pq_params_free(¶ms
);
1081 throw_exce(EX_IO TSRMLS_CC
, "Failed to execute query (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1082 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1083 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1085 php_pq_callback_recurse(&obj
->intern
->onevent
, &resolver TSRMLS_CC
);
1086 obj
->intern
->poller
= PQconsumeInput
;
1087 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1091 zend_restore_error_handling(&zeh TSRMLS_CC
);
1094 STATUS
php_pqconn_prepare(zval
*object
, php_pqconn_object_t
*obj
, const char *name
, const char *query
, php_pq_params_t
*params TSRMLS_DC
)
1100 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1103 res
= PQprepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
);
1107 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1109 rv
= php_pqres_success(res TSRMLS_CC
);
1111 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1117 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare
, 0, 0, 2)
1118 ZEND_ARG_INFO(0, name
)
1119 ZEND_ARG_INFO(0, query
)
1120 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1121 ZEND_END_ARG_INFO();
1122 static PHP_METHOD(pqconn
, prepare
) {
1123 zend_error_handling zeh
;
1124 zval
*ztypes
= NULL
;
1125 char *name_str
, *query_str
;
1126 int name_len
, *query_len
;
1129 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1130 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1131 zend_restore_error_handling(&zeh TSRMLS_CC
);
1133 if (SUCCESS
== rv
) {
1134 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1137 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1139 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL TSRMLS_CC
);
1141 if (SUCCESS
!= php_pqconn_prepare(getThis(), obj
, name_str
, query_str
, params TSRMLS_CC
)) {
1142 php_pq_params_free(¶ms
);
1144 php_pqstm_t
*stm
= ecalloc(1, sizeof(*stm
));
1146 php_pq_object_addref(obj TSRMLS_CC
);
1148 stm
->name
= estrdup(name_str
);
1149 stm
->params
= params
;
1150 ZEND_INIT_SYMTABLE(&stm
->bound
);
1152 return_value
->type
= IS_OBJECT
;
1153 return_value
->value
.obj
= php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
, NULL TSRMLS_CC
);
1159 STATUS
php_pqconn_prepare_async(zval
*object
, php_pqconn_object_t
*obj
, const char *name
, const char *query
, php_pq_params_t
*params TSRMLS_DC
)
1164 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1167 if (!PQsendPrepare(obj
->intern
->conn
, name
, query
, params
->type
.count
, params
->type
.oids
)) {
1169 throw_exce(EX_IO TSRMLS_CC
, "Failed to prepare statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1170 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1172 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1175 obj
->intern
->poller
= PQconsumeInput
;
1176 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1182 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_prepare_async
, 0, 0, 2)
1183 ZEND_ARG_INFO(0, name
)
1184 ZEND_ARG_INFO(0, query
)
1185 ZEND_ARG_ARRAY_INFO(0, types
, 1)
1186 ZEND_END_ARG_INFO();
1187 static PHP_METHOD(pqconn
, prepareAsync
) {
1188 zend_error_handling zeh
;
1189 zval
*ztypes
= NULL
;
1190 char *name_str
, *query_str
;
1191 int name_len
, *query_len
;
1194 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1195 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!", &name_str
, &name_len
, &query_str
, &query_len
, &ztypes
);
1196 zend_restore_error_handling(&zeh TSRMLS_CC
);
1198 if (SUCCESS
== rv
) {
1199 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1202 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1204 php_pq_params_t
*params
= php_pq_params_init(&obj
->intern
->converters
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL
, NULL TSRMLS_CC
);
1206 if (SUCCESS
!= php_pqconn_prepare_async(getThis(), obj
, name_str
, query_str
, params TSRMLS_CC
)) {
1207 php_pq_params_free(¶ms
);
1209 php_pqstm_t
*stm
= ecalloc(1, sizeof(*stm
));
1211 php_pq_object_addref(obj TSRMLS_CC
);
1213 stm
->name
= estrdup(name_str
);
1214 stm
->params
= params
;
1215 ZEND_INIT_SYMTABLE(&stm
->bound
);
1217 return_value
->type
= IS_OBJECT
;
1218 return_value
->value
.obj
= php_pqstm_create_object_ex(php_pqstm_class_entry
, stm
, NULL TSRMLS_CC
);
1224 static inline char *declare_str(const char *name_str
, size_t name_len
, unsigned flags
, const char *query_str
, size_t query_len
)
1226 size_t decl_len
= name_len
+ query_len
+ sizeof("DECLARE BINARY INSENSITIVE NO SCROLL CURSOR WITHOUT HOLD FOR ");
1229 decl_str
= emalloc(decl_len
);
1230 decl_len
= slprintf(decl_str
, decl_len
, "DECLARE %s %s %s %s CURSOR %s FOR %s",
1232 (flags
& PHP_PQ_DECLARE_BINARY
) ? "BINARY" : "",
1233 (flags
& PHP_PQ_DECLARE_INSENSITIVE
) ? "INSENSITIVE" : "",
1234 (flags
& PHP_PQ_DECLARE_NO_SCROLL
) ? "NO SCROLL" :
1235 (flags
& PHP_PQ_DECLARE_SCROLL
) ? "SCROLL" : "",
1236 (flags
& PHP_PQ_DECLARE_WITH_HOLD
) ? "WITH HOLD" : "",
1242 STATUS
php_pqconn_declare(zval
*object
, php_pqconn_object_t
*obj
, const char *decl TSRMLS_DC
)
1248 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1251 res
= PQexec(obj
->intern
->conn
, decl
);
1255 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1257 rv
= php_pqres_success(res TSRMLS_CC
);
1259 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1265 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare
, 0, 0, 3)
1266 ZEND_ARG_INFO(0, name
)
1267 ZEND_ARG_INFO(0, flags
)
1268 ZEND_ARG_INFO(0, query
)
1269 ZEND_END_ARG_INFO();
1270 static PHP_METHOD(pqconn
, declare
) {
1271 zend_error_handling zeh
;
1272 char *name_str
, *query_str
;
1273 int name_len
, query_len
;
1277 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1278 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1279 zend_restore_error_handling(&zeh TSRMLS_CC
);
1281 if (SUCCESS
== rv
) {
1282 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1285 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1287 char *decl
= declare_str(name_str
, name_len
, flags
, query_str
, query_len
);
1289 if (SUCCESS
!= php_pqconn_declare(getThis(), obj
, decl TSRMLS_CC
)) {
1292 php_pqcur_t
*cur
= ecalloc(1, sizeof(*cur
));
1294 php_pq_object_addref(obj TSRMLS_CC
);
1297 cur
->name
= estrdup(name_str
);
1300 return_value
->type
= IS_OBJECT
;
1301 return_value
->value
.obj
= php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
, NULL TSRMLS_CC
);
1307 STATUS
php_pqconn_declare_async(zval
*object
, php_pqconn_object_t
*obj
, const char *decl TSRMLS_DC
)
1312 obj
= zend_object_store_get_object(object TSRMLS_CC
);
1315 if (!PQsendQuery(obj
->intern
->conn
, decl
)) {
1317 throw_exce(EX_IO TSRMLS_CC
, "Failed to declare cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1318 } else if (obj
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
)) {
1320 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1323 obj
->intern
->poller
= PQconsumeInput
;
1324 php_pqconn_notify_listeners(obj TSRMLS_CC
);
1330 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_declare_async
, 0, 0, 2)
1331 ZEND_ARG_INFO(0, name
)
1332 ZEND_ARG_INFO(0, flags
)
1333 ZEND_ARG_INFO(0, query
)
1334 ZEND_END_ARG_INFO();
1335 static PHP_METHOD(pqconn
, declareAsync
) {
1336 zend_error_handling zeh
;
1337 char *name_str
, *query_str
;
1338 int name_len
, query_len
;
1342 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1343 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sls", &name_str
, &name_len
, &flags
, &query_str
, &query_len
);
1344 zend_restore_error_handling(&zeh TSRMLS_CC
);
1346 if (SUCCESS
== rv
) {
1347 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1350 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1352 char *decl
= declare_str(name_str
, name_len
, flags
, query_str
, query_len
);
1354 if (SUCCESS
!= php_pqconn_declare_async(getThis(), obj
, decl TSRMLS_CC
)) {
1357 php_pqcur_t
*cur
= ecalloc(1, sizeof(*cur
));
1359 php_pq_object_addref(obj TSRMLS_CC
);
1362 cur
->name
= estrdup(name_str
);
1365 return_value
->type
= IS_OBJECT
;
1366 return_value
->value
.obj
= php_pqcur_create_object_ex(php_pqcur_class_entry
, cur
, NULL TSRMLS_CC
);
1372 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote
, 0, 0, 1)
1373 ZEND_ARG_INFO(0, string
)
1374 ZEND_END_ARG_INFO();
1375 static PHP_METHOD(pqconn
, quote
) {
1379 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1380 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1383 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1385 char *quoted
= PQescapeLiteral(obj
->intern
->conn
, str
, len
);
1388 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to quote string (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1391 RETVAL_STRING(quoted
, 1);
1398 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_quote_name
, 0, 0, 1)
1399 ZEND_ARG_INFO(0, type
)
1400 ZEND_END_ARG_INFO();
1401 static PHP_METHOD(pqconn
, quoteName
) {
1405 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1406 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1409 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1411 char *quoted
= PQescapeIdentifier(obj
->intern
->conn
, str
, len
);
1414 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to quote name (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1417 RETVAL_STRING(quoted
, 1);
1424 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_escape_bytea
, 0, 0, 1)
1425 ZEND_ARG_INFO(0, bytea
)
1426 ZEND_END_ARG_INFO();
1427 static PHP_METHOD(pqconn
, escapeBytea
) {
1431 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1432 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1435 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1438 char *escaped_str
= (char *) PQescapeByteaConn(obj
->intern
->conn
, (unsigned char *) str
, len
, &escaped_len
);
1441 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to escape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1444 RETVAL_STRINGL(escaped_str
, escaped_len
- 1, 1);
1445 PQfreemem(escaped_str
);
1451 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unescape_bytea
, 0, 0, 1)
1452 ZEND_ARG_INFO(0, bytea
)
1453 ZEND_END_ARG_INFO();
1454 static PHP_METHOD(pqconn
, unescapeBytea
) {
1458 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1459 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1462 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1464 size_t unescaped_len
;
1465 char *unescaped_str
= (char *) PQunescapeBytea((unsigned char *)str
, &unescaped_len
);
1467 if (!unescaped_str
) {
1468 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to unescape bytea (%s)", PHP_PQerrorMessage(obj
->intern
->conn
));
1471 RETVAL_STRINGL(unescaped_str
, unescaped_len
, 1);
1472 PQfreemem(unescaped_str
);
1478 STATUS
php_pqconn_start_transaction(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable TSRMLS_DC
)
1480 STATUS rv
= FAILURE
;
1483 conn_obj
= zend_object_store_get_object(zconn TSRMLS_CC
);
1486 if (!conn_obj
->intern
) {
1487 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1490 smart_str cmd
= {0};
1491 const char *il
= isolation_level(&isolation
);
1493 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1494 smart_str_appends(&cmd
, il
);
1495 smart_str_appends(&cmd
, ", READ ");
1496 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1497 smart_str_appends(&cmd
, ",");
1498 smart_str_appends(&cmd
, deferrable
? "" : " NOT");
1499 smart_str_appends(&cmd
, " DEFERRABLE");
1502 res
= PQexec(conn_obj
->intern
->conn
, cmd
.c
);
1505 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1507 rv
= php_pqres_success(res TSRMLS_CC
);
1509 php_pqconn_notify_listeners(conn_obj TSRMLS_CC
);
1512 smart_str_free(&cmd
);
1518 STATUS
php_pqconn_start_transaction_async(zval
*zconn
, php_pqconn_object_t
*conn_obj
, long isolation
, zend_bool readonly
, zend_bool deferrable TSRMLS_DC
)
1520 STATUS rv
= FAILURE
;
1523 conn_obj
= zend_object_store_get_object(zconn TSRMLS_CC
);
1526 if (!conn_obj
->intern
) {
1527 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1529 smart_str cmd
= {0};
1530 const char *il
= isolation_level(&isolation
);
1532 smart_str_appends(&cmd
, "START TRANSACTION ISOLATION LEVEL ");
1533 smart_str_appends(&cmd
, il
);
1534 smart_str_appends(&cmd
, ", READ ");
1535 smart_str_appends(&cmd
, readonly
? "ONLY" : "WRITE");
1536 smart_str_appends(&cmd
, ",");
1537 smart_str_appends(&cmd
, deferrable
? "" : "NOT ");
1538 smart_str_appends(&cmd
, " DEFERRABLE");
1541 if (!PQsendQuery(conn_obj
->intern
->conn
, cmd
.c
)) {
1542 throw_exce(EX_IO TSRMLS_CC
, "Failed to start transaction (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
1545 conn_obj
->intern
->poller
= PQconsumeInput
;
1546 php_pqconn_notify_listeners(conn_obj TSRMLS_CC
);
1549 smart_str_free(&cmd
);
1555 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction
, 0, 0, 0)
1556 ZEND_ARG_INFO(0, isolation
)
1557 ZEND_ARG_INFO(0, readonly
)
1558 ZEND_ARG_INFO(0, deferrable
)
1559 ZEND_END_ARG_INFO();
1560 static PHP_METHOD(pqconn
, startTransaction
) {
1561 zend_error_handling zeh
;
1562 long isolation
= PHP_PQTXN_READ_COMMITTED
;
1563 zend_bool readonly
= 0, deferrable
= 0;
1566 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1567 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lbb", &isolation
, &readonly
, &deferrable
);
1568 zend_restore_error_handling(&zeh TSRMLS_CC
);
1570 if (SUCCESS
== rv
) {
1571 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1573 rv
= php_pqconn_start_transaction(getThis(), obj
, isolation
, readonly
, deferrable TSRMLS_CC
);
1575 if (SUCCESS
== rv
) {
1576 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1578 php_pq_object_addref(obj TSRMLS_CC
);
1581 txn
->isolation
= isolation
;
1582 txn
->readonly
= readonly
;
1583 txn
->deferrable
= deferrable
;
1585 return_value
->type
= IS_OBJECT
;
1586 return_value
->value
.obj
= php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
, NULL TSRMLS_CC
);
1591 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_start_transaction_async
, 0, 0, 0)
1592 ZEND_ARG_INFO(0, isolation
)
1593 ZEND_ARG_INFO(0, readonly
)
1594 ZEND_ARG_INFO(0, deferrable
)
1595 ZEND_END_ARG_INFO();
1596 static PHP_METHOD(pqconn
, startTransactionAsync
) {
1597 zend_error_handling zeh
;
1598 long isolation
= PHP_PQTXN_READ_COMMITTED
;
1599 zend_bool readonly
= 0, deferrable
= 0;
1602 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1603 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lbb", &isolation
, &readonly
, &deferrable
);
1604 zend_restore_error_handling(&zeh TSRMLS_CC
);
1605 if (SUCCESS
== rv
) {
1606 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1608 rv
= php_pqconn_start_transaction_async(getThis(), obj
, isolation
, readonly
, deferrable TSRMLS_CC
);
1610 if (SUCCESS
== rv
) {
1611 php_pqtxn_t
*txn
= ecalloc(1, sizeof(*txn
));
1613 php_pq_object_addref(obj TSRMLS_CC
);
1615 txn
->isolation
= isolation
;
1616 txn
->readonly
= readonly
;
1617 txn
->deferrable
= deferrable
;
1619 return_value
->type
= IS_OBJECT
;
1620 return_value
->value
.obj
= php_pqtxn_create_object_ex(php_pqtxn_class_entry
, txn
, NULL TSRMLS_CC
);
1625 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_trace
, 0, 0, 0)
1626 ZEND_ARG_INFO(0, stdio_stream
)
1627 ZEND_END_ARG_INFO();
1628 static PHP_METHOD(pqconn
, trace
) {
1629 zval
*zstream
= NULL
;
1631 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|r!", &zstream
)) {
1632 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1635 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1638 PQuntrace(obj
->intern
->conn
);
1642 php_stream
*stream
= NULL
;
1644 php_stream_from_zval(stream
, &zstream
);
1646 if (SUCCESS
!= php_stream_cast(stream
, PHP_STREAM_AS_STDIO
, (void *) &fp
, REPORT_ERRORS
)) {
1649 stream
->flags
|= PHP_STREAM_FLAG_NO_CLOSE
;
1650 PQtrace(obj
->intern
->conn
, fp
);
1658 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_off
, 0, 0, 1)
1659 ZEND_ARG_INFO(0, type
)
1660 ZEND_END_ARG_INFO();
1661 static PHP_METHOD(pqconn
, off
) {
1662 zend_error_handling zeh
;
1667 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1668 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &type_str
, &type_len
);
1669 zend_restore_error_handling(&zeh TSRMLS_CC
);
1671 if (SUCCESS
== rv
) {
1672 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1675 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1677 RETURN_BOOL(SUCCESS
== zend_hash_del(&obj
->intern
->eventhandlers
, type_str
, type_len
+ 1));
1682 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_on
, 0, 0, 2)
1683 ZEND_ARG_INFO(0, type
)
1684 ZEND_ARG_INFO(0, callable
)
1685 ZEND_END_ARG_INFO();
1686 static PHP_METHOD(pqconn
, on
) {
1687 zend_error_handling zeh
;
1690 php_pq_callback_t cb
= {{0}};
1693 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1694 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sf", &type_str
, &type_len
, &cb
.fci
, &cb
.fcc
);
1695 zend_restore_error_handling(&zeh TSRMLS_CC
);
1697 if (SUCCESS
== rv
) {
1698 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1701 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1703 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1705 RETVAL_LONG(php_pqconn_add_eventhandler(obj
, type_str
, type_len
, &cb TSRMLS_CC
));
1710 static int apply_set_converter(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
1712 zval
*tmp
, **zoid
= p
, **zcnv
= va_arg(argv
, zval
**);
1713 HashTable
*converters
= va_arg(argv
, HashTable
*);
1716 convert_to_long_ex(&tmp
);
1718 zend_hash_index_update(converters
, Z_LVAL_P(tmp
), zcnv
, sizeof(zval
*), NULL
);
1720 zval_ptr_dtor(&tmp
);
1723 return ZEND_HASH_APPLY_KEEP
;
1726 ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_set_converter
, 0, 0, 1)
1727 ZEND_ARG_OBJ_INFO(0, converter
, pq
\\ConverterInterface
, 0)
1728 ZEND_END_ARG_INFO();
1729 static PHP_METHOD(pqconn
, setConverter
) {
1731 zend_error_handling zeh
;
1734 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
1735 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "O", &zcnv
, php_pqconv_class_entry
);
1736 zend_restore_error_handling(&zeh TSRMLS_CC
);
1738 if (SUCCESS
== rv
) {
1739 php_pqconn_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1742 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
1744 zval
*tmp
, *zoids
= NULL
;
1746 zend_call_method_with_0_params(&zcnv
, NULL
, NULL
, "converttypes", &zoids
);
1748 convert_to_array_ex(&zoids
);
1749 zend_hash_apply_with_arguments(Z_ARRVAL_P(zoids
) TSRMLS_CC
, apply_set_converter
, 2, &zcnv
, &obj
->intern
->converters
);
1751 zval_ptr_dtor(&tmp
);
1753 zval_ptr_dtor(&zoids
);
1758 static zend_function_entry php_pqconn_methods
[] = {
1759 PHP_ME(pqconn
, __construct
, ai_pqconn_construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
1760 PHP_ME(pqconn
, reset
, ai_pqconn_reset
, ZEND_ACC_PUBLIC
)
1761 PHP_ME(pqconn
, resetAsync
, ai_pqconn_reset_async
, ZEND_ACC_PUBLIC
)
1762 PHP_ME(pqconn
, poll
, ai_pqconn_poll
, ZEND_ACC_PUBLIC
)
1763 PHP_ME(pqconn
, exec
, ai_pqconn_exec
, ZEND_ACC_PUBLIC
)
1764 PHP_ME(pqconn
, execAsync
, ai_pqconn_exec_async
, ZEND_ACC_PUBLIC
)
1765 PHP_ME(pqconn
, execParams
, ai_pqconn_exec_params
, ZEND_ACC_PUBLIC
)
1766 PHP_ME(pqconn
, execParamsAsync
, ai_pqconn_exec_params_async
, ZEND_ACC_PUBLIC
)
1767 PHP_ME(pqconn
, prepare
, ai_pqconn_prepare
, ZEND_ACC_PUBLIC
)
1768 PHP_ME(pqconn
, prepareAsync
, ai_pqconn_prepare_async
, ZEND_ACC_PUBLIC
)
1769 PHP_ME(pqconn
, declare
, ai_pqconn_declare
, ZEND_ACC_PUBLIC
)
1770 PHP_ME(pqconn
, declareAsync
, ai_pqconn_declare_async
, ZEND_ACC_PUBLIC
)
1771 PHP_ME(pqconn
, unlisten
, ai_pqconn_unlisten
, ZEND_ACC_PUBLIC
)
1772 PHP_ME(pqconn
, unlistenAsync
, ai_pqconn_unlisten_async
, ZEND_ACC_PUBLIC
)
1773 PHP_ME(pqconn
, listen
, ai_pqconn_listen
, ZEND_ACC_PUBLIC
)
1774 PHP_ME(pqconn
, listenAsync
, ai_pqconn_listen_async
, ZEND_ACC_PUBLIC
)
1775 PHP_ME(pqconn
, notify
, ai_pqconn_notify
, ZEND_ACC_PUBLIC
)
1776 PHP_ME(pqconn
, notifyAsync
, ai_pqconn_notify_async
, ZEND_ACC_PUBLIC
)
1777 PHP_ME(pqconn
, getResult
, ai_pqconn_get_result
, ZEND_ACC_PUBLIC
)
1778 PHP_ME(pqconn
, quote
, ai_pqconn_quote
, ZEND_ACC_PUBLIC
)
1779 PHP_ME(pqconn
, quoteName
, ai_pqconn_quote_name
, ZEND_ACC_PUBLIC
)
1780 PHP_ME(pqconn
, escapeBytea
, ai_pqconn_escape_bytea
, ZEND_ACC_PUBLIC
)
1781 PHP_ME(pqconn
, unescapeBytea
, ai_pqconn_unescape_bytea
, ZEND_ACC_PUBLIC
)
1782 PHP_ME(pqconn
, startTransaction
, ai_pqconn_start_transaction
, ZEND_ACC_PUBLIC
)
1783 PHP_ME(pqconn
, startTransactionAsync
, ai_pqconn_start_transaction_async
, ZEND_ACC_PUBLIC
)
1784 PHP_ME(pqconn
, trace
, ai_pqconn_trace
, ZEND_ACC_PUBLIC
)
1785 PHP_ME(pqconn
, off
, ai_pqconn_off
, ZEND_ACC_PUBLIC
)
1786 PHP_ME(pqconn
, on
, ai_pqconn_on
, ZEND_ACC_PUBLIC
)
1787 PHP_ME(pqconn
, setConverter
, ai_pqconn_set_converter
, ZEND_ACC_PUBLIC
)
1791 PHP_MSHUTDOWN_FUNCTION(pqconn
)
1793 zend_hash_destroy(&php_pqconn_object_prophandlers
);
1797 PHP_MINIT_FUNCTION(pqconn
)
1799 zend_class_entry ce
= {0};
1800 php_pq_object_prophandler_t ph
= {0};
1802 INIT_NS_CLASS_ENTRY(ce
, "pq", "Connection", php_pqconn_methods
);
1803 php_pqconn_class_entry
= zend_register_internal_class_ex(&ce
, NULL
, NULL TSRMLS_CC
);
1804 php_pqconn_class_entry
->create_object
= php_pqconn_create_object
;
1806 memcpy(&php_pqconn_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
1807 php_pqconn_object_handlers
.read_property
= php_pq_object_read_prop
;
1808 php_pqconn_object_handlers
.write_property
= php_pq_object_write_prop
;
1809 php_pqconn_object_handlers
.clone_obj
= NULL
;
1810 php_pqconn_object_handlers
.get_property_ptr_ptr
= NULL
;
1811 php_pqconn_object_handlers
.get_gc
= NULL
;
1812 php_pqconn_object_handlers
.get_properties
= php_pq_object_properties
;
1813 php_pqconn_object_handlers
.get_debug_info
= php_pq_object_debug_info
;
1815 zend_hash_init(&php_pqconn_object_prophandlers
, 14, NULL
, NULL
, 1);
1817 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("status"), CONNECTION_BAD
, ZEND_ACC_PUBLIC TSRMLS_CC
);
1818 ph
.read
= php_pqconn_object_read_status
;
1819 zend_hash_add(&php_pqconn_object_prophandlers
, "status", sizeof("status"), (void *) &ph
, sizeof(ph
), NULL
);
1821 zend_declare_property_long(php_pqconn_class_entry
, ZEND_STRL("transactionStatus"), PQTRANS_UNKNOWN
, ZEND_ACC_PUBLIC TSRMLS_CC
);
1822 ph
.read
= php_pqconn_object_read_transaction_status
;
1823 zend_hash_add(&php_pqconn_object_prophandlers
, "transactionStatus", sizeof("transactionStatus"), (void *) &ph
, sizeof(ph
), NULL
);
1825 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("socket"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1826 ph
.read
= NULL
; /* forward to std prophandler */
1827 zend_hash_add(&php_pqconn_object_prophandlers
, "socket", sizeof("socket"), (void *) &ph
, sizeof(ph
), NULL
);
1829 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("errorMessage"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1830 ph
.read
= php_pqconn_object_read_error_message
;
1831 zend_hash_add(&php_pqconn_object_prophandlers
, "errorMessage", sizeof("errorMessage"), (void *) &ph
, sizeof(ph
), NULL
);
1833 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("busy"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
1834 ph
.read
= php_pqconn_object_read_busy
;
1835 zend_hash_add(&php_pqconn_object_prophandlers
, "busy", sizeof("busy"), (void *) &ph
, sizeof(ph
), NULL
);
1837 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("encoding"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1838 ph
.read
= php_pqconn_object_read_encoding
;
1839 ph
.write
= php_pqconn_object_write_encoding
;
1840 zend_hash_add(&php_pqconn_object_prophandlers
, "encoding", sizeof("encoding"), (void *) &ph
, sizeof(ph
), NULL
);
1843 zend_declare_property_bool(php_pqconn_class_entry
, ZEND_STRL("unbuffered"), 0, ZEND_ACC_PUBLIC TSRMLS_CC
);
1844 ph
.read
= php_pqconn_object_read_unbuffered
;
1845 ph
.write
= php_pqconn_object_write_unbuffered
;
1846 zend_hash_add(&php_pqconn_object_prophandlers
, "unbuffered", sizeof("unbuffered"), (void *) &ph
, sizeof(ph
), NULL
);
1849 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("db"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1850 ph
.read
= php_pqconn_object_read_db
;
1851 zend_hash_add(&php_pqconn_object_prophandlers
, "db", sizeof("db"), (void *) &ph
, sizeof(ph
), NULL
);
1853 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("user"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1854 ph
.read
= php_pqconn_object_read_user
;
1855 zend_hash_add(&php_pqconn_object_prophandlers
, "user", sizeof("user"), (void *) &ph
, sizeof(ph
), NULL
);
1857 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("pass"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1858 ph
.read
= php_pqconn_object_read_pass
;
1859 zend_hash_add(&php_pqconn_object_prophandlers
, "pass", sizeof("pass"), (void *) &ph
, sizeof(ph
), NULL
);
1861 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("host"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1862 ph
.read
= php_pqconn_object_read_host
;
1863 zend_hash_add(&php_pqconn_object_prophandlers
, "host", sizeof("host"), (void *) &ph
, sizeof(ph
), NULL
);
1865 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("port"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1866 ph
.read
= php_pqconn_object_read_port
;
1867 zend_hash_add(&php_pqconn_object_prophandlers
, "port", sizeof("port"), (void *) &ph
, sizeof(ph
), NULL
);
1869 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("options"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1870 ph
.read
= php_pqconn_object_read_options
;
1871 zend_hash_add(&php_pqconn_object_prophandlers
, "options", sizeof("options"), (void *) &ph
, sizeof(ph
), NULL
);
1873 zend_declare_property_null(php_pqconn_class_entry
, ZEND_STRL("eventHandlers"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1874 ph
.read
= php_pqconn_object_read_event_handlers
;
1875 zend_hash_add(&php_pqconn_object_prophandlers
, "eventHandlers", sizeof("eventHandlers"), (void *) &ph
, sizeof(ph
), NULL
);
1877 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("OK"), CONNECTION_OK TSRMLS_CC
);
1878 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("BAD"), CONNECTION_BAD TSRMLS_CC
);
1879 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("STARTED"), CONNECTION_STARTED TSRMLS_CC
);
1880 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("MADE"), CONNECTION_MADE TSRMLS_CC
);
1881 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AWAITING_RESPONSE"), CONNECTION_AWAITING_RESPONSE TSRMLS_CC
);
1882 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("AUTH_OK"), CONNECTION_AUTH_OK TSRMLS_CC
);
1883 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SSL_STARTUP"), CONNECTION_SSL_STARTUP TSRMLS_CC
);
1884 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("SETENV"), CONNECTION_SETENV TSRMLS_CC
);
1886 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_IDLE"), PQTRANS_IDLE TSRMLS_CC
);
1887 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_ACTIVE"), PQTRANS_ACTIVE TSRMLS_CC
);
1888 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INTRANS"), PQTRANS_INTRANS TSRMLS_CC
);
1889 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_INERROR"), PQTRANS_INERROR TSRMLS_CC
);
1890 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("TRANS_UNKNOWN"), PQTRANS_UNKNOWN TSRMLS_CC
);
1892 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_FAILED"), PGRES_POLLING_FAILED TSRMLS_CC
);
1893 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_READING"), PGRES_POLLING_READING TSRMLS_CC
);
1894 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_WRITING"), PGRES_POLLING_WRITING TSRMLS_CC
);
1895 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("POLLING_OK"), PGRES_POLLING_OK TSRMLS_CC
);
1897 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_NOTICE"), ZEND_STRL("notice") TSRMLS_CC
);
1898 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESULT"), ZEND_STRL("result") TSRMLS_CC
);
1899 zend_declare_class_constant_stringl(php_pqconn_class_entry
, ZEND_STRL("EVENT_RESET"), ZEND_STRL("reset") TSRMLS_CC
);
1901 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("ASYNC"), 0x1 TSRMLS_CC
);
1902 zend_declare_class_constant_long(php_pqconn_class_entry
, ZEND_STRL("PERSISTENT"), 0x2 TSRMLS_CC
);
1912 * vim600: noet sw=4 ts=4 fdm=marker
1913 * vim<600: noet sw=4 ts=4