2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
18 #include <ext/standard/php_smart_str.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"
28 zend_class_entry
*php_pqcur_class_entry
;
29 static zend_object_handlers php_pqcur_object_handlers
;
30 static HashTable php_pqcur_object_prophandlers
;
32 static void cur_close(php_pqcur_object_t
*obj
, zend_bool async
, zend_bool silent TSRMLS_DC
)
34 if (obj
->intern
->open
&& obj
->intern
->conn
->intern
) {
38 smart_str_appends(&cmd
, "CLOSE ");
39 smart_str_appends(&cmd
, obj
->intern
->name
);
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
);
47 throw_exce(EX_IO TSRMLS_CC
, "Failed to close cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
50 if ((res
= PQexec(obj
->intern
->conn
->intern
->conn
, cmd
.c
))) {
53 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to close cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
58 obj
->intern
->open
= 0;
62 static void cur_open(INTERNAL_FUNCTION_PARAMETERS
, zend_bool async
)
64 zend_error_handling zeh
;
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
);
75 php_pqcur_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
78 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Cursor not initialized");
80 } else if (obj
->intern
->open
) {
85 rv
= php_pqconn_declare_async(NULL
, obj
->intern
->conn
, obj
->intern
->decl TSRMLS_CC
);
87 rv
= php_pqconn_declare(NULL
, obj
->intern
->conn
, obj
->intern
->decl TSRMLS_CC
);
91 obj
->intern
->open
= 1;
95 static void cur_fetch_or_move(INTERNAL_FUNCTION_PARAMETERS
, const char *action
, zend_bool async
)
100 php_pq_callback_t resolver
= {{0}};
101 zend_error_handling zeh
;
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
);
108 php_pqcur_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
111 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Cursor not initialized");
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
);
122 int rc
= PQsendQuery(obj
->intern
->conn
->intern
->conn
, cmd
.c
);
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
));
131 php_pq_callback_recurse(&obj
->intern
->conn
->intern
->onevent
, &resolver TSRMLS_CC
);
132 obj
->intern
->conn
->intern
->poller
= PQconsumeInput
;
135 PGresult
*res
= PQexec(obj
->intern
->conn
->intern
->conn
, cmd
.c
);
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
);
144 smart_str_free(&cmd
);
145 php_pqconn_notify_listeners(obj
->intern
->conn TSRMLS_CC
);
150 static void php_pqcur_object_free(void *o TSRMLS_DC
)
152 php_pqcur_object_t
*obj
= o
;
154 fprintf(stderr
, "FREE cur(#%d) %p (conn: %p)\n", obj
->zv
.handle
, obj
, obj
->intern
->conn
);
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
);
164 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
168 zend_object_value
php_pqcur_create_object_ex(zend_class_entry
*ce
, php_pqcur_t
*intern
, php_pqcur_object_t
**ptr TSRMLS_DC
)
170 php_pqcur_object_t
*o
;
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
;
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
;
191 static zend_object_value
php_pqcur_create_object(zend_class_entry
*class_type TSRMLS_DC
)
193 return php_pqcur_create_object_ex(class_type
, NULL
, NULL TSRMLS_CC
);
196 static void php_pqcur_object_read_name(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
198 php_pqcur_object_t
*obj
= o
;
200 RETVAL_STRING(obj
->intern
->name
, 1);
203 static void php_pqcur_object_read_connection(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
205 php_pqcur_object_t
*obj
= o
;
207 php_pq_object_to_zval(obj
->intern
->conn
, &return_value TSRMLS_CC
);
210 char *php_pqcur_declare_str(const char *name_str
, size_t name_len
, unsigned flags
, const char *query_str
, size_t query_len
)
212 size_t decl_len
= name_len
+ query_len
+ sizeof("DECLARE BINARY INSENSITIVE NO SCROLL CURSOR WITH HOLD FOR ");
215 decl_str
= emalloc(decl_len
);
216 decl_len
= slprintf(decl_str
, decl_len
, "DECLARE %s %s %s %s CURSOR %s FOR %s",
218 (flags
& PHP_PQ_DECLARE_BINARY
) ? "BINARY" : "",
219 (flags
& PHP_PQ_DECLARE_INSENSITIVE
) ? "INSENSITIVE" : "",
220 (flags
& PHP_PQ_DECLARE_NO_SCROLL
) ? "NO SCROLL" :
221 (flags
& PHP_PQ_DECLARE_SCROLL
) ? "SCROLL" : "",
222 (flags
& PHP_PQ_DECLARE_WITH_HOLD
) ? "WITH HOLD" : "",
228 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur___construct
, 0, 0, 4)
229 ZEND_ARG_OBJ_INFO(0, connection
, pq
\\Connection
, 0)
230 ZEND_ARG_INFO(0, name
)
231 ZEND_ARG_INFO(0, flags
)
232 ZEND_ARG_INFO(0, query
)
233 ZEND_ARG_INFO(0, async
)
235 static PHP_METHOD(pqcur
, __construct
) {
236 zend_error_handling zeh
;
237 char *name_str
, *query_str
;
238 int name_len
, query_len
;
244 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
245 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
);
246 zend_restore_error_handling(&zeh TSRMLS_CC
);
249 php_pqcur_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
250 php_pqconn_object_t
*conn_obj
= zend_object_store_get_object(zconn TSRMLS_CC
);
253 throw_exce(EX_BAD_METHODCALL TSRMLS_CC
, "pq\\Cursor already initialized");
254 } if (!conn_obj
->intern
) {
255 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
257 char *decl
= php_pqcur_declare_str(name_str
, name_len
, flags
, query_str
, query_len
);
260 rv
= php_pqconn_declare_async(zconn
, conn_obj
, decl TSRMLS_CC
);
262 rv
= php_pqconn_declare(zconn
, conn_obj
, decl TSRMLS_CC
);
268 php_pqcur_t
*cur
= ecalloc(1, sizeof(*cur
));
270 php_pq_object_addref(conn_obj TSRMLS_CC
);
271 cur
->conn
= conn_obj
;
273 cur
->name
= estrdup(name_str
);
281 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_open
, 0, 0, 0)
283 static PHP_METHOD(pqcur
, open
)
285 cur_open(INTERNAL_FUNCTION_PARAM_PASSTHRU
, 0);
288 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_openAsync
, 0, 0, 0)
290 static PHP_METHOD(pqcur
, openAsync
)
292 cur_open(INTERNAL_FUNCTION_PARAM_PASSTHRU
, 1);
295 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_close
, 0, 0, 0)
297 static PHP_METHOD(pqcur
, close
)
299 zend_error_handling zeh
;
302 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
303 rv
= zend_parse_parameters_none();
304 zend_restore_error_handling(&zeh TSRMLS_CC
);
307 php_pqcur_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
310 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Cursor not initialized");
312 cur_close(obj
, 0, 0 TSRMLS_CC
);
317 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_closeAsync
, 0, 0, 0)
319 static PHP_METHOD(pqcur
, closeAsync
)
321 zend_error_handling zeh
;
324 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
325 rv
= zend_parse_parameters_none();
326 zend_restore_error_handling(&zeh TSRMLS_CC
);
329 php_pqcur_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
332 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Cursor not initialized");
334 cur_close(obj
, 1, 0 TSRMLS_CC
);
339 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_fetch
, 0, 0, 1)
340 ZEND_ARG_INFO(0, spec
)
342 static PHP_METHOD(pqcur
, fetch
)
344 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU
, "fetch", 0);
347 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_move
, 0, 0, 0)
348 ZEND_ARG_INFO(0, spec
)
350 static PHP_METHOD(pqcur
, move
)
352 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU
, "move", 0);
355 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_fetchAsync
, 0, 0, 0)
356 ZEND_ARG_INFO(0, spec
)
357 ZEND_ARG_INFO(0, callback
)
359 static PHP_METHOD(pqcur
, fetchAsync
)
361 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU
, "fetch", 1);
364 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_moveAsync
, 0, 0, 0)
365 ZEND_ARG_INFO(0, spec
)
366 ZEND_ARG_INFO(0, callback
)
368 static PHP_METHOD(pqcur
, moveAsync
)
370 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU
, "move", 1);
373 static zend_function_entry php_pqcur_methods
[] = {
374 PHP_ME(pqcur
, __construct
, ai_pqcur___construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
375 PHP_ME(pqcur
, open
, ai_pqcur_open
, ZEND_ACC_PUBLIC
)
376 PHP_ME(pqcur
, openAsync
, ai_pqcur_open
, ZEND_ACC_PUBLIC
)
377 PHP_ME(pqcur
, close
, ai_pqcur_close
, ZEND_ACC_PUBLIC
)
378 PHP_ME(pqcur
, closeAsync
, ai_pqcur_closeAsync
, ZEND_ACC_PUBLIC
)
379 PHP_ME(pqcur
, fetch
, ai_pqcur_fetch
, ZEND_ACC_PUBLIC
)
380 PHP_ME(pqcur
, move
, ai_pqcur_move
, ZEND_ACC_PUBLIC
)
381 PHP_ME(pqcur
, fetchAsync
, ai_pqcur_fetchAsync
, ZEND_ACC_PUBLIC
)
382 PHP_ME(pqcur
, moveAsync
, ai_pqcur_moveAsync
, ZEND_ACC_PUBLIC
)
386 PHP_MSHUTDOWN_FUNCTION(pqcur
)
388 zend_hash_destroy(&php_pqcur_object_prophandlers
);
392 PHP_MINIT_FUNCTION(pqcur
)
394 zend_class_entry ce
= {0};
395 php_pq_object_prophandler_t ph
= {0};
397 INIT_NS_CLASS_ENTRY(ce
, "pq", "Cursor", php_pqcur_methods
);
398 php_pqcur_class_entry
= zend_register_internal_class_ex(&ce
, NULL
, NULL TSRMLS_CC
);
399 php_pqcur_class_entry
->create_object
= php_pqcur_create_object
;
401 memcpy(&php_pqcur_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
402 php_pqcur_object_handlers
.read_property
= php_pq_object_read_prop
;
403 php_pqcur_object_handlers
.write_property
= php_pq_object_write_prop
;
404 php_pqcur_object_handlers
.clone_obj
= NULL
;
405 php_pqcur_object_handlers
.get_property_ptr_ptr
= NULL
;
406 php_pqcur_object_handlers
.get_gc
= NULL
;
407 php_pqcur_object_handlers
.get_properties
= php_pq_object_properties
;
408 php_pqcur_object_handlers
.get_debug_info
= php_pq_object_debug_info
;
410 zend_hash_init(&php_pqcur_object_prophandlers
, 2, NULL
, NULL
, 1);
412 zend_declare_class_constant_long(php_pqcur_class_entry
, ZEND_STRL("BINARY"), PHP_PQ_DECLARE_BINARY TSRMLS_CC
);
413 zend_declare_class_constant_long(php_pqcur_class_entry
, ZEND_STRL("INSENSITIVE"), PHP_PQ_DECLARE_INSENSITIVE TSRMLS_CC
);
414 zend_declare_class_constant_long(php_pqcur_class_entry
, ZEND_STRL("WITH_HOLD"), PHP_PQ_DECLARE_WITH_HOLD TSRMLS_CC
);
415 zend_declare_class_constant_long(php_pqcur_class_entry
, ZEND_STRL("SCROLL"), PHP_PQ_DECLARE_SCROLL TSRMLS_CC
);
416 zend_declare_class_constant_long(php_pqcur_class_entry
, ZEND_STRL("NO_SCROLL"), PHP_PQ_DECLARE_NO_SCROLL TSRMLS_CC
);
418 zend_declare_property_null(php_pqcur_class_entry
, ZEND_STRL("name"), ZEND_ACC_PUBLIC TSRMLS_CC
);
419 ph
.read
= php_pqcur_object_read_name
;
420 zend_hash_add(&php_pqcur_object_prophandlers
, "name", sizeof("name"), (void *) &ph
, sizeof(ph
), NULL
);
422 zend_declare_property_null(php_pqcur_class_entry
, ZEND_STRL("connection"), ZEND_ACC_PUBLIC TSRMLS_CC
);
423 ph
.read
= php_pqcur_object_read_connection
;
424 zend_hash_add(&php_pqcur_object_prophandlers
, "connection", sizeof("connection"), (void *) &ph
, sizeof(ph
), NULL
);
434 * vim600: noet sw=4 ts=4 fdm=marker
435 * vim<600: noet sw=4 ts=4