refactor auto conversion
[m6w6/ext-pq] / src / php_pqtypes.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: pq |
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 +--------------------------------------------------------------------+
11 */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <php.h>
18 #include <ext/standard/php_smart_str.h>
19
20 #include "php_pq.h"
21 #include "php_pq_misc.h"
22 #include "php_pq_object.h"
23 #include "php_pqexc.h"
24 #include "php_pqres.h"
25 #include "php_pqtypes.h"
26
27 zend_class_entry *php_pqtypes_class_entry;
28 static zend_object_handlers php_pqtypes_object_handlers;
29 static HashTable php_pqtypes_object_prophandlers;
30
31 static void php_pqtypes_object_free(void *o TSRMLS_DC)
32 {
33 php_pqtypes_object_t *obj = o;
34 #if DBG_GC
35 fprintf(stderr, "FREE types(#%d) %p (conn(#%d): %p)\n", obj->zv.handle, obj, obj->intern->conn->zv.handle, obj->intern->conn);
36 #endif
37 if (obj->intern) {
38 zend_hash_destroy(&obj->intern->types);
39 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
40 efree(obj->intern);
41 obj->intern = NULL;
42 }
43 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
44 efree(obj);
45 }
46
47 zend_object_value php_pqtypes_create_object_ex(zend_class_entry *ce, php_pqtypes_t *intern, php_pqtypes_object_t **ptr TSRMLS_DC)
48 {
49 php_pqtypes_object_t *o;
50
51 o = ecalloc(1, sizeof(*o));
52 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
53 object_properties_init((zend_object *) o, ce);
54 o->prophandler = &php_pqtypes_object_prophandlers;
55
56 if (ptr) {
57 *ptr = o;
58 }
59
60 if (intern) {
61 o->intern = intern;
62 }
63
64 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqtypes_object_free, NULL TSRMLS_CC);
65 o->zv.handlers = &php_pqtypes_object_handlers;
66
67 return o->zv;
68 }
69
70 static zend_object_value php_pqtypes_create_object(zend_class_entry *class_type TSRMLS_DC)
71 {
72 return php_pqtypes_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
73 }
74
75 static void php_pqtypes_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
76 {
77 php_pqtypes_object_t *obj = o;
78
79 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
80 }
81
82 static int has_dimension(HashTable *ht, zval *member, char **key_str, int *key_len, ulong *index TSRMLS_DC)
83 {
84 long lval = 0;
85 zval *tmp = member;
86
87 switch (Z_TYPE_P(member)) {
88 default:
89 convert_to_string_ex(&tmp);
90 /* no break */
91 case IS_STRING:
92 if (!is_numeric_string(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp), &lval, NULL, 0)) {
93 int exists = zend_hash_exists(ht, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp) + 1);
94
95 if (key_str) {
96 *key_str = estrndup(Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
97 if (key_len) {
98 *key_len = Z_STRLEN_P(tmp) + 1;
99 }
100 }
101 if (member != tmp) {
102 zval_ptr_dtor(&tmp);
103 }
104
105 return exists;
106 }
107 break;
108 case IS_LONG:
109 lval = Z_LVAL_P(member);
110 break;
111 }
112
113 if (member != tmp) {
114 zval_ptr_dtor(&tmp);
115 }
116 if (index) {
117 *index = lval;
118 }
119 return zend_hash_index_exists(ht, lval);
120 }
121
122 static int php_pqtypes_object_has_dimension(zval *object, zval *member, int check_empty TSRMLS_DC)
123 {
124 php_pqtypes_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
125 char *key_str = NULL;
126 int key_len = 0;
127 ulong index = 0;
128
129 if (check_empty) {
130 if (has_dimension(&obj->intern->types, member, &key_str, &key_len, &index TSRMLS_CC)) {
131 zval **data;
132
133 if (key_str && key_len) {
134 if (SUCCESS == zend_hash_find(&obj->intern->types, key_str, key_len, (void *) &data)) {
135 efree(key_str);
136 return Z_TYPE_PP(data) != IS_NULL;
137 }
138 efree(key_str);
139 key_str = NULL;
140 } else {
141 if (SUCCESS == zend_hash_index_find(&obj->intern->types, index, (void *) &data)) {
142 return Z_TYPE_PP(data) != IS_NULL;
143 }
144 }
145 }
146 if (key_str) {
147 efree(key_str);
148 }
149 } else {
150 return has_dimension(&obj->intern->types, member, NULL, NULL, NULL TSRMLS_CC);
151 }
152
153 return 0;
154 }
155
156 static zval *php_pqtypes_object_read_dimension(zval *object, zval *member, int type TSRMLS_DC)
157 {
158 ulong index = 0;
159 char *key_str = NULL;
160 int key_len = 0;
161 php_pqtypes_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
162
163 if (has_dimension(&obj->intern->types, member, &key_str, &key_len, &index TSRMLS_CC)) {
164 zval **data;
165
166 if (key_str && key_len) {
167 if (SUCCESS == zend_hash_find(&obj->intern->types, key_str, key_len, (void *) &data)) {
168 efree(key_str);
169 return *data;
170 }
171 } else {
172 if (SUCCESS == zend_hash_index_find(&obj->intern->types, index, (void *) &data)) {
173 return *data;
174 }
175 }
176 }
177
178 if (key_str) {
179 efree(key_str);
180 }
181
182 return NULL;
183 }
184
185 ZEND_BEGIN_ARG_INFO_EX(ai_pqtypes_construct, 0, 0, 1)
186 ZEND_ARG_OBJ_INFO(0, connection, pq\\Connection, 0)
187 ZEND_ARG_ARRAY_INFO(0, namespaces, 1)
188 ZEND_END_ARG_INFO();
189 static PHP_METHOD(pqtypes, __construct) {
190 zend_error_handling zeh;
191 zval *zconn, *znsp = NULL;
192 STATUS rv;
193
194 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
195 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|a!", &zconn, php_pqconn_class_entry, &znsp);
196 zend_restore_error_handling(&zeh TSRMLS_CC);
197
198 if (SUCCESS == rv) {
199 php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
200
201 if (!conn_obj->intern) {
202 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
203 } else {
204 php_pqtypes_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
205 zval *retval = NULL;
206
207 obj->intern = ecalloc(1, sizeof(*obj->intern));
208 obj->intern->conn = conn_obj;
209 php_pq_object_addref(conn_obj TSRMLS_CC);
210 zend_hash_init(&obj->intern->types, 300, NULL, ZVAL_PTR_DTOR, 0);
211
212 if (znsp) {
213 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "refresh", &retval, znsp);
214 } else {
215 zend_call_method_with_0_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "refresh", &retval);
216 }
217
218 if (retval) {
219 zval_ptr_dtor(&retval);
220 }
221 }
222 }
223 }
224
225 #define PHP_PQ_TYPES_QUERY \
226 "select t.oid, t.* " \
227 "from pg_type t join pg_namespace n on t.typnamespace=n.oid " \
228 "where typisdefined " \
229 "and typrelid=0"
230 #ifndef PHP_PQ_OID_TEXT
231 # define PHP_PQ_OID_TEXT 25
232 #endif
233
234 static int apply_nsp(void *p TSRMLS_DC, int argc, va_list argv, zend_hash_key *key)
235 {
236 zval **zp = p;
237 unsigned pcount, tcount;
238 php_pq_params_t *params = va_arg(argv, php_pq_params_t *);
239 smart_str *str = va_arg(argv, smart_str *);
240
241 tcount = php_pq_params_add_type_oid(params, PHP_PQ_OID_TEXT);
242 pcount = php_pq_params_add_param(params, *zp);
243
244 if (tcount != pcount) {
245 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Param/Type count mismatch");
246 return ZEND_HASH_APPLY_STOP;
247 }
248 if (pcount > 1) {
249 smart_str_appendc(str, ',');
250 }
251 smart_str_appendc(str, '$');
252 smart_str_append_unsigned(str, pcount);
253
254 return ZEND_HASH_APPLY_KEEP;
255 }
256
257 ZEND_BEGIN_ARG_INFO_EX(ai_pqtypes_refresh, 0, 0, 0)
258 ZEND_ARG_ARRAY_INFO(0, namespaces, 1)
259 ZEND_END_ARG_INFO();
260 static PHP_METHOD(pqtypes, refresh) {
261 HashTable *nsp = NULL;
262 zend_error_handling zeh;
263 STATUS rv;
264
265 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
266 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|H/!", &nsp);
267 zend_restore_error_handling(&zeh TSRMLS_CC);
268
269 if (SUCCESS == rv) {
270 php_pqtypes_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
271
272 if (!obj->intern) {
273 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Types not initialized");
274 } else {
275 PGresult *res;
276
277 if (!nsp || !zend_hash_num_elements(nsp)) {
278 res = PQexec(obj->intern->conn->intern->conn, PHP_PQ_TYPES_QUERY " and nspname in ('public', 'pg_catalog')");
279 } else {
280 smart_str str = {0};
281 php_pq_params_t *params = php_pq_params_init(&obj->intern->conn->intern->converters, NULL, NULL TSRMLS_CC);
282
283 smart_str_appends(&str, PHP_PQ_TYPES_QUERY " and nspname in(");
284 zend_hash_apply_with_arguments(nsp TSRMLS_CC, apply_nsp, 2, params, &str);
285 smart_str_appendc(&str, ')');
286 smart_str_0(&str);
287
288 res = PQexecParams(obj->intern->conn->intern->conn, str.c, params->param.count, params->type.oids, (const char *const*) params->param.strings, NULL, NULL, 0);
289
290 smart_str_free(&str);
291 php_pq_params_free(&params);
292 }
293
294 if (!res) {
295 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to fetch types (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
296 } else {
297 if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
298 int r, rows;
299
300 for (r = 0, rows = PQntuples(res); r < rows; ++r) {
301 zval *row = php_pqres_row_to_zval(res, r, PHP_PQRES_FETCH_OBJECT, NULL TSRMLS_CC);
302 long oid = atol(PQgetvalue(res, r, 0 ));
303 char *name = PQgetvalue(res, r, 1);
304
305 Z_ADDREF_P(row);
306
307 zend_hash_index_update(&obj->intern->types, oid, (void *) &row, sizeof(zval *), NULL);
308 zend_hash_update(&obj->intern->types, name, strlen(name) + 1, (void *) &row, sizeof(zval *), NULL);
309 }
310 }
311
312 PHP_PQclear(res);
313 php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
314 }
315 }
316 }
317 }
318
319 static zend_function_entry php_pqtypes_methods[] = {
320 PHP_ME(pqtypes, __construct, ai_pqtypes_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
321 PHP_ME(pqtypes, refresh, ai_pqtypes_refresh, ZEND_ACC_PUBLIC)
322 {0}
323 };
324
325 PHP_MSHUTDOWN_FUNCTION(pqtypes)
326 {
327 zend_hash_destroy(&php_pqtypes_object_prophandlers);
328 return SUCCESS;
329 }
330
331 PHP_MINIT_FUNCTION(pqtypes)
332 {
333 zend_class_entry ce = {0};
334 php_pq_object_prophandler_t ph = {0};
335
336 INIT_NS_CLASS_ENTRY(ce, "pq", "Types", php_pqtypes_methods);
337 php_pqtypes_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
338 php_pqtypes_class_entry->create_object = php_pqtypes_create_object;
339
340 memcpy(&php_pqtypes_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
341 php_pqtypes_object_handlers.read_property = php_pq_object_read_prop;
342 php_pqtypes_object_handlers.write_property = php_pq_object_write_prop;
343 php_pqtypes_object_handlers.clone_obj = NULL;
344 php_pqtypes_object_handlers.get_property_ptr_ptr = NULL;
345 php_pqtypes_object_handlers.get_gc = NULL;
346 php_pqtypes_object_handlers.get_properties = php_pq_object_properties;
347 php_pqtypes_object_handlers.get_debug_info = php_pq_object_debug_info;
348 php_pqtypes_object_handlers.has_dimension = php_pqtypes_object_has_dimension;
349 php_pqtypes_object_handlers.read_dimension = php_pqtypes_object_read_dimension;
350 php_pqtypes_object_handlers.unset_dimension = NULL;
351 php_pqtypes_object_handlers.write_dimension = NULL;
352
353 zend_hash_init(&php_pqtypes_object_prophandlers, 1, NULL, NULL, 1);
354
355 zend_declare_property_null(php_pqtypes_class_entry, ZEND_STRL("connection"), ZEND_ACC_PUBLIC TSRMLS_CC);
356 ph.read = php_pqtypes_object_read_connection;
357 zend_hash_add(&php_pqtypes_object_prophandlers, "connection", sizeof("connection"), (void *) &ph, sizeof(ph), NULL);
358
359 # undef PHP_PQ_TYPE
360 # define PHP_PQ_TYPE(name, oid) zend_declare_class_constant_long(php_pqtypes_class_entry, ZEND_STRL(name), oid TSRMLS_CC);
361 # include "php_pq_type.h"
362
363 return SUCCESS;
364 }
365
366 /*
367 * Local variables:
368 * tab-width: 4
369 * c-basic-offset: 4
370 * End:
371 * vim600: noet sw=4 ts=4 fdm=marker
372 * vim<600: noet sw=4 ts=4
373 */