09b781b6b50b45046c5f72b0be50edddb5a863ec
[m6w6/ext-pq] / src / php_pqstm.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_pqconn.h"
25 #include "php_pqres.h"
26 #include "php_pqstm.h"
27
28 zend_class_entry *php_pqstm_class_entry;
29 static zend_object_handlers php_pqstm_object_handlers;
30 static HashTable php_pqstm_object_prophandlers;
31
32 static void php_pqstm_object_free(void *o TSRMLS_DC)
33 {
34 php_pqstm_object_t *obj = o;
35 #if DBG_GC
36 fprintf(stderr, "FREE stm(#%d) %p (conn(#%d): %p)\n", obj->zv.handle, obj, obj->intern->conn->zv.handle, obj->intern->conn);
37 #endif
38 if (obj->intern) {
39 char *quoted_name = PQescapeIdentifier(obj->intern->conn->intern->conn, obj->intern->name, strlen(obj->intern->name));
40
41 php_pq_callback_dtor(&obj->intern->conn->intern->onevent);
42
43 if (quoted_name) {
44 PGresult *res;
45 smart_str cmd = {0};
46
47 smart_str_appends(&cmd, "DEALLOCATE ");
48 smart_str_appends(&cmd, quoted_name);
49 smart_str_0(&cmd);
50 PQfreemem(quoted_name);
51
52 if ((res = PQexec(obj->intern->conn->intern->conn, cmd.c))) {
53 PHP_PQclear(res);
54 }
55 smart_str_free(&cmd);
56 }
57
58 php_pq_object_delref(obj->intern->conn TSRMLS_CC);
59 efree(obj->intern->name);
60 zend_hash_destroy(&obj->intern->bound);
61 if (obj->intern->params) {
62 php_pq_params_free(&obj->intern->params);
63 }
64 efree(obj->intern);
65 obj->intern = NULL;
66 }
67 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
68 efree(obj);
69 }
70
71 zend_object_value php_pqstm_create_object_ex(zend_class_entry *ce, php_pqstm_t *intern, php_pqstm_object_t **ptr TSRMLS_DC)
72 {
73 php_pqstm_object_t *o;
74
75 o = ecalloc(1, sizeof(*o));
76 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
77 object_properties_init((zend_object *) o, ce);
78 o->prophandler = &php_pqstm_object_prophandlers;
79
80 if (ptr) {
81 *ptr = o;
82 }
83
84 if (intern) {
85 o->intern = intern;
86 }
87
88 o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_pqstm_object_free, NULL TSRMLS_CC);
89 o->zv.handlers = &php_pqstm_object_handlers;
90
91 return o->zv;
92 }
93
94 static zend_object_value php_pqstm_create_object(zend_class_entry *class_type TSRMLS_DC)
95 {
96 return php_pqstm_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
97 }
98
99 static void php_pqstm_object_read_name(zval *object, void *o, zval *return_value TSRMLS_DC)
100 {
101 php_pqstm_object_t *obj = o;
102
103 RETVAL_STRING(obj->intern->name, 1);
104 }
105
106 static void php_pqstm_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
107 {
108 php_pqstm_object_t *obj = o;
109
110 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
111 }
112
113
114 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_construct, 0, 0, 3)
115 ZEND_ARG_OBJ_INFO(0, connection, pq\\Connection, 0)
116 ZEND_ARG_INFO(0, name)
117 ZEND_ARG_INFO(0, query)
118 ZEND_ARG_ARRAY_INFO(0, types, 1)
119 ZEND_ARG_INFO(0, async)
120 ZEND_END_ARG_INFO();
121 static PHP_METHOD(pqstm, __construct) {
122 zend_error_handling zeh;
123 zval *zconn, *ztypes = NULL;
124 char *name_str, *query_str;
125 int name_len, *query_len;
126 zend_bool async = 0;
127 STATUS rv;
128
129 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
130 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oss|a/!b", &zconn, php_pqconn_class_entry, &name_str, &name_len, &query_str, &query_len, &ztypes, &async);
131 zend_restore_error_handling(&zeh TSRMLS_CC);
132
133 if (SUCCESS == rv) {
134 php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
135 php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
136
137 if (obj->intern) {
138 throw_exce(EX_BAD_METHODCALL TSRMLS_CC, "pq\\Statement already initialized");
139 } else if (!conn_obj->intern) {
140 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
141 } else {
142 php_pq_params_t *params = php_pq_params_init(&conn_obj->intern->converters, ztypes ? Z_ARRVAL_P(ztypes) : NULL, NULL TSRMLS_CC);
143
144 if (async) {
145 rv = php_pqconn_prepare_async(zconn, conn_obj, name_str, query_str, params TSRMLS_CC);
146 } else {
147 rv = php_pqconn_prepare(zconn, conn_obj, name_str, query_str, params TSRMLS_CC);
148 }
149
150 if (SUCCESS == rv) {
151 php_pqstm_t *stm = ecalloc(1, sizeof(*stm));
152
153 php_pq_object_addref(conn_obj TSRMLS_CC);
154 stm->conn = conn_obj;
155 stm->name = estrdup(name_str);
156 stm->params = params;
157 ZEND_INIT_SYMTABLE(&stm->bound);
158 obj->intern = stm;
159 }
160 }
161 }
162 }
163 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_bind, 0, 0, 2)
164 ZEND_ARG_INFO(0, param_no)
165 ZEND_ARG_INFO(1, param_ref)
166 ZEND_END_ARG_INFO();
167 static PHP_METHOD(pqstm, bind) {
168 long param_no;
169 zval **param_ref;
170 zend_error_handling zeh;
171 STATUS rv;
172
173 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
174 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lZ", &param_no, &param_ref);
175 zend_restore_error_handling(&zeh TSRMLS_CC);
176
177 if (SUCCESS == rv) {
178 php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
179
180 if (!obj->intern) {
181 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement not initialized");
182 } else {
183 SEPARATE_ZVAL_TO_MAKE_IS_REF(param_ref);
184 Z_ADDREF_PP(param_ref);
185 zend_hash_index_update(&obj->intern->bound, param_no, (void *) param_ref, sizeof(zval *), NULL);
186 zend_hash_sort(&obj->intern->bound, zend_qsort, php_pq_compare_index, 0 TSRMLS_CC);
187 }
188 }
189 }
190
191 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_exec, 0, 0, 0)
192 ZEND_ARG_ARRAY_INFO(0, params, 1)
193 ZEND_END_ARG_INFO();
194 static PHP_METHOD(pqstm, exec) {
195 zend_error_handling zeh;
196 zval *zparams = NULL;
197 STATUS rv;
198
199 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
200 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &zparams);
201 zend_restore_error_handling(&zeh TSRMLS_CC);
202
203 if (SUCCESS == rv) {
204 php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
205
206 if (!obj->intern) {
207 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement not initialized");
208 } else {
209 PGresult *res;
210
211 php_pq_params_set_params(obj->intern->params, zparams ? Z_ARRVAL_P(zparams) : &obj->intern->bound);
212 res = PQexecPrepared(obj->intern->conn->intern->conn, obj->intern->name, obj->intern->params->param.count, (const char *const*) obj->intern->params->param.strings, NULL, NULL, 0);
213 php_pq_params_set_params(obj->intern->params, NULL);
214
215 if (!res) {
216 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to execute statement (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
217 } else if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
218 php_pq_object_to_zval_no_addref(PQresultInstanceData(res, php_pqconn_event), &return_value TSRMLS_CC);
219 php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
220 }
221 }
222 }
223 }
224
225 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_exec_async, 0, 0, 0)
226 ZEND_ARG_ARRAY_INFO(0, params, 1)
227 ZEND_ARG_INFO(0, callable)
228 ZEND_END_ARG_INFO();
229 static PHP_METHOD(pqstm, execAsync) {
230 zend_error_handling zeh;
231 zval *zparams = NULL;
232 php_pq_callback_t resolver = {{0}};
233 STATUS rv;
234
235 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
236 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!f", &zparams, &resolver.fci, &resolver.fcc);
237 zend_restore_error_handling(&zeh TSRMLS_CC);
238
239 if (SUCCESS == rv) {
240 php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
241
242 if (!obj->intern) {
243 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement not initialized");
244 } else {
245 int rc;
246
247 php_pq_params_set_params(obj->intern->params, zparams ? Z_ARRVAL_P(zparams) : &obj->intern->bound);
248 rc = PQsendQueryPrepared(obj->intern->conn->intern->conn, obj->intern->name, obj->intern->params->param.count, (const char *const*) obj->intern->params->param.strings, NULL, NULL, 0);
249 php_pq_params_set_params(obj->intern->params, NULL);
250
251 if (!rc) {
252 throw_exce(EX_IO TSRMLS_CC, "Failed to execute statement (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
253 #if HAVE_PQSETSINGLEROWMODE
254 } else if (obj->intern->conn->intern->unbuffered && !PQsetSingleRowMode(obj->intern->conn->intern->conn)) {
255 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
256 #endif
257 } else {
258 php_pq_callback_recurse(&obj->intern->conn->intern->onevent, &resolver TSRMLS_CC);
259 obj->intern->conn->intern->poller = PQconsumeInput;
260 }
261
262 php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
263 }
264 }
265 }
266
267 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_desc, 0, 0, 0)
268 ZEND_END_ARG_INFO();
269 static PHP_METHOD(pqstm, desc) {
270 zend_error_handling zeh;
271 STATUS rv;
272
273 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
274 rv = zend_parse_parameters_none();
275 zend_restore_error_handling(&zeh TSRMLS_CC);
276
277 if (SUCCESS == rv) {
278 php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
279
280 if (!obj->intern) {
281 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement not initialized");
282 } else {
283 PGresult *res = PQdescribePrepared(obj->intern->conn->intern->conn, obj->intern->name);
284
285 if (!res) {
286 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to describe statement (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
287 } else {
288 if (SUCCESS == php_pqres_success(res TSRMLS_CC)) {
289 int p, params;
290
291 array_init(return_value);
292 for (p = 0, params = PQnparams(res); p < params; ++p) {
293 add_next_index_long(return_value, PQparamtype(res, p));
294 }
295 }
296 PHP_PQclear(res);
297 php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
298 }
299 }
300 }
301 }
302
303 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_desc_async, 0, 0, 1)
304 ZEND_ARG_INFO(0, callable)
305 ZEND_END_ARG_INFO();
306 static PHP_METHOD(pqstm, descAsync) {
307 zend_error_handling zeh;
308 php_pq_callback_t resolver = {{0}};
309 STATUS rv;
310
311 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
312 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f", &resolver.fci, &resolver.fcc);
313 zend_restore_error_handling(&zeh TSRMLS_CC);
314
315 if (SUCCESS == rv) {
316 php_pqstm_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
317
318 if (!obj->intern) {
319 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Statement not initialized");
320 } else if (!PQsendDescribePrepared(obj->intern->conn->intern->conn, obj->intern->name)) {
321 throw_exce(EX_IO TSRMLS_CC, "Failed to describe statement: %s", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
322 } else {
323 php_pq_callback_recurse(&obj->intern->conn->intern->onevent, &resolver TSRMLS_CC);
324 obj->intern->conn->intern->poller = PQconsumeInput;
325 php_pqconn_notify_listeners(obj->intern->conn TSRMLS_CC);
326 }
327 }
328 }
329
330 static zend_function_entry php_pqstm_methods[] = {
331 PHP_ME(pqstm, __construct, ai_pqstm_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
332 PHP_ME(pqstm, bind, ai_pqstm_bind, ZEND_ACC_PUBLIC)
333 PHP_ME(pqstm, exec, ai_pqstm_exec, ZEND_ACC_PUBLIC)
334 PHP_ME(pqstm, desc, ai_pqstm_desc, ZEND_ACC_PUBLIC)
335 PHP_ME(pqstm, execAsync, ai_pqstm_exec_async, ZEND_ACC_PUBLIC)
336 PHP_ME(pqstm, descAsync, ai_pqstm_desc_async, ZEND_ACC_PUBLIC)
337 {0}
338 };
339
340 PHP_MSHUTDOWN_FUNCTION(pqstm)
341 {
342 zend_hash_destroy(&php_pqstm_object_prophandlers);
343 return SUCCESS;
344 }
345
346 PHP_MINIT_FUNCTION(pqstm)
347 {
348 zend_class_entry ce = {0};
349 php_pq_object_prophandler_t ph = {0};
350
351 INIT_NS_CLASS_ENTRY(ce, "pq", "Statement", php_pqstm_methods);
352 php_pqstm_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
353 php_pqstm_class_entry->create_object = php_pqstm_create_object;
354
355 memcpy(&php_pqstm_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
356 php_pqstm_object_handlers.read_property = php_pq_object_read_prop;
357 php_pqstm_object_handlers.write_property = php_pq_object_write_prop;
358 php_pqstm_object_handlers.clone_obj = NULL;
359 php_pqstm_object_handlers.get_property_ptr_ptr = NULL;
360 php_pqstm_object_handlers.get_gc = NULL;
361 php_pqstm_object_handlers.get_properties = php_pq_object_properties;
362 php_pqstm_object_handlers.get_debug_info = php_pq_object_debug_info;
363
364 zend_hash_init(&php_pqstm_object_prophandlers, 2, NULL, NULL, 1);
365
366 zend_declare_property_null(php_pqstm_class_entry, ZEND_STRL("name"), ZEND_ACC_PUBLIC TSRMLS_CC);
367 ph.read = php_pqstm_object_read_name;
368 zend_hash_add(&php_pqstm_object_prophandlers, "name", sizeof("name"), (void *) &ph, sizeof(ph), NULL);
369
370 zend_declare_property_null(php_pqstm_class_entry, ZEND_STRL("connection"), ZEND_ACC_PUBLIC TSRMLS_CC);
371 ph.read = php_pqstm_object_read_connection;
372 zend_hash_add(&php_pqstm_object_prophandlers, "connection", sizeof("connection"), (void *) &ph, sizeof(ph), NULL);
373
374 return SUCCESS;
375 }
376
377 /*
378 * Local variables:
379 * tab-width: 4
380 * c-basic-offset: 4
381 * End:
382 * vim600: noet sw=4 ts=4 fdm=marker
383 * vim<600: noet sw=4 ts=4
384 */