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/standard/php_string.h>
19 #include <ext/standard/php_smart_str.h>
20 #include <ext/json/php_json.h>
22 #include <Zend/zend_interfaces.h>
27 #include "php_pq_params.h"
28 #include "php_pq_misc.h"
30 #include "php_pq_type.h"
32 void php_pq_params_set_type_conv(php_pq_params_t
*p
, HashTable
*conv
)
34 zend_hash_clean(&p
->type
.conv
);
35 zend_hash_copy(&p
->type
.conv
, conv
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
38 static int apply_to_oid(void *p
, void *arg TSRMLS_DC
)
43 if (Z_TYPE_PP(ztype
) != IS_LONG
) {
44 convert_to_long_ex(ztype
);
47 **types
= Z_LVAL_PP(ztype
);
50 if (*ztype
!= *(zval
**)p
) {
53 return ZEND_HASH_APPLY_KEEP
;
56 unsigned php_pq_params_set_type_oids(php_pq_params_t
*p
, HashTable
*oids
)
58 p
->type
.count
= oids
? zend_hash_num_elements(oids
) : 0;
66 Oid
*ptr
= ecalloc(p
->type
.count
+ 1, sizeof(*p
->type
.oids
));
67 /* +1 for when less types than params are specified */
69 zend_hash_apply_with_argument(oids
, apply_to_oid
, &ptr TSRMLS_CC
);
74 unsigned php_pq_params_add_type_oid(php_pq_params_t
*p
, Oid type
)
76 p
->type
.oids
= safe_erealloc(p
->type
.oids
, ++p
->type
.count
, sizeof(*p
->type
.oids
), sizeof(*p
->type
.oids
));
77 p
->type
.oids
[p
->type
.count
] = 0;
78 p
->type
.oids
[p
->type
.count
-1] = type
;
83 static zval
*object_param_to_string(php_pq_params_t
*p
, zval
*zobj
, Oid type TSRMLS_DC
)
85 zval
*return_value
= NULL
;
89 #ifdef PHP_PQ_OID_JSON
90 # ifdef PHP_PQ_OID_JSONB
91 case PHP_PQ_OID_JSONB
:
94 php_json_encode(&str
, zobj
, PHP_JSON_UNESCAPED_UNICODE TSRMLS_CC
);
100 php_pqdt_to_string(zobj
, "Y-m-d", &str
.c
, &str
.len TSRMLS_CC
);
103 case PHP_PQ_OID_ABSTIME
:
104 php_pqdt_to_string(zobj
, "Y-m-d H:i:s", &str
.c
, &str
.len TSRMLS_CC
);
107 case PHP_PQ_OID_TIMESTAMP
:
108 php_pqdt_to_string(zobj
, "Y-m-d H:i:s.u", &str
.c
, &str
.len TSRMLS_CC
);
111 case PHP_PQ_OID_TIMESTAMPTZ
:
112 php_pqdt_to_string(zobj
, "Y-m-d H:i:s.uO", &str
.c
, &str
.len TSRMLS_CC
);
116 MAKE_STD_ZVAL(return_value
);
117 MAKE_COPY_ZVAL(&zobj
, return_value
);
118 convert_to_string(return_value
);
123 MAKE_STD_ZVAL(return_value
);
124 RETVAL_STRINGL(str
.c
, str
.len
, 0);
130 struct apply_to_param_from_array_arg
{
131 php_pq_params_t
*params
;
138 static int apply_to_param_from_array(void *ptr
, void *arg_ptr TSRMLS_DC
)
140 struct apply_to_param_from_array_arg subarg
, *arg
= arg_ptr
;
141 zval
*ztmp
, **zparam
= ptr
, *zcopy
= *zparam
;
147 smart_str_appendc(arg
->buffer
, ',');
151 zval
*ztype
, *rv
= NULL
;
153 MAKE_STD_ZVAL(ztype
);
154 ZVAL_LONG(ztype
, arg
->type
);
155 zend_call_method_with_2_params(arg
->zconv
, NULL
, NULL
, "converttostring", &rv
, zcopy
, ztype
);
156 zval_ptr_dtor(&ztype
);
159 convert_to_string(rv
);
162 return ZEND_HASH_APPLY_STOP
;
168 switch (Z_TYPE_P(zcopy
)) {
170 smart_str_appends(arg
->buffer
, "NULL");
174 smart_str_appends(arg
->buffer
, Z_BVAL_P(zcopy
) ? "t" : "f");
178 smart_str_append_long(arg
->buffer
, Z_LVAL_P(zcopy
));
182 len
= spprintf(&tmp
, 0, "%F", Z_DVAL_P(zcopy
));
183 smart_str_appendl(arg
->buffer
, tmp
, len
);
190 smart_str_appendc(arg
->buffer
, '{');
191 zend_hash_apply_with_argument(Z_ARRVAL_P(zcopy
), apply_to_param_from_array
, &subarg TSRMLS_CC
);
192 smart_str_appendc(arg
->buffer
, '}');
196 if ((ztmp
= object_param_to_string(arg
->params
, zcopy
, arg
->type TSRMLS_CC
))) {
201 SEPARATE_ZVAL(&zcopy
);
202 convert_to_string(zcopy
);
205 tmp
= php_addslashes(Z_STRVAL_P(zcopy
), Z_STRLEN_P(zcopy
), &tmp_len
, 0 TSRMLS_CC
);
206 smart_str_appendc(arg
->buffer
, '"');
207 smart_str_appendl(arg
->buffer
, tmp
, tmp_len
);
208 smart_str_appendc(arg
->buffer
, '"');
210 if (zcopy
!= *zparam
) {
211 zval_ptr_dtor(&zcopy
);
218 return ZEND_HASH_APPLY_KEEP
;
221 static zval
*array_param_to_string(php_pq_params_t
*p
, zval
*zarr
, Oid type TSRMLS_DC
)
223 zval
*zcopy
, *return_value
;
225 struct apply_to_param_from_array_arg arg
= {NULL
};
228 #ifdef PHP_PQ_OID_JSON
229 # ifdef PHP_PQ_OID_JSONB
230 case PHP_PQ_OID_JSONB
:
232 case PHP_PQ_OID_JSON
:
233 php_json_encode(&s
, zarr
, PHP_JSON_UNESCAPED_UNICODE TSRMLS_CC
);
241 arg
.type
= PHP_PQ_TYPE_OF_ARRAY(type
);
242 zend_hash_index_find(&p
->type
.conv
, PHP_PQ_TYPE_OF_ARRAY(type
), (void *) &arg
.zconv
);
243 smart_str_appendc(arg
.buffer
, '{');
244 MAKE_STD_ZVAL(zcopy
);
245 MAKE_COPY_ZVAL(&zarr
, zcopy
);
246 zend_hash_apply_with_argument(Z_ARRVAL_P(zcopy
), apply_to_param_from_array
, &arg TSRMLS_CC
);
247 zval_ptr_dtor(&zcopy
);
248 smart_str_appendc(arg
.buffer
, '}');
253 /* must not return NULL */
254 MAKE_STD_ZVAL(return_value
);
257 RETVAL_STRINGL(s
.c
, s
.len
, 0);
259 RETVAL_EMPTY_STRING();
265 static void php_pq_params_set_param(php_pq_params_t
*p
, unsigned index
, zval
**zpp
)
268 Oid type
= p
->type
.count
> index
? p
->type
.oids
[index
] : 0;
271 if (type
&& SUCCESS
== zend_hash_index_find(&p
->type
.conv
, type
, (void *) &zconv
)) {
272 zval
*ztype
, *rv
= NULL
;
274 MAKE_STD_ZVAL(ztype
);
275 ZVAL_LONG(ztype
, type
);
276 zend_call_method_with_2_params(zconv
, NULL
, NULL
, "converttostring", &rv
, *zpp
, ztype
);
277 zval_ptr_dtor(&ztype
);
279 convert_to_string(rv
);
280 p
->param
.strings
[index
] = Z_STRVAL_P(rv
);
281 zend_hash_next_index_insert(&p
->param
.dtor
, (void *) &rv
, sizeof(zval
*), NULL
);
284 zval
*tmp
, *zcopy
= *zpp
;
286 switch (Z_TYPE_P(zcopy
)) {
288 p
->param
.strings
[index
] = NULL
;
292 p
->param
.strings
[index
] = Z_BVAL_P(zcopy
) ? "t" : "f";
296 MAKE_STD_ZVAL(zcopy
);
297 MAKE_COPY_ZVAL(zpp
, zcopy
);
298 Z_TYPE_P(zcopy
) = IS_STRING
;
299 Z_STRLEN_P(zcopy
) = spprintf(&Z_STRVAL_P(zcopy
), 0, "%F", Z_DVAL_PP(zpp
));
303 MAKE_STD_ZVAL(zcopy
);
304 MAKE_COPY_ZVAL(zpp
, zcopy
);
305 tmp
= array_param_to_string(p
, zcopy
, type TSRMLS_CC
);
306 zval_ptr_dtor(&zcopy
);
311 if ((tmp
= object_param_to_string(p
, zcopy
, type TSRMLS_CC
))) {
318 convert_to_string_ex(&zcopy
);
322 p
->param
.strings
[index
] = Z_STRVAL_P(zcopy
);
325 zend_hash_next_index_insert(&p
->param
.dtor
, (void *) &zcopy
, sizeof(zval
*), NULL
);
330 struct apply_to_params_arg
{
331 php_pq_params_t
*params
;
335 static int apply_to_params(void *zp
, void *arg_ptr TSRMLS_DC
)
337 struct apply_to_params_arg
*arg
= arg_ptr
;
339 SEPARATE_ZVAL_IF_NOT_REF((zval
**) zp
);
340 php_pq_params_set_param(arg
->params
, arg
->index
++, zp
);
341 return ZEND_HASH_APPLY_KEEP
;
344 unsigned php_pq_params_add_param(php_pq_params_t
*p
, zval
*param
)
346 p
->param
.strings
= safe_erealloc(p
->param
.strings
, ++p
->param
.count
, sizeof(*p
->param
.strings
), 0);
347 php_pq_params_set_param(p
, p
->param
.count
-1, ¶m
);
348 return p
->type
.count
;
351 unsigned php_pq_params_set_params(php_pq_params_t
*p
, HashTable
*params
)
353 p
->param
.count
= params
? zend_hash_num_elements(params
) : 0;
356 if (p
->param
.strings
) {
357 efree(p
->param
.strings
);
358 p
->param
.strings
= NULL
;
360 zend_hash_clean(&p
->param
.dtor
);
361 if (p
->param
.count
) {
362 struct apply_to_params_arg arg
= {p
, 0};
363 p
->param
.strings
= ecalloc(p
->param
.count
, sizeof(*p
->param
.strings
));
364 zend_hash_apply_with_argument(params
, apply_to_params
, &arg TSRMLS_CC
);
366 return p
->param
.count
;
369 void php_pq_params_free(php_pq_params_t
**p
)
372 php_pq_params_set_type_oids(*p
, NULL
);
373 php_pq_params_set_params(*p
, NULL
);
375 zend_hash_destroy(&(*p
)->param
.dtor
);
376 zend_hash_destroy(&(*p
)->type
.conv
);
383 php_pq_params_t
*php_pq_params_init(HashTable
*conv
, HashTable
*oids
, HashTable
*params TSRMLS_DC
)
385 php_pq_params_t
*p
= ecalloc(1, sizeof(*p
));
388 zend_hash_init(&p
->type
.conv
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
389 zend_hash_init(&p
->param
.dtor
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
392 php_pq_params_set_type_conv(p
, conv
);
395 php_pq_params_set_type_oids(p
, oids
);
398 php_pq_params_set_params(p
, params
);
409 * vim600: noet sw=4 ts=4 fdm=marker
410 * vim<600: noet sw=4 ts=4