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