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 +--------------------------------------------------------------------+
22 #include "php_pq_misc.h"
23 #include "php_pq_object.h"
24 #include "php_pqexc.h"
25 #include "php_pqcancel.h"
27 zend_class_entry
*php_pqcancel_class_entry
;
28 static zend_object_handlers php_pqcancel_object_handlers
;
29 static HashTable php_pqcancel_object_prophandlers
;
31 static void php_pqcancel_object_free(zend_object
*o
)
33 php_pqcancel_object_t
*obj
= PHP_PQ_OBJ(NULL
, o
);
35 fprintf(stderr
, "FREE cancel(#%d) %p (conn(#%d): %p)\n", obj
->zo
.handle
, obj
, obj
->intern
->conn
->zo
.handle
, obj
->intern
->conn
);
38 PQfreeCancel(obj
->intern
->cancel
);
39 php_pq_object_delref(obj
->intern
->conn
);
43 php_pq_object_dtor(o
);
46 php_pqcancel_object_t
*php_pqcancel_create_object_ex(zend_class_entry
*ce
, php_pqcancel_t
*intern
)
48 return php_pq_object_create(ce
, intern
, sizeof(php_pqcancel_object_t
),
49 &php_pqcancel_object_handlers
, &php_pqcancel_object_prophandlers
);
52 static zend_object
*php_pqcancel_create_object(zend_class_entry
*class_type
)
54 return &php_pqcancel_create_object_ex(class_type
, NULL
)->zo
;
57 static void php_pqcancel_object_read_connection(zval
*object
, void *o
, zval
*return_value
)
59 php_pqcancel_object_t
*obj
= o
;
61 php_pq_object_to_zval(obj
->intern
->conn
, return_value
);
64 static void php_pqcancel_object_gc_connection(zval
*object
, void *o
, zval
*return_value
)
66 php_pqcancel_object_t
*obj
= o
;
69 php_pq_object_to_zval_no_addref(obj
->intern
->conn
, &zconn
);
70 add_next_index_zval(return_value
, &zconn
);
73 ZEND_BEGIN_ARG_INFO_EX(ai_pqcancel_construct
, 0, 0, 1)
74 ZEND_ARG_OBJ_INFO(0, connection
, pq
\\Connection
, 0)
76 static PHP_METHOD(pqcancel
, __construct
) {
77 zend_error_handling zeh
;
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
);
86 php_pqconn_object_t
*conn_obj
= PHP_PQ_OBJ(zconn
, NULL
);
88 if (!conn_obj
->intern
) {
89 throw_exce(EX_UNINITIALIZED
, "pq\\Connection not initialized");
91 PGcancel
*cancel
= PQgetCancel(conn_obj
->intern
->conn
);
94 throw_exce(EX_RUNTIME
, "Failed to acquire cancel (%s)", PHP_PQerrorMessage(conn_obj
->intern
->conn
));
96 php_pqcancel_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
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
;
107 ZEND_BEGIN_ARG_INFO_EX(ai_pqcancel_cancel
, 0, 0, 0)
109 static PHP_METHOD(pqcancel
, cancel
) {
110 zend_error_handling zeh
;
113 zend_replace_error_handling(EH_THROW
, exce(EX_INVALID_ARGUMENT
), &zeh
);
114 rv
= zend_parse_parameters_none();
115 zend_restore_error_handling(&zeh
);
118 php_pqcancel_object_t
*obj
= PHP_PQ_OBJ(getThis(), NULL
);
121 throw_exce(EX_UNINITIALIZED
, "pq\\Cancel not initialized");
125 if (!PQcancel(obj
->intern
->cancel
, err
, sizeof(err
))) {
126 throw_exce(EX_RUNTIME
, "Failed to request cancellation (%s)", err
);
132 static zend_function_entry php_pqcancel_methods
[] = {
133 PHP_ME(pqcancel
, __construct
, ai_pqcancel_construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
134 PHP_ME(pqcancel
, cancel
, ai_pqcancel_cancel
, ZEND_ACC_PUBLIC
)
138 PHP_MSHUTDOWN_FUNCTION(pqcancel
)
140 zend_hash_destroy(&php_pqcancel_object_prophandlers
);
144 PHP_MINIT_FUNCTION(pqcancel
)
146 zend_class_entry ce
= {0};
147 php_pq_object_prophandler_t ph
= {0};
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
;
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
= 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
;
164 zend_hash_init(&php_pqcancel_object_prophandlers
, 1, NULL
, php_pq_object_prophandler_dtor
, 1);
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
));
179 * vim600: noet sw=4 ts=4 fdm=marker
180 * vim<600: noet sw=4 ts=4