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
)
81 #if PHP_PQ_HAVE_PHP_JSON_H && defined(PHP_PQ_OID_JSON)
82 # ifdef PHP_PQ_OID_JSONB
83 case PHP_PQ_OID_JSONB
:
86 php_json_encode(&str
, zobj
, PHP_JSON_UNESCAPED_UNICODE
);
92 return php_pqdt_to_string(zobj
, "Y-m-d");
94 case PHP_PQ_OID_ABSTIME
:
95 return php_pqdt_to_string(zobj
, "Y-m-d H:i:s");
97 case PHP_PQ_OID_TIMESTAMP
:
98 return php_pqdt_to_string(zobj
, "Y-m-d H:i:s.u");
100 case PHP_PQ_OID_TIMESTAMPTZ
:
101 return php_pqdt_to_string(zobj
, "Y-m-d H:i:s.uO");
104 return zval_get_string(zobj
);
107 struct apply_to_param_from_array_arg
{
108 php_pq_params_t
*params
;
116 static int apply_to_param_from_array(zval
*zparam
, void *arg_ptr
)
118 struct apply_to_param_from_array_arg subarg
, *arg
= arg_ptr
;
124 smart_str_appendc(arg
->buffer
, arg
->delim
);
130 ZVAL_LONG(&ztype
, arg
->type
);
131 zend_call_method_with_2_params(arg
->zconv
, NULL
, NULL
, "converttostring", &rv
, zparam
, &ztype
);
132 str
= zval_get_string(&rv
);
138 switch (Z_TYPE_P(zparam
)) {
144 smart_str_appends(arg
->buffer
, "NULL");
148 smart_str_appends(arg
->buffer
, "t");
152 smart_str_appends(arg
->buffer
, "f");
156 smart_str_append_long(arg
->buffer
, Z_LVAL_P(zparam
));
160 len
= spprintf(&tmp
, 0, "%F", Z_DVAL_P(zparam
));
161 smart_str_appendl(arg
->buffer
, tmp
, len
);
168 smart_str_appendc(arg
->buffer
, '{');
169 zend_hash_apply_with_argument(Z_ARRVAL_P(zparam
), apply_to_param_from_array
, &subarg
);
170 smart_str_appendc(arg
->buffer
, '}');
174 if ((str
= object_param_to_string(arg
->params
, zparam
, arg
->type
))) {
179 str
= zval_get_string(zparam
);
182 str
= php_addslashes(str
, 1);
183 smart_str_appendc(arg
->buffer
, '"');
184 smart_str_appendl(arg
->buffer
, str
->val
, str
->len
);
185 smart_str_appendc(arg
->buffer
, '"');
186 zend_string_release(str
);
191 return ZEND_HASH_APPLY_KEEP
;
194 static zend_string
*array_param_to_string(php_pq_params_t
*p
, zval
*zarr
, Oid type
)
197 struct apply_to_param_from_array_arg arg
= {NULL
};
200 #if PHP_PQ_HAVE_PHP_JSON_H && defined(PHP_PQ_OID_JSON)
201 # ifdef PHP_PQ_OID_JSONB
202 case PHP_PQ_OID_JSONB
:
204 case PHP_PQ_OID_JSON
:
205 php_json_encode(&s
, zarr
, PHP_JSON_UNESCAPED_UNICODE
);
212 arg
.type
= PHP_PQ_TYPE_OF_ARRAY(type
);
213 arg
.delim
= PHP_PQ_DELIM_OF_ARRAY(type
);
214 arg
.zconv
= zend_hash_index_find(&p
->type
.conv
, PHP_PQ_TYPE_OF_ARRAY(type
));
215 smart_str_appendc(arg
.buffer
, '{');
217 zend_hash_apply_with_argument(Z_ARRVAL_P(zarr
), apply_to_param_from_array
, &arg
);
218 smart_str_appendc(arg
.buffer
, '}');
226 static void php_pq_params_set_param(php_pq_params_t
*p
, unsigned index
, zval
*zpp
)
229 Oid type
= p
->type
.count
> index
? p
->type
.oids
[index
] : 0;
231 if (type
&& (zconv
= zend_hash_index_find(&p
->type
.conv
, type
))) {
235 ZVAL_LONG(&ztype
, type
);
236 zend_call_method_with_2_params(zconv
, NULL
, NULL
, "converttostring", &rv
, zpp
, &ztype
);
237 convert_to_string(&rv
);
238 p
->param
.strings
[index
] = Z_STRVAL_P(&rv
);
239 zend_hash_next_index_insert(&p
->param
.dtor
, &rv
);
242 zend_string
*str
= NULL
;
247 switch (Z_TYPE_P(zpp
)) {
253 p
->param
.strings
[index
] = NULL
;
257 p
->param
.strings
[index
] = "t";
261 p
->param
.strings
[index
] = "f";
265 tmp_len
= slprintf(tmp_str
, sizeof(tmp_str
), "%F", Z_DVAL_P(zpp
));
266 str
= zend_string_init(tmp_str
, tmp_len
, 0);
270 str
= array_param_to_string(p
, zpp
, type
);
274 if ((str
= object_param_to_string(p
, zpp
, type
))) {
279 str
= zval_get_string(zpp
);
285 p
->param
.strings
[index
] = Z_STRVAL(tmp
);
286 zend_hash_next_index_insert(&p
->param
.dtor
, &tmp
);
291 struct apply_to_params_arg
{
292 php_pq_params_t
*params
;
296 static int apply_to_params(zval
*zp
, void *arg_ptr
)
298 struct apply_to_params_arg
*arg
= arg_ptr
;
300 SEPARATE_ZVAL_IF_NOT_REF(zp
);
301 php_pq_params_set_param(arg
->params
, arg
->index
++, zp
);
302 return ZEND_HASH_APPLY_KEEP
;
305 unsigned php_pq_params_add_param(php_pq_params_t
*p
, zval
*param
)
307 p
->param
.strings
= safe_erealloc(p
->param
.strings
, ++p
->param
.count
, sizeof(*p
->param
.strings
), 0);
308 php_pq_params_set_param(p
, p
->param
.count
-1, param
);
309 return p
->type
.count
;
312 unsigned php_pq_params_set_params(php_pq_params_t
*p
, HashTable
*params
)
314 p
->param
.count
= params
? zend_hash_num_elements(params
) : 0;
316 if (p
->param
.strings
) {
317 efree(p
->param
.strings
);
318 p
->param
.strings
= NULL
;
320 zend_hash_clean(&p
->param
.dtor
);
321 if (p
->param
.count
) {
322 struct apply_to_params_arg arg
= {p
, 0};
323 p
->param
.strings
= ecalloc(p
->param
.count
, sizeof(*p
->param
.strings
));
324 zend_hash_apply_with_argument(params
, apply_to_params
, &arg
);
326 return p
->param
.count
;
329 void php_pq_params_free(php_pq_params_t
**p
)
332 php_pq_params_set_type_oids(*p
, NULL
);
333 php_pq_params_set_params(*p
, NULL
);
335 zend_hash_destroy(&(*p
)->param
.dtor
);
336 zend_hash_destroy(&(*p
)->type
.conv
);
343 php_pq_params_t
*php_pq_params_init(HashTable
*conv
, HashTable
*oids
, HashTable
*params
)
345 php_pq_params_t
*p
= ecalloc(1, sizeof(*p
));
347 zend_hash_init(&p
->type
.conv
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
348 zend_hash_init(&p
->param
.dtor
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
351 php_pq_params_set_type_conv(p
, conv
);
354 php_pq_params_set_type_oids(p
, oids
);
357 php_pq_params_set_params(p
, params
);
368 * vim600: noet sw=4 ts=4 fdm=marker
369 * vim<600: noet sw=4 ts=4