test
[m6w6/ext-pq] / src / php_pqcancel.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
19 #include <libpq-fe.h>
20
21 #include "php_pq.h"
22 #include "php_pq_misc.h"
23 #include "php_pq_object.h"
24 #include "php_pqexc.h"
25 #include "php_pqcancel.h"
26
27 zend_class_entry *php_pqcancel_class_entry;
28 static zend_object_handlers php_pqcancel_object_handlers;
29 static HashTable php_pqcancel_object_prophandlers;
30
31 static void php_pqcancel_object_free(void *o TSRMLS_DC)
32 {
33 php_pqcancel_object_t *obj = o;
34 #if DBG_GC
35 fprintf(stderr, "FREE cancel(#%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 PQfreeCancel(obj->intern->cancel);
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_pqcancel_create_object_ex(zend_class_entry *ce, php_pqcancel_t *intern, php_pqcancel_object_t **ptr TSRMLS_DC)
48 {
49 php_pqcancel_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_pqcancel_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_pqcancel_object_free, NULL TSRMLS_CC);
65 o->zv.handlers = &php_pqcancel_object_handlers;
66
67 return o->zv;
68 }
69
70 static zend_object_value php_pqcancel_create_object(zend_class_entry *class_type TSRMLS_DC)
71 {
72 return php_pqcancel_create_object_ex(class_type, NULL, NULL TSRMLS_CC);
73 }
74
75 static void php_pqcancel_object_read_connection(zval *object, void *o, zval *return_value TSRMLS_DC)
76 {
77 php_pqcancel_object_t *obj = o;
78
79 php_pq_object_to_zval(obj->intern->conn, &return_value TSRMLS_CC);
80 }
81
82 ZEND_BEGIN_ARG_INFO_EX(ai_pqcancel_construct, 0, 0, 1)
83 ZEND_ARG_OBJ_INFO(0, connection, pq\\Connection, 0)
84 ZEND_END_ARG_INFO();
85 static PHP_METHOD(pqcancel, __construct) {
86 zend_error_handling zeh;
87 zval *zconn;
88 STATUS rv;
89
90 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
91 rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zconn, php_pqconn_class_entry);
92 zend_restore_error_handling(&zeh TSRMLS_CC);
93
94 if (SUCCESS == rv) {
95 php_pqconn_object_t *conn_obj = zend_object_store_get_object(zconn TSRMLS_CC);
96
97 if (!conn_obj->intern) {
98 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Connection not initialized");
99 } else {
100 PGcancel *cancel = PQgetCancel(conn_obj->intern->conn);
101
102 if (!cancel) {
103 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to acquire cancel (%s)", PHP_PQerrorMessage(conn_obj->intern->conn));
104 } else {
105 php_pqcancel_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
106
107 obj->intern = ecalloc(1, sizeof(*obj->intern));
108 obj->intern->cancel = cancel;
109 php_pq_object_addref(conn_obj TSRMLS_CC);
110 obj->intern->conn = conn_obj;
111 }
112 }
113 }
114 }
115
116 ZEND_BEGIN_ARG_INFO_EX(ai_pqcancel_cancel, 0, 0, 0)
117 ZEND_END_ARG_INFO();
118 static PHP_METHOD(pqcancel, cancel) {
119 zend_error_handling zeh;
120 STATUS rv;
121
122 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh TSRMLS_CC);
123 rv = zend_parse_parameters_none();
124 zend_restore_error_handling(&zeh TSRMLS_CC);
125
126 if (SUCCESS == rv) {
127 php_pqcancel_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
128
129 if (!obj->intern) {
130 throw_exce(EX_UNINITIALIZED TSRMLS_CC, "pq\\Cancel not initialized");
131 } else {
132 char err[256] = {0};
133
134 if (!PQcancel(obj->intern->cancel, err, sizeof(err))) {
135 throw_exce(EX_RUNTIME TSRMLS_CC, "Failed to request cancellation (%s)", err);
136 }
137 }
138 }
139 }
140
141 static zend_function_entry php_pqcancel_methods[] = {
142 PHP_ME(pqcancel, __construct, ai_pqcancel_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
143 PHP_ME(pqcancel, cancel, ai_pqcancel_cancel, ZEND_ACC_PUBLIC)
144 {0}
145 };
146
147 PHP_MINIT_FUNCTION(pqcancel)
148 {
149 zend_class_entry ce = {0};
150 php_pq_object_prophandler_t ph = {0};
151
152 INIT_NS_CLASS_ENTRY(ce, "pq", "Cancel", php_pqcancel_methods);
153 php_pqcancel_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
154 php_pqcancel_class_entry->create_object = php_pqcancel_create_object;
155
156 memcpy(&php_pqcancel_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
157 php_pqcancel_object_handlers.read_property = php_pq_object_read_prop;
158 php_pqcancel_object_handlers.write_property = php_pq_object_write_prop;
159 php_pqcancel_object_handlers.clone_obj = NULL;
160 php_pqcancel_object_handlers.get_property_ptr_ptr = NULL;
161 php_pqcancel_object_handlers.get_gc = NULL;
162 php_pqcancel_object_handlers.get_properties = php_pq_object_properties;
163 php_pqcancel_object_handlers.get_debug_info = php_pq_object_debug_info;
164
165 zend_hash_init(&php_pqcancel_object_prophandlers, 1, NULL, NULL, 1);
166
167 zend_declare_property_null(php_pqcancel_class_entry, ZEND_STRL("connection"), ZEND_ACC_PUBLIC TSRMLS_CC);
168 ph.read = php_pqcancel_object_read_connection;
169 zend_hash_add(&php_pqcancel_object_prophandlers, "connection", sizeof("connection"), (void *) &ph, sizeof(ph), NULL);
170
171 return SUCCESS;
172 }
173
174 /*
175 * Local variables:
176 * tab-width: 4
177 * c-basic-offset: 4
178 * End:
179 * vim600: noet sw=4 ts=4 fdm=marker
180 * vim<600: noet sw=4 ts=4
181 */