better checks for json support
[m6w6/ext-pq] / src / php_pq_misc.c
index 0f4c83a0f72810098418d410eecde3520efa3471..2bc430c7c57202d3e5f45d765942034477de585a 100644 (file)
@@ -17,9 +17,6 @@
 #include <php.h>
 #include <ext/date/php_date.h>
 #include <ext/standard/php_string.h>
-#if defined(HAVE_JSON) && !defined(COMPILE_DL_JSON)
-#      include <ext/json/php_json.h>
-#endif
 
 #include <Zend/zend_interfaces.h>
 
@@ -66,215 +63,19 @@ int compare_index(const void *lptr, const void *rptr TSRMLS_DC)
        return 0;
 }
 
-static int apply_to_oid(void *p, void *arg TSRMLS_DC)
-{
-       Oid **types = arg;
-       zval **ztype = p;
-
-       if (Z_TYPE_PP(ztype) != IS_LONG) {
-               convert_to_long_ex(ztype);
-       }
-
-       **types = Z_LVAL_PP(ztype);
-       ++*types;
-
-       if (*ztype != *(zval **)p) {
-               zval_ptr_dtor(ztype);
-       }
-       return ZEND_HASH_APPLY_KEEP;
-}
-
-static int apply_to_param_from_array(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
-{
-       zval **zparam = p;
-       unsigned j, *i = va_arg(argv, unsigned *);
-       smart_str *s = va_arg(argv, smart_str *);
-       char *tmp;
-       size_t len;
-       int tmp_len;
-
-       if ((*i)++) {
-               smart_str_appendc(s, ',');
-       }
-
-       switch (Z_TYPE_PP(zparam)) {
-       case IS_NULL:
-               smart_str_appends(s, "NULL");
-               break;
-
-       case IS_BOOL:
-               smart_str_appends(s, Z_BVAL_PP(zparam) ? "t" : "f");
-               break;
-
-       case IS_LONG:
-               smart_str_append_long(s, Z_LVAL_PP(zparam));
-               break;
-
-       case IS_DOUBLE:
-               len = spprintf(&tmp, 0, "%F", Z_DVAL_PP(zparam));
-               smart_str_appendl(s, tmp, len);
-               efree(tmp);
-               break;
-
-       case IS_ARRAY:
-               j = 0;
-               smart_str_appendc(s, '{');
-               zend_hash_apply_with_arguments(Z_ARRVAL_PP(zparam) TSRMLS_CC, apply_to_param_from_array, 2, &j, s);
-               smart_str_appendc(s, '}');
-               break;
-
-       default:
-       {
-               SEPARATE_ZVAL(zparam);
-               if (Z_TYPE_PP(zparam) != IS_STRING) {
-                       convert_to_string(*zparam);
-               }
-
-               tmp = php_addslashes(Z_STRVAL_PP(zparam), Z_STRLEN_PP(zparam), &tmp_len, 0 TSRMLS_CC);
-               smart_str_appendc(s, '"');
-               smart_str_appendl(s, tmp, tmp_len);
-               smart_str_appendc(s, '"');
-
-               if (*zparam != *((zval **) p)) {
-                       zval_ptr_dtor(zparam);
-               }
-               break;
-       }
-       }
-
-       ++(*i);
-       return ZEND_HASH_APPLY_KEEP;
-}
-
-static void array_param_to_string(HashTable *ht, char **str, int *len TSRMLS_DC)
-{
-       smart_str s = {0};
-       unsigned i = 0;
-
-       smart_str_appendc(&s, '{');
-       zend_hash_apply_with_arguments(ht TSRMLS_CC, apply_to_param_from_array, 2, &i, &s);
-       smart_str_appendc(&s, '}');
-
-       smart_str_0(&s);
-       *str = s.c;
-       *len = s.len;
-}
-
-static int apply_to_param(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
-{
-       char ***params;
-       HashTable *zdtor;
-       zval **zparam = p;
-
-       params = (char ***) va_arg(argv, char ***);
-       zdtor = (HashTable *) va_arg(argv, HashTable *);
-
-       switch (Z_TYPE_PP(zparam)) {
-       case IS_NULL:
-               **params = NULL;
-               ++*params;
-               return ZEND_HASH_APPLY_KEEP;
-
-       case IS_BOOL:
-               **params = Z_BVAL_PP(zparam) ? "t" : "f";
-               ++*params;
-               return ZEND_HASH_APPLY_KEEP;
-
-       case IS_DOUBLE:
-               SEPARATE_ZVAL(zparam);
-               Z_TYPE_PP(zparam) = IS_STRING;
-               Z_STRLEN_PP(zparam) = spprintf(&Z_STRVAL_PP(zparam), 0, "%F", Z_DVAL_PP((zval **)p));
-               break;
-
-       case IS_ARRAY:
-       {
-               zval *tmp;
-               MAKE_STD_ZVAL(tmp);
-               Z_TYPE_P(tmp) = IS_STRING;
-               array_param_to_string(Z_ARRVAL_PP(zparam), &Z_STRVAL_P(tmp), &Z_STRLEN_P(tmp) TSRMLS_CC);
-               zparam = &tmp;
-               break;
-       }
-
-       default:
-               convert_to_string_ex(zparam);
-               break;
-       }
-
-       **params = Z_STRVAL_PP(zparam);
-       ++*params;
-
-       if (*zparam != *(zval **)p) {
-               zend_hash_next_index_insert(zdtor, zparam, sizeof(zval *), NULL);
-       }
-       return ZEND_HASH_APPLY_KEEP;
-}
-
-int php_pq_types_to_array(HashTable *ht, Oid **types TSRMLS_DC)
-{
-       int count = zend_hash_num_elements(ht);
-
-       *types = NULL;
-
-       if (count) {
-               Oid *tmp;
-
-               /* +1 for when less types than params are specified */
-               *types = tmp = ecalloc(count + 1, sizeof(**types));
-               zend_hash_apply_with_argument(ht, apply_to_oid, &tmp TSRMLS_CC);
-       }
-
-       return count;
-}
-
-int php_pq_params_to_array(HashTable *ht, char ***params, HashTable *zdtor TSRMLS_DC)
-{
-       int count = zend_hash_num_elements(ht);
-
-       *params = NULL;
-
-       if (count) {
-               char **tmp;
-
-               *params = tmp = ecalloc(count, sizeof(char *));
-               zend_hash_apply_with_arguments(ht TSRMLS_CC, apply_to_param, 2, &tmp, zdtor);
-       }
-
-       return count;
-}
-
-/*
-Oid *php_pq_ntypes_to_array(zend_bool fill, int argc, ...)
-{
-       int i;
-       Oid *oids = ecalloc(argc + 1, sizeof(*oids));
-       va_list argv;
-
-       va_start(argv, argc);
-       for (i = 0; i < argc; ++i) {
-               if (!fill || !i) {
-                       oids[i] = va_arg(argv, Oid);
-               } else {
-                       oids[i] = oids[0];
-               }
-       }
-       va_end(argv);
-
-       return oids;
-}
-*/
-
 zend_class_entry *php_pqdt_class_entry;
 
 ZEND_BEGIN_ARG_INFO_EX(ai_pqdt_to_string, 0, 0, 0)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(pqdt, __toString)
 {
-       zval *rv;
+       zval *rv = NULL;
 
        zend_call_method_with_1_params(&getThis(), php_pqdt_class_entry, NULL, "format", &rv,
                        zend_read_property(php_pqdt_class_entry, getThis(), ZEND_STRL("format"), 0 TSRMLS_CC));
-       RETVAL_ZVAL(rv, 1, 1);
+       if (rv) {
+               RETVAL_ZVAL(rv, 1, 1);
+       }
 }
 
 static zend_function_entry php_pqdt_methods[] = {
@@ -303,14 +104,71 @@ zval *php_pqdt_from_string(char *dt_str, size_t dt_len, char *fmt, zval *zv TSRM
        return zv;
 }
 
+void php_pqdt_to_string(zval *zdt, const char *format, char **str_buf, size_t *str_len TSRMLS_DC)
+{
+       zval rv;
+
+       INIT_PZVAL(&rv);
+       ZVAL_NULL(&rv);
+
+       if (Z_OBJ_HT_P(zdt)->cast_object
+       &&      SUCCESS == Z_OBJ_HT_P(zdt)->cast_object(zdt, &rv, IS_STRING TSRMLS_CC)
+       ) {
+               *str_len = Z_STRLEN(rv);
+               *str_buf = Z_STRVAL(rv);
+       } else if (instanceof_function(Z_OBJCE_P(zdt), php_date_get_date_ce() TSRMLS_CC)) {
+               zval *rv = NULL, *zfmt;
+
+               MAKE_STD_ZVAL(zfmt);
+               ZVAL_STRING(zfmt, format, 1);
+               zend_call_method_with_1_params(&zdt, Z_OBJCE_P(zdt), NULL, "format", &rv, zfmt);
+               zval_ptr_dtor(&zfmt);
+
+               if (rv) {
+                       if (Z_TYPE_P(rv) == IS_STRING) {
+                               *str_len = Z_STRLEN_P(rv);
+                               *str_buf = estrndup(Z_STRVAL_P(rv), *str_len);
+                       }
+                       zval_ptr_dtor(&rv);
+               }
+       }
+}
+
+zend_class_entry *php_pqconv_class_entry;
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconv_convert_types, 0, 0, 0)
+ZEND_END_ARG_INFO();
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconv_convert_from_string, 0, 0, 2)
+       ZEND_ARG_INFO(0, data)
+       ZEND_ARG_INFO(0, type)
+ZEND_END_ARG_INFO();
+
+ZEND_BEGIN_ARG_INFO_EX(ai_pqconv_convert_to_string, 0, 0, 2)
+       ZEND_ARG_INFO(0, data)
+       ZEND_ARG_INFO(0, type)
+ZEND_END_ARG_INFO();
+
+zend_function_entry php_pqconv_methods[] = {
+       PHP_ABSTRACT_ME(pqconv, convertTypes, ai_pqconv_convert_types)
+       PHP_ABSTRACT_ME(pqconv, convertFromString, ai_pqconv_convert_from_string)
+       PHP_ABSTRACT_ME(pqconv, convertToString, ai_pqconv_convert_to_string)
+       {0}
+};
+
+
 PHP_MINIT_FUNCTION(pq_misc)
 {
        zend_class_entry **json, ce = {0};
 
+       INIT_NS_CLASS_ENTRY(ce, "pq", "Converter", php_pqconv_methods);
+       php_pqconv_class_entry = zend_register_internal_interface(&ce TSRMLS_CC);
+
+       memset(&ce, 0, sizeof(ce));
        INIT_NS_CLASS_ENTRY(ce ,"pq", "DateTime", php_pqdt_methods);
        php_pqdt_class_entry = zend_register_internal_class_ex(&ce, php_date_get_date_ce(), "DateTime" TSRMLS_CC);
 
-       zend_declare_property_stringl(php_pqdt_class_entry, ZEND_STRL("format"), ZEND_STRL("Y-m-d H:i:s.u"), ZEND_ACC_PUBLIC TSRMLS_CC);
+       zend_declare_property_stringl(php_pqdt_class_entry, ZEND_STRL("format"), ZEND_STRL("Y-m-d H:i:s.uO"), ZEND_ACC_PUBLIC TSRMLS_CC);
 
        /* stop reading this file right here! */
        if (SUCCESS == zend_hash_find(CG(class_table), ZEND_STRS("jsonserializable"), (void *) &json)) {
@@ -328,6 +186,7 @@ typedef struct _HashTableList {
 typedef struct _ArrayParserState {
        const char *ptr, *end;
        HashTableList *list;
+       php_pqres_t *res;
 #ifdef ZTS
        void ***ts;
 #endif
@@ -374,7 +233,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);
        }
@@ -501,7 +360,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};
@@ -510,6 +369,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) {
@@ -533,70 +393,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: