Fix releases link
[m6w6/ext-pq] / src / php_pqcopy.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 <Zend/zend_smart_str.h>
19
20 #include <libpq-events.h>
21
22 #include "php_pq.h"
23 #include "php_pq_misc.h"
24 #include "php_pq_object.h"
25 #include "php_pqexc.h"
26 #include "php_pqres.h"
27 #include "php_pqconn.h"
28 #include "php_pqcopy.h"
29
30 zend_class_entry *php_pqcopy_class_entry;
31 static zend_object_handlers php_pqcopy_object_handlers;
32 static HashTable php_pqcopy_object_prophandlers;
33
34 static void php_pqcopy_object_free(zend_object *o)
35 {
36 php_pqcopy_object_t *obj = PHP_PQ_OBJ(NULL, o);
37 #if DBG_GC
38 fprintf(stderr, "FREE copy(#%d) %p (conn(#%d): %p)\n", obj->zo.handle, obj, obj->intern->conn->zo.handle, obj->intern->conn);
39 #endif
40 if (obj->intern) {
41 efree(obj->intern->expression);
42 efree(obj->intern->options);
43 php_pq_object_delref(obj->intern->conn);
44 efree(obj->intern);
45 obj->intern = NULL;
46 }
47 php_pq_object_dtor(o);
48 }
49
50 php_pqcopy_object_t *php_pqcopy_create_object_ex(zend_class_entry *ce, php_pqcopy_t *intern)
51 {
52 return php_pq_object_create(ce, intern, sizeof(php_pqcopy_object_t),
53 &php_pqcopy_object_handlers, &php_pqcopy_object_prophandlers);
54 }
55
56 static zend_object *php_pqcopy_create_object(zend_class_entry *class_type)
57 {
58 return &php_pqcopy_create_object_ex(class_type, NULL)->zo;
59 }
60
61 static void php_pqcopy_object_read_connection(zval *object, void *o, zval *return_value)
62 {
63 php_pqcopy_object_t *obj = o;
64
65 php_pq_object_to_zval(obj->intern->conn, return_value);
66 }
67
68 static void php_pqcopy_object_gc_connection(zval *object, void *o, zval *return_value)
69 {
70 php_pqcopy_object_t *obj = o;
71 zval zconn;
72
73 php_pq_object_to_zval_no_addref(obj->intern->conn, &zconn);
74 add_next_index_zval(return_value, &zconn);
75 }
76
77 static void php_pqcopy_object_read_direction(zval *object, void *o, zval *return_value)
78 {
79 php_pqcopy_object_t *obj = o;
80
81 RETVAL_LONG(obj->intern->direction);
82 }
83
84 static void php_pqcopy_object_read_expression(zval *object, void *o, zval *return_value)
85 {
86 php_pqcopy_object_t *obj = o;
87
88 RETURN_STRING(obj->intern->expression);
89 }
90
91 static void php_pqcopy_object_read_options(zval *object, void *o, zval *return_value)
92 {
93 php_pqcopy_object_t *obj = o;
94
95 RETURN_STRING(obj->intern->options);
96 }
97
98 ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_construct, 0, 0, 3)
99 ZEND_ARG_OBJ_INFO(0, connection, pq\\Connection, 0)
100 ZEND_ARG_INFO(0, expression)
101 ZEND_ARG_INFO(0, direction)
102 ZEND_ARG_INFO(0, options)
103 ZEND_END_ARG_INFO();
104 static PHP_METHOD(pqcopy, __construct) {
105 zend_error_handling zeh;
106 zval *zconn;
107 char *expr_str, *opt_str = "";
108 size_t expr_len, opt_len = 0;
109 zend_long direction;
110 ZEND_RESULT_CODE rv;
111
112 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh);
113 rv = zend_parse_parameters(ZEND_NUM_ARGS(), "Osl|s", &zconn, php_pqconn_class_entry, &expr_str, &expr_len, &direction, &opt_str, &opt_len);
114 zend_restore_error_handling(&zeh);
115
116 if (SUCCESS == rv) {
117 php_pqconn_object_t *conn_obj = PHP_PQ_OBJ(zconn, NULL);
118
119 if (!conn_obj->intern) {
120 throw_exce(EX_UNINITIALIZED, "pq\\Connection not initialized");
121 } else {
122 php_pqcopy_object_t *obj = PHP_PQ_OBJ(getThis(), NULL);
123 smart_str cmd = {0};
124 PGresult *res;
125
126 smart_str_appends(&cmd, "COPY ");
127 smart_str_appendl(&cmd, expr_str, expr_len);
128
129 switch (direction) {
130 case PHP_PQCOPY_FROM_STDIN:
131 smart_str_appends(&cmd, " FROM STDIN ");
132 break;
133 case PHP_PQCOPY_TO_STDOUT:
134 smart_str_appends(&cmd, " TO STDOUT ");
135 break;
136 default:
137 throw_exce(EX_RUNTIME, "Invalid COPY direction, expected one of FROM_STDIN (%d) TO_STDOUT (%d), got %ld", PHP_PQCOPY_FROM_STDIN, PHP_PQCOPY_TO_STDOUT, direction);
138 smart_str_free(&cmd);
139 return;
140 }
141 smart_str_appendl(&cmd, opt_str, opt_len);
142 smart_str_0(&cmd);
143
144 res = php_pq_exec(conn_obj->intern->conn, smart_str_v(&cmd));
145
146 if (!res) {
147 throw_exce(EX_RUNTIME, "Failed to start %s (%s)", smart_str_v(&cmd), PHP_PQerrorMessage(obj->intern->conn->intern->conn));
148 } else {
149 if (SUCCESS == php_pqres_success(res)) {
150 obj->intern = ecalloc(1, sizeof(*obj->intern));
151 obj->intern->direction = direction;
152 obj->intern->expression = estrdup(expr_str);
153 obj->intern->options = estrdup(opt_str);
154 obj->intern->conn = conn_obj;
155 php_pq_object_addref(conn_obj);
156 }
157
158 php_pqres_clear(res);
159 }
160
161 smart_str_free(&cmd);
162 php_pqconn_notify_listeners(obj->intern->conn);
163 }
164 }
165 }
166
167 ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_put, 0, 0, 1)
168 ZEND_ARG_INFO(0, data)
169 ZEND_END_ARG_INFO();
170 static PHP_METHOD(pqcopy, put) {
171 zend_error_handling zeh;
172 char *data_str;
173 size_t data_len;
174 ZEND_RESULT_CODE rv;
175
176 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh);
177 rv = zend_parse_parameters(ZEND_NUM_ARGS(), "s", &data_str, &data_len);
178 zend_restore_error_handling(&zeh);
179
180 if (SUCCESS == rv) {
181 php_pqcopy_object_t *obj = PHP_PQ_OBJ(getThis(), NULL);
182
183 if (!obj->intern) {
184 throw_exce(EX_UNINITIALIZED, "pq\\COPY not initialized");
185 } else if (obj->intern->direction != PHP_PQCOPY_FROM_STDIN) {
186 throw_exce(EX_BAD_METHODCALL, "pq\\COPY was not initialized with FROM_STDIN");
187 } else {
188 if (1 != PQputCopyData(obj->intern->conn->intern->conn, data_str, data_len)) {
189 throw_exce(EX_RUNTIME, "Failed to put COPY data (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
190 }
191 php_pqconn_notify_listeners(obj->intern->conn);
192 }
193 }
194 }
195
196 ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_end, 0, 0, 0)
197 ZEND_ARG_INFO(0, error)
198 ZEND_END_ARG_INFO();
199 static PHP_METHOD(pqcopy, end) {
200 zend_error_handling zeh;
201 char *error_str = NULL;
202 size_t error_len = 0;
203 ZEND_RESULT_CODE rv;
204
205 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh);
206 rv = zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &error_str, &error_len);
207 zend_restore_error_handling(&zeh);
208
209 if (SUCCESS == rv) {
210 php_pqcopy_object_t *obj = PHP_PQ_OBJ(getThis(), NULL);
211
212 if (!obj->intern) {
213 throw_exce(EX_UNINITIALIZED, "pq\\COPY not intitialized");
214 } else if (obj->intern->direction != PHP_PQCOPY_FROM_STDIN) {
215 throw_exce(EX_BAD_METHODCALL, "pq\\COPY was not intitialized with FROM_STDIN");
216 } else {
217 if (1 != PQputCopyEnd(obj->intern->conn->intern->conn, error_str)) {
218 throw_exce(EX_RUNTIME, "Failed to end COPY (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
219 } else {
220 PGresult *res = PQgetResult(obj->intern->conn->intern->conn);
221
222 if (!res) {
223 throw_exce(EX_RUNTIME, "Failed to fetch COPY result (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
224 } else {
225 php_pqres_success(res);
226 php_pqres_clear(res);
227 }
228 }
229
230 php_pqconn_notify_listeners(obj->intern->conn);
231 }
232 }
233 }
234
235 ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_get, 0, 0, 1)
236 ZEND_ARG_INFO(1, data)
237 ZEND_END_ARG_INFO();
238 static PHP_METHOD(pqcopy, get) {
239 zend_error_handling zeh;
240 zval *zdata;
241 ZEND_RESULT_CODE rv;
242
243 zend_replace_error_handling(EH_THROW, exce(EX_INVALID_ARGUMENT), &zeh);
244 rv = zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zdata);
245 zend_restore_error_handling(&zeh);
246
247 if (SUCCESS == rv) {
248 php_pqcopy_object_t *obj = PHP_PQ_OBJ(getThis(), NULL);
249
250 if (!obj->intern) {
251 throw_exce(EX_UNINITIALIZED, "pq\\COPY not initialized");
252 } else if (obj->intern->direction != PHP_PQCOPY_TO_STDOUT) {
253 throw_exce(EX_RUNTIME, "pq\\COPY was not intialized with TO_STDOUT");
254 } else {
255 PGresult *res;
256 char *buffer = NULL;
257 int bytes = PQgetCopyData(obj->intern->conn->intern->conn, &buffer, 0);
258
259 switch (bytes) {
260 case -2:
261 throw_exce(EX_RUNTIME, "Failed to fetch COPY data (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
262 break;
263
264 case -1:
265 res = PQgetResult(obj->intern->conn->intern->conn);
266
267 if (!res) {
268 throw_exce(EX_RUNTIME, "Failed to fetch COPY result (%s)", PHP_PQerrorMessage(obj->intern->conn->intern->conn));
269 } else {
270 php_pqres_success(res);
271 php_pqres_clear(res);
272 RETVAL_FALSE;
273 }
274 break;
275
276 default:
277 ZVAL_DEREF(zdata);
278 zval_dtor(zdata);
279 if (buffer) {
280 ZVAL_STRINGL(zdata, buffer, bytes);
281 } else {
282 ZVAL_EMPTY_STRING(zdata);
283 }
284 RETVAL_TRUE;
285 break;
286 }
287
288 if (buffer) {
289 PQfreemem(buffer);
290 }
291 }
292 }
293 }
294
295 static zend_function_entry php_pqcopy_methods[] = {
296 PHP_ME(pqcopy, __construct, ai_pqcopy_construct, ZEND_ACC_PUBLIC)
297 PHP_ME(pqcopy, put, ai_pqcopy_put, ZEND_ACC_PUBLIC)
298 PHP_ME(pqcopy, end, ai_pqcopy_end, ZEND_ACC_PUBLIC)
299 PHP_ME(pqcopy, get, ai_pqcopy_get, ZEND_ACC_PUBLIC)
300 {0}
301 };
302
303 PHP_MSHUTDOWN_FUNCTION(pqcopy)
304 {
305 zend_hash_destroy(&php_pqcopy_object_prophandlers);
306 return SUCCESS;
307 }
308
309 PHP_MINIT_FUNCTION(pqcopy)
310 {
311 zend_class_entry ce = {0};
312 php_pq_object_prophandler_t ph = {0};
313
314 INIT_NS_CLASS_ENTRY(ce, "pq", "COPY", php_pqcopy_methods);
315 php_pqcopy_class_entry = zend_register_internal_class_ex(&ce, NULL);
316 php_pqcopy_class_entry->create_object = php_pqcopy_create_object;
317
318 memcpy(&php_pqcopy_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
319 php_pqcopy_object_handlers.offset = XtOffsetOf(php_pqcopy_object_t, zo);
320 php_pqcopy_object_handlers.free_obj = php_pqcopy_object_free;
321 php_pqcopy_object_handlers.read_property = php_pq_object_read_prop;
322 php_pqcopy_object_handlers.write_property = php_pq_object_write_prop;
323 php_pqcopy_object_handlers.clone_obj = NULL;
324 php_pqcopy_object_handlers.get_property_ptr_ptr = NULL;
325 php_pqcopy_object_handlers.get_gc = php_pq_object_get_gc;
326 php_pqcopy_object_handlers.get_properties = php_pq_object_properties;
327 php_pqcopy_object_handlers.get_debug_info = php_pq_object_debug_info;
328
329 zend_hash_init(&php_pqcopy_object_prophandlers, 4, NULL, php_pq_object_prophandler_dtor, 1);
330
331 zend_declare_property_null(php_pqcopy_class_entry, ZEND_STRL("connection"), ZEND_ACC_PUBLIC);
332 ph.read = php_pqcopy_object_read_connection;
333 ph.gc = php_pqcopy_object_gc_connection;
334 zend_hash_str_add_mem(&php_pqcopy_object_prophandlers, "connection", sizeof("connection")-1, (void *) &ph, sizeof(ph));
335 ph.gc = NULL;
336
337 zend_declare_property_null(php_pqcopy_class_entry, ZEND_STRL("expression"), ZEND_ACC_PUBLIC);
338 ph.read = php_pqcopy_object_read_expression;
339 zend_hash_str_add_mem(&php_pqcopy_object_prophandlers, "expression", sizeof("expression")-1, (void *) &ph, sizeof(ph));
340
341 zend_declare_property_null(php_pqcopy_class_entry, ZEND_STRL("direction"), ZEND_ACC_PUBLIC);
342 ph.read = php_pqcopy_object_read_direction;
343 zend_hash_str_add_mem(&php_pqcopy_object_prophandlers, "direction", sizeof("direction")-1, (void *) &ph, sizeof(ph));
344
345 zend_declare_property_null(php_pqcopy_class_entry, ZEND_STRL("options"), ZEND_ACC_PUBLIC);
346 ph.read = php_pqcopy_object_read_options;
347 zend_hash_str_add_mem(&php_pqcopy_object_prophandlers, "options", sizeof("options")-1, (void *) &ph, sizeof(ph));
348
349 zend_declare_class_constant_long(php_pqcopy_class_entry, ZEND_STRL("FROM_STDIN"), PHP_PQCOPY_FROM_STDIN);
350 zend_declare_class_constant_long(php_pqcopy_class_entry, ZEND_STRL("TO_STDOUT"), PHP_PQCOPY_TO_STDOUT);
351
352 return SUCCESS;
353 }
354 /*
355 * Local variables:
356 * tab-width: 4
357 * c-basic-offset: 4
358 * End:
359 * vim600: noet sw=4 ts=4 fdm=marker
360 * vim<600: noet sw=4 ts=4
361 */