19c31a958b59451e981fe00d22d9b767c80a8160
[m6w6/ext-pq] / src / php_pqcur.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 #include <ext/standard/php_smart_str.h>
19
20 #include "php_pq.h"
21 #include "php_pq_misc.h"
22 #include "php_pq_object.h"
23 #include "php_pqexc.h"
24 #include "php_pqconn.h"
25 #include "php_pqres.h"
26 #include "php_pqcur.h"
27
28 zend_class_entry *php_pqcur_class_entry;
29 static zend_object_handlers php_pqcur_object_handlers;
30 static HashTable php_pqcur_object_prophandlers;
31
32 static void cur_close(php_pqcur_object_t *obj TSRMLS_DC)
33 {
34 if (obj->intern->open) {
35 PGresult *res;
36 smart_str cmd = {0};
37
38 smart_str_appends(&cmd, "CLOSE ");
39 smart_str_appends(&cmd, obj->intern->name);
40 smart_str_0(&cmd);
41
42 if ((res = PQexec(obj->intern->conn->intern->conn, cmd.c))) {
43 PHP_PQclear(res);
44 }
45 smart_str_free(&cmd);
46
47 obj->intern->open = 0;
48 }
49 }
50
51 static void cur_fetch_or_move(INTERNAL_FUNCTION_PARAMETERS, const char *action, zend_bool async)
52 {
53 char *spec_str = "1";
54 int spec_len = 1;
55 STATUS rv;
56 php_pq_callback_t resolver = {{0}};
57 zend_error_handling zeh;
58
59 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
60 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, async ? "|sf" : "|s", &spec_str, &spec_len, &resolver.fci, &resolver.fcc);
61 zend_restore_error_handling(&zeh TSRMLS_CC);
62
63 if (SUCCESS == rv) {
64 php_pqcur_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
65
66 if (!obj->intern) {
67 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Cursor not initialized");
68 } else {
69 smart_str cmd = {0};
70
71 smart_str_appends(&cmd, *action == 'f' ? "FETCH " : "MOVE ");
72 smart_str_appendl(&cmd, spec_str, spec_len);
73 smart_str_appends(&cmd, " FROM ");
74 smart_str_appends(&cmd, obj->intern->name);
75 smart_str_0(&cmd);
76
77 if (async) {
78 int rc = PQsendQuery(obj->intern->conn->intern->conn, cmd.c);
79
80 if (!rc) {
81 throw_exce(EX_IO TSRMLS_CC, "Failed to %s cursor (%s)", *action == 'f' ? "fetch from" : "move in", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
82 } else if (obj->intern->conn->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn->intern->conn)) {
83 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
84 } else {
85 php_pq_callback_recurse(&obj->intern->conn->intern->onevent, &resolver TSRMLS_CC);
86 obj->intern->conn->intern->poller = PQconsumeInput;
87 }
88 } else {
89 PGresult *res = PQexec(obj->intern->conn->intern->conn, cmd.c);
90
91 if (!res) {
92 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to %s cursor (%s)", *action == 'f' ? "fetch from" : "move in", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
93 } else if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
94 php_pq_object_to_zval_no_addref(PQresultInstanceData(res, php_pqconn_event), &return_value TSRMLS_CC);
95
96 }
97 }
98 smart_str_free(&cmd);
99 php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
100 }
101 }
102 }
103
104 static void php_pqcur_object_free(void *o TSRMLS_DC)
105 {
106 php_pqcur_object_t *obj = o;
107 #if DBG_GC
108 fprintf(stderr, "FREE cur(#%d) %p (conn: %p)\n", obj->zv.handle, obj, obj->intern->conn);
109 #endif
110 if (obj->intern) {
111 cur_close(obj TSRMLS_CC);
112 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
113 efree(obj->intern->decl);
114 efree(obj->intern->name);
115 efree(obj->intern);
116 obj->intern = NULL;
117 }
118 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
119 efree(obj);
120 }
121
122 zend_object_value php_pqcur_create_object_ex(zend_class_entry *ce, php_pqcur_t *intern, php_pqcur_object_t **ptr TSRMLS_DC)
123 {
124 php_pqcur_object_t *o;
125
126 o = ecalloc(1, sizeof(*o));
127 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
128 object_properties_init((zend_object *) o, ce);
129 o->prophandler = &php_pqcur_object_prophandlers;
130
131 if (ptr) {
132 *ptr = o;
133 }
134
135 if (intern) {
136 o->intern = intern;
137 }
138
139 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqcur_object_free, NULL TSRMLS_CC);
140 o->zv.handlers = &php_pqcur_object_handlers;
141
142 return o->zv;
143 }
144
145 static zend_object_value php_pqcur_create_object(zend_class_entry *class_type TSRMLS_DC)
146 {
147 return php_pqcur_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
148 }
149
150 static void php_pqcur_object_read_name(zval *object, void *o, zval *return_value TSRMLS_DC)
151 {
152 php_pqcur_object_t *obj = o;
153
154 RETVAL_STRING(obj->intern->name, 1);
155 }
156
157 static void php_pqcur_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
158 {
159 php_pqcur_object_t *obj = o;
160
161 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
162 }
163
164 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_open, 0, 0, 0)
165 ZEND_END_ARG_INFO();
166 static PHP_METHOD(pqcur, open)
167 {
168 zend_error_handling zeh;
169 STATUS rv;
170
171 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
172 rv = zend_parse_parameters_none();
173 zend_restore_error_handling(&zeh TSRMLS_CC);
174
175 if (rv == SUCCESS) {
176 php_pqcur_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
177
178 if (!obj->intern) {
179 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Cursor not initialized");
180 } else if (!obj->intern->open) {
181 if (SUCCESS == php_pqconn_declare(NULL, obj->intern->conn, obj->intern->decl TSRMLS_CC)) {
182 obj->intern->open = 1;
183 }
184 }
185 }
186 }
187
188 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_close, 0, 0, 0)
189 ZEND_END_ARG_INFO();
190 static PHP_METHOD(pqcur, close)
191 {
192 zend_error_handling zeh;
193 STATUS rv;
194
195 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
196 rv = zend_parse_parameters_none();
197 zend_restore_error_handling(&zeh TSRMLS_CC);
198
199 if (rv == SUCCESS) {
200 php_pqcur_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
201
202 if (!obj->intern) {
203 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Cursor not initialized");
204 } else {
205 cur_close(obj TSRMLS_CC);
206 }
207 }
208 }
209
210 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_fetch, 0, 0, 1)
211 ZEND_ARG_INFO(0, spec)
212 ZEND_END_ARG_INFO();
213 static PHP_METHOD(pqcur, fetch)
214 {
215 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU, "fetch", 0);
216 }
217
218 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_move, 0, 0, 0)
219 ZEND_ARG_INFO(0, spec)
220 ZEND_END_ARG_INFO();
221 static PHP_METHOD(pqcur, move)
222 {
223 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU, "move", 0);
224 }
225
226 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_fetchAsync, 0, 0, 0)
227 ZEND_ARG_INFO(0, spec)
228 ZEND_ARG_INFO(0, callback)
229 ZEND_END_ARG_INFO();
230 static PHP_METHOD(pqcur, fetchAsync)
231 {
232 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU, "fetch", 1);
233 }
234
235 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_moveAsync, 0, 0, 0)
236 ZEND_ARG_INFO(0, spec)
237 ZEND_ARG_INFO(0, callback)
238 ZEND_END_ARG_INFO();
239 static PHP_METHOD(pqcur, moveAsync)
240 {
241 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU, "move", 1);
242 }
243
244 static zend_function_entry php_pqcur_methods[] = {
245 PHP_ME(pqcur, open, ai_pqcur_open, ZEND_ACC_PUBLIC)
246 PHP_ME(pqcur, close, ai_pqcur_close, ZEND_ACC_PUBLIC)
247 PHP_ME(pqcur, fetch, ai_pqcur_fetch, ZEND_ACC_PUBLIC)
248 PHP_ME(pqcur, move, ai_pqcur_move, ZEND_ACC_PUBLIC)
249 PHP_ME(pqcur, fetchAsync, ai_pqcur_fetchAsync, ZEND_ACC_PUBLIC)
250 PHP_ME(pqcur, moveAsync, ai_pqcur_moveAsync, ZEND_ACC_PUBLIC)
251 {NULL, NULL, NULL}
252 };
253
254 PHP_MSHUTDOWN_FUNCTION(pqcur)
255 {
256 zend_hash_destroy(&php_pqcur_object_prophandlers);
257 return SUCCESS;
258 }
259
260 PHP_MINIT_FUNCTION(pqcur)
261 {
262 zend_class_entry ce = {0};
263 php_pq_object_prophandler_t ph = {0};
264
265 INIT_NS_CLASS_ENTRY(ce, "pq", "Cursor", php_pqcur_methods);
266 php_pqcur_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
267 php_pqcur_class_entry->create_object = php_pqcur_create_object;
268
269 memcpy(&php_pqcur_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
270 php_pqcur_object_handlers.read_property = php_pq_object_read_prop;
271 php_pqcur_object_handlers.write_property = php_pq_object_write_prop;
272 php_pqcur_object_handlers.clone_obj = NULL;
273 php_pqcur_object_handlers.get_property_ptr_ptr = NULL;
274 php_pqcur_object_handlers.get_gc = NULL;
275 php_pqcur_object_handlers.get_properties = php_pq_object_properties;
276 php_pqcur_object_handlers.get_debug_info = php_pq_object_debug_info;
277
278 zend_hash_init(&php_pqcur_object_prophandlers, 2, NULL, NULL, 1);
279
280 zend_declare_class_constant_long(php_pqcur_class_entry, ZEND_STRL("BINARY"), PHP_PQ_DECLARE_BINARY TSRMLS_CC);
281 zend_declare_class_constant_long(php_pqcur_class_entry, ZEND_STRL("INSENSITIVE"), PHP_PQ_DECLARE_INSENSITIVE TSRMLS_CC);
282 zend_declare_class_constant_long(php_pqcur_class_entry, ZEND_STRL("WITH_HOLD"), PHP_PQ_DECLARE_WITH_HOLD TSRMLS_CC);
283 zend_declare_class_constant_long(php_pqcur_class_entry, ZEND_STRL("SCROLL"), PHP_PQ_DECLARE_SCROLL TSRMLS_CC);
284 zend_declare_class_constant_long(php_pqcur_class_entry, ZEND_STRL("NO_SCROLL"), PHP_PQ_DECLARE_NO_SCROLL TSRMLS_CC);
285
286 zend_declare_property_null(php_pqcur_class_entry, ZEND_STRL("name"), ZEND_ACC_PUBLIC TSRMLS_CC);
287 ph.read = php_pqcur_object_read_name;
288 zend_hash_add(&php_pqcur_object_prophandlers, "name", sizeof("name"), (void *) &ph, sizeof(ph), NULL);
289
290 zend_declare_property_null(php_pqcur_class_entry, ZEND_STRL("connection"), ZEND_ACC_PUBLIC TSRMLS_CC);
291 ph.read = php_pqcur_object_read_connection;
292 zend_hash_add(&php_pqcur_object_prophandlers, "connection", sizeof("connection"), (void *) &ph, sizeof(ph), NULL);
293
294 return SUCCESS;
295 }
296
297 /*
298 * Local variables:
299 * tab-width: 4
300 * c-basic-offset: 4
301 * End:
302 * vim600: noet sw=4 ts=4 fdm=marker
303 * vim<600: noet sw=4 ts=4
304 */