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 #if PHP_PQ_HAVE_PHP_JSON_H
21 #include <php_json.h> /* we've added the include directory to INCLUDES */
24 #include <Zend/zend_interfaces.h>
29 #include "php_pq_params.h"
30 #include "php_pq_misc.h"
32 #include "php_pq_type.h"
34 void php_pq_params_set_type_conv(php_pq_params_t
*p
, HashTable
*conv
)
36 zend_hash_clean(&p
->type
.conv
);
37 zend_hash_copy(&p
->type
.conv
, conv
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
40 static int apply_to_oid(void *p
, void *arg TSRMLS_DC
)
45 if (Z_TYPE_PP(ztype
) != IS_LONG
) {
46 convert_to_long_ex(ztype
);
49 **types
= Z_LVAL_PP(ztype
);
52 if (*ztype
!= *(zval
**)p
) {
55 return ZEND_HASH_APPLY_KEEP
;
58 unsigned php_pq_params_set_type_oids(php_pq_params_t
*p
, HashTable
*oids
)
60 p
->type
.count
= oids
? zend_hash_num_elements(oids
) : 0;
68 Oid
*ptr
= ecalloc(p
->type
.count
+ 1, sizeof(*p
->type
.oids
));
69 /* +1 for when less types than params are specified */
71 zend_hash_apply_with_argument(oids
, apply_to_oid
, &ptr TSRMLS_CC
);
76 unsigned php_pq_params_add_type_oid(php_pq_params_t
*p
, Oid type
)
78 p
->type
.oids
= safe_erealloc(p
->type
.oids
, ++p
->type
.count
, sizeof(*p
->type
.oids
), sizeof(*p
->type
.oids
));
79 p
->type
.oids
[p
->type
.count
] = 0;
80 p
->type
.oids
[p
->type
.count
-1] = type
;
85 static zval
*object_param_to_string(php_pq_params_t
*p
, zval
*zobj
, Oid type TSRMLS_DC
)
87 zval
*return_value
= NULL
;
91 #if HAVE_JSON && defined(PHP_PQ_OID_JSON)
92 # ifdef PHP_PQ_OID_JSONB
93 case PHP_PQ_OID_JSONB
:
96 php_json_encode(&str
, zobj
, PHP_JSON_UNESCAPED_UNICODE TSRMLS_CC
);
101 case PHP_PQ_OID_DATE
:
102 php_pqdt_to_string(zobj
, "Y-m-d", &str
.c
, &str
.len TSRMLS_CC
);
105 case PHP_PQ_OID_ABSTIME
:
106 php_pqdt_to_string(zobj
, "Y-m-d H:i:s", &str
.c
, &str
.len TSRMLS_CC
);
109 case PHP_PQ_OID_TIMESTAMP
:
110 php_pqdt_to_string(zobj
, "Y-m-d H:i:s.u", &str
.c
, &str
.len TSRMLS_CC
);
113 case PHP_PQ_OID_TIMESTAMPTZ
:
114 php_pqdt_to_string(zobj
, "Y-m-d H:i:s.uO", &str
.c
, &str
.len TSRMLS_CC
);
118 MAKE_STD_ZVAL(return_value
);
119 MAKE_COPY_ZVAL(&zobj
, return_value
);
120 convert_to_string(return_value
);
125 MAKE_STD_ZVAL(return_value
);
126 RETVAL_STRINGL(str
.c
, str
.len
, 0);
132 struct apply_to_param_from_array_arg
{
133 php_pq_params_t
*params
;
140 static int apply_to_param_from_array(void *ptr
, void *arg_ptr TSRMLS_DC
)
142 struct apply_to_param_from_array_arg subarg
, *arg
= arg_ptr
;
143 zval
*ztmp
, **zparam
= ptr
, *zcopy
= *zparam
;
149 smart_str_appendc(arg
->buffer
, ',');
153 zval
*ztype
, *rv
= NULL
;
155 MAKE_STD_ZVAL(ztype
);
156 ZVAL_LONG(ztype
, arg
->type
);
157 zend_call_method_with_2_params(arg
->zconv
, NULL
, NULL
, "converttostring", &rv
, zcopy
, ztype
);
158 zval_ptr_dtor(&ztype
);
161 convert_to_string(rv
);
164 return ZEND_HASH_APPLY_STOP
;
170 switch (Z_TYPE_P(zcopy
)) {
172 smart_str_appends(arg
->buffer
, "NULL");
176 smart_str_appends(arg
->buffer
, Z_BVAL_P(zcopy
) ? "t" : "f");
180 smart_str_append_long(arg
->buffer
, Z_LVAL_P(zcopy
));
184 len
= spprintf(&tmp
, 0, "%F", Z_DVAL_P(zcopy
));
185 smart_str_appendl(arg
->buffer
, tmp
, len
);
192 smart_str_appendc(arg
->buffer
, '{');
193 zend_hash_apply_with_argument(Z_ARRVAL_P(zcopy
), apply_to_param_from_array
, &subarg TSRMLS_CC
);
194 smart_str_appendc(arg
->buffer
, '}');
198 if ((ztmp
= object_param_to_string(arg
->params
, zcopy
, arg
->type TSRMLS_CC
))) {
203 SEPARATE_ZVAL(&zcopy
);
204 convert_to_string(zcopy
);
207 tmp
= php_addslashes(Z_STRVAL_P(zcopy
), Z_STRLEN_P(zcopy
), &tmp_len
, 0 TSRMLS_CC
);
208 smart_str_appendc(arg
->buffer
, '"');
209 smart_str_appendl(arg
->buffer
, tmp
, tmp_len
);
210 smart_str_appendc(arg
->buffer
, '"');
212 if (zcopy
!= *zparam
) {
213 zval_ptr_dtor(&zcopy
);
220 return ZEND_HASH_APPLY_KEEP
;
223 static zval
*array_param_to_string(php_pq_params_t
*p
, zval
*zarr
, Oid type TSRMLS_DC
)
225 zval
*zcopy
, *return_value
;
227 struct apply_to_param_from_array_arg arg
= {NULL
};
230 #if HAVE_JSON && defined(PHP_PQ_OID_JSON)
231 # ifdef PHP_PQ_OID_JSONB
232 case PHP_PQ_OID_JSONB
:
234 case PHP_PQ_OID_JSON
:
235 php_json_encode(&s
, zarr
, PHP_JSON_UNESCAPED_UNICODE TSRMLS_CC
);
243 arg
.type
= PHP_PQ_TYPE_OF_ARRAY(type
);
244 zend_hash_index_find(&p
->type
.conv
, PHP_PQ_TYPE_OF_ARRAY(type
), (void *) &arg
.zconv
);
245 smart_str_appendc(arg
.buffer
, '{');
246 MAKE_STD_ZVAL(zcopy
);
247 MAKE_COPY_ZVAL(&zarr
, zcopy
);
248 zend_hash_apply_with_argument(Z_ARRVAL_P(zcopy
), apply_to_param_from_array
, &arg TSRMLS_CC
);
249 zval_ptr_dtor(&zcopy
);
250 smart_str_appendc(arg
.buffer
, '}');
255 /* must not return NULL */
256 MAKE_STD_ZVAL(return_value
);
259 RETVAL_STRINGL(s
.c
, s
.len
, 0);
261 RETVAL_EMPTY_STRING();
267 static void php_pq_params_set_param(php_pq_params_t
*p
, unsigned index
, zval
**zpp
)
270 Oid type
= p
->type
.count
> index
? p
->type
.oids
[index
] : 0;
273 if (type
&& SUCCESS
== zend_hash_index_find(&p
->type
.conv
, type
, (void *) &zconv
)) {
274 zval
*ztype
, *rv
= NULL
;
276 MAKE_STD_ZVAL(ztype
);
277 ZVAL_LONG(ztype
, type
);
278 zend_call_method_with_2_params(zconv
, NULL
, NULL
, "converttostring", &rv
, *zpp
, ztype
);
279 zval_ptr_dtor(&ztype
);
281 convert_to_string(rv
);
282 p
->param
.strings
[index
] = Z_STRVAL_P(rv
);
283 zend_hash_next_index_insert(&p
->param
.dtor
, (void *) &rv
, sizeof(zval
*), NULL
);
286 zval
*tmp
, *zcopy
= *zpp
;
288 switch (Z_TYPE_P(zcopy
)) {
290 p
->param
.strings
[index
] = NULL
;
294 p
->param
.strings
[index
] = Z_BVAL_P(zcopy
) ? "t" : "f";
298 MAKE_STD_ZVAL(zcopy
);
299 MAKE_COPY_ZVAL(zpp
, zcopy
);
300 Z_TYPE_P(zcopy
) = IS_STRING
;
301 Z_STRLEN_P(zcopy
) = spprintf(&Z_STRVAL_P(zcopy
), 0, "%F", Z_DVAL_PP(zpp
));
305 MAKE_STD_ZVAL(zcopy
);
306 MAKE_COPY_ZVAL(zpp
, zcopy
);
307 tmp
= array_param_to_string(p
, zcopy
, type TSRMLS_CC
);
308 zval_ptr_dtor(&zcopy
);
313 if ((tmp
= object_param_to_string(p
, zcopy
, type TSRMLS_CC
))) {
320 convert_to_string_ex(&zcopy
);
324 p
->param
.strings
[index
] = Z_STRVAL_P(zcopy
);
327 zend_hash_next_index_insert(&p
->param
.dtor
, (void *) &zcopy
, sizeof(zval
*), NULL
);
332 struct apply_to_params_arg
{
333 php_pq_params_t
*params
;
337 static int apply_to_params(void *zp
, void *arg_ptr TSRMLS_DC
)
339 struct apply_to_params_arg
*arg
= arg_ptr
;
341 SEPARATE_ZVAL_IF_NOT_REF((zval
**) zp
);
342 php_pq_params_set_param(arg
->params
, arg
->index
++, zp
);
343 return ZEND_HASH_APPLY_KEEP
;
346 unsigned php_pq_params_add_param(php_pq_params_t
*p
, zval
*param
)
348 p
->param
.strings
= safe_erealloc(p
->param
.strings
, ++p
->param
.count
, sizeof(*p
->param
.strings
), 0);
349 php_pq_params_set_param(p
, p
->param
.count
-1, ¶m
);
350 return p
->type
.count
;
353 unsigned php_pq_params_set_params(php_pq_params_t
*p
, HashTable
*params
)
355 p
->param
.count
= params
? zend_hash_num_elements(params
) : 0;
358 if (p
->param
.strings
) {
359 efree(p
->param
.strings
);
360 p
->param
.strings
= NULL
;
362 zend_hash_clean(&p
->param
.dtor
);
363 if (p
->param
.count
) {
364 struct apply_to_params_arg arg
= {p
, 0};
365 p
->param
.strings
= ecalloc(p
->param
.count
, sizeof(*p
->param
.strings
));
366 zend_hash_apply_with_argument(params
, apply_to_params
, &arg TSRMLS_CC
);
368 return p
->param
.count
;
371 void php_pq_params_free(php_pq_params_t
**p
)
374 php_pq_params_set_type_oids(*p
, NULL
);
375 php_pq_params_set_params(*p
, NULL
);
377 zend_hash_destroy(&(*p
)->param
.dtor
);
378 zend_hash_destroy(&(*p
)->type
.conv
);
385 php_pq_params_t
*php_pq_params_init(HashTable
*conv
, HashTable
*oids
, HashTable
*params TSRMLS_DC
)
387 php_pq_params_t
*p
= ecalloc(1, sizeof(*p
));
390 zend_hash_init(&p
->type
.conv
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
391 zend_hash_init(&p
->param
.dtor
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
394 php_pq_params_set_type_conv(p
, conv
);
397 php_pq_params_set_type_oids(p
, oids
);
400 php_pq_params_set_params(p
, params
);
411 * vim600: noet sw=4 ts=4 fdm=marker
412 * vim<600: noet sw=4 ts=4