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