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 <ext/standard/php_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(void *o TSRMLS_DC
)
36 php_pqcopy_object_t
*obj
= o
;
38 fprintf(stderr
, "FREE copy(#%d) %p (conn(#%d): %p)\n", obj
->zv
.handle
, obj
, obj
->intern
->conn
->zv
.handle
, obj
->intern
->conn
);
41 efree(obj
->intern
->expression
);
42 efree(obj
->intern
->options
);
43 php_pq_object_delref(obj
->intern
->conn TSRMLS_CC
);
47 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
51 zend_object_value
php_pqcopy_create_object_ex(zend_class_entry
*ce
, php_pqcopy_t
*intern
, php_pqcopy_object_t
**ptr TSRMLS_DC
)
53 php_pqcopy_object_t
*o
;
55 o
= ecalloc(1, sizeof(*o
));
56 zend_object_std_init((zend_object
*) o
, ce TSRMLS_CC
);
57 object_properties_init((zend_object
*) o
, ce
);
58 o
->prophandler
= &php_pqcopy_object_prophandlers
;
68 o
->zv
.handle
= zend_objects_store_put((zend_object
*) o
, NULL
, php_pqcopy_object_free
, NULL TSRMLS_CC
);
69 o
->zv
.handlers
= &php_pqcopy_object_handlers
;
74 static zend_object_value
php_pqcopy_create_object(zend_class_entry
*class_type TSRMLS_DC
)
76 return php_pqcopy_create_object_ex(class_type
, NULL
, NULL TSRMLS_CC
);
79 static void php_pqcopy_object_read_connection(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
81 php_pqcopy_object_t
*obj
= o
;
83 php_pq_object_to_zval(obj
->intern
->conn
, &return_value TSRMLS_CC
);
86 static void php_pqcopy_object_read_direction(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
88 php_pqcopy_object_t
*obj
= o
;
90 RETVAL_LONG(obj
->intern
->direction
);
93 static void php_pqcopy_object_read_expression(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
95 php_pqcopy_object_t
*obj
= o
;
97 RETURN_STRING(obj
->intern
->expression
, 1);
100 static void php_pqcopy_object_read_options(zval
*object
, void *o
, zval
*return_value TSRMLS_DC
)
102 php_pqcopy_object_t
*obj
= o
;
104 RETURN_STRING(obj
->intern
->options
, 1);
107 ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_construct
, 0, 0, 3)
108 ZEND_ARG_OBJ_INFO(0, connection
, pq
\\Connection
, 0)
109 ZEND_ARG_INFO(0, expression
)
110 ZEND_ARG_INFO(0, direction
)
111 ZEND_ARG_INFO(0, options
)
113 static PHP_METHOD(pqcopy
, __construct
) {
114 zend_error_handling zeh
;
116 char *expr_str
, *opt_str
= "";
117 int expr_len
, opt_len
= 0;
121 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
122 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "Osl|s", &zconn
, php_pqconn_class_entry
, &expr_str
, &expr_len
, &direction
, &opt_str
, &opt_len
);
123 zend_restore_error_handling(&zeh TSRMLS_CC
);
126 php_pqconn_object_t
*conn_obj
= zend_object_store_get_object(zconn TSRMLS_CC
);
128 if (!conn_obj
->intern
) {
129 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\Connection not initialized");
131 php_pqcopy_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
135 smart_str_appends(&cmd
, "COPY ");
136 smart_str_appendl(&cmd
, expr_str
, expr_len
);
139 case PHP_PQCOPY_FROM_STDIN
:
140 smart_str_appends(&cmd
, " FROM STDIN ");
142 case PHP_PQCOPY_TO_STDOUT
:
143 smart_str_appends(&cmd
, " TO STDOUT ");
146 throw_exce(EX_RUNTIME TSRMLS_CC
, "Invalid COPY direction, expected one of FROM_STDIN (%d) TO_STDOUT (%d), got %ld", PHP_PQCOPY_FROM_STDIN
, PHP_PQCOPY_TO_STDOUT
, direction
);
147 smart_str_free(&cmd
);
150 smart_str_appendl(&cmd
, opt_str
, opt_len
);
153 res
= PQexec(conn_obj
->intern
->conn
, cmd
.c
);
156 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to start %s (%s)", cmd
.c
, PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
158 if (SUCCESS
== php_pqres_success(res TSRMLS_CC
)) {
159 obj
->intern
= ecalloc(1, sizeof(*obj
->intern
));
160 obj
->intern
->direction
= direction
;
161 obj
->intern
->expression
= estrdup(expr_str
);
162 obj
->intern
->options
= estrdup(opt_str
);
163 obj
->intern
->conn
= conn_obj
;
164 php_pq_object_addref(conn_obj TSRMLS_CC
);
170 smart_str_free(&cmd
);
171 php_pqconn_notify_listeners(obj
->intern
->conn TSRMLS_CC
);
176 ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_put
, 0, 0, 1)
177 ZEND_ARG_INFO(0, data
)
179 static PHP_METHOD(pqcopy
, put
) {
180 zend_error_handling zeh
;
185 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
186 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data_str
, &data_len
);
187 zend_restore_error_handling(&zeh TSRMLS_CC
);
190 php_pqcopy_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
193 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\COPY not initialized");
194 } else if (obj
->intern
->direction
!= PHP_PQCOPY_FROM_STDIN
) {
195 throw_exce(EX_BAD_METHODCALL TSRMLS_CC
, "pq\\COPY was not initialized with FROM_STDIN");
197 if (1 != PQputCopyData(obj
->intern
->conn
->intern
->conn
, data_str
, data_len
)) {
198 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to put COPY data (%s)", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
200 php_pqconn_notify_listeners(obj
->intern
->conn TSRMLS_CC
);
205 ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_end
, 0, 0, 0)
206 ZEND_ARG_INFO(0, error
)
208 static PHP_METHOD(pqcopy
, end
) {
209 zend_error_handling zeh
;
210 char *error_str
= NULL
;
214 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
215 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s!", &error_str
, &error_len
);
216 zend_restore_error_handling(&zeh TSRMLS_CC
);
219 php_pqcopy_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
222 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\COPY not intitialized");
223 } else if (obj
->intern
->direction
!= PHP_PQCOPY_FROM_STDIN
) {
224 throw_exce(EX_BAD_METHODCALL TSRMLS_CC
, "pq\\COPY was not intitialized with FROM_STDIN");
226 if (1 != PQputCopyEnd(obj
->intern
->conn
->intern
->conn
, error_str
)) {
227 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to end COPY (%s)", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
229 PGresult
*res
= PQgetResult(obj
->intern
->conn
->intern
->conn
);
232 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to fetch COPY result (%s)", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
234 php_pqres_success(res TSRMLS_CC
);
239 php_pqconn_notify_listeners(obj
->intern
->conn TSRMLS_CC
);
244 ZEND_BEGIN_ARG_INFO_EX(ai_pqcopy_get
, 0, 0, 1)
245 ZEND_ARG_INFO(1, data
)
247 static PHP_METHOD(pqcopy
, get
) {
248 zend_error_handling zeh
;
252 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh TSRMLS_CC
);
253 rv
= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
);
254 zend_restore_error_handling(&zeh TSRMLS_CC
);
257 php_pqcopy_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
260 throw_exce(EX_UNINITIALIZED TSRMLS_CC
, "pq\\COPY not initialized");
261 } else if (obj
->intern
->direction
!= PHP_PQCOPY_TO_STDOUT
) {
262 throw_exce(EX_RUNTIME TSRMLS_CC
, "pq\\COPY was not intialized with TO_STDOUT");
266 int bytes
= PQgetCopyData(obj
->intern
->conn
->intern
->conn
, &buffer
, 0);
270 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to fetch COPY data (%s)", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
274 res
= PQgetResult(obj
->intern
->conn
->intern
->conn
);
277 throw_exce(EX_RUNTIME TSRMLS_CC
, "Failed to fetch COPY result (%s)", PHP_PQerrorMessage(obj
->intern
->conn
->intern
->conn
));
279 php_pqres_success(res TSRMLS_CC
);
288 ZVAL_STRINGL(zdata
, buffer
, bytes
, 1);
290 ZVAL_EMPTY_STRING(zdata
);
303 static zend_function_entry php_pqcopy_methods
[] = {
304 PHP_ME(pqcopy
, __construct
, ai_pqcopy_construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
305 PHP_ME(pqcopy
, put
, ai_pqcopy_put
, ZEND_ACC_PUBLIC
)
306 PHP_ME(pqcopy
, end
, ai_pqcopy_end
, ZEND_ACC_PUBLIC
)
307 PHP_ME(pqcopy
, get
, ai_pqcopy_get
, ZEND_ACC_PUBLIC
)
311 PHP_MSHUTDOWN_FUNCTION(pqcopy
)
313 zend_hash_destroy(&php_pqcopy_object_prophandlers
);
317 PHP_MINIT_FUNCTION(pqcopy
)
319 zend_class_entry ce
= {0};
320 php_pq_object_prophandler_t ph
= {0};
322 INIT_NS_CLASS_ENTRY(ce
, "pq", "COPY", php_pqcopy_methods
);
323 php_pqcopy_class_entry
= zend_register_internal_class_ex(&ce
, NULL
, NULL TSRMLS_CC
);
324 php_pqcopy_class_entry
->create_object
= php_pqcopy_create_object
;
326 memcpy(&php_pqcopy_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
327 php_pqcopy_object_handlers
.read_property
= php_pq_object_read_prop
;
328 php_pqcopy_object_handlers
.write_property
= php_pq_object_write_prop
;
329 php_pqcopy_object_handlers
.clone_obj
= NULL
;
330 php_pqcopy_object_handlers
.get_property_ptr_ptr
= NULL
;
331 php_pqcopy_object_handlers
.get_gc
= NULL
;
332 php_pqcopy_object_handlers
.get_properties
= php_pq_object_properties
;
333 php_pqcopy_object_handlers
.get_debug_info
= php_pq_object_debug_info
;
335 zend_hash_init(&php_pqcopy_object_prophandlers
, 4, NULL
, NULL
, 1);
337 zend_declare_property_null(php_pqcopy_class_entry
, ZEND_STRL("connection"), ZEND_ACC_PUBLIC TSRMLS_CC
);
338 ph
.read
= php_pqcopy_object_read_connection
;
339 zend_hash_add(&php_pqcopy_object_prophandlers
, "connection", sizeof("connection"), (void *) &ph
, sizeof(ph
), NULL
);
341 zend_declare_property_null(php_pqcopy_class_entry
, ZEND_STRL("expression"), ZEND_ACC_PUBLIC TSRMLS_CC
);
342 ph
.read
= php_pqcopy_object_read_expression
;
343 zend_hash_add(&php_pqcopy_object_prophandlers
, "expression", sizeof("expression"), (void *) &ph
, sizeof(ph
), NULL
);
345 zend_declare_property_null(php_pqcopy_class_entry
, ZEND_STRL("direction"), ZEND_ACC_PUBLIC TSRMLS_CC
);
346 ph
.read
= php_pqcopy_object_read_direction
;
347 zend_hash_add(&php_pqcopy_object_prophandlers
, "direction", sizeof("direction"), (void *) &ph
, sizeof(ph
), NULL
);
349 zend_declare_property_null(php_pqcopy_class_entry
, ZEND_STRL("options"), ZEND_ACC_PUBLIC TSRMLS_CC
);
350 ph
.read
= php_pqcopy_object_read_options
;
351 zend_hash_add(&php_pqcopy_object_prophandlers
, "options", sizeof("options"), (void *) &ph
, sizeof(ph
), NULL
);
353 zend_declare_class_constant_long(php_pqcopy_class_entry
, ZEND_STRL("FROM_STDIN"), PHP_PQCOPY_FROM_STDIN TSRMLS_CC
);
354 zend_declare_class_constant_long(php_pqcopy_class_entry
, ZEND_STRL("TO_STDOUT"), PHP_PQCOPY_TO_STDOUT TSRMLS_CC
);
363 * vim600: noet sw=4 ts=4 fdm=marker
364 * vim<600: noet sw=4 ts=4