Merge branch 'v1.0.x'
[m6w6/ext-pq] / src / php_pqconn_event.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: pq |
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 +--------------------------------------------------------------------+
11 */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <php.h>
18
19 #include <libpq-events.h>
20
21 #include "php_pq.h"
22 #include "php_pq_misc.h"
23 #include "php_pq_object.h"
24 #include "php_pqconn_event.h"
25 #include "php_pqres.h"
26
27 static int apply_event(zval *p, void *a)
28 {
29 php_pq_callback_t *cb = Z_PTR_P(p);
30 zval *args = a;
31 zval rv;
32
33 ZVAL_NULL(&rv);
34 zend_fcall_info_args(&cb->fci, args);
35 zend_fcall_info_call(&cb->fci, &cb->fcc, &rv, NULL);
36 zend_fcall_info_args_clear(&cb->fci, 0);
37 zval_ptr_dtor(&rv);
38
39 return ZEND_HASH_APPLY_KEEP;
40 }
41
42 static void php_pqconn_event_connreset(PGEventConnReset *event)
43 {
44 php_pqconn_event_data_t *data = PQinstanceData(event->conn, php_pqconn_event);
45
46 if (data) {
47 zval *zevhs;
48
49 if ((zevhs = zend_hash_str_find(&data->obj->intern->eventhandlers, ZEND_STRL("reset")))) {
50 zval args, connection;
51
52 array_init(&args);
53 php_pq_object_to_zval(data->obj, &connection);
54 add_next_index_zval(&args, &connection);
55 zend_hash_apply_with_argument(Z_ARRVAL_P(zevhs), apply_event, &args);
56 zval_ptr_dtor(&args);
57 }
58 }
59 }
60
61 static void php_pqconn_event_resultcreate(PGEventResultCreate *event)
62 {
63 php_pqconn_event_data_t *data = PQinstanceData(event->conn, php_pqconn_event);
64
65 if (data) {
66 php_pqres_object_t *obj = php_pqres_init_instance_data(event->result, data->obj);
67 zval *zevhs;
68
69 /* event listener */
70 if ((zevhs = zend_hash_str_find(&data->obj->intern->eventhandlers, ZEND_STRL("result")))) {
71 zval args, connection, res;
72
73 array_init(&args);
74 php_pq_object_to_zval(data->obj, &connection);
75 add_next_index_zval(&args, &connection);
76 php_pq_object_to_zval(obj, &res);
77 add_next_index_zval(&args, &res);
78 zend_hash_apply_with_argument(Z_ARRVAL_P(zevhs), apply_event, &args);
79 zval_ptr_dtor(&args);
80 }
81
82 /* async callback */
83 if (data->obj->intern->onevent.fci.size > 0) {
84 zval res;
85
86 php_pq_object_to_zval(obj, &res);
87 zend_fcall_info_argn(&data->obj->intern->onevent.fci, 1, &res);
88 zend_fcall_info_call(&data->obj->intern->onevent.fci, &data->obj->intern->onevent.fcc, NULL, NULL);
89 zval_ptr_dtor(&res);
90 }
91
92 }
93 }
94
95 static void php_pqconn_event_resultdestroy(PGEventResultDestroy *event)
96 {
97 php_pqres_object_t *obj = PQresultInstanceData(event->result, php_pqconn_event);
98
99 if (obj) {
100 obj->intern->res = NULL;
101 assert(GC_REFCOUNT(&obj->zo));
102 php_pq_object_delref(obj);
103 }
104 }
105
106 int php_pqconn_event(PGEventId id, void *e, void *data)
107 {
108 switch (id) {
109 case PGEVT_CONNRESET:
110 php_pqconn_event_connreset(e);
111 break;
112 case PGEVT_RESULTCREATE:
113 php_pqconn_event_resultcreate(e);
114 break;
115 case PGEVT_RESULTDESTROY:
116 php_pqconn_event_resultdestroy(e);
117 break;
118 default:
119 break;
120 }
121
122 return 1;
123 }
124
125 php_pqconn_event_data_t *php_pqconn_event_data_init(php_pqconn_object_t *obj)
126 {
127 php_pqconn_event_data_t *data = emalloc(sizeof(*data));
128
129 data->obj = obj;
130 data->res = NULL;
131
132 return data;
133 }
134
135 void php_pqconn_notice_recv(void *p, const PGresult *res)
136 {
137 php_pqconn_event_data_t *data = p;
138
139 if (data) {
140 zval *zevhs;
141
142 if ((zevhs = zend_hash_str_find(&data->obj->intern->eventhandlers, ZEND_STRL("notice")))) {
143 zval args, connection;
144
145 array_init(&args);
146 php_pq_object_to_zval(data->obj, &connection);
147 add_next_index_zval(&args, &connection);
148 add_next_index_string(&args, PHP_PQresultErrorMessage(res));
149 zend_hash_apply_with_argument(Z_ARRVAL_P(zevhs), apply_event, &args);
150 zval_ptr_dtor(&args);
151 }
152 }
153 }
154
155 void php_pqconn_notice_ignore(void *p, const PGresult *res)
156 {
157 }
158
159 /*
160 * Local variables:
161 * tab-width: 4
162 * c-basic-offset: 4
163 * End:
164 * vim600: noet sw=4 ts=4 fdm=marker
165 * vim<600: noet sw=4 ts=4
166 */