2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2013, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
18 #include <ext/date/php_date.h>
19 #if defined(HAVE_JSON) && !defined(COMPILE_DL_JSON)
20 # include <ext/json/php_json.h>
23 #include <Zend/zend_interfaces.h>
25 #include <libpq/libpq-fs.h>
28 #include "php_pq_misc.h"
34 while (l
-- > 0 && e
[l
] == '\n') {
40 const char *strmode(long mode
)
42 switch (mode
& (INV_READ
|INV_WRITE
)) {
43 case INV_READ
|INV_WRITE
:
54 int compare_index(const void *lptr
, const void *rptr TSRMLS_DC
)
56 const Bucket
*l
= *(const Bucket
**) lptr
;
57 const Bucket
*r
= *(const Bucket
**) rptr
;
68 static int apply_to_oid(void *p
, void *arg TSRMLS_DC
)
73 if (Z_TYPE_PP(ztype
) != IS_LONG
) {
74 convert_to_long_ex(ztype
);
77 **types
= Z_LVAL_PP(ztype
);
80 if (*ztype
!= *(zval
**)p
) {
83 return ZEND_HASH_APPLY_KEEP
;
86 static int apply_to_param(void *p TSRMLS_DC
, int argc
, va_list argv
, zend_hash_key
*key
)
92 params
= (char ***) va_arg(argv
, char ***);
93 zdtor
= (HashTable
*) va_arg(argv
, HashTable
*);
95 switch (Z_TYPE_PP(zparam
)) {
102 **params
= Z_BVAL_PP(zparam
) ? "t" : "f";
107 SEPARATE_ZVAL(zparam
);
108 Z_TYPE_PP(zparam
) = IS_STRING
;
109 Z_STRLEN_PP(zparam
) = spprintf(&Z_STRVAL_PP(zparam
), 0, "%F", Z_DVAL_PP((zval
**)p
));
113 convert_to_string_ex(zparam
);
117 **params
= Z_STRVAL_PP(zparam
);
120 if (*zparam
!= *(zval
**)p
) {
121 zend_hash_next_index_insert(zdtor
, zparam
, sizeof(zval
*), NULL
);
126 return ZEND_HASH_APPLY_KEEP
;
129 int php_pq_types_to_array(HashTable
*ht
, Oid
**types TSRMLS_DC
)
131 int count
= zend_hash_num_elements(ht
);
138 /* +1 for when less types than params are specified */
139 *types
= tmp
= ecalloc(count
+ 1, sizeof(**types
));
140 zend_hash_apply_with_argument(ht
, apply_to_oid
, &tmp TSRMLS_CC
);
146 int php_pq_params_to_array(HashTable
*ht
, char ***params
, HashTable
*zdtor TSRMLS_DC
)
148 int count
= zend_hash_num_elements(ht
);
155 *params
= tmp
= ecalloc(count
, sizeof(char *));
156 zend_hash_apply_with_arguments(ht TSRMLS_CC
, apply_to_param
, 2, &tmp
, zdtor
);
163 Oid *php_pq_ntypes_to_array(zend_bool fill, int argc, ...)
166 Oid *oids = ecalloc(argc + 1, sizeof(*oids));
169 va_start(argv, argc);
170 for (i = 0; i < argc; ++i) {
172 oids[i] = va_arg(argv, Oid);
183 zend_class_entry
*php_pqdt_class_entry
;
185 ZEND_BEGIN_ARG_INFO_EX(ai_pqdt_to_string
, 0, 0, 0)
187 static PHP_METHOD(pqdt
, __toString
)
191 zend_call_method_with_1_params(&getThis(), php_pqdt_class_entry
, NULL
, "format", &rv
,
192 zend_read_property(php_pqdt_class_entry
, getThis(), ZEND_STRL("format"), 0 TSRMLS_CC
));
193 RETVAL_ZVAL(rv
, 1, 1);
196 static zend_function_entry php_pqdt_methods
[] = {
197 PHP_ME(pqdt
, __toString
, ai_pqdt_to_string
, ZEND_ACC_PUBLIC
)
198 PHP_MALIAS(pqdt
, jsonSerialize
, __toString
, ai_pqdt_to_string
, ZEND_ACC_PUBLIC
)
202 zval
*php_pqdt_from_string(char *dt_str
, size_t dt_len
, char *fmt
, zval
*zv TSRMLS_DC
)
210 php_date_instantiate(php_pqdt_class_entry
, zv TSRMLS_CC
);
211 dobj
= zend_object_store_get_object(zv TSRMLS_CC
);
212 if (!php_date_initialize(dobj
, dt_str
, dt_len
, NULL
, NULL
, 1 TSRMLS_CC
)) {
216 zend_update_property_string(php_pqdt_class_entry
, zv
, ZEND_STRL("format"), fmt TSRMLS_CC
);
222 PHP_MINIT_FUNCTION(pq_misc
)
224 zend_class_entry
**json
, ce
= {0};
226 INIT_NS_CLASS_ENTRY(ce
,"pq", "DateTime", php_pqdt_methods
);
227 php_pqdt_class_entry
= zend_register_internal_class_ex(&ce
, php_date_get_date_ce(), "DateTime" TSRMLS_CC
);
229 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
);
231 /* stop reading this file right here! */
232 if (SUCCESS
== zend_hash_find(CG(class_table
), ZEND_STRS("jsonserializable"), (void *) &json
)) {
233 zend_class_implements(php_pqdt_class_entry TSRMLS_CC
, 1, *json
);
244 * vim600: noet sw=4 ts=4 fdm=marker
245 * vim<600: noet sw=4 ts=4