mirror of
https://github.com/danog/ext-pq.git
synced 2024-12-02 09:18:02 +01:00
refactor type conversion
This commit is contained in:
parent
d613997b30
commit
218002ba90
@ -155,6 +155,7 @@ typedef struct _HashTableList {
|
||||
typedef struct _ArrayParserState {
|
||||
const char *ptr, *end;
|
||||
HashTableList *list;
|
||||
php_pqres_t *res;
|
||||
#ifdef ZTS
|
||||
void ***ts;
|
||||
#endif
|
||||
@ -201,7 +202,7 @@ static STATUS add_element(ArrayParserState *a, const char *start)
|
||||
MAKE_STD_ZVAL(zelem);
|
||||
ZVAL_NULL(zelem);
|
||||
} else {
|
||||
zelem = php_pq_typed_zval(el_str, el_len, a->typ TSRMLS_CC);
|
||||
zelem = php_pqres_typed_zval(a->res, el_str, el_len, a->typ TSRMLS_CC);
|
||||
|
||||
efree(el_str);
|
||||
}
|
||||
@ -328,7 +329,7 @@ static STATUS parse_array(ArrayParserState *a)
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
HashTable *php_pq_parse_array(const char *val_str, size_t val_len, Oid typ TSRMLS_DC)
|
||||
HashTable *php_pq_parse_array(php_pqres_t *res, const char *val_str, size_t val_len, Oid typ TSRMLS_DC)
|
||||
{
|
||||
HashTable *ht = NULL;
|
||||
ArrayParserState a = {0};
|
||||
@ -337,6 +338,7 @@ HashTable *php_pq_parse_array(const char *val_str, size_t val_len, Oid typ TSRML
|
||||
a.typ = typ;
|
||||
a.ptr = val_str;
|
||||
a.end = val_str + val_len;
|
||||
a.res = res;
|
||||
|
||||
if (SUCCESS != parse_array(&a)) {
|
||||
while (a.list) {
|
||||
@ -360,70 +362,6 @@ HashTable *php_pq_parse_array(const char *val_str, size_t val_len, Oid typ TSRML
|
||||
return ht;
|
||||
}
|
||||
|
||||
zval *php_pq_typed_zval(char *val, size_t len, Oid typ TSRMLS_DC)
|
||||
{
|
||||
zval *zv;
|
||||
|
||||
MAKE_STD_ZVAL(zv);
|
||||
|
||||
switch (typ) {
|
||||
#ifdef HAVE_PHP_PQ_TYPE_H
|
||||
# undef PHP_PQ_TYPE
|
||||
# include "php_pq_type.h"
|
||||
case PHP_PQ_OID_BOOL:
|
||||
ZVAL_BOOL(zv, *val == 't');
|
||||
break;
|
||||
#if SIZEOF_LONG >= 8
|
||||
case PHP_PQ_OID_INT8:
|
||||
case PHP_PQ_OID_TID:
|
||||
#endif
|
||||
case PHP_PQ_OID_INT4:
|
||||
case PHP_PQ_OID_INT2:
|
||||
case PHP_PQ_OID_XID:
|
||||
case PHP_PQ_OID_OID:
|
||||
ZVAL_LONG(zv, zend_atol(val, len));
|
||||
break;
|
||||
|
||||
case PHP_PQ_OID_FLOAT4:
|
||||
case PHP_PQ_OID_FLOAT8:
|
||||
ZVAL_DOUBLE(zv, zend_strtod(val, NULL));
|
||||
break;
|
||||
|
||||
case PHP_PQ_OID_DATE:
|
||||
php_pqdt_from_string(val, len, "Y-m-d", zv TSRMLS_CC);
|
||||
break;
|
||||
|
||||
case PHP_PQ_OID_ABSTIME:
|
||||
php_pqdt_from_string(val, len, "Y-m-d H:i:s", zv TSRMLS_CC);
|
||||
break;
|
||||
|
||||
case PHP_PQ_OID_TIMESTAMP:
|
||||
php_pqdt_from_string(val, len, "Y-m-d H:i:s.u", zv TSRMLS_CC);
|
||||
break;
|
||||
|
||||
case PHP_PQ_OID_TIMESTAMPTZ:
|
||||
php_pqdt_from_string(val, len, "Y-m-d H:i:s.uO", zv TSRMLS_CC);
|
||||
break;
|
||||
|
||||
default:
|
||||
if (PHP_PQ_TYPE_IS_ARRAY(typ) && (Z_ARRVAL_P(zv) = php_pq_parse_array(val, len, PHP_PQ_TYPE_OF_ARRAY(typ) TSRMLS_CC))) {
|
||||
Z_TYPE_P(zv) = IS_ARRAY;
|
||||
} else {
|
||||
ZVAL_STRINGL(zv, val, len, 1);
|
||||
}
|
||||
break;
|
||||
#else
|
||||
case 16: /* BOOL */
|
||||
ZVAL_BOOL(zv, *val == 't');
|
||||
break;
|
||||
|
||||
default:
|
||||
ZVAL_STRINGL(zv, val, len, 1);
|
||||
#endif
|
||||
}
|
||||
|
||||
return zv;
|
||||
}
|
||||
|
||||
/*
|
||||
* Local variables:
|
||||
|
@ -15,6 +15,7 @@
|
||||
#define PHP_PQ_ERROR_H
|
||||
|
||||
#include <libpq-fe.h>
|
||||
#include "php_pqres.h"
|
||||
|
||||
typedef int STATUS; /* SUCCESS/FAILURE */
|
||||
|
||||
@ -42,8 +43,7 @@ zval *php_pqdt_from_string(char *datetime_str, size_t datetime_len, char *fmt, z
|
||||
|
||||
zend_class_entry *php_pqconv_class_entry;
|
||||
|
||||
HashTable *php_pq_parse_array(const char *val_str, size_t val_len, Oid typ TSRMLS_DC);
|
||||
zval *php_pq_typed_zval(char *val_str, size_t val_len, Oid typ TSRMLS_DC);
|
||||
HashTable *php_pq_parse_array(php_pqres_t *res, const char *val_str, size_t val_len, Oid typ TSRMLS_DC);
|
||||
|
||||
PHP_MINIT_FUNCTION(pq_misc);
|
||||
|
||||
|
@ -1798,12 +1798,17 @@ static int apply_set_converter(void *p TSRMLS_DC, int argc, va_list argv, zend_h
|
||||
{
|
||||
zval *tmp, **zoid = p, **zcnv = va_arg(argv, zval **);
|
||||
HashTable *converters = va_arg(argv, HashTable *);
|
||||
int add = va_arg(argv, int);
|
||||
|
||||
tmp = *zoid;
|
||||
Z_ADDREF_P(tmp);
|
||||
convert_to_long_ex(&tmp);
|
||||
Z_ADDREF_PP(zcnv);
|
||||
zend_hash_index_update(converters, Z_LVAL_P(tmp), zcnv, sizeof(zval *), NULL);
|
||||
if (add) {
|
||||
Z_ADDREF_PP(zcnv);
|
||||
zend_hash_index_update(converters, Z_LVAL_P(tmp), zcnv, sizeof(zval *), NULL);
|
||||
} else {
|
||||
zend_hash_index_del(converters, Z_LVAL_P(tmp));
|
||||
}
|
||||
zval_ptr_dtor(&tmp);
|
||||
|
||||
return ZEND_HASH_APPLY_KEEP;
|
||||
@ -1833,7 +1838,38 @@ static PHP_METHOD(pqconn, setConverter) {
|
||||
tmp = zoids;
|
||||
Z_ADDREF_P(tmp);
|
||||
convert_to_array_ex(&tmp);
|
||||
zend_hash_apply_with_arguments(Z_ARRVAL_P(tmp) TSRMLS_CC, apply_set_converter, 2, &zcnv, &obj->intern->converters);
|
||||
zend_hash_apply_with_arguments(Z_ARRVAL_P(tmp) TSRMLS_CC, apply_set_converter, 3, &zcnv, &obj->intern->converters, 1);
|
||||
zval_ptr_dtor(&tmp);
|
||||
zval_ptr_dtor(&zoids);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ZEND_BEGIN_ARG_INFO_EX(ai_pqconn_unset_converter, 0, 0, 1)
|
||||
ZEND_ARG_OBJ_INFO(0, converter, pq\\ConverterInterface, 0)
|
||||
ZEND_END_ARG_INFO();
|
||||
static PHP_METHOD(pqconn, unsetConverter) {
|
||||
STATUS rv;
|
||||
zend_error_handling zeh;
|
||||
zval *zcnv;
|
||||
|
||||
zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
|
||||
rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zcnv, php_pqconv_class_entry);
|
||||
zend_restore_error_handling(&zeh TSRMLS_CC);
|
||||
|
||||
if (SUCCESS == rv) {
|
||||
php_pqconn_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
|
||||
|
||||
if (!obj->intern) {
|
||||
throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
|
||||
} else {
|
||||
zval *tmp, *zoids = NULL;
|
||||
|
||||
zend_call_method_with_0_params(&zcnv, NULL, NULL, "converttypes", &zoids);
|
||||
tmp = zoids;
|
||||
Z_ADDREF_P(tmp);
|
||||
convert_to_array_ex(&tmp);
|
||||
zend_hash_apply_with_arguments(Z_ARRVAL_P(tmp) TSRMLS_CC, apply_set_converter, 3, &zcnv, &obj->intern->converters, 0);
|
||||
zval_ptr_dtor(&tmp);
|
||||
zval_ptr_dtor(&zoids);
|
||||
}
|
||||
@ -1870,6 +1906,7 @@ static zend_function_entry php_pqconn_methods[] = {
|
||||
PHP_ME(pqconn, off, ai_pqconn_off, ZEND_ACC_PUBLIC)
|
||||
PHP_ME(pqconn, on, ai_pqconn_on, ZEND_ACC_PUBLIC)
|
||||
PHP_ME(pqconn, setConverter, ai_pqconn_set_converter, ZEND_ACC_PUBLIC)
|
||||
PHP_ME(pqconn, unsetConverter, ai_pqconn_unset_converter, ZEND_ACC_PUBLIC)
|
||||
{0}
|
||||
};
|
||||
|
||||
|
122
src/php_pqres.c
122
src/php_pqres.c
@ -88,6 +88,111 @@ static STATUS php_pqres_iterator_valid(zend_object_iterator *i TSRMLS_DC)
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
zval *php_pqres_typed_zval(php_pqres_t *res, char *val, size_t len, Oid typ TSRMLS_DC)
|
||||
{
|
||||
zval *zv, **zconv;
|
||||
|
||||
MAKE_STD_ZVAL(zv);
|
||||
|
||||
if (SUCCESS == zend_hash_index_find(&res->converters, typ, (void *) &zconv)) {
|
||||
zval *tmp = NULL;
|
||||
|
||||
ZVAL_STRINGL(zv, val, len, 1);
|
||||
zend_call_method_with_1_params(zconv, NULL, NULL, "convertfromstring", &tmp, zv);
|
||||
|
||||
if (tmp) {
|
||||
zval_ptr_dtor(&zv);
|
||||
zv = tmp;
|
||||
}
|
||||
|
||||
return zv;
|
||||
}
|
||||
|
||||
switch (typ) {
|
||||
#ifdef HAVE_PHP_PQ_TYPE_H
|
||||
# undef PHP_PQ_TYPE
|
||||
# include "php_pq_type.h"
|
||||
case PHP_PQ_OID_BOOL:
|
||||
if (!(res->auto_convert & PHP_PQRES_CONV_BOOL)) {
|
||||
goto noconversion;
|
||||
}
|
||||
ZVAL_BOOL(zv, *val == 't');
|
||||
break;
|
||||
#if SIZEOF_LONG >= 8
|
||||
case PHP_PQ_OID_INT8:
|
||||
case PHP_PQ_OID_TID:
|
||||
#endif
|
||||
case PHP_PQ_OID_INT4:
|
||||
case PHP_PQ_OID_INT2:
|
||||
case PHP_PQ_OID_XID:
|
||||
case PHP_PQ_OID_OID:
|
||||
if (!(res->auto_convert & PHP_PQRES_CONV_INT)) {
|
||||
goto noconversion;
|
||||
}
|
||||
ZVAL_LONG(zv, zend_atol(val, len));
|
||||
break;
|
||||
|
||||
case PHP_PQ_OID_FLOAT4:
|
||||
case PHP_PQ_OID_FLOAT8:
|
||||
if (!(res->auto_convert & PHP_PQRES_CONV_FLOAT)) {
|
||||
goto noconversion;
|
||||
}
|
||||
ZVAL_DOUBLE(zv, zend_strtod(val, NULL));
|
||||
break;
|
||||
|
||||
case PHP_PQ_OID_DATE:
|
||||
if (!(res->auto_convert & PHP_PQRES_CONV_DATETIME)) {
|
||||
goto noconversion;
|
||||
}
|
||||
php_pqdt_from_string(val, len, "Y-m-d", zv TSRMLS_CC);
|
||||
break;
|
||||
|
||||
case PHP_PQ_OID_ABSTIME:
|
||||
if (!(res->auto_convert & PHP_PQRES_CONV_DATETIME)) {
|
||||
goto noconversion;
|
||||
}
|
||||
php_pqdt_from_string(val, len, "Y-m-d H:i:s", zv TSRMLS_CC);
|
||||
break;
|
||||
|
||||
case PHP_PQ_OID_TIMESTAMP:
|
||||
if (!(res->auto_convert & PHP_PQRES_CONV_DATETIME)) {
|
||||
goto noconversion;
|
||||
}
|
||||
php_pqdt_from_string(val, len, "Y-m-d H:i:s.u", zv TSRMLS_CC);
|
||||
break;
|
||||
|
||||
case PHP_PQ_OID_TIMESTAMPTZ:
|
||||
if (!(res->auto_convert & PHP_PQRES_CONV_DATETIME)) {
|
||||
goto noconversion;
|
||||
}
|
||||
php_pqdt_from_string(val, len, "Y-m-d H:i:s.uO", zv TSRMLS_CC);
|
||||
break;
|
||||
|
||||
default:
|
||||
if (!(res->auto_convert & PHP_PQRES_CONV_ARRAY)) {
|
||||
goto noconversion;
|
||||
}
|
||||
if (PHP_PQ_TYPE_IS_ARRAY(typ) && (Z_ARRVAL_P(zv) = php_pq_parse_array(res, val, len, PHP_PQ_TYPE_OF_ARRAY(typ) TSRMLS_CC))) {
|
||||
Z_TYPE_P(zv) = IS_ARRAY;
|
||||
} else {
|
||||
noconversion:
|
||||
ZVAL_STRINGL(zv, val, len, 1);
|
||||
}
|
||||
break;
|
||||
#else
|
||||
case 16: /* BOOL */
|
||||
if (res->auto_convert & PHP_PQRES_CONV_BOOL) {
|
||||
ZVAL_BOOL(zv, *val == 't');
|
||||
break;
|
||||
}
|
||||
default:
|
||||
ZVAL_STRINGL(zv, val, len, 1);
|
||||
#endif
|
||||
}
|
||||
|
||||
return zv;
|
||||
}
|
||||
|
||||
zval *php_pqres_row_to_zval(PGresult *res, unsigned row, php_pqres_fetch_t fetch_type, zval **data_ptr TSRMLS_DC)
|
||||
{
|
||||
zval *data = NULL;
|
||||
@ -126,22 +231,9 @@ zval *php_pqres_row_to_zval(PGresult *res, unsigned row, php_pqres_fetch_t fetch
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
zval *zv, **zconv;
|
||||
zval *zv;
|
||||
|
||||
if (res_obj && (SUCCESS == zend_hash_index_find(&res_obj->intern->converters, PQftype(res, c), (void *) &zconv))) {
|
||||
zval *tmp = NULL;
|
||||
|
||||
MAKE_STD_ZVAL(zv);
|
||||
ZVAL_STRINGL(zv, PQgetvalue(res, row, c), PQgetlength(res, row, c), 1);
|
||||
zend_call_method_with_1_params(zconv, NULL, NULL, "convertfromstring", &tmp, zv);
|
||||
|
||||
if (tmp) {
|
||||
zval_ptr_dtor(&zv);
|
||||
zv = tmp;
|
||||
}
|
||||
} else {
|
||||
zv = php_pq_typed_zval(PQgetvalue(res, row, c), PQgetlength(res, row, c), PQftype(res, c) TSRMLS_CC);
|
||||
}
|
||||
zv = php_pq_typed_zval(&res_obj->intern->converters, PQgetvalue(res, row, c), PQgetlength(res, row, c), PQftype(res, c) TSRMLS_CC);
|
||||
|
||||
switch (fetch_type) {
|
||||
case PHP_PQRES_FETCH_OBJECT:
|
||||
|
@ -21,6 +21,12 @@ typedef enum php_pqres_fetch {
|
||||
PHP_PQRES_FETCH_OBJECT
|
||||
} php_pqres_fetch_t;
|
||||
|
||||
#define PHP_PQRES_CONV_BOOL 0x01
|
||||
#define PHP_PQRES_CONV_INT 0x02
|
||||
#define PHP_PQRES_CONV_FLOAT 0x04
|
||||
#define PHP_PQRES_CONV_ARRAY 0x08
|
||||
#define PHP_PQRES_CONV_DATETIME 0x10
|
||||
|
||||
typedef struct php_pqres_iterator {
|
||||
zend_object_iterator zi;
|
||||
zval *current_val;
|
||||
@ -34,6 +40,7 @@ typedef struct php_pqres {
|
||||
HashTable bound;
|
||||
HashTable converters;
|
||||
unsigned default_fetch_type:2;
|
||||
unsigned auto_convert:6;
|
||||
} php_pqres_t;
|
||||
|
||||
typedef struct php_pqres_object {
|
||||
@ -46,6 +53,7 @@ typedef struct php_pqres_object {
|
||||
STATUS php_pqres_success(PGresult *res TSRMLS_DC);
|
||||
void php_pqres_init_instance_data(PGresult *res, php_pqconn_object_t *obj, php_pqres_object_t **ptr TSRMLS_DC);
|
||||
zval *php_pqres_row_to_zval(PGresult *res, unsigned row, php_pqres_fetch_t fetch_type, zval **data_ptr TSRMLS_DC);
|
||||
zval *php_pqres_typed_zval(php_pqres_t *res, char *val, size_t len, Oid typ TSRMLS_DC);
|
||||
|
||||
#include "php_pq_object.h"
|
||||
#include "php_pqconn_event.h"
|
||||
|
Loading…
Reference in New Issue
Block a user