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