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 <Zend/zend_smart_str.h>
20 #include <libpq-events.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"
30 zend_class_entry
*php_pqcopy_class_entry
;
31 static zend_object_handlers php_pqcopy_object_handlers
;
32 static HashTable php_pqcopy_object_prophandlers
;
34 static void php_pqcopy_object_free(zend_object
*o
)
36 php_pqcopy_object_t
*obj
= PHP_PQ_OBJ(NULL
, o
);
38 fprintf(stderr
, "FREE copy(#%d) %p (conn(#%d): %p)\n", obj
->zo
.handle
, obj
, obj
->intern
->conn
->zo
.handle
, obj
->intern
->conn
);
41 efree(obj
->intern
->expression
);
42 efree(obj
->intern
->options
);
43 php_pq_object_delref(obj
->intern
->conn
);
47 php_pq_object_dtor(o
);
50 php_pqcopy_object_t
*php_pqcopy_create_object_ex(zend_class_entry
*ce
, php_pqcopy_t
*intern
)
52 return php_pq_object_create(ce
, intern
, sizeof(php_pqcopy_object_t
),
53 &php_pqcopy_object_handlers
, &php_pqcopy_object_prophandlers
);
56 static zend_object
*php_pqcopy_create_object(zend_class_entry
*class_type
)
58 return &php_pqcopy_create_object_ex(class_type
, NULL
)->zo
;
61 static void php_pqcopy_object_read_connection(zval
*object
, void *o
, zval
*return_value
)
63 php_pqcopy_object_t
*obj
= o
;
65 php_pq_object_to_zval(obj
->intern
->conn
, return_value
);
68 static void php_pqcopy_object_gc_connection(zval
*object
, void *o
, zval
*return_value
)
70 php_pqcopy_object_t
*obj
= o
;
73 php_pq_object_to_zval_no_addref(obj
->intern
->conn
, &zconn
);
74 add_next_index_zval(return_value
, &zconn
);
77 static void php_pqcopy_object_read_direction(zval
*object
, void *o
, zval
*return_value
)
79 php_pqcopy_object_t
*obj
= o
;
81 RETVAL_LONG(obj
->intern
->direction
);
84 static void php_pqcopy_object_read_expression(zval
*object
, void *o
, zval
*return_value
)
86 php_pqcopy_object_t
*obj
= o
;
88 RETURN_STRING(obj
->intern
->expression
);
91 static void php_pqcopy_object_read_options(zval
*object
, void *o
, zval
*return_value
)
93 php_pqcopy_object_t
*obj
= o
;
95 RETURN_STRING(obj
->intern
->options
);
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
)
104 static PHP_METHOD(pqcopy
, __construct
) {
105 zend_error_handling zeh
;
107 char *expr_str
, *opt_str
= "";
108 size_t expr_len
, opt_len
= 0;
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
);
117 php_pqconn_object_t
*conn_obj
= PHP_PQ_OBJ(zconn
, NULL
);
119 if (!conn_obj
->intern
) {
120 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
122 php_pqcopy_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
126 smart_str_appends(&cmd
, "COPY ");
127 smart_str_appendl(&cmd
, expr_str
, expr_len
);
130 case PHP_PQCOPY_FROM_STDIN
:
131 smart_str_appends(&cmd
, " FROM STDIN ");
133 case PHP_PQCOPY_TO_STDOUT
:
134 smart_str_appends(&cmd
, " TO STDOUT ");
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
);
141 smart_str_appendl(&cmd
, opt_str
, opt_len
);
144 res
= php_pq_exec(conn_obj
->intern
->conn
, smart_str_v(&cmd
));
147 throw_exce(EX_RUNTIME
, "Failed to start %s (%s)", smart_str_v(&cmd
), PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
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
);
158 php_pqres_clear(res
);
161 smart_str_free(&cmd
);
162 php_pqconn_notify_listeners(obj
->intern
->conn
);
167 ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_put
, 0, 0, 1)
168 ZEND_ARG_INFO(0, data
)
170 static PHP_METHOD(pqcopy
, put
) {
171 zend_error_handling zeh
;
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
);
181 php_pqcopy_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
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");
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
));
191 php_pqconn_notify_listeners(obj
->intern
->conn
);
196 ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_end
, 0, 0, 0)
197 ZEND_ARG_INFO(0, error
)
199 static PHP_METHOD(pqcopy
, end
) {
200 zend_error_handling zeh
;
201 char *error_str
= NULL
;
202 size_t error_len
= 0;
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
);
210 php_pqcopy_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
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");
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
));
220 PGresult
*res
= PQgetResult(obj
->intern
->conn
->intern
->conn
);
223 throw_exce(EX_RUNTIME
, "Failed to fetch COPY result (%s)", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
225 php_pqres_success(res
);
226 php_pqres_clear(res
);
230 php_pqconn_notify_listeners(obj
->intern
->conn
);
235 ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_get
, 0, 0, 1)
236 ZEND_ARG_INFO(1, data
)
238 static PHP_METHOD(pqcopy
, get
) {
239 zend_error_handling zeh
;
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
);
248 php_pqcopy_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
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");
257 int bytes
= PQgetCopyData(obj
->intern
->conn
->intern
->conn
, &buffer
, 0);
261 throw_exce(EX_RUNTIME
, "Failed to fetch COPY data (%s)", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
265 res
= PQgetResult(obj
->intern
->conn
->intern
->conn
);
268 throw_exce(EX_RUNTIME
, "Failed to fetch COPY result (%s)", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
270 php_pqres_success(res
);
271 php_pqres_clear(res
);
280 ZVAL_STRINGL(zdata
, buffer
, bytes
);
282 ZVAL_EMPTY_STRING(zdata
);
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
)
303 PHP_MSHUTDOWN_FUNCTION(pqcopy
)
305 zend_hash_destroy(&php_pqcopy_object_prophandlers
);
309 PHP_MINIT_FUNCTION(pqcopy
)
311 zend_class_entry ce
= {0};
312 php_pq_object_prophandler_t ph
= {0};
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
;
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
= php_pq_object_get_prop_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
;
329 zend_hash_init(&php_pqcopy_object_prophandlers
, 4, NULL
, php_pq_object_prophandler_dtor
, 1);
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
));
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
));
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
));
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
));
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
);
359 * vim600: noet sw=4 ts=4 fdm=marker
360 * vim<600: noet sw=4 ts=4