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