better datetime handling
[m6w6/ext-pq] / src / php_pq_misc.c
index c1f83337b13ef6a68fc6c9cb94e5ea99921c55b4..8c7ec6b241bc988cb667ce938c8efd6fc9183a3f 100644 (file)
 #endif
 
 #include <php.h>
+#include <ext/date/php_date.h>
+#if defined(HAVE_JSON) && !defined(COMPILE_DL_JSON)
+#      include <ext/json/php_json.h>
+#endif
+
+#include <Zend/zend_interfaces.h>
 
 #include <libpq/libpq-fs.h>
 
@@ -86,21 +92,37 @@ static int apply_to_param(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_k
        params = (char ***) va_arg(argv, char ***);
        zdtor = (HashTable *) va_arg(argv, HashTable *);
 
-       if (Z_TYPE_PP(zparam) == IS_NULL) {
+       switch (Z_TYPE_PP(zparam)) {
+       case IS_NULL:
                **params = NULL;
                ++*params;
-       } else {
-               if (Z_TYPE_PP(zparam) != IS_STRING) {
-                       convert_to_string_ex(zparam);
-               }
+               break;
+
+       case IS_BOOL:
+               **params = Z_BVAL_PP(zparam) ? "t" : "f";
+               ++*params;
+               break;
+
+       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));
+               /* no break */
+
+       default:
+               convert_to_string_ex(zparam);
+               /* no break */
 
+       case IS_STRING:
                **params = Z_STRVAL_PP(zparam);
                ++*params;
 
                if (*zparam != *(zval **)p) {
                        zend_hash_next_index_insert(zdtor, zparam, sizeof(zval *), NULL);
                }
+               break;
        }
+
        return ZEND_HASH_APPLY_KEEP;
 }
 
@@ -158,6 +180,62 @@ Oid *php_pq_ntypes_to_array(zend_bool fill, int argc, ...)
 }
 */
 
+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;
+
+       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);
+}
+
+static zend_function_entry php_pqdt_methods[] = {
+       PHP_ME(pqdt, __toString, ai_pqdt_to_string, ZEND_ACC_PUBLIC)
+       PHP_MALIAS(pqdt, jsonSerialize, __toString, ai_pqdt_to_string, ZEND_ACC_PUBLIC)
+       {0}
+};
+
+zval *php_pqdt_from_string(char *dt_str, size_t dt_len, char *fmt, zval *zv TSRMLS_DC)
+{
+       php_date_obj *dobj;
+
+       if (!zv) {
+               MAKE_STD_ZVAL(zv);
+       }
+
+       php_date_instantiate(php_pqdt_class_entry, zv TSRMLS_CC);
+       dobj = zend_object_store_get_object(zv TSRMLS_CC);
+       if (!php_date_initialize(dobj, dt_str, dt_len, NULL, NULL, 1 TSRMLS_CC)) {
+               zval_dtor(zv);
+               ZVAL_NULL(zv);
+       } else if (fmt) {
+               zend_update_property_string(php_pqdt_class_entry, zv, ZEND_STRL("format"), fmt TSRMLS_CC);
+       }
+
+       return zv;
+}
+
+PHP_MINIT_FUNCTION(pq_misc)
+{
+       zend_class_entry **json, ce = {0};
+
+       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);
+
+       /* stop reading this file right here! */
+       if (SUCCESS == zend_hash_find(CG(class_table), ZEND_STRS("jsonserializable"), (void *) &json)) {
+               zend_class_implements(php_pqdt_class_entry TSRMLS_CC, 1, *json);
+       }
+
+       return SUCCESS;
+}
+
 /*
  * Local variables:
  * tab-width: 4