#include <php.h>
#include <ext/spl/spl_iterators.h>
-#include <ext/json/php_json.h>
+#if PHP_PQ_HAVE_PHP_JSON_H
+#include <php_json.h> /* we've added the include directory to INCLUDES */
+#endif
+
#include <libpq-events.h>
#include "php_pq.h"
switch (PQresultStatus(obj->intern->res)) {
case PGRES_TUPLES_OK:
+#ifdef HAVE_PGRES_SINGLE_TUPLE
case PGRES_SINGLE_TUPLE:
+#endif
if (PQntuples(obj->intern->res) <= iter->index) {
return FAILURE;
}
return SUCCESS;
}
+#define PHP_PQRES_JSON_OPTIONS(res) \
+ (php_pqres_fetch_type(res) != PHP_PQRES_FETCH_OBJECT ? PHP_JSON_OBJECT_AS_ARRAY:0)
+
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 *ztype, *tmp = NULL;
+ MAKE_STD_ZVAL(ztype);
+ ZVAL_LONG(ztype, typ);
ZVAL_STRINGL(zv, val, len, 1);
- zend_call_method_with_1_params(zconv, NULL, NULL, "convertfromstring", &tmp, zv);
+ zend_call_method_with_2_params(zconv, NULL, NULL, "convertfromstring", &tmp, zv, ztype);
+ zval_ptr_dtor(&ztype);
if (tmp) {
zval_ptr_dtor(&zv);
}
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:
if (!(res->auto_convert & PHP_PQRES_CONV_INT)) {
goto noconversion;
}
- ZVAL_LONG(zv, zend_atol(val, len));
+ {
+ long lval;
+ double dval;
+
+ switch (is_numeric_string(val, len, &lval, &dval, 0)) {
+ case IS_LONG:
+ ZVAL_LONG(zv, lval);
+ break;
+ case IS_DOUBLE:
+ ZVAL_DOUBLE(zv, dval);
+ break;
+ default:
+ goto noconversion;
+ }
+ }
break;
case PHP_PQ_OID_FLOAT4:
php_pqdt_from_string(val, len, "Y-m-d H:i:s.uO", zv TSRMLS_CC);
break;
-#ifdef PHP_PQ_OID_JSON
+#if PHP_PQ_HAVE_PHP_JSON_H && defined(PHP_PQ_OID_JSON)
# ifdef PHP_PQ_OID_JSONB
case PHP_PQ_OID_JSONB:
# endif
if (!(res->auto_convert & PHP_PQRES_CONV_JSON)) {
goto noconversion;
}
- php_json_decode_ex(zv, val, len, 0, 512 /* PHP_JSON_DEFAULT_DEPTH */ TSRMLS_CC);
+ php_json_decode_ex(zv, val, len, PHP_PQRES_JSON_OPTIONS(res), 512 /* PHP_JSON_DEFAULT_DEPTH */ TSRMLS_CC);
break;
#endif
}
}
+php_pqres_fetch_t php_pqres_fetch_type(php_pqres_t *res)
+{
+ return res->iter ? res->iter->fetch_type : res->default_fetch_type;
+}
+
static void php_pqres_object_free(void *o TSRMLS_DC)
{
php_pqres_object_t *obj = o;
{
php_pqres_object_t *obj = o;
- if (obj->intern->iter) {
- RETVAL_LONG(obj->intern->iter->fetch_type);
- } else {
- RETVAL_LONG(obj->intern->default_fetch_type);
- }
+ RETVAL_LONG(php_pqres_fetch_type(obj->intern));
}
static void php_pqres_object_write_fetch_type(zval *object, void *o, zval *value TSRMLS_DC)
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to bind column %s@%d", col.name, col.num);
RETVAL_FALSE;
} else {
- zend_hash_sort(&obj->intern->bound, zend_qsort, compare_index, 0 TSRMLS_CC);
+ zend_hash_sort(&obj->intern->bound, zend_qsort, php_pq_compare_index, 0 TSRMLS_CC);
RETVAL_TRUE;
}
}
zval **row = NULL;
if (fetch_type == -1) {
- fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : obj->intern->default_fetch_type;
+ fetch_type = php_pqres_fetch_type(obj->intern);
}
zend_replace_error_handling(EH_THROW, exce(EX_RUNTIME), &zeh TSRMLS_CC);
zval **row = NULL;
zend_replace_error_handling(EH_THROW, exce(EX_RUNTIME), &zeh TSRMLS_CC);
- php_pqres_iteration(getThis(), obj, obj->intern->iter ? obj->intern->iter->fetch_type : 0, &row TSRMLS_CC);
+ php_pqres_iteration(getThis(), obj, php_pqres_fetch_type(obj->intern), &row TSRMLS_CC);
if (row) {
php_pqres_col_t col;
}
}
-static int apply_to_col(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
+struct apply_to_col_arg {
+ php_pqres_object_t *obj;
+ php_pqres_col_t *cols;
+ STATUS status;
+};
+
+static int apply_to_col(void *p, void *a TSRMLS_DC)
{
zval **c = p;
- php_pqres_object_t *obj = va_arg(argv, php_pqres_object_t *);
- php_pqres_col_t *col, **cols = va_arg(argv, php_pqres_col_t **);
- STATUS *rv = va_arg(argv, STATUS *);
-
- col = *cols;
+ struct apply_to_col_arg *arg = a;
- if (SUCCESS != column_nn(obj, *c, col TSRMLS_CC)) {
- *rv = FAILURE;
+ if (SUCCESS != column_nn(arg->obj, *c, arg->cols TSRMLS_CC)) {
+ arg->status = FAILURE;
return ZEND_HASH_APPLY_STOP;
} else {
- *rv = SUCCESS;
- ++*cols;
+ arg->status = SUCCESS;
+ ++arg->cols;
return ZEND_HASH_APPLY_KEEP;
}
}
static php_pqres_col_t *php_pqres_convert_to_cols(php_pqres_object_t *obj, HashTable *ht TSRMLS_DC)
{
- php_pqres_col_t *tmp, *cols = ecalloc(zend_hash_num_elements(ht), sizeof(*cols));
- STATUS rv = SUCCESS;
+ struct apply_to_col_arg arg = {NULL};
+ php_pqres_col_t *tmp;
- tmp = cols;
- zend_hash_apply_with_arguments(ht TSRMLS_CC, apply_to_col, 2, obj, &tmp, &rv);
+ arg.obj = obj;
+ arg.cols = ecalloc(zend_hash_num_elements(ht), sizeof(*tmp));
+ tmp = arg.cols;
+ zend_hash_apply_with_argument(ht, apply_to_col, &arg TSRMLS_CC);
- if (SUCCESS == rv) {
- return cols;
+ if (SUCCESS == arg.status) {
+ return tmp;
} else {
- efree(cols);
+ efree(tmp);
return NULL;
}
}
}
if (fetch_type == -1) {
- fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : obj->intern->default_fetch_type;
+ fetch_type = php_pqres_fetch_type(obj->intern);
}
if (keys) {
int r, rows = PQntuples(obj->intern->res);
if (fetch_type == -1) {
- fetch_type = obj->intern->iter ? obj->intern->iter->fetch_type : obj->intern->default_fetch_type;
+ fetch_type = php_pqres_fetch_type(obj->intern);
}
array_init(return_value);
ZEND_BEGIN_ARG_INFO_EX(ai_pqres_count, 0, 0, 0)
ZEND_END_ARG_INFO();
static PHP_METHOD(pqres, count) {
- if (SUCCESS == zend_parse_parameters_none()) {
+ zend_error_handling zeh;
+ STATUS rv;
+
+ zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
+ rv = zend_parse_parameters_none();
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+
+ if (SUCCESS == rv) {
long count;
if (SUCCESS != php_pqres_count_elements(getThis(), &count TSRMLS_CC)) {
ZEND_BEGIN_ARG_INFO_EX(ai_pqres_desc, 0, 0, 0)
ZEND_END_ARG_INFO();
static PHP_METHOD(pqres, desc) {
- if (SUCCESS == zend_parse_parameters_none()) {
+ zend_error_handling zeh;
+ STATUS rv;
+
+ zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
+ rv = zend_parse_parameters_none();
+ zend_restore_error_handling(&zeh TSRMLS_CC);
+
+ if (SUCCESS == rv) {
php_pqres_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
if (!obj->intern) {
zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("BAD_RESPONSE"), PGRES_BAD_RESPONSE TSRMLS_CC);
zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("NONFATAL_ERROR"), PGRES_NONFATAL_ERROR TSRMLS_CC);
zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("FATAL_ERROR"), PGRES_FATAL_ERROR TSRMLS_CC);
+#ifdef HAVE_PGRES_COPY_BOTH
zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("COPY_BOTH"), PGRES_COPY_BOTH TSRMLS_CC);
+#endif
+#ifdef HAVE_PGRES_SINGLE_TUPLE
zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("SINGLE_TUPLE"), PGRES_SINGLE_TUPLE TSRMLS_CC);
+#endif
zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("FETCH_ARRAY"), PHP_PQRES_FETCH_ARRAY TSRMLS_CC);
zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("FETCH_ASSOC"), PHP_PQRES_FETCH_ASSOC TSRMLS_CC);
zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("CONV_SCALAR"), PHP_PQRES_CONV_SCALAR TSRMLS_CC);
zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("CONV_ARRAY"), PHP_PQRES_CONV_ARRAY TSRMLS_CC);
zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("CONV_DATETIME"), PHP_PQRES_CONV_DATETIME TSRMLS_CC);
+#if PHP_PQ_HAVE_PHP_JSON_H
zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("CONV_JSON"), PHP_PQRES_CONV_JSON TSRMLS_CC);
+#endif
zend_declare_class_constant_long(php_pqres_class_entry, ZEND_STRL("CONV_ALL"), PHP_PQRES_CONV_ALL TSRMLS_CC);
return SUCCESS;