forked from FirebirdSQL/php-firebird
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfbird_query_array.c
More file actions
368 lines (333 loc) · 10.9 KB
/
fbird_query_array.c
File metadata and controls
368 lines (333 loc) · 10.9 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* SPDX-License-Identifier: PHP-3.01
* SPDX-FileCopyrightText: The PHP Group and contributors (see CREDITS) */
/**
* Array handling functions.
*
* This file contains functions for allocating array descriptors and binding
* PHP arrays to Firebird array format.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include <string.h>
#include "php.h"
#include "php_ini.h"
#if HAVE_FIREBIRD
#include "php_firebird.h"
#include "php_fbird_includes.h"
#include "php_fbird_query_internal.h"
#include "php_fbird_query_array.h"
#include "php_fbird_query_prepare.h"
#include "firebird_utils.h"
#include "fbird_datetime.h"
#define ISC_LONG_MIN INT_MIN
#define ISC_LONG_MAX INT_MAX
int _php_fbird_bind_array(zval *val, char *buf, zend_ulong buf_size,
fbird_array *array, int dim)
{
zval null_val, *pnull_val = &null_val;
int u_bound = array->ar_desc.array_desc_bounds[dim].array_bound_upper,
l_bound = array->ar_desc.array_desc_bounds[dim].array_bound_lower,
dim_len = 1 + u_bound - l_bound;
ZVAL_NULL(pnull_val);
if (dim < array->ar_desc.array_desc_dimensions) {
zend_ulong slice_size = buf_size / dim_len;
int i;
zval *subval = val;
if (Z_TYPE_P(val) == IS_ARRAY) {
zend_hash_internal_pointer_reset(Z_ARRVAL_P(val));
}
for (i = 0; i < dim_len; ++i) {
if (Z_TYPE_P(val) == IS_ARRAY &&
(subval = zend_hash_get_current_data(Z_ARRVAL_P(val))) == NULL)
{
subval = pnull_val;
}
if (_php_fbird_bind_array(subval, buf, slice_size, array, dim+1) == FAILURE)
{
return FAILURE;
}
buf += slice_size;
if (Z_TYPE_P(val) == IS_ARRAY) {
zend_hash_move_forward(Z_ARRVAL_P(val));
}
}
if (Z_TYPE_P(val) == IS_ARRAY) {
zend_hash_internal_pointer_reset(Z_ARRVAL_P(val));
}
} else {
/* expect a single value */
if (Z_TYPE_P(val) == IS_NULL) {
memset(buf, 0, buf_size);
} else if (array->ar_desc.array_desc_scale < 0) {
/* no coercion for array types
* Use zval_get_double() to avoid modifying the original zval in-place,
* which would corrupt PHP arrays when processing multiple elements. */
double l;
double dval = zval_get_double(val);
if (dval > 0) {
l = dval * pow(10, -array->ar_desc.array_desc_scale) + .5;
} else {
l = dval * pow(10, -array->ar_desc.array_desc_scale) - .5;
}
switch (array->el_type) {
case SQL_SHORT:
if (l > SHRT_MAX || l < SHRT_MIN) {
_php_fbird_module_error("Array parameter exceeds field width");
return FAILURE;
}
*(short*) buf = (short) l;
break;
case SQL_LONG:
if (l > ISC_LONG_MAX || l < ISC_LONG_MIN) {
_php_fbird_module_error("Array parameter exceeds field width");
return FAILURE;
}
*(ISC_LONG*) buf = (ISC_LONG) l;
break;
case SQL_INT64:
{
long double ld;
/* Use zval_get_string() to get a copy without modifying the original zval */
zend_string *str = zval_get_string(val);
if (!sscanf(ZSTR_VAL(str), "%Lf", &ld)) {
_php_fbird_module_error("Cannot convert '%s' to long double",
ZSTR_VAL(str));
zend_string_release(str);
return FAILURE;
}
zend_string_release(str);
if (ld > 0) {
*(ISC_INT64 *) buf = (ISC_INT64) (ld * pow(10,
-array->ar_desc.array_desc_scale) + .5);
} else {
*(ISC_INT64 *) buf = (ISC_INT64) (ld * pow(10,
-array->ar_desc.array_desc_scale) - .5);
}
}
break;
}
} else {
switch (array->el_type) {
case SQL_SHORT:
{
/* Use zval_get_long() to avoid modifying the original zval in-place */
zend_long lval = zval_get_long(val);
if (lval > SHRT_MAX || lval < SHRT_MIN) {
_php_fbird_module_error("Array parameter exceeds field width");
return FAILURE;
}
*(short *) buf = (short) lval;
}
break;
case SQL_LONG:
{
/* Use zval_get_long() to avoid modifying the original zval in-place */
zend_long lval = zval_get_long(val);
#if (SIZEOF_ZEND_LONG > 4)
if (lval > ISC_LONG_MAX || lval < ISC_LONG_MIN) {
_php_fbird_module_error("Array parameter exceeds field width");
return FAILURE;
}
#endif
*(ISC_LONG *) buf = (ISC_LONG) lval;
}
break;
case SQL_INT64:
{
#if (SIZEOF_ZEND_LONG >= 8)
/* Use zval_get_long() to avoid modifying the original zval in-place */
zend_long lval = zval_get_long(val);
*(zend_long *) buf = lval;
#else
/* Use zval_get_string() to get a copy without modifying the original zval */
ISC_INT64 l;
zend_string *str = zval_get_string(val);
if (!sscanf(ZSTR_VAL(str), "%" LL_MASK "d", &l)) {
_php_fbird_module_error("Cannot convert '%s' to long integer",
ZSTR_VAL(str));
zend_string_release(str);
return FAILURE;
}
zend_string_release(str);
*(ISC_INT64 *) buf = l;
#endif
}
break;
case SQL_FLOAT:
{
/* Use zval_get_double() to avoid modifying the original zval in-place */
double dval = zval_get_double(val);
*(float*) buf = (float) dval;
}
break;
#ifdef SQL_BOOLEAN
case SQL_BOOLEAN:
/* zend_is_true() does not modify the zval */
*(FB_BOOLEAN*) buf = zend_is_true(val) ? FB_TRUE : FB_FALSE;
break;
#endif
case SQL_DOUBLE:
{
/* Use zval_get_double() to avoid modifying the original zval in-place */
double dval = zval_get_double(val);
*(double*) buf = dval;
}
break;
case SQL_TIMESTAMP:
{
/* Cross-platform timestamp parsing using fbird_datetime utilities */
zend_string *str = zval_get_string(val);
fbird_datetime_components dt;
if (fbird_parse_timestamp(ZSTR_VAL(str), &dt)) {
/* Use OO API encoding */
*(ISC_TIMESTAMP *)buf = fbu_encode_timestamp(IBG(master_instance),
dt.year, dt.month, dt.day,
dt.hours, dt.minutes, dt.seconds,
dt.fractions);
} else {
/* Parsing failed - encode zero timestamp via OO API */
*(ISC_TIMESTAMP *)buf = fbu_encode_timestamp(IBG(master_instance), 0, 0, 0, 0, 0, 0, 0);
}
zend_string_release(str);
}
break;
#if FB_API_VER >= 40
case SQL_TIMESTAMP_TZ:
{
/* Timezone types in arrays require Firebird 4.0+ master interface */
if (!IBG(master_instance)) {
_php_fbird_module_error("TIMESTAMP WITH TIME ZONE arrays require Firebird 4.0+ client library");
return FAILURE;
}
/* Cross-platform timestamp+timezone parsing */
zend_string *str = zval_get_string(val);
fbird_datetime_components dt;
fbird_datetime_init(&dt);
strncpy(dt.timezone, "GMT", sizeof(dt.timezone) - 1);
if (fbird_parse_timestamp(ZSTR_VAL(str), &dt)) {
/* If no timezone was parsed, use default GMT */
if (!dt.has_timezone) {
strncpy(dt.timezone, "GMT", sizeof(dt.timezone) - 1);
}
}
zend_string_release(str);
if (fbu_encode_timestamp_tz(IBG(master_instance), (ISC_TIMESTAMP_TZ *)buf,
dt.year, dt.month, dt.day,
dt.hours, dt.minutes, dt.seconds, dt.fractions, dt.timezone) != 0) {
_php_fbird_module_error("Failed to encode TIMESTAMP WITH TIME ZONE array element");
return FAILURE;
}
}
break;
#endif
case SQL_TYPE_DATE:
{
/* Cross-platform date parsing using fbird_datetime utilities */
zend_string *str = zval_get_string(val);
fbird_datetime_components dt;
if (fbird_parse_date(ZSTR_VAL(str), &dt)) {
/* Use OO API encoding */
*(ISC_DATE *)buf = fbu_encode_date(IBG(master_instance),
dt.year, dt.month, dt.day);
} else {
/* Parsing failed - encode zero date via OO API */
*(ISC_DATE *)buf = fbu_encode_date(IBG(master_instance), 0, 0, 0);
}
zend_string_release(str);
}
break;
case SQL_TYPE_TIME:
{
/* Cross-platform time parsing using fbird_datetime utilities */
zend_string *str = zval_get_string(val);
fbird_datetime_components dt;
if (fbird_parse_time(ZSTR_VAL(str), &dt)) {
/* Use OO API encoding */
*(ISC_TIME *)buf = fbu_encode_time(IBG(master_instance),
dt.hours, dt.minutes, dt.seconds,
dt.fractions);
} else {
/* Parsing failed - encode zero time via OO API */
*(ISC_TIME *)buf = fbu_encode_time(IBG(master_instance), 0, 0, 0, 0);
}
zend_string_release(str);
}
break;
#if FB_API_VER >= 40
case SQL_TIME_TZ:
{
/* Timezone types in arrays require Firebird 4.0+ master interface */
if (!IBG(master_instance)) {
_php_fbird_module_error("TIME WITH TIME ZONE arrays require Firebird 4.0+ client library");
return FAILURE;
}
/* Cross-platform time+timezone parsing */
zend_string *str = zval_get_string(val);
fbird_datetime_components dt;
fbird_datetime_init(&dt);
strncpy(dt.timezone, "GMT", sizeof(dt.timezone) - 1);
if (fbird_parse_time(ZSTR_VAL(str), &dt)) {
/* If no timezone was parsed, use default GMT */
if (!dt.has_timezone) {
strncpy(dt.timezone, "GMT", sizeof(dt.timezone) - 1);
}
}
zend_string_release(str);
if (fbu_encode_time_tz(IBG(master_instance), (ISC_TIME_TZ *)buf,
dt.hours, dt.minutes, dt.seconds, dt.fractions, dt.timezone) != 0) {
_php_fbird_module_error("Failed to encode TIME WITH TIME ZONE array element");
return FAILURE;
}
}
break;
#endif
case SQL_VARYING:
{
/* VARCHAR array workaround: Use null-terminated strings.
* buf_size = el_size = array_desc_length + 1 (for null terminator)
*
* See fb_array.hpp for explanation of dtype_cstring bug workaround.
*
* Use zval_get_string() to get copy without modifying original zval.
* This fixes PHP 8.x issue where convert_to_string() modifies in-place,
* causing all array elements to contain the last value */
zend_string *str = zval_get_string(val);
size_t str_len = ZSTR_LEN(str);
size_t max_len = buf_size - 1; /* Reserve space for null terminator */
if (str_len > max_len) {
str_len = max_len;
}
/* Copy string data */
if (str_len > 0) {
memcpy(buf, ZSTR_VAL(str), str_len);
}
/* Null-terminate */
buf[str_len] = '\0';
zend_string_release(str);
}
break;
default:
{
/* Use zval_get_string() to get copy without modifying original zval
* This fixes PHP 8.x issue where convert_to_string() modifies in-place,
* causing all CHAR array elements to contain the last value */
zend_string *str = zval_get_string(val);
size_t copy_len = ZSTR_LEN(str);
if (copy_len >= buf_size) {
copy_len = buf_size - 1;
}
memcpy(buf, ZSTR_VAL(str), copy_len);
/* Pad remaining buffer with spaces for CHAR fields (SQL_TEXT) */
if (copy_len < buf_size) {
memset(buf + copy_len, ' ', buf_size - copy_len);
}
zend_string_release(str);
}
}
}
}
return SUCCESS;
}
#endif /* HAVE_FIREBIRD */