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 <Zend/zend_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
)
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
, smart_str_v(&cmd
))) {
44 obj
->intern
->conn
->intern
->poller
= PQconsumeInput
;
45 php_pqconn_notify_listeners(obj
->intern
->conn
);
47 throw_exce(EX_IO
, "Failed to close cursor (%s)", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
50 if ((res
= PQexec(obj
->intern
->conn
->intern
->conn
, smart_str_v(&cmd
)))) {
53 throw_exce(EX_RUNTIME
, "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
;
66 php_pqcur_object_t
*obj
;
68 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
69 rv
= zend_parse_parameters_none();
70 zend_restore_error_handling(&zeh
);
76 obj
= PHP_PQ_OBJ(getThis(), NULL
);
79 throw_exce(EX_UNINITIALIZED
, "pq\\Cursor not initialized");
81 } else if (obj
->intern
->open
) {
86 rv
= php_pqconn_declare_async(NULL
, obj
->intern
->conn
, obj
->intern
->decl
);
88 rv
= php_pqconn_declare(NULL
, obj
->intern
->conn
, obj
->intern
->decl
);
92 obj
->intern
->open
= 1;
96 static void cur_fetch_or_move(INTERNAL_FUNCTION_PARAMETERS
, const char *action
, zend_bool async
)
101 php_pq_callback_t resolver
= {{0}};
102 zend_error_handling zeh
;
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
);
109 php_pqcur_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
112 throw_exce(EX_UNINITIALIZED
, "pq\\Cursor not initialized");
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
);
123 int rc
= PQsendQuery(obj
->intern
->conn
->intern
->conn
, smart_str_v(&cmd
));
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
));
132 php_pq_callback_recurse(&obj
->intern
->conn
->intern
->onevent
, &resolver
);
133 obj
->intern
->conn
->intern
->poller
= PQconsumeInput
;
136 PGresult
*res
= PQexec(obj
->intern
->conn
->intern
->conn
, smart_str_v(&cmd
));
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
);
145 smart_str_free(&cmd
);
146 php_pqconn_notify_listeners(obj
->intern
->conn
);
151 static void php_pqcur_object_free(zend_object
*o
)
153 php_pqcur_object_t
*obj
= PHP_PQ_OBJ(NULL
, o
);
155 fprintf(stderr
, "FREE cur(#%d) %p (conn: %p)\n", obj
->zv
.handle
, obj
, obj
->intern
->conn
);
158 cur_close(obj
, 0, 1);
159 php_pq_object_delref(obj
->intern
->conn
);
160 efree(obj
->intern
->decl
);
161 efree(obj
->intern
->name
);
165 zend_object_std_dtor(o
);
169 php_pqcur_object_t
*php_pqcur_create_object_ex(zend_class_entry
*ce
, php_pqcur_t
*intern
)
171 php_pqcur_object_t
*o
;
173 o
= ecalloc(1, sizeof(*o
) + zend_object_properties_size(ce
));
174 zend_object_std_init(&o
->zo
, ce
);
175 object_properties_init(&o
->zo
, ce
);
176 o
->prophandler
= &php_pqcur_object_prophandlers
;
182 o
->zo
.handlers
= &php_pqcur_object_handlers
;
187 static zend_object
*php_pqcur_create_object(zend_class_entry
*class_type
)
189 return &php_pqcur_create_object_ex(class_type
, NULL
)->zo
;
192 static void php_pqcur_object_read_name(zval
*object
, void *o
, zval
*return_value
)
194 php_pqcur_object_t
*obj
= o
;
196 RETVAL_STRING(obj
->intern
->name
);
199 static void php_pqcur_object_read_connection(zval
*object
, void *o
, zval
*return_value
)
201 php_pqcur_object_t
*obj
= o
;
203 php_pq_object_to_zval(obj
->intern
->conn
, return_value
);
206 static void php_pqcur_object_read_query(zval
*object
, void *o
, zval
*return_value
)
208 php_pqcur_object_t
*obj
= o
;
210 RETVAL_STRING(obj
->intern
->decl
+ obj
->intern
->query_offset
);
213 static void php_pqcur_object_read_flags(zval
*object
, void *o
, zval
*return_value
)
215 php_pqcur_object_t
*obj
= o
;
217 RETVAL_LONG(obj
->intern
->flags
);
220 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
)
222 size_t decl_len
= name_len
+ query_len
+ sizeof("DECLARE BINARY INSENSITIVE NO SCROLL CURSOR WITH HOLD FOR ");
225 decl_str
= emalloc(decl_len
);
226 decl_len
= slprintf(decl_str
, decl_len
, "DECLARE %s %s %s %s CURSOR %s FOR %s",
228 (flags
& PHP_PQ_DECLARE_BINARY
) ? "BINARY" : "",
229 (flags
& PHP_PQ_DECLARE_INSENSITIVE
) ? "INSENSITIVE" : "",
230 (flags
& PHP_PQ_DECLARE_NO_SCROLL
) ? "NO SCROLL" :
231 (flags
& PHP_PQ_DECLARE_SCROLL
) ? "SCROLL" : "",
232 (flags
& PHP_PQ_DECLARE_WITH_HOLD
) ? "WITH HOLD" : "",
237 /* sizeof() includes the terminating null byte, so no need for spaces in the string literals */
238 *query_offset
= sizeof("DECLARE")
240 + ((flags
& PHP_PQ_DECLARE_BINARY
) ? sizeof("BINARY") : 1)
241 + ((flags
& PHP_PQ_DECLARE_INSENSITIVE
) ? sizeof("INSENSITIVE") : 1)
242 + ((flags
& PHP_PQ_DECLARE_NO_SCROLL
) ? sizeof("NO SCROLL") :
243 (flags
& PHP_PQ_DECLARE_SCROLL
) ? sizeof("SCROLL") : 1)
245 + ((flags
& PHP_PQ_DECLARE_WITH_HOLD
) ? sizeof("WITH HOLD") : 1)
252 php_pqcur_t
*php_pqcur_init(php_pqconn_object_t
*conn
, const char *name
, char *decl
, int query_offset
, long flags
)
254 php_pqcur_t
*cur
= ecalloc(1, sizeof(*cur
));
256 php_pq_object_addref(conn
);
258 cur
->name
= estrdup(name
);
260 cur
->query_offset
= query_offset
;
267 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur___construct
, 0, 0, 4)
268 ZEND_ARG_OBJ_INFO(0, connection
, pq
\\Connection
, 0)
269 ZEND_ARG_INFO(0, name
)
270 ZEND_ARG_INFO(0, flags
)
271 ZEND_ARG_INFO(0, query
)
272 ZEND_ARG_INFO(0, async
)
274 static PHP_METHOD(pqcur
, __construct
) {
275 zend_error_handling zeh
;
276 char *name_str
, *query_str
;
277 size_t name_len
, query_len
;
283 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
284 rv
= zend_parse_parameters(ZEND_NUM_ARGS(), "Osls|b", &zconn
, php_pqconn_class_entry
, &name_str
, &name_len
, &flags
, &query_str
, &query_len
, &async
);
285 zend_restore_error_handling(&zeh
);
288 php_pqcur_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
289 php_pqconn_object_t
*conn_obj
= PHP_PQ_OBJ(zconn
, NULL
);
292 throw_exce(EX_BAD_METHODCALL
, "pq\\Cursor already initialized");
293 } if (!conn_obj
->intern
) {
294 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
297 char *decl
= php_pqcur_declare_str(name_str
, name_len
, flags
, query_str
, query_len
, &query_offset
);
300 rv
= php_pqconn_declare_async(zconn
, conn_obj
, decl
);
302 rv
= php_pqconn_declare(zconn
, conn_obj
, decl
);
308 obj
->intern
= php_pqcur_init(conn_obj
, name_str
, decl
, query_offset
, flags
);
314 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_open
, 0, 0, 0)
316 static PHP_METHOD(pqcur
, open
)
318 cur_open(INTERNAL_FUNCTION_PARAM_PASSTHRU
, 0);
321 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_openAsync
, 0, 0, 0)
323 static PHP_METHOD(pqcur
, openAsync
)
325 cur_open(INTERNAL_FUNCTION_PARAM_PASSTHRU
, 1);
328 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_close
, 0, 0, 0)
330 static PHP_METHOD(pqcur
, close
)
332 zend_error_handling zeh
;
335 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
336 rv
= zend_parse_parameters_none();
337 zend_restore_error_handling(&zeh
);
340 php_pqcur_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
343 throw_exce(EX_UNINITIALIZED
, "pq\\Cursor not initialized");
345 cur_close(obj
, 0, 0);
350 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_closeAsync
, 0, 0, 0)
352 static PHP_METHOD(pqcur
, closeAsync
)
354 zend_error_handling zeh
;
357 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
358 rv
= zend_parse_parameters_none();
359 zend_restore_error_handling(&zeh
);
362 php_pqcur_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
365 throw_exce(EX_UNINITIALIZED
, "pq\\Cursor not initialized");
367 cur_close(obj
, 1, 0);
372 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_fetch
, 0, 0, 1)
373 ZEND_ARG_INFO(0, spec
)
375 static PHP_METHOD(pqcur
, fetch
)
377 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU
, "fetch", 0);
380 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_move
, 0, 0, 0)
381 ZEND_ARG_INFO(0, spec
)
383 static PHP_METHOD(pqcur
, move
)
385 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU
, "move", 0);
388 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_fetchAsync
, 0, 0, 0)
389 ZEND_ARG_INFO(0, spec
)
390 ZEND_ARG_INFO(0, callback
)
392 static PHP_METHOD(pqcur
, fetchAsync
)
394 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU
, "fetch", 1);
397 ZEND_BEGIN_ARG_INFO_EX(ai_pqcur_moveAsync
, 0, 0, 0)
398 ZEND_ARG_INFO(0, spec
)
399 ZEND_ARG_INFO(0, callback
)
401 static PHP_METHOD(pqcur
, moveAsync
)
403 cur_fetch_or_move(INTERNAL_FUNCTION_PARAM_PASSTHRU
, "move", 1);
406 static zend_function_entry php_pqcur_methods
[] = {
407 PHP_ME(pqcur
, __construct
, ai_pqcur___construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
408 PHP_ME(pqcur
, open
, ai_pqcur_open
, ZEND_ACC_PUBLIC
)
409 PHP_ME(pqcur
, openAsync
, ai_pqcur_openAsync
, ZEND_ACC_PUBLIC
)
410 PHP_ME(pqcur
, close
, ai_pqcur_close
, ZEND_ACC_PUBLIC
)
411 PHP_ME(pqcur
, closeAsync
, ai_pqcur_closeAsync
, ZEND_ACC_PUBLIC
)
412 PHP_ME(pqcur
, fetch
, ai_pqcur_fetch
, ZEND_ACC_PUBLIC
)
413 PHP_ME(pqcur
, move
, ai_pqcur_move
, ZEND_ACC_PUBLIC
)
414 PHP_ME(pqcur
, fetchAsync
, ai_pqcur_fetchAsync
, ZEND_ACC_PUBLIC
)
415 PHP_ME(pqcur
, moveAsync
, ai_pqcur_moveAsync
, ZEND_ACC_PUBLIC
)
419 PHP_MSHUTDOWN_FUNCTION(pqcur
)
421 zend_hash_destroy(&php_pqcur_object_prophandlers
);
425 PHP_MINIT_FUNCTION(pqcur
)
427 zend_class_entry ce
= {0};
428 php_pq_object_prophandler_t ph
= {0};
430 INIT_NS_CLASS_ENTRY(ce
, "pq", "Cursor", php_pqcur_methods
);
431 php_pqcur_class_entry
= zend_register_internal_class_ex(&ce
, NULL
);
432 php_pqcur_class_entry
->create_object
= php_pqcur_create_object
;
434 memcpy(&php_pqcur_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
435 php_pqcur_object_handlers
.offset
= XtOffsetOf(php_pqcur_object_t
, zo
);
436 php_pqcur_object_handlers
.free_obj
= php_pqcur_object_free
;
437 php_pqcur_object_handlers
.read_property
= php_pq_object_read_prop
;
438 php_pqcur_object_handlers
.write_property
= php_pq_object_write_prop
;
439 php_pqcur_object_handlers
.clone_obj
= NULL
;
440 php_pqcur_object_handlers
.get_property_ptr_ptr
= NULL
;
441 php_pqcur_object_handlers
.get_gc
= NULL
;
442 php_pqcur_object_handlers
.get_properties
= php_pq_object_properties
;
443 php_pqcur_object_handlers
.get_debug_info
= php_pq_object_debug_info
;
445 zend_hash_init(&php_pqcur_object_prophandlers
, 4, NULL
, NULL
, 1);
447 zend_declare_class_constant_long(php_pqcur_class_entry
, ZEND_STRL("BINARY"), PHP_PQ_DECLARE_BINARY
);
448 zend_declare_class_constant_long(php_pqcur_class_entry
, ZEND_STRL("INSENSITIVE"), PHP_PQ_DECLARE_INSENSITIVE
);
449 zend_declare_class_constant_long(php_pqcur_class_entry
, ZEND_STRL("WITH_HOLD"), PHP_PQ_DECLARE_WITH_HOLD
);
450 zend_declare_class_constant_long(php_pqcur_class_entry
, ZEND_STRL("SCROLL"), PHP_PQ_DECLARE_SCROLL
);
451 zend_declare_class_constant_long(php_pqcur_class_entry
, ZEND_STRL("NO_SCROLL"), PHP_PQ_DECLARE_NO_SCROLL
);
453 zend_declare_property_null(php_pqcur_class_entry
, ZEND_STRL("name"), ZEND_ACC_PUBLIC
);
454 ph
.read
= php_pqcur_object_read_name
;
455 zend_hash_str_add_mem(&php_pqcur_object_prophandlers
, "name", sizeof("name")-1, (void *) &ph
, sizeof(ph
));
457 zend_declare_property_null(php_pqcur_class_entry
, ZEND_STRL("connection"), ZEND_ACC_PUBLIC
);
458 ph
.read
= php_pqcur_object_read_connection
;
459 zend_hash_str_add_mem(&php_pqcur_object_prophandlers
, "connection", sizeof("connection")-1, (void *) &ph
, sizeof(ph
));
461 zend_declare_property_null(php_pqcur_class_entry
, ZEND_STRL("query"), ZEND_ACC_PUBLIC
);
462 ph
.read
= php_pqcur_object_read_query
;
463 zend_hash_str_add_mem(&php_pqcur_object_prophandlers
, "query", sizeof("query")-1, (void *) &ph
, sizeof(ph
));
465 zend_declare_property_null(php_pqcur_class_entry
, ZEND_STRL("flags"), ZEND_ACC_PUBLIC TSRMLS_CC
);
466 ph
.read
= php_pqcur_object_read_flags
;
467 zend_hash_str_add_mem(&php_pqcur_object_prophandlers
, "flags", sizeof("flags")-1, (void *) &ph
, sizeof(ph
));
477 * vim600: noet sw=4 ts=4 fdm=marker
478 * vim<600: noet sw=4 ts=4