Add Cursor::openAsync()
[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 && obj->intern->conn->intern) {
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_open(INTERNAL_FUNCTION_PARAMETERS, zend_bool async)
52 {
53 zend_error_handling zeh;
54 STATUS rv;
55
56 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
57 rv = zend_parse_parameters_none();
58 zend_restore_error_handling(&zeh TSRMLS_CC);
59
60 if (rv == FAILURE) {
61 return;
62 }
63
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 return;
69 } else if (obj->intern->open) {
70 return;
71 }
72
73 if (async) {
74 rv = php_pqconn_declare_async(NULL, obj->intern->conn, obj->intern->decl TSRMLS_CC);
75 } else {
76 rv = php_pqconn_declare(NULL, obj->intern->conn, obj->intern->decl TSRMLS_CC);
77 }
78
79 if (rv == SUCCESS) {
80 obj->intern->open = 1;
81 }
82 }
83
84 static void cur_fetch_or_move(INTERNAL_FUNCTION_PARAMETERS, const char *action, zend_bool async)
85 {
86 char *spec_str = "1";
87 int spec_len = 1;
88 STATUS rv;
89 php_pq_callback_t resolver = {{0}};
90 zend_error_handling zeh;
91
92 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
93 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, async ? "|sf" : "|s", &spec_str, &spec_len, &resolver.fci, &resolver.fcc);
94 zend_restore_error_handling(&zeh TSRMLS_CC);
95
96 if (SUCCESS == rv) {
97 php_pqcur_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
98
99 if (!obj->intern) {
100 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Cursor not initialized");
101 } else {
102 smart_str cmd = {0};
103
104 smart_str_appends(&cmd, *action == 'f' ? "FETCH " : "MOVE ");
105 smart_str_appendl(&cmd, spec_str, spec_len);
106 smart_str_appends(&cmd, " FROM ");
107 smart_str_appends(&cmd, obj->intern->name);
108 smart_str_0(&cmd);
109
110 if (async) {
111 int rc = PQsendQuery(obj->intern->conn->intern->conn, cmd.c);
112
113 if (!rc) {
114 throw_exce(EX_IO TSRMLS_CC, "Failed to %s cursor (%s)", *action == 'f' ? "fetch from" : "move in", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
115 #if HAVE_PQSETSINGLEROWMODE
116 } else if (obj->intern->conn->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn->intern->conn)) {
117 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
118 #endif
119 } else {
120 php_pq_callback_recurse(&obj->intern->conn->intern->onevent, &resolver TSRMLS_CC);
121 obj->intern->conn->intern->poller = PQconsumeInput;
122 }
123 } else {
124 PGresult *res = PQexec(obj->intern->conn->intern->conn, cmd.c);
125
126 if (!res) {
127 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to %s cursor (%s)", *action == 'f' ? "fetch from" : "move in", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
128 } else if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
129 php_pq_object_to_zval_no_addref(PQresultInstanceData(res, php_pqconn_event), &return_value TSRMLS_CC);
130
131 }
132 }
133 smart_str_free(&cmd);
134 php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
135 }
136 }
137 }
138
139 static void php_pqcur_object_free(void *o TSRMLS_DC)
140 {
141 php_pqcur_object_t *obj = o;
142 #if DBG_GC
143 fprintf(stderr, "FREE cur(#%d) %p (conn: %p)\n", obj->zv.handle, obj, obj->intern->conn);
144 #endif
145 if (obj->intern) {
146 cur_close(obj TSRMLS_CC);
147 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
148 efree(obj->intern->decl);
149 efree(obj->intern->name);
150 efree(obj->intern);
151 obj->intern = NULL;
152 }
153 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
154 efree(obj);
155 }
156
157 zend_object_value php_pqcur_create_object_ex(zend_class_entry *ce, php_pqcur_t *intern, php_pqcur_object_t **ptr TSRMLS_DC)
158 {
159 php_pqcur_object_t *o;
160
161 o = ecalloc(1, sizeof(*o));
162 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
163 object_properties_init((zend_object *) o, ce);
164 o->prophandler = &php_pqcur_object_prophandlers;
165
166 if (ptr) {
167 *ptr = o;
168 }
169
170 if (intern) {
171 o->intern = intern;
172 }
173
174 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqcur_object_free, NULL TSRMLS_CC);
175 o->zv.handlers = &php_pqcur_object_handlers;
176
177 return o->zv;
178 }
179
180 static zend_object_value php_pqcur_create_object(zend_class_entry *class_type TSRMLS_DC)
181 {
182 return php_pqcur_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
183 }
184
185 static void php_pqcur_object_read_name(zval *object, void *o, zval *return_value TSRMLS_DC)
186 {
187 php_pqcur_object_t *obj = o;
188
189 RETVAL_STRING(obj->intern->name, 1);
190 }
191
192 static void php_pqcur_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
193 {
194 php_pqcur_object_t *obj = o;
195
196 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
197 }
198
199 char *php_pqcur_declare_str(const char *name_str, size_t name_len, unsigned flags, const char *query_str, size_t query_len)
200 {
201 size_t decl_len = name_len + query_len + sizeof("DECLARE BINARY INSENSITIVE NO SCROLL CURSOR WITH HOLD FOR ");
202 char *decl_str;
203
204 decl_str = emalloc(decl_len);
205 decl_len = slprintf(decl_str, decl_len, "DECLARE %s %s %s %s CURSOR %s FOR %s",
206 name_str,
207 (flags & PHP_PQ_DECLARE_BINARY) ? "BINARY" : "",
208 (flags & PHP_PQ_DECLARE_INSENSITIVE) ? "INSENSITIVE" : "",
209 (flags & PHP_PQ_DECLARE_NO_SCROLL) ? "NO SCROLL" :
210 (flags & PHP_PQ_DECLARE_SCROLL) ? "SCROLL" : "",
211 (flags & PHP_PQ_DECLARE_WITH_HOLD) ? "WITH HOLD" : "",
212 query_str
213 );
214 return decl_str;
215 }
216
217 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur___construct, 0, 0, 4)
218 ZEND_ARG_OBJ_INFO(0, connection, pq\\Connection, 0)
219 ZEND_ARG_INFO(0, name)
220 ZEND_ARG_INFO(0, flags)
221 ZEND_ARG_INFO(0, query)
222 ZEND_ARG_INFO(0, async)
223 ZEND_END_ARG_INFO();
224 static PHP_METHOD(pqcur, __construct) {
225 zend_error_handling zeh;
226 char *name_str, *query_str;
227 int name_len, query_len;
228 long flags;
229 zval *zconn;
230 STATUS rv;
231 zend_bool async = 0;
232
233 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
234 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Osls|b", &zconn, php_pqconn_class_entry, &name_str, &name_len, &flags, &query_str, &query_len, &async);
235 zend_restore_error_handling(&zeh TSRMLS_CC);
236
237 if (SUCCESS == rv) {
238 php_pqcur_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
239 php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
240
241 if (obj->intern) {
242 throw_exce(EX_BAD_METHODCALL TSRMLS_CC, "pq\\Cursor already initialized");
243 } if (!conn_obj->intern) {
244 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
245 } else {
246 char *decl = php_pqcur_declare_str(name_str, name_len, flags, query_str, query_len);
247
248 if (async) {
249 rv = php_pqconn_declare_async(zconn, conn_obj, decl TSRMLS_CC);
250 } else {
251 rv = php_pqconn_declare(zconn, conn_obj, decl TSRMLS_CC);
252 }
253
254 if (SUCCESS != rv) {
255 efree(decl);
256 } else {
257 php_pqcur_t *cur = ecalloc(1, sizeof(*cur));
258
259 php_pq_object_addref(conn_obj TSRMLS_CC);
260 cur->conn = conn_obj;
261 cur->open = 1;
262 cur->name = estrdup(name_str);
263 cur->decl = decl;
264 obj->intern = cur;
265 }
266 }
267 }
268 }
269
270 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_open, 0, 0, 0)
271 ZEND_END_ARG_INFO();
272 static PHP_METHOD(pqcur, open)
273 {
274 cur_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
275 }
276
277 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_openAsync, 0, 0, 0)
278 ZEND_END_ARG_INFO();
279 static PHP_METHOD(pqcur, openAsync)
280 {
281 cur_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
282 }
283
284 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_close, 0, 0, 0)
285 ZEND_END_ARG_INFO();
286 static PHP_METHOD(pqcur, close)
287 {
288 zend_error_handling zeh;
289 STATUS rv;
290
291 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
292 rv = zend_parse_parameters_none();
293 zend_restore_error_handling(&zeh TSRMLS_CC);
294
295 if (rv == SUCCESS) {
296 php_pqcur_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
297
298 if (!obj->intern) {
299 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Cursor not initialized");
300 } else {
301 cur_close(obj TSRMLS_CC);
302 }
303 }
304 }
305
306 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_fetch, 0, 0, 1)
307 ZEND_ARG_INFO(0, spec)
308 ZEND_END_ARG_INFO();
309 static PHP_METHOD(pqcur, fetch)
310 {
311 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU, "fetch", 0);
312 }
313
314 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_move, 0, 0, 0)
315 ZEND_ARG_INFO(0, spec)
316 ZEND_END_ARG_INFO();
317 static PHP_METHOD(pqcur, move)
318 {
319 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU, "move", 0);
320 }
321
322 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_fetchAsync, 0, 0, 0)
323 ZEND_ARG_INFO(0, spec)
324 ZEND_ARG_INFO(0, callback)
325 ZEND_END_ARG_INFO();
326 static PHP_METHOD(pqcur, fetchAsync)
327 {
328 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU, "fetch", 1);
329 }
330
331 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_moveAsync, 0, 0, 0)
332 ZEND_ARG_INFO(0, spec)
333 ZEND_ARG_INFO(0, callback)
334 ZEND_END_ARG_INFO();
335 static PHP_METHOD(pqcur, moveAsync)
336 {
337 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU, "move", 1);
338 }
339
340 static zend_function_entry php_pqcur_methods[] = {
341 PHP_ME(pqcur, __construct, ai_pqcur___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
342 PHP_ME(pqcur, open, ai_pqcur_open, ZEND_ACC_PUBLIC)
343 PHP_ME(pqcur, openAsync, ai_pqcur_open, ZEND_ACC_PUBLIC)
344 PHP_ME(pqcur, close, ai_pqcur_close, ZEND_ACC_PUBLIC)
345 PHP_ME(pqcur, fetch, ai_pqcur_fetch, ZEND_ACC_PUBLIC)
346 PHP_ME(pqcur, move, ai_pqcur_move, ZEND_ACC_PUBLIC)
347 PHP_ME(pqcur, fetchAsync, ai_pqcur_fetchAsync, ZEND_ACC_PUBLIC)
348 PHP_ME(pqcur, moveAsync, ai_pqcur_moveAsync, ZEND_ACC_PUBLIC)
349 {NULL, NULL, NULL}
350 };
351
352 PHP_MSHUTDOWN_FUNCTION(pqcur)
353 {
354 zend_hash_destroy(&php_pqcur_object_prophandlers);
355 return SUCCESS;
356 }
357
358 PHP_MINIT_FUNCTION(pqcur)
359 {
360 zend_class_entry ce = {0};
361 php_pq_object_prophandler_t ph = {0};
362
363 INIT_NS_CLASS_ENTRY(ce, "pq", "Cursor", php_pqcur_methods);
364 php_pqcur_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
365 php_pqcur_class_entry->create_object = php_pqcur_create_object;
366
367 memcpy(&php_pqcur_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
368 php_pqcur_object_handlers.read_property = php_pq_object_read_prop;
369 php_pqcur_object_handlers.write_property = php_pq_object_write_prop;
370 php_pqcur_object_handlers.clone_obj = NULL;
371 php_pqcur_object_handlers.get_property_ptr_ptr = NULL;
372 php_pqcur_object_handlers.get_gc = NULL;
373 php_pqcur_object_handlers.get_properties = php_pq_object_properties;
374 php_pqcur_object_handlers.get_debug_info = php_pq_object_debug_info;
375
376 zend_hash_init(&php_pqcur_object_prophandlers, 2, NULL, NULL, 1);
377
378 zend_declare_class_constant_long(php_pqcur_class_entry, ZEND_STRL("BINARY"), PHP_PQ_DECLARE_BINARY TSRMLS_CC);
379 zend_declare_class_constant_long(php_pqcur_class_entry, ZEND_STRL("INSENSITIVE"), PHP_PQ_DECLARE_INSENSITIVE TSRMLS_CC);
380 zend_declare_class_constant_long(php_pqcur_class_entry, ZEND_STRL("WITH_HOLD"), PHP_PQ_DECLARE_WITH_HOLD TSRMLS_CC);
381 zend_declare_class_constant_long(php_pqcur_class_entry, ZEND_STRL("SCROLL"), PHP_PQ_DECLARE_SCROLL TSRMLS_CC);
382 zend_declare_class_constant_long(php_pqcur_class_entry, ZEND_STRL("NO_SCROLL"), PHP_PQ_DECLARE_NO_SCROLL TSRMLS_CC);
383
384 zend_declare_property_null(php_pqcur_class_entry, ZEND_STRL("name"), ZEND_ACC_PUBLIC TSRMLS_CC);
385 ph.read = php_pqcur_object_read_name;
386 zend_hash_add(&php_pqcur_object_prophandlers, "name", sizeof("name"), (void *) &ph, sizeof(ph), NULL);
387
388 zend_declare_property_null(php_pqcur_class_entry, ZEND_STRL("connection"), ZEND_ACC_PUBLIC TSRMLS_CC);
389 ph.read = php_pqcur_object_read_connection;
390 zend_hash_add(&php_pqcur_object_prophandlers, "connection", sizeof("connection"), (void *) &ph, sizeof(ph), NULL);
391
392 return SUCCESS;
393 }
394
395 /*
396 * Local variables:
397 * tab-width: 4
398 * c-basic-offset: 4
399 * End:
400 * vim600: noet sw=4 ts=4 fdm=marker
401 * vim<600: noet sw=4 ts=4
402 */