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 #if PHP_PQ_HAVE_PHP_JSON_H
20 #include <php_json.h> /* we've added the include directory to INCLUDES */
23 #include <Zend/zend_smart_str.h>
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
);
40 static int apply_to_oid(zval
*ztype
, void *arg
)
44 **types
= zval_get_long(ztype
);
47 return ZEND_HASH_APPLY_KEEP
;
50 unsigned php_pq_params_set_type_oids(php_pq_params_t
*p
, HashTable
*oids
)
52 p
->type
.count
= oids
? zend_hash_num_elements(oids
) : 0;
59 Oid
*ptr
= ecalloc(p
->type
.count
+ 1, sizeof(*p
->type
.oids
));
60 /* +1 for when less types than params are specified */
62 zend_hash_apply_with_argument(oids
, apply_to_oid
, &ptr
);
67 unsigned php_pq_params_add_type_oid(php_pq_params_t
*p
, Oid type
)
69 p
->type
.oids
= safe_erealloc(p
->type
.oids
, ++p
->type
.count
, sizeof(*p
->type
.oids
), sizeof(*p
->type
.oids
));
70 p
->type
.oids
[p
->type
.count
] = 0;
71 p
->type
.oids
[p
->type
.count
-1] = type
;
76 static zend_string
*object_param_to_string(php_pq_params_t
*p
, zval
*zobj
, Oid type
)
78 #if PHP_PQ_HAVE_PHP_JSON_H && defined(PHP_PQ_OID_JSON)
83 #if PHP_PQ_HAVE_PHP_JSON_H && defined(PHP_PQ_OID_JSON)
84 # ifdef PHP_PQ_OID_JSONB
85 case PHP_PQ_OID_JSONB
:
88 # if PHP_VERSION_ID >= 70100
89 JSON_G(encode_max_depth
) = PHP_JSON_PARSER_DEFAULT_DEPTH
;
91 php_json_encode(&str
, zobj
, PHP_JSON_UNESCAPED_UNICODE
);
97 return php_pqdt_to_string(zobj
, "Y-m-d");
99 case PHP_PQ_OID_ABSTIME
:
100 return php_pqdt_to_string(zobj
, "Y-m-d H:i:s");
102 case PHP_PQ_OID_TIMESTAMP
:
103 return php_pqdt_to_string(zobj
, "Y-m-d H:i:s.u");
105 case PHP_PQ_OID_TIMESTAMPTZ
:
106 return php_pqdt_to_string(zobj
, "Y-m-d H:i:s.uO");
109 return zval_get_string(zobj
);
112 struct apply_to_param_from_array_arg
{
113 php_pq_params_t
*params
;
121 static int apply_to_param_from_array(zval
*zparam
, void *arg_ptr
)
123 struct apply_to_param_from_array_arg subarg
, *arg
= arg_ptr
;
129 smart_str_appendc(arg
->buffer
, arg
->delim
);
135 ZVAL_LONG(&ztype
, arg
->type
);
136 zend_call_method_with_2_params(arg
->zconv
, NULL
, NULL
, "converttostring", &rv
, zparam
, &ztype
);
137 str
= zval_get_string(&rv
);
143 switch (Z_TYPE_P(zparam
)) {
149 smart_str_appends(arg
->buffer
, "NULL");
153 smart_str_appends(arg
->buffer
, "t");
157 smart_str_appends(arg
->buffer
, "f");
161 smart_str_append_long(arg
->buffer
, Z_LVAL_P(zparam
));
165 len
= spprintf(&tmp
, 0, "%F", Z_DVAL_P(zparam
));
166 smart_str_appendl(arg
->buffer
, tmp
, len
);
173 smart_str_appendc(arg
->buffer
, '{');
174 zend_hash_apply_with_argument(Z_ARRVAL_P(zparam
), apply_to_param_from_array
, &subarg
);
175 smart_str_appendc(arg
->buffer
, '}');
179 if ((str
= object_param_to_string(arg
->params
, zparam
, arg
->type
))) {
184 str
= zval_get_string(zparam
);
187 str
= php_addslashes(str
, 1);
188 smart_str_appendc(arg
->buffer
, '"');
189 smart_str_appendl(arg
->buffer
, str
->val
, str
->len
);
190 smart_str_appendc(arg
->buffer
, '"');
191 zend_string_release(str
);
196 return ZEND_HASH_APPLY_KEEP
;
199 static zend_string
*array_param_to_string(php_pq_params_t
*p
, zval
*zarr
, Oid type
)
202 struct apply_to_param_from_array_arg arg
= {NULL
};
205 #if PHP_PQ_HAVE_PHP_JSON_H && defined(PHP_PQ_OID_JSON)
206 # ifdef PHP_PQ_OID_JSONB
207 case PHP_PQ_OID_JSONB
:
209 case PHP_PQ_OID_JSON
:
210 php_json_encode(&s
, zarr
, PHP_JSON_UNESCAPED_UNICODE
);
217 arg
.type
= PHP_PQ_TYPE_OF_ARRAY(type
);
218 arg
.delim
= PHP_PQ_DELIM_OF_ARRAY(type
);
219 arg
.zconv
= zend_hash_index_find(&p
->type
.conv
, PHP_PQ_TYPE_OF_ARRAY(type
));
220 smart_str_appendc(arg
.buffer
, '{');
222 zend_hash_apply_with_argument(Z_ARRVAL_P(zarr
), apply_to_param_from_array
, &arg
);
223 smart_str_appendc(arg
.buffer
, '}');
231 static void php_pq_params_set_param(php_pq_params_t
*p
, unsigned index
, zval
*zpp
)
234 Oid type
= p
->type
.count
> index
? p
->type
.oids
[index
] : 0;
236 if (type
&& (zconv
= zend_hash_index_find(&p
->type
.conv
, type
))) {
240 ZVAL_LONG(&ztype
, type
);
241 zend_call_method_with_2_params(zconv
, NULL
, NULL
, "converttostring", &rv
, zpp
, &ztype
);
242 convert_to_string(&rv
);
243 p
->param
.strings
[index
] = Z_STRVAL_P(&rv
);
244 zend_hash_next_index_insert(&p
->param
.dtor
, &rv
);
247 zend_string
*str
= NULL
;
252 switch (Z_TYPE_P(zpp
)) {
258 p
->param
.strings
[index
] = NULL
;
262 p
->param
.strings
[index
] = "t";
266 p
->param
.strings
[index
] = "f";
270 tmp_len
= slprintf(tmp_str
, sizeof(tmp_str
), "%F", Z_DVAL_P(zpp
));
271 str
= zend_string_init(tmp_str
, tmp_len
, 0);
275 str
= array_param_to_string(p
, zpp
, type
);
279 if ((str
= object_param_to_string(p
, zpp
, type
))) {
284 str
= zval_get_string(zpp
);
290 p
->param
.strings
[index
] = Z_STRVAL(tmp
);
291 zend_hash_next_index_insert(&p
->param
.dtor
, &tmp
);
296 struct apply_to_params_arg
{
297 php_pq_params_t
*params
;
301 static int apply_to_params(zval
*zp
, void *arg_ptr
)
303 struct apply_to_params_arg
*arg
= arg_ptr
;
305 SEPARATE_ZVAL_IF_NOT_REF(zp
);
306 php_pq_params_set_param(arg
->params
, arg
->index
++, zp
);
307 return ZEND_HASH_APPLY_KEEP
;
310 unsigned php_pq_params_add_param(php_pq_params_t
*p
, zval
*param
)
312 p
->param
.strings
= safe_erealloc(p
->param
.strings
, ++p
->param
.count
, sizeof(*p
->param
.strings
), 0);
313 php_pq_params_set_param(p
, p
->param
.count
-1, param
);
314 return p
->type
.count
;
317 unsigned php_pq_params_set_params(php_pq_params_t
*p
, HashTable
*params
)
319 p
->param
.count
= params
? zend_hash_num_elements(params
) : 0;
321 if (p
->param
.strings
) {
322 efree(p
->param
.strings
);
323 p
->param
.strings
= NULL
;
325 zend_hash_clean(&p
->param
.dtor
);
326 if (p
->param
.count
) {
327 struct apply_to_params_arg arg
= {p
, 0};
328 p
->param
.strings
= ecalloc(p
->param
.count
, sizeof(*p
->param
.strings
));
329 zend_hash_apply_with_argument(params
, apply_to_params
, &arg
);
331 return p
->param
.count
;
334 void php_pq_params_free(php_pq_params_t
**p
)
337 php_pq_params_set_type_oids(*p
, NULL
);
338 php_pq_params_set_params(*p
, NULL
);
340 zend_hash_destroy(&(*p
)->param
.dtor
);
341 zend_hash_destroy(&(*p
)->type
.conv
);
348 php_pq_params_t
*php_pq_params_init(HashTable
*conv
, HashTable
*oids
, HashTable
*params
)
350 php_pq_params_t
*p
= ecalloc(1, sizeof(*p
));
352 zend_hash_init(&p
->type
.conv
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
353 zend_hash_init(&p
->param
.dtor
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
356 php_pq_params_set_type_conv(p
, conv
);
359 php_pq_params_set_type_oids(p
, oids
);
362 php_pq_params_set_params(p
, params
);
373 * vim600: noet sw=4 ts=4 fdm=marker
374 * vim<600: noet sw=4 ts=4