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_smart_str.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"
28 zend_class_entry
*php_pqstm_class_entry
;
29 static zend_object_handlers php_pqstm_object_handlers
;
30 static HashTable php_pqstm_object_prophandlers
;
32 static void php_pqstm_object_free(void *o TSRMLS_DC
)
34 php_pqstm_object_t
*obj
= o
;
36 fprintf(stderr
, "FREE stm(#%d) %p (conn(#%d): %p)\n", obj
->zv
.handle
, obj
, obj
->intern
->conn
->zv
.handle
, obj
->intern
->conn
);
39 char *quoted_name
= PQescapeIdentifier(obj
->intern
->conn
->intern
->conn
, obj
->intern
->name
, strlen(obj
->intern
->name
));
41 php_pq_callback_dtor(&obj
->intern
->conn
->intern
->onevent
);
47 smart_str_appends(&cmd
, "DEALLOCATE ");
48 smart_str_appends(&cmd
, quoted_name
);
50 PQfreemem(quoted_name
);
52 if ((res
= PQexec(obj
->intern
->conn
->intern
->conn
, cmd
.c
))) {
58 php_pq_object_delref(obj
->intern
->conn TSRMLS_CC
);
59 efree(obj
->intern
->name
);
60 zend_hash_destroy(&obj
->intern
->bound
);
64 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
68 zend_object_value
php_pqstm_create_object_ex(zend_class_entry
*ce
, php_pqstm_t
*intern
, php_pqstm_object_t
**ptr TSRMLS_DC
)
70 php_pqstm_object_t
*o
;
72 o
= ecalloc(1, sizeof(*o
));
73 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
74 object_properties_init((zend_object
*) o
, ce
);
75 o
->prophandler
= &php_pqstm_object_prophandlers
;
85 o
->zv
.handle
= zend_objects_store_put((zend_object
*) o
, NULL
, php_pqstm_object_free
, NULL TSRMLS_CC
);
86 o
->zv
.handlers
= &php_pqstm_object_handlers
;
91 static zend_object_value
php_pqstm_create_object(zend_class_entry
*class_type TSRMLS_DC
)
93 return php_pqstm_create_object_ex(class_type
, NULL
, NULL TSRMLS_CC
);
96 static void php_pqstm_object_read_name(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
98 php_pqstm_object_t
*obj
= o
;
100 RETVAL_STRING(obj
->intern
->name
, 1);
103 static void php_pqstm_object_read_connection(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
105 php_pqstm_object_t
*obj
= o
;
107 php_pq_object_to_zval(obj
->intern
->conn
, &return_value TSRMLS_CC
);
111 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_construct
, 0, 0, 3)
112 ZEND_ARG_OBJ_INFO(0, Connection
, pq
\\Connection
, 0)
113 ZEND_ARG_INFO(0, type
)
114 ZEND_ARG_INFO(0, query
)
115 ZEND_ARG_ARRAY_INFO(0, types
, 1)
116 ZEND_ARG_INFO(0, async
)
118 static PHP_METHOD(pqstm
, __construct
) {
119 zend_error_handling zeh
;
120 zval
*zconn
, *ztypes
= NULL
;
121 char *name_str
, *query_str
;
122 int name_len
, *query_len
;
126 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
127 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
);
128 zend_restore_error_handling(&zeh TSRMLS_CC
);
131 php_pqstm_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
132 php_pqconn_object_t
*conn_obj
= zend_object_store_get_object(zconn TSRMLS_CC
);
134 if (!conn_obj
->intern
) {
135 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
138 rv
= php_pqconn_prepare_async(zconn
, conn_obj
, name_str
, query_str
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL TSRMLS_CC
);
140 rv
= php_pqconn_prepare(zconn
, conn_obj
, name_str
, query_str
, ztypes
? Z_ARRVAL_P(ztypes
) : NULL TSRMLS_CC
);
144 php_pqstm_t
*stm
= ecalloc(1, sizeof(*stm
));
146 php_pq_object_addref(conn_obj TSRMLS_CC
);
147 stm
->conn
= conn_obj
;
148 stm
->name
= estrdup(name_str
);
149 ZEND_INIT_SYMTABLE(&stm
->bound
);
155 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_bind
, 0, 0, 2)
156 ZEND_ARG_INFO(0, param_no
)
157 ZEND_ARG_INFO(1, param_ref
)
159 static PHP_METHOD(pqstm
, bind
) {
163 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "lz", ¶m_no
, ¶m_ref
)) {
164 php_pqstm_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
167 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Statement not initialized");
169 Z_ADDREF_P(param_ref
);
170 zend_hash_index_update(&obj
->intern
->bound
, param_no
, (void *) ¶m_ref
, sizeof(zval
*), NULL
);
171 zend_hash_sort(&obj
->intern
->bound
, zend_qsort
, compare_index
, 0 TSRMLS_CC
);
176 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_exec
, 0, 0, 0)
177 ZEND_ARG_ARRAY_INFO(0, params
, 1)
179 static PHP_METHOD(pqstm
, exec
) {
180 zend_error_handling zeh
;
181 zval
*zparams
= NULL
;
184 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
185 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!", &zparams
);
186 zend_restore_error_handling(&zeh TSRMLS_CC
);
189 php_pqstm_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
192 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Statement not initialized");
195 char **params
= NULL
;
199 ZEND_INIT_SYMTABLE(&zdtor
);
202 count
= php_pq_params_to_array(Z_ARRVAL_P(zparams
), ¶ms
, &zdtor TSRMLS_CC
);
204 count
= php_pq_params_to_array(&obj
->intern
->bound
, ¶ms
, &zdtor TSRMLS_CC
);
207 res
= PQexecPrepared(obj
->intern
->conn
->intern
->conn
, obj
->intern
->name
, count
, (const char *const*) params
, NULL
, NULL
, 0);
212 zend_hash_destroy(&zdtor
);
215 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to execute statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
216 } else if (SUCCESS
== php_pqres_success(res TSRMLS_CC
)) {
217 php_pq_object_to_zval_no_addref(PQresultInstanceData(res
, php_pqconn_event
), &return_value TSRMLS_CC
);
218 php_pqconn_notify_listeners(obj
->intern
->conn TSRMLS_CC
);
224 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_exec_async
, 0, 0, 0)
225 ZEND_ARG_ARRAY_INFO(0, params
, 1)
226 ZEND_ARG_INFO(0, callable
)
228 static PHP_METHOD(pqstm
, execAsync
) {
229 zend_error_handling zeh
;
230 zval
*zparams
= NULL
;
231 php_pq_callback_t resolver
= {{0}};
234 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
235 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|a/!f", &zparams
, &resolver
.fci
, &resolver
.fcc
);
236 zend_restore_error_handling(&zeh TSRMLS_CC
);
239 php_pqstm_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
242 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Statement not initialized");
245 char **params
= NULL
;
249 ZEND_INIT_SYMTABLE(&zdtor
);
250 count
= php_pq_params_to_array(Z_ARRVAL_P(zparams
), ¶ms
, &zdtor TSRMLS_CC
);
253 if (!PQsendQueryPrepared(obj
->intern
->conn
->intern
->conn
, obj
->intern
->name
, count
, (const char *const*) params
, NULL
, NULL
, 0)) {
254 throw_exce(EX_IO TSRMLS_CC
, "Failed to execute statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
255 } else if (obj
->intern
->conn
->intern
->unbuffered
&& !PQsetSingleRowMode(obj
->intern
->conn
->intern
->conn
)) {
256 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to enable unbuffered mode (%s)", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
258 php_pq_callback_dtor(&obj
->intern
->conn
->intern
->onevent
);
259 if (resolver
.fci
.size
> 0) {
260 obj
->intern
->conn
->intern
->onevent
= resolver
;
261 php_pq_callback_addref(&obj
->intern
->conn
->intern
->onevent
);
263 obj
->intern
->conn
->intern
->poller
= PQconsumeInput
;
270 zend_hash_destroy(&zdtor
);
273 php_pqconn_notify_listeners(obj
->intern
->conn TSRMLS_CC
);
278 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_desc
, 0, 0, 0)
280 static PHP_METHOD(pqstm
, desc
) {
281 zend_error_handling zeh
;
284 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
285 rv
= zend_parse_parameters_none();
286 zend_restore_error_handling(&zeh TSRMLS_CC
);
289 php_pqstm_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
292 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Statement not initialized");
294 PGresult
*res
= PQdescribePrepared(obj
->intern
->conn
->intern
->conn
, obj
->intern
->name
);
297 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to describe statement (%s)", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
299 if (SUCCESS
== php_pqres_success(res TSRMLS_CC
)) {
302 array_init(return_value
);
303 for (p
= 0, params
= PQnparams(res
); p
< params
; ++p
) {
304 add_next_index_long(return_value
, PQparamtype(res
, p
));
308 php_pqconn_notify_listeners(obj
->intern
->conn TSRMLS_CC
);
314 ZEND_BEGIN_ARG_INFO_EX(ai_pqstm_desc_async
, 0, 0, 0)
316 static PHP_METHOD(pqstm
, descAsync
) {
317 zend_error_handling zeh
;
320 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
321 rv
= zend_parse_parameters_none();
322 zend_restore_error_handling(&zeh TSRMLS_CC
);
325 php_pqstm_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
328 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Statement not initialized");
329 } else if (!PQsendDescribePrepared(obj
->intern
->conn
->intern
->conn
, obj
->intern
->name
)) {
330 throw_exce(EX_IO TSRMLS_CC
, "Failed to describe statement: %s", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
332 obj
->intern
->conn
->intern
->poller
= PQconsumeInput
;
333 php_pqconn_notify_listeners(obj
->intern
->conn TSRMLS_CC
);
338 static zend_function_entry php_pqstm_methods
[] = {
339 PHP_ME(pqstm
, __construct
, ai_pqstm_construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
340 PHP_ME(pqstm
, bind
, ai_pqstm_bind
, ZEND_ACC_PUBLIC
)
341 PHP_ME(pqstm
, exec
, ai_pqstm_exec
, ZEND_ACC_PUBLIC
)
342 PHP_ME(pqstm
, desc
, ai_pqstm_desc
, ZEND_ACC_PUBLIC
)
343 PHP_ME(pqstm
, execAsync
, ai_pqstm_exec_async
, ZEND_ACC_PUBLIC
)
344 PHP_ME(pqstm
, descAsync
, ai_pqstm_desc_async
, ZEND_ACC_PUBLIC
)
348 PHP_MINIT_FUNCTION(pqstm
)
350 zend_class_entry ce
= {0};
351 php_pq_object_prophandler_t ph
= {0};
353 INIT_NS_CLASS_ENTRY(ce
, "pq", "Statement", php_pqstm_methods
);
354 php_pqstm_class_entry
= zend_register_internal_class_ex(&ce
, NULL
, NULL TSRMLS_CC
);
355 php_pqstm_class_entry
->create_object
= php_pqstm_create_object
;
357 memcpy(&php_pqstm_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
358 php_pqstm_object_handlers
.read_property
= php_pq_object_read_prop
;
359 php_pqstm_object_handlers
.write_property
= php_pq_object_write_prop
;
360 php_pqstm_object_handlers
.clone_obj
= NULL
;
361 php_pqstm_object_handlers
.get_property_ptr_ptr
= NULL
;
362 php_pqstm_object_handlers
.get_debug_info
= php_pq_object_debug_info
;
364 zend_hash_init(&php_pqstm_object_prophandlers
, 2, NULL
, NULL
, 1);
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
);
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
);
382 * vim600: noet sw=4 ts=4 fdm=marker
383 * vim<600: noet sw=4 ts=4