forked from FirebirdSQL/php-firebird
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfbird_class_batch.c
More file actions
289 lines (232 loc) · 7.71 KB
/
Copy pathfbird_class_batch.c
File metadata and controls
289 lines (232 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* fbird_class_batch.c - Firebird\BatchHandle OOP class (FB4+) */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_firebird.h"
#include "php_fbird_includes.h"
#include "firebird_utils.h"
#include "fbird_class_internal.h"
#if FB_API_VER >= 40
/* {{{ arginfo for BatchHandle methods */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_BatchHandle_getBlobAlignment, 0, 0, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_BatchHandle_setDefaultBpb, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, bpb, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_BatchHandle_cancel, 0, 0, _IS_BOOL, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_BatchHandle_execute, 0, 0, MAY_BE_ARRAY|MAY_BE_FALSE)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_BatchHandle_add, 0, 0, _IS_BOOL, 0)
ZEND_ARG_VARIADIC_INFO(0, args)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_BatchHandle_addBlob, 0, 1, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, blob_type, IS_LONG, 0)
ZEND_END_ARG_INFO()
/* }}} */
zend_object_handlers fbird_batch_handlers;
zend_object *fbird_batch_create_obj(zend_class_entry *ce)
{
fbird_batch_obj *intern = zend_object_alloc(sizeof(fbird_batch_obj), ce);
intern->batch = NULL;
zend_object_std_init(&intern->std, ce);
object_properties_init(&intern->std, ce);
intern->std.handlers = &fbird_batch_handlers;
return &intern->std;
}
void fbird_batch_free_obj(zend_object *obj)
{
fbird_batch_obj *intern = fbird_batch_from_obj(obj);
ISC_STATUS status[256];
if (intern->batch) {
fbird_batch *batch = intern->batch;
if (batch->fbbatch_wrapper != NULL) {
fbbatch_cancel(FBG(master_instance), batch->fbbatch_wrapper, status);
fbbatch_close(FBG(master_instance), batch->fbbatch_wrapper, status);
batch->fbbatch_wrapper = NULL;
}
if (batch->in_msg_buffer != NULL) {
efree(batch->in_msg_buffer);
batch->in_msg_buffer = NULL;
}
efree(batch);
intern->batch = NULL;
}
zend_object_std_dtor(obj);
}
void fbird_setup_batch_object(zval *rv, fbird_batch *batch)
{
object_init_ex(rv, fbird_batch_ce);
fbird_batch_obj *intern = fbird_batch_from_obj(Z_OBJ_P(rv));
intern->batch = batch;
}
fbird_batch *fbird_batch_get_ptr(zend_object *obj)
{
fbird_batch_obj *intern = fbird_batch_from_obj(obj);
return intern ? intern->batch : NULL;
}
/* {{{ BatchHandle::getBlobAlignment(): int|false */
PHP_METHOD(BatchHandle, getBlobAlignment)
{
ISC_STATUS status[256];
fbird_batch *fb_batch;
unsigned alignment;
RESET_ERRMSG;
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
fb_batch = fbird_batch_get_ptr(Z_OBJ_P(getThis()));
if (!fb_batch || !fb_batch->fbbatch_wrapper) {
_php_fbird_module_error("Invalid batch handle");
RETURN_FALSE;
}
alignment = fbbatch_get_blob_alignment(FBG(master_instance), fb_batch->fbbatch_wrapper, status);
if (alignment == 0) {
_php_fbird_error(status);
RETURN_FALSE;
}
RETURN_LONG((zend_long)alignment);
}
/* {{{ BatchHandle::setDefaultBpb(string $bpb): bool */
PHP_METHOD(BatchHandle, setDefaultBpb)
{
ISC_STATUS status[256];
char *bpb;
size_t bpb_len;
fbird_batch *fb_batch;
RESET_ERRMSG;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &bpb, &bpb_len) == FAILURE) {
RETURN_THROWS();
}
fb_batch = fbird_batch_get_ptr(Z_OBJ_P(getThis()));
if (!fb_batch || !fb_batch->fbbatch_wrapper) {
_php_fbird_module_error("Invalid batch handle");
RETURN_FALSE;
}
/* Empty BPB is a no-op (reset to Firebird defaults) */
if (bpb_len == 0) {
RETURN_TRUE;
}
if (!fbbatch_set_default_bpb(FBG(master_instance), fb_batch->fbbatch_wrapper,
(unsigned)bpb_len, (const unsigned char *)bpb, status)) {
_php_fbird_error(status);
RETURN_FALSE;
}
RETURN_TRUE;
}
/* {{{ BatchHandle::cancel(): bool */
PHP_METHOD(BatchHandle, cancel)
{
ISC_STATUS status[256];
fbird_batch *fb_batch;
RESET_ERRMSG;
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
fb_batch = fbird_batch_get_ptr(Z_OBJ_P(getThis()));
if (!fb_batch || !fb_batch->fbbatch_wrapper) {
_php_fbird_module_error("Invalid batch handle");
RETURN_FALSE;
}
if (!fbbatch_cancel(FBG(master_instance), fb_batch->fbbatch_wrapper, status)) {
_php_fbird_error(status);
RETURN_FALSE;
}
fbbatch_close(FBG(master_instance), fb_batch->fbbatch_wrapper, status);
fb_batch->fbbatch_wrapper = NULL;
RETURN_TRUE;
}
/* {{{ BatchHandle::execute(): array|false */
PHP_METHOD(BatchHandle, execute)
{
ISC_STATUS status[256];
fbird_batch *fb_batch;
void *trans_ptr;
unsigned total_processed = 0;
unsigned error_count = 0;
RESET_ERRMSG;
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
fb_batch = fbird_batch_get_ptr(Z_OBJ_P(getThis()));
if (!fb_batch || !fb_batch->fbbatch_wrapper) {
_php_fbird_module_error("Invalid batch handle");
RETURN_FALSE;
}
if (!fb_batch->trans || !fb_batch->trans->fbt_transaction) {
_php_fbird_module_error("Batch has no valid transaction");
RETURN_FALSE;
}
trans_ptr = fbt_get_handle(fb_batch->trans->fbt_transaction);
if (!trans_ptr) {
_php_fbird_module_error("Failed to get transaction handle");
RETURN_FALSE;
}
if (!fbbatch_execute(FBG(master_instance), fb_batch->fbbatch_wrapper, trans_ptr,
&total_processed, &error_count, status)) {
_php_fbird_error(status);
RETURN_FALSE;
}
/* Close the batch after execution */
fbbatch_close(FBG(master_instance), fb_batch->fbbatch_wrapper, status);
fb_batch->fbbatch_wrapper = NULL;
unsigned success_count = (total_processed >= error_count) ? (total_processed - error_count) : 0;
array_init(return_value);
add_assoc_long(return_value, "total_processed", total_processed);
add_assoc_long(return_value, "success_count", success_count);
add_assoc_long(return_value, "error_count", error_count);
}
/* {{{ BatchHandle::add(mixed ...$args): bool
* Delegates to fbird_batch_add_impl() - shared with procedural API. */
PHP_METHOD(BatchHandle, add)
{
zval *args = NULL;
int argc = 0;
fbird_batch *fb_batch;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "*", &args, &argc) == FAILURE) {
RETURN_THROWS();
}
fb_batch = fbird_batch_get_ptr(Z_OBJ_P(getThis()));
if (fbird_batch_add_impl(fb_batch, args, argc) == FAILURE) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* {{{ BatchHandle::addBlob(string $data, int $blob_type = 0): string|false */
PHP_METHOD(BatchHandle, addBlob)
{
ISC_STATUS status[256];
char *data;
size_t data_len;
zend_long blob_type = 0;
fbird_batch *fb_batch;
ISC_QUAD blob_id;
RESET_ERRMSG;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &data, &data_len, &blob_type) == FAILURE) {
RETURN_THROWS();
}
fb_batch = fbird_batch_get_ptr(Z_OBJ_P(getThis()));
if (!fb_batch || !fb_batch->fbbatch_wrapper) {
_php_fbird_module_error("Invalid batch handle");
RETURN_FALSE;
}
memset(&blob_id, 0, sizeof(blob_id));
if (!fbbatch_add_blob(FBG(master_instance), fb_batch->fbbatch_wrapper,
(unsigned)data_len, (const void *)data, &blob_id, 0, NULL, status)) {
_php_fbird_error(status);
RETURN_FALSE;
}
RETURN_NEW_STR(_php_fbird_quad_to_string(blob_id));
}
const zend_function_entry fbird_batch_methods[] = {
PHP_ME(BatchHandle, getBlobAlignment, arginfo_BatchHandle_getBlobAlignment, ZEND_ACC_PUBLIC)
PHP_ME(BatchHandle, setDefaultBpb, arginfo_BatchHandle_setDefaultBpb, ZEND_ACC_PUBLIC)
PHP_ME(BatchHandle, cancel, arginfo_BatchHandle_cancel, ZEND_ACC_PUBLIC)
PHP_ME(BatchHandle, execute, arginfo_BatchHandle_execute, ZEND_ACC_PUBLIC)
PHP_ME(BatchHandle, add, arginfo_BatchHandle_add, ZEND_ACC_PUBLIC)
PHP_ME(BatchHandle, addBlob, arginfo_BatchHandle_addBlob, ZEND_ACC_PUBLIC)
PHP_FE_END
};
#endif /* FB_API_VER >= 40 */