flush
[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(zend_object *o)
32 {
33 php_pqcancel_object_t *obj = PHP_PQ_OBJ(NULL, 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);
40 efree(obj->intern);
41 obj->intern = NULL;
42 }
43 zend_object_std_dtor(o);
44 efree(obj);
45 }
46
47 php_pqcancel_object_t *php_pqcancel_create_object_ex(zend_class_entry *ce, php_pqcancel_t *intern)
48 {
49 php_pqcancel_object_t *o;
50
51 o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce));
52 zend_object_std_init(&o->zo, ce);
53 object_properties_init(&o->zo, ce);
54 o->prophandler = &php_pqcancel_object_prophandlers;
55
56 if (intern) {
57 o->intern = intern;
58 }
59
60 o->zo.handlers = &php_pqcancel_object_handlers;
61
62 return o;
63 }
64
65 static zend_object *php_pqcancel_create_object(zend_class_entry *class_type TSRMLS_DC)
66 {
67 return &php_pqcancel_create_object_ex(class_type, NULL)->zo;
68 }
69
70 static void php_pqcancel_object_read_connection(zval *object, void *o, zval *return_value)
71 {
72 php_pqcancel_object_t *obj = o;
73
74 php_pq_object_to_zval(obj->intern->conn, return_value);
75 }
76
77 ZEND_BEGIN_ARG_INFO_EX(ai_pqcancel_construct, 0, 0, 1)
78 ZEND_ARG_OBJ_INFO(0, connection, pq\\Connection, 0)
79 ZEND_END_ARG_INFO();
80 static PHP_METHOD(pqcancel, __construct) {
81 zend_error_handling zeh;
82 zval *zconn;
83 ZEND_RESULT_CODE rv;
84
85 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh);
86 rv = zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zconn, php_pqconn_class_entry);
87 zend_restore_error_handling(&zeh);
88
89 if (SUCCESS == rv) {
90 php_pqconn_object_t *conn_obj = PHP_PQ_OBJ(zconn, NULL);
91
92 if (!conn_obj->intern) {
93 throw_exce(EX_UNINITIALIZED, "pq\\Connection not initialized");
94 } else {
95 PGcancel *cancel = PQgetCancel(conn_obj->intern->conn);
96
97 if (!cancel) {
98 throw_exce(EX_RUNTIME, "Failed to acquire cancel (%s)", PHP_PQerrorMessage(conn_obj->intern->conn));
99 } else {
100 php_pqcancel_object_t *obj = PHP_PQ_OBJ(getThis(), NULL);
101
102 obj->intern = ecalloc(1, sizeof(*obj->intern));
103 obj->intern->cancel = cancel;
104 php_pq_object_addref(conn_obj);
105 obj->intern->conn = conn_obj;
106 }
107 }
108 }
109 }
110
111 ZEND_BEGIN_ARG_INFO_EX(ai_pqcancel_cancel, 0, 0, 0)
112 ZEND_END_ARG_INFO();
113 static PHP_METHOD(pqcancel, cancel) {
114 zend_error_handling zeh;
115 ZEND_RESULT_CODE rv;
116
117 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh);
118 rv = zend_parse_parameters_none();
119 zend_restore_error_handling(&zeh);
120
121 if (SUCCESS == rv) {
122 php_pqcancel_object_t *obj = PHP_PQ_OBJ(getThis(), NULL);
123
124 if (!obj->intern) {
125 throw_exce(EX_UNINITIALIZED, "pq\\Cancel not initialized");
126 } else {
127 char err[256] = {0};
128
129 if (!PQcancel(obj->intern->cancel, err, sizeof(err))) {
130 throw_exce(EX_RUNTIME, "Failed to request cancellation (%s)", err);
131 }
132 }
133 }
134 }
135
136 static zend_function_entry php_pqcancel_methods[] = {
137 PHP_ME(pqcancel, __construct, ai_pqcancel_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
138 PHP_ME(pqcancel, cancel, ai_pqcancel_cancel, ZEND_ACC_PUBLIC)
139 {0}
140 };
141
142 PHP_MSHUTDOWN_FUNCTION(pqcancel)
143 {
144 zend_hash_destroy(&php_pqcancel_object_prophandlers);
145 return SUCCESS;
146 }
147
148 PHP_MINIT_FUNCTION(pqcancel)
149 {
150 zend_class_entry ce = {0};
151 php_pq_object_prophandler_t ph = {0};
152
153 INIT_NS_CLASS_ENTRY(ce, "pq", "Cancel", php_pqcancel_methods);
154 php_pqcancel_class_entry = zend_register_internal_class_ex(&ce, NULL);
155 php_pqcancel_class_entry->create_object = php_pqcancel_create_object;
156
157 memcpy(&php_pqcancel_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
158 php_pqcancel_object_handlers.offset = XtOffsetOf(php_pqcancel_object_t, zo);
159 php_pqcancel_object_handlers.free_obj = php_pqcancel_object_free;
160 php_pqcancel_object_handlers.read_property = php_pq_object_read_prop;
161 php_pqcancel_object_handlers.write_property = php_pq_object_write_prop;
162 php_pqcancel_object_handlers.clone_obj = NULL;
163 php_pqcancel_object_handlers.get_property_ptr_ptr = NULL;
164 php_pqcancel_object_handlers.get_gc = NULL;
165 php_pqcancel_object_handlers.get_properties = php_pq_object_properties;
166 php_pqcancel_object_handlers.get_debug_info = php_pq_object_debug_info;
167
168 zend_hash_init(&php_pqcancel_object_prophandlers, 1, NULL, NULL, 1);
169
170 zend_declare_property_null(php_pqcancel_class_entry, ZEND_STRL("connection"), ZEND_ACC_PUBLIC TSRMLS_CC);
171 ph.read = php_pqcancel_object_read_connection;
172 zend_hash_str_add_mem(&php_pqcancel_object_prophandlers, ZEND_STRL("connection"), (void *) &ph, sizeof(ph));
173
174 return SUCCESS;
175 }
176
177 /*
178 * Local variables:
179 * tab-width: 4
180 * c-basic-offset: 4
181 * End:
182 * vim600: noet sw=4 ts=4 fdm=marker
183 * vim<600: noet sw=4 ts=4
184 */