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 #include <libpq-events.h>
22 #include "php_pq_misc.h"
23 #include "php_pq_object.h"
24 #include "php_pqconn_event.h"
25 #include "php_pqres.h"
27 static int apply_event(void *p
, void *a TSRMLS_DC
)
29 php_pq_callback_t
*cb
= p
;
33 zend_fcall_info_args(&cb
->fci
, args TSRMLS_CC
);
34 zend_fcall_info_call(&cb
->fci
, &cb
->fcc
, &retval
, NULL TSRMLS_CC
);
36 zval_ptr_dtor(&retval
);
39 return ZEND_HASH_APPLY_KEEP
;
42 static void php_pqconn_event_connreset(PGEventConnReset
*event
)
44 php_pqconn_event_data_t
*data
= PQinstanceData(event
->conn
, php_pqconn_event
);
50 if (SUCCESS
== zend_hash_find(&data
->obj
->intern
->eventhandlers
, ZEND_STRS("reset"), (void *) &evhs
)) {
51 zval
*args
, *connection
= NULL
;
55 php_pq_object_to_zval(data
->obj
, &connection TSRMLS_CC
);
56 add_next_index_zval(args
, connection
);
57 zend_hash_apply_with_argument(evhs
, apply_event
, args TSRMLS_CC
);
63 static void php_pqconn_event_resultcreate(PGEventResultCreate
*event
)
65 php_pqconn_event_data_t
*data
= PQinstanceData(event
->conn
, php_pqconn_event
);
68 php_pqres_object_t
*obj
;
72 php_pqres_init_instance_data(event
->result
, data
->obj
, &obj TSRMLS_CC
);
75 if (SUCCESS
== zend_hash_find(&data
->obj
->intern
->eventhandlers
, ZEND_STRS("result"), (void *) &evhs
)) {
76 zval
*args
, *connection
= NULL
, *res
= NULL
;
80 php_pq_object_to_zval(data
->obj
, &connection TSRMLS_CC
);
81 add_next_index_zval(args
, connection
);
82 php_pq_object_to_zval(obj
, &res TSRMLS_CC
);
83 add_next_index_zval(args
, res
);
84 zend_hash_apply_with_argument(evhs
, apply_event
, args TSRMLS_CC
);
89 if (data
->obj
->intern
->onevent
.fci
.size
> 0) {
92 php_pq_object_to_zval(obj
, &res TSRMLS_CC
);
93 zend_fcall_info_argn(&data
->obj
->intern
->onevent
.fci TSRMLS_CC
, 1, &res
);
94 zend_fcall_info_call(&data
->obj
->intern
->onevent
.fci
, &data
->obj
->intern
->onevent
.fcc
, NULL
, NULL TSRMLS_CC
);
101 static void php_pqconn_event_resultdestroy(PGEventResultDestroy
*event
)
103 php_pqres_object_t
*obj
= PQresultInstanceData(event
->result
, php_pqconn_event
);
106 obj
->intern
->res
= NULL
;
110 int php_pqconn_event(PGEventId id
, void *e
, void *data
)
113 case PGEVT_CONNRESET
:
114 php_pqconn_event_connreset(e
);
116 case PGEVT_RESULTCREATE
:
117 php_pqconn_event_resultcreate(e
);
119 case PGEVT_RESULTDESTROY
:
120 php_pqconn_event_resultdestroy(e
);
129 php_pqconn_event_data_t
*php_pqconn_event_data_init(php_pqconn_object_t
*obj TSRMLS_DC
)
131 php_pqconn_event_data_t
*data
= emalloc(sizeof(*data
));
139 void php_pqconn_notice_recv(void *p
, const PGresult
*res
)
141 php_pqconn_event_data_t
*data
= p
;
147 if (SUCCESS
== zend_hash_find(&data
->obj
->intern
->eventhandlers
, ZEND_STRS("notice"), (void *) &evhs
)) {
148 zval
*args
, *connection
= NULL
;
152 php_pq_object_to_zval(data
->obj
, &connection TSRMLS_CC
);
153 add_next_index_zval(args
, connection
);
154 add_next_index_string(args
, PHP_PQresultErrorMessage(res
), 1);
155 zend_hash_apply_with_argument(evhs
, apply_event
, args TSRMLS_CC
);
156 zval_ptr_dtor(&args
);
161 void php_pqconn_notice_ignore(void *p
, const PGresult
*res
)
170 * vim600: noet sw=4 ts=4 fdm=marker
171 * vim<600: noet sw=4 ts=4