X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-pq;a=blobdiff_plain;f=src%2Fphp_pqcur.c;h=58ffd866c65128bfedf3d433cfbded2f2fb296cf;hp=2df4f73f3b7dacf71724d40e125d527c169c2afe;hb=83b4777e8aee1a30a29d41971b0f2b8315e6b501;hpb=ed63ffdb6068a001a65a9e0e892079f97594afed diff --git a/src/php_pqcur.c b/src/php_pqcur.c index 2df4f73..58ffd86 100644 --- a/src/php_pqcur.c +++ b/src/php_pqcur.c @@ -62,7 +62,8 @@ static void cur_close(php_pqcur_object_t *obj, zend_bool async, zend_bool silent static void cur_open(INTERNAL_FUNCTION_PARAMETERS, zend_bool async) { zend_error_handling zeh; - STATUS rv; + ZEND_RESULT_CODE rv; + php_pqcur_object_t *obj; zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); rv = zend_parse_parameters_none(); @@ -72,7 +73,7 @@ static void cur_open(INTERNAL_FUNCTION_PARAMETERS, zend_bool async) return; } - php_pqcur_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + obj = zend_object_store_get_object(getThis() TSRMLS_CC); if (!obj->intern) { throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Cursor not initialized"); @@ -96,7 +97,7 @@ static void cur_fetch_or_move(INTERNAL_FUNCTION_PARAMETERS, const char *action, { char *spec_str = "1"; int spec_len = 1; - STATUS rv; + ZEND_RESULT_CODE rv; php_pq_callback_t resolver = {{0}}; zend_error_handling zeh; @@ -207,6 +208,13 @@ static void php_pqcur_object_read_connection(zval *object, void *o, zval *return php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC); } +static void php_pqcur_object_read_query(zval *object, void *o, zval *return_value TSRMLS_DC) +{ + php_pqcur_object_t *obj = o; + + RETVAL_STRING(obj->intern->decl + obj->intern->query_offset, 1); +} + static void php_pqcur_object_read_flags(zval *object, void *o, zval *return_value TSRMLS_DC) { php_pqcur_object_t *obj = o; @@ -214,7 +222,7 @@ static void php_pqcur_object_read_flags(zval *object, void *o, zval *return_valu RETVAL_LONG(obj->intern->flags); } -char *php_pqcur_declare_str(const char *name_str, size_t name_len, unsigned flags, const char *query_str, size_t query_len) +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) { size_t decl_len = name_len + query_len + sizeof("DECLARE BINARY INSENSITIVE NO SCROLL CURSOR WITH HOLD FOR "); char *decl_str; @@ -229,9 +237,38 @@ char *php_pqcur_declare_str(const char *name_str, size_t name_len, unsigned flag (flags & PHP_PQ_DECLARE_WITH_HOLD) ? "WITH HOLD" : "", query_str ); + + if (query_offset) { + /* sizeof() includes the terminating null byte, so no need for spaces in the string literals */ + *query_offset = sizeof("DECLARE") + + (name_len + 1) + + ((flags & PHP_PQ_DECLARE_BINARY) ? sizeof("BINARY") : 1) + + ((flags & PHP_PQ_DECLARE_INSENSITIVE) ? sizeof("INSENSITIVE") : 1) + + ((flags & PHP_PQ_DECLARE_NO_SCROLL) ? sizeof("NO SCROLL") : + (flags & PHP_PQ_DECLARE_SCROLL) ? sizeof("SCROLL") : 1) + + sizeof("CURSOR") + + ((flags & PHP_PQ_DECLARE_WITH_HOLD) ? sizeof("WITH HOLD") : 1) + + sizeof("FOR"); + } + return decl_str; } +php_pqcur_t *php_pqcur_init(php_pqconn_object_t *conn, const char *name, char *decl, int query_offset, long flags TSRMLS_DC) +{ + php_pqcur_t *cur = ecalloc(1, sizeof(*cur)); + + php_pq_object_addref(conn TSRMLS_CC); + cur->conn = conn; + cur->name = estrdup(name); + cur->decl = decl; + cur->query_offset = query_offset; + cur->flags = flags; + cur->open = 1; + + return cur; +} + ZEND_BEGIN_ARG_INFO_EX(ai_pqcur___construct, 0, 0, 4) ZEND_ARG_OBJ_INFO(0, connection, pq\\Connection, 0) ZEND_ARG_INFO(0, name) @@ -245,7 +282,7 @@ static PHP_METHOD(pqcur, __construct) { int name_len, query_len; long flags; zval *zconn; - STATUS rv; + ZEND_RESULT_CODE rv; zend_bool async = 0; zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); @@ -261,7 +298,8 @@ static PHP_METHOD(pqcur, __construct) { } if (!conn_obj->intern) { throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized"); } else { - char *decl = php_pqcur_declare_str(name_str, name_len, flags, query_str, query_len); + int query_offset; + char *decl = php_pqcur_declare_str(name_str, name_len, flags, query_str, query_len, &query_offset); if (async) { rv = php_pqconn_declare_async(zconn, conn_obj, decl TSRMLS_CC); @@ -272,15 +310,7 @@ static PHP_METHOD(pqcur, __construct) { if (SUCCESS != rv) { efree(decl); } else { - php_pqcur_t *cur = ecalloc(1, sizeof(*cur)); - - php_pq_object_addref(conn_obj TSRMLS_CC); - cur->conn = conn_obj; - cur->open = 1; - cur->name = estrdup(name_str); - cur->decl = decl; - cur->flags = flags; - obj->intern = cur; + obj->intern = php_pqcur_init(conn_obj, name_str, decl, query_offset, flags TSRMLS_CC); } } } @@ -305,7 +335,7 @@ ZEND_END_ARG_INFO(); static PHP_METHOD(pqcur, close) { zend_error_handling zeh; - STATUS rv; + ZEND_RESULT_CODE rv; zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); rv = zend_parse_parameters_none(); @@ -327,7 +357,7 @@ ZEND_END_ARG_INFO(); static PHP_METHOD(pqcur, closeAsync) { zend_error_handling zeh; - STATUS rv; + ZEND_RESULT_CODE rv; zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC); rv = zend_parse_parameters_none(); @@ -415,7 +445,7 @@ PHP_MINIT_FUNCTION(pqcur) php_pqcur_object_handlers.get_properties = php_pq_object_properties; php_pqcur_object_handlers.get_debug_info = php_pq_object_debug_info; - zend_hash_init(&php_pqcur_object_prophandlers, 2, NULL, NULL, 1); + zend_hash_init(&php_pqcur_object_prophandlers, 4, NULL, NULL, 1); zend_declare_class_constant_long(php_pqcur_class_entry, ZEND_STRL("BINARY"), PHP_PQ_DECLARE_BINARY TSRMLS_CC); zend_declare_class_constant_long(php_pqcur_class_entry, ZEND_STRL("INSENSITIVE"), PHP_PQ_DECLARE_INSENSITIVE TSRMLS_CC); @@ -431,6 +461,10 @@ PHP_MINIT_FUNCTION(pqcur) ph.read = php_pqcur_object_read_connection; zend_hash_add(&php_pqcur_object_prophandlers, "connection", sizeof("connection"), (void *) &ph, sizeof(ph), NULL); + zend_declare_property_null(php_pqcur_class_entry, ZEND_STRL("query"), ZEND_ACC_PUBLIC TSRMLS_CC); + ph.read = php_pqcur_object_read_query; + zend_hash_add(&php_pqcur_object_prophandlers, "query", sizeof("query"), (void *) &ph, sizeof(ph), NULL); + zend_declare_property_null(php_pqcur_class_entry, ZEND_STRL("flags"), ZEND_ACC_PUBLIC TSRMLS_CC); ph.read = php_pqcur_object_read_flags; zend_hash_add(&php_pqcur_object_prophandlers, "flags", sizeof("flags"), (void *) &ph, sizeof(ph), NULL);