php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
firebird_statement.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Ard Biesheuvel <abies@php.net> |
14 +----------------------------------------------------------------------+
15*/
16
17#ifdef HAVE_CONFIG_H
18#include <config.h>
19#endif
20
21#include "php.h"
22#include "php_ini.h"
23#include "ext/standard/info.h"
24#include "ext/pdo/php_pdo.h"
26#include "php_pdo_firebird.h"
28#include "pdo_firebird_utils.h"
29
30#include <time.h>
31
32#define READ_AND_RETURN_USING_MEMCPY(type, sqldata) do { \
33 type ret; \
34 memcpy(&ret, sqldata, sizeof(ret)); \
35 return ret; \
36 } while (0);
37
38static zend_always_inline ISC_INT64 php_get_isc_int64_from_sqldata(const ISC_SCHAR *sqldata)
39{
40 READ_AND_RETURN_USING_MEMCPY(ISC_INT64, sqldata);
41}
42
43static zend_always_inline ISC_LONG php_get_isc_long_from_sqldata(const ISC_SCHAR *sqldata)
44{
45 READ_AND_RETURN_USING_MEMCPY(ISC_LONG, sqldata);
46}
47
48static zend_always_inline double php_get_double_from_sqldata(const ISC_SCHAR *sqldata)
49{
50 READ_AND_RETURN_USING_MEMCPY(double, sqldata);
51}
52
53static zend_always_inline float php_get_float_from_sqldata(const ISC_SCHAR *sqldata)
54{
55 READ_AND_RETURN_USING_MEMCPY(float, sqldata);
56}
57
58static zend_always_inline ISC_TIMESTAMP php_get_isc_timestamp_from_sqldata(const ISC_SCHAR *sqldata)
59{
60 READ_AND_RETURN_USING_MEMCPY(ISC_TIMESTAMP, sqldata);
61}
62
63static zend_always_inline ISC_QUAD php_get_isc_quad_from_sqldata(const ISC_SCHAR *sqldata)
64{
65 READ_AND_RETURN_USING_MEMCPY(ISC_QUAD, sqldata);
66}
67
68#if FB_API_VER >= 40
69
70static zend_always_inline ISC_TIME_TZ php_get_isc_time_tz_from_sqldata(const ISC_SCHAR *sqldata)
71{
72 READ_AND_RETURN_USING_MEMCPY(ISC_TIME_TZ, sqldata);
73}
74
75static zend_always_inline ISC_TIMESTAMP_TZ php_get_isc_timestamp_tz_from_sqldata(const ISC_SCHAR *sqldata)
76{
77 READ_AND_RETURN_USING_MEMCPY(ISC_TIMESTAMP_TZ, sqldata);
78}
79
80/* fetch formatted time with time zone */
81static int get_formatted_time_tz(pdo_stmt_t *stmt, const ISC_TIME_TZ* timeTz, zval *result)
82{
84 unsigned hours = 0, minutes = 0, seconds = 0, fractions = 0;
85 char timeZoneBuffer[40] = {0};
86 char *fmt;
87 struct tm t;
88 ISC_TIME time;
89 char timeBuf[80] = {0};
90 char timeTzBuf[124] = {0};
91 if (fb_decode_time_tz(S->H->isc_status, timeTz, &hours, &minutes, &seconds, &fractions, sizeof(timeZoneBuffer), timeZoneBuffer)) {
92 return 1;
93 }
94 time = fb_encode_time(hours, minutes, seconds, fractions);
95 isc_decode_sql_time(&time, &t);
96 fmt = S->H->time_format ? S->H->time_format : PDO_FB_DEF_TIME_FMT;
97
98 size_t len = strftime(timeBuf, sizeof(timeBuf), fmt, &t);
99 if (len == 0) {
100 return 1;
101 }
102
103 size_t time_tz_len = sprintf(timeTzBuf, "%s %s", timeBuf, timeZoneBuffer);
104 ZVAL_STRINGL(result, timeTzBuf, time_tz_len);
105 return 0;
106}
107
108/* fetch formatted timestamp with time zone */
109static int get_formatted_timestamp_tz(pdo_stmt_t *stmt, const ISC_TIMESTAMP_TZ* timestampTz, zval *result)
110{
112 unsigned year, month, day, hours, minutes, seconds, fractions;
113 char timeZoneBuffer[40] = {0};
114 char *fmt;
115 struct tm t;
116 ISC_TIMESTAMP ts;
117 char timestampBuf[80] = {0};
118 char timestampTzBuf[124] = {0};
119 if (fb_decode_timestamp_tz(S->H->isc_status, timestampTz, &year, &month, &day, &hours, &minutes, &seconds, &fractions, sizeof(timeZoneBuffer), timeZoneBuffer)) {
120 return 1;
121 }
122 ts.timestamp_date = fb_encode_date(year, month, day);
123 ts.timestamp_time = fb_encode_time(hours, minutes, seconds, fractions);
124 isc_decode_timestamp(&ts, &t);
125
126 fmt = S->H->timestamp_format ? S->H->timestamp_format : PDO_FB_DEF_TIMESTAMP_FMT;
127
128 size_t len = strftime(timestampBuf, sizeof(timestampBuf), fmt, &t);
129 if (len == 0) {
130 return 1;
131 }
132
133 size_t timestamp_tz_len = sprintf(timestampTzBuf, "%s %s", timestampBuf, timeZoneBuffer);
134 ZVAL_STRINGL(result, timestampTzBuf, timestamp_tz_len);
135 return 0;
136}
137
138#endif
139
140/* free the allocated space for passing field values to the db and back */
141static void php_firebird_free_sqlda(XSQLDA const *sqlda) /* {{{ */
142{
143 int i;
144
145 for (i = 0; i < sqlda->sqld; ++i) {
146 XSQLVAR const *var = &sqlda->sqlvar[i];
147
148 if (var->sqlind) {
149 efree(var->sqlind);
150 }
151 }
152}
153/* }}} */
154
155/* called by PDO to clean up a statement handle */
156static int pdo_firebird_stmt_dtor(pdo_stmt_t *stmt) /* {{{ */
157{
159 int result = 1;
160
161 /* TODO: for master, move this check to a separate function shared between pdo drivers.
162 * pdo_pgsql and pdo_mysql do this exact same thing */
163 bool server_obj_usable = !Z_ISUNDEF(stmt->database_object_handle)
164 && IS_OBJ_VALID(EG(objects_store).object_buckets[Z_OBJ_HANDLE(stmt->database_object_handle)])
165 && !(OBJ_FLAGS(Z_OBJ(stmt->database_object_handle)) & IS_OBJ_FREE_CALLED);
166
167 /* release the statement.
168 * Note: if the server object is already gone then the statement was closed already as well. */
169 if (server_obj_usable && isc_dsql_free_statement(S->H->isc_status, &S->stmt, DSQL_drop)) {
171 result = 0;
172 }
173
174 zend_hash_destroy(S->named_params);
175 FREE_HASHTABLE(S->named_params);
176
177 /* clean up the input descriptor */
178 if (S->in_sqlda) {
179 php_firebird_free_sqlda(S->in_sqlda);
180 efree(S->in_sqlda);
181 }
182
183 php_firebird_free_sqlda(&S->out_sqlda);
184 efree(S);
185
186 return result;
187}
188/* }}} */
189
190/* called by PDO to execute a prepared query */
191static int pdo_firebird_stmt_execute(pdo_stmt_t *stmt) /* {{{ */
192{
195 zend_ulong affected_rows = 0;
196 static char info_count[] = {isc_info_sql_records};
197 char result[64];
198
199 do {
200 /* named or open cursors should be closed first */
201 if ((*S->name || S->cursor_open) && isc_dsql_free_statement(H->isc_status, &S->stmt, DSQL_close)) {
202 break;
203 }
204 S->cursor_open = 0;
205
206 /* allocate storage for the output data */
207 if (S->out_sqlda.sqld) {
208 unsigned int i;
209 for (i = 0; i < S->out_sqlda.sqld; i++) {
210 XSQLVAR *var = &S->out_sqlda.sqlvar[i];
211 if (var->sqlind) {
212 efree(var->sqlind);
213 }
214 var->sqlind = (void*)ecalloc(1, var->sqllen + 2 * sizeof(short));
215 var->sqldata = &((char*)var->sqlind)[sizeof(short)];
216 }
217 }
218
219 if (S->statement_type == isc_info_sql_stmt_exec_procedure) {
220 if (isc_dsql_execute2(H->isc_status, &H->tr, &S->stmt, PDO_FB_SQLDA_VERSION, S->in_sqlda, &S->out_sqlda)) {
221 break;
222 }
223 } else if (isc_dsql_execute(H->isc_status, &H->tr, &S->stmt, PDO_FB_SQLDA_VERSION, S->in_sqlda)) {
224 break;
225 }
226
227 /* Determine how many rows have changed. In this case we are
228 * only interested in rows changed, not rows retrieved. That
229 * should be handled by the client when fetching. */
230 stmt->row_count = affected_rows;
231
232 switch (S->statement_type) {
233 case isc_info_sql_stmt_insert:
234 case isc_info_sql_stmt_update:
235 case isc_info_sql_stmt_delete:
236 case isc_info_sql_stmt_exec_procedure:
237 if (isc_dsql_sql_info(H->isc_status, &S->stmt, sizeof ( info_count),
238 info_count, sizeof(result), result)) {
239 break;
240 }
241 if (result[0] == isc_info_sql_records) {
242 unsigned i = 3, result_size = isc_vax_integer(&result[1], 2);
243 if (result_size > sizeof(result)) {
244 goto error;
245 }
246 while (result[i] != isc_info_end && i < result_size) {
247 short len = (short) isc_vax_integer(&result[i + 1], 2);
248 if (len != 1 && len != 2 && len != 4) {
249 goto error;
250 }
251 if (result[i] != isc_info_req_select_count) {
252 affected_rows += isc_vax_integer(&result[i + 3], len);
253 }
254 i += len + 3;
255 }
256 stmt->row_count = affected_rows;
257 }
258 /* TODO Dead code or assert one of the previous cases are hit? */
259 default:
260 ;
261 }
262
263 if (stmt->dbh->auto_commit && !S->H->in_manually_txn && !php_firebird_commit_transaction(stmt->dbh, /* retain */ true)) {
264 break;
265 }
266
267 *S->name = 0;
268 S->cursor_open = S->out_sqlda.sqln && (S->statement_type != isc_info_sql_stmt_exec_procedure);
269 S->exhausted = !S->out_sqlda.sqln; /* There are data to fetch */
270
271 return 1;
272 } while (0);
273
274error:
276
277 return 0;
278}
279/* }}} */
280
281/* called by PDO to fetch the next row from a statement */
282static int pdo_firebird_stmt_fetch(pdo_stmt_t *stmt, /* {{{ */
284{
287
288 if (!stmt->executed) {
289 const char *msg = "Cannot fetch from a closed cursor";
290 php_firebird_error_stmt_with_info(stmt, "HY000", strlen("HY000"), msg, strlen(msg));
291 } else if (!S->exhausted) {
292 if (S->statement_type == isc_info_sql_stmt_exec_procedure) {
293 stmt->row_count = 1;
294 S->exhausted = 1;
295 return 1;
296 }
297 if (isc_dsql_fetch(H->isc_status, &S->stmt, PDO_FB_SQLDA_VERSION, &S->out_sqlda)) {
298 if (H->isc_status[0] && H->isc_status[1]) {
300 }
301 S->exhausted = 1;
302 return 0;
303 }
304 stmt->row_count++;
305 return 1;
306 }
307 return 0;
308}
309/* }}} */
310
311/* called by PDO to retrieve information about the fields being returned */
312static int pdo_firebird_stmt_describe(pdo_stmt_t *stmt, int colno) /* {{{ */
313{
315 struct pdo_column_data *col = &stmt->columns[colno];
316 XSQLVAR *var = &S->out_sqlda.sqlvar[colno];
317 int colname_len;
318 char *cp;
319
320 if ((var->sqltype & ~1) == SQL_TEXT) {
321 var->sqltype = SQL_VARYING | (var->sqltype & 1);
322 }
323 colname_len = (S->H->fetch_table_names && var->relname_length)
324 ? (var->aliasname_length + var->relname_length + 1)
325 : (var->aliasname_length);
326 col->precision = -var->sqlscale;
327 col->maxlen = var->sqllen;
328 col->name = zend_string_alloc(colname_len, 0);
329 cp = ZSTR_VAL(col->name);
330 if (colname_len > var->aliasname_length) {
331 memmove(cp, var->relname, var->relname_length);
332 cp += var->relname_length;
333 *cp++ = '.';
334 }
335 memmove(cp, var->aliasname, var->aliasname_length);
336 *(cp+var->aliasname_length) = '\0';
337
338 return 1;
339}
340/* }}} */
341
342static int pdo_firebird_stmt_get_column_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value)
343{
345 XSQLVAR *var = &S->out_sqlda.sqlvar[colno];
346
347 enum pdo_param_type param_type;
348 if (var->sqlscale < 0) {
349 param_type = PDO_PARAM_STR;
350 } else {
351 switch (var->sqltype & ~1) {
352 case SQL_SHORT:
353 case SQL_LONG:
354#if SIZEOF_ZEND_LONG >= 8
355 case SQL_INT64:
356#endif
357 param_type = PDO_PARAM_INT;
358 break;
359 case SQL_BOOLEAN:
360 param_type = PDO_PARAM_BOOL;
361 break;
362 default:
363 param_type = PDO_PARAM_STR;
364 break;
365 }
366 }
367
369 add_assoc_long(return_value, "pdo_type", param_type);
370 return 1;
371}
372
373/* fetch a blob into a fetch buffer */
374static int php_firebird_fetch_blob(pdo_stmt_t *stmt, int colno, zval *result, ISC_QUAD *blob_id)
375{
378 isc_blob_handle blobh = PDO_FIREBIRD_HANDLE_INITIALIZER;
379 char const bl_item = isc_info_blob_total_length;
380 char bl_info[20];
381 unsigned short i;
382 int retval = 0;
383 size_t len = 0;
384
385 if (isc_open_blob(H->isc_status, &H->db, &H->tr, &blobh, blob_id)) {
387 return 0;
388 }
389
390 if (isc_blob_info(H->isc_status, &blobh, 1, const_cast(&bl_item),
391 sizeof(bl_info), bl_info)) {
393 goto fetch_blob_end;
394 }
395
396 /* find total length of blob's data */
397 for (i = 0; i < sizeof(bl_info); ) {
398 unsigned short item_len;
399 char item = bl_info[i++];
400
401 if (item == isc_info_end || item == isc_info_truncated || item == isc_info_error
402 || i >= sizeof(bl_info)) {
403 const char *msg = "Couldn't determine BLOB size";
404 php_firebird_error_stmt_with_info(stmt, "HY000", strlen("HY000"), msg, strlen(msg));
405 goto fetch_blob_end;
406 }
407
408 item_len = (unsigned short) isc_vax_integer(&bl_info[i], 2);
409
410 if (item == isc_info_blob_total_length) {
411 len = isc_vax_integer(&bl_info[i+2], item_len);
412 break;
413 }
414 i += item_len+2;
415 }
416
417 /* we've found the blob's length, now fetch! */
418
419 if (len) {
420 zend_ulong cur_len;
421 unsigned short seg_len;
422 ISC_STATUS stat;
423 zend_string *str;
424
425 /* prevent overflow */
426 if (len > ZSTR_MAX_LEN) {
427 result = 0;
428 goto fetch_blob_end;
429 }
430
431 str = zend_string_alloc(len, 0);
432
433 for (cur_len = stat = 0; (!stat || stat == isc_segment) && cur_len < len; cur_len += seg_len) {
434
435 unsigned short chunk_size = (len - cur_len) > USHRT_MAX ? USHRT_MAX
436 : (unsigned short)(len - cur_len);
437
438 stat = isc_get_segment(H->isc_status, &blobh, &seg_len, chunk_size, ZSTR_VAL(str) + cur_len);
439 }
440
441 ZSTR_VAL(str)[len] = '\0';
442 ZVAL_STR(result, str);
443
444 if (H->isc_status[0] == 1 && (stat != 0 && stat != isc_segstr_eof && stat != isc_segment)) {
445 const char *msg = "Error reading from BLOB";
446 php_firebird_error_stmt_with_info(stmt, "HY000", strlen("HY000"), msg, strlen(msg));
447 goto fetch_blob_end;
448 }
449 }
450 retval = 1;
451
452fetch_blob_end:
453 if (isc_close_blob(H->isc_status, &blobh)) {
455 return 0;
456 }
457 return retval;
458}
459/* }}} */
460
461static int pdo_firebird_stmt_get_col(
462 pdo_stmt_t *stmt, int colno, zval *result, enum pdo_param_type *type)
463{
465 XSQLVAR const *var = &S->out_sqlda.sqlvar[colno];
466
467 if (*var->sqlind == -1) {
469 } else {
470 if (var->sqlscale < 0) {
471 static ISC_INT64 const scales[] = { 1, 10, 100, 1000,
472 10000,
473 100000,
474 1000000,
475 10000000,
476 100000000,
477 1000000000,
478 LL_LIT(10000000000),
479 LL_LIT(100000000000),
480 LL_LIT(1000000000000),
481 LL_LIT(10000000000000),
482 LL_LIT(100000000000000),
483 LL_LIT(1000000000000000),
484 LL_LIT(10000000000000000),
485 LL_LIT(100000000000000000),
486 LL_LIT(1000000000000000000)
487 };
488 ISC_INT64 n, f = scales[-var->sqlscale];
489 zend_string *str;
490
491 switch (var->sqltype & ~1) {
492 case SQL_SHORT:
493 n = *(short*)var->sqldata;
494 break;
495 case SQL_LONG:
496 n = php_get_isc_long_from_sqldata(var->sqldata);
497 break;
498 case SQL_INT64:
499 n = php_get_isc_int64_from_sqldata(var->sqldata);
500 break;
501 case SQL_DOUBLE:
502 break;
504 }
505
506 if ((var->sqltype & ~1) == SQL_DOUBLE) {
507 str = zend_strpprintf(0, "%.*F", -var->sqlscale, php_get_double_from_sqldata(var->sqldata));
508 } else if (n >= 0) {
509 str = zend_strpprintf(0, "%" LL_MASK "d.%0*" LL_MASK "d",
510 n / f, -var->sqlscale, n % f);
511 } else if (n <= -f) {
512 str = zend_strpprintf(0, "%" LL_MASK "d.%0*" LL_MASK "d",
513 n / f, -var->sqlscale, -n % f);
514 } else {
515 str = zend_strpprintf(0, "-0.%0*" LL_MASK "d", -var->sqlscale, -n % f);
516 }
517 ZVAL_STR(result, str);
518 } else {
519 switch (var->sqltype & ~1) {
520 struct tm t;
521 char *fmt;
522
523 case SQL_VARYING:
524 ZVAL_STRINGL_FAST(result, &var->sqldata[2], *(short*)var->sqldata);
525 break;
526 case SQL_TEXT:
527 ZVAL_STRINGL_FAST(result, var->sqldata, var->sqllen);
528 break;
529 case SQL_SHORT:
530 ZVAL_LONG(result, *(short*)var->sqldata);
531 break;
532 case SQL_LONG:
533 ZVAL_LONG(result, php_get_isc_long_from_sqldata(var->sqldata));
534 break;
535 case SQL_INT64:
536#if SIZEOF_ZEND_LONG >= 8
537 ZVAL_LONG(result, php_get_isc_int64_from_sqldata(var->sqldata));
538#else
539 ZVAL_STR(result, zend_strpprintf(0, "%" LL_MASK "d", php_get_isc_int64_from_sqldata(var->sqldata)));
540#endif
541 break;
542 case SQL_FLOAT:
543 /* TODO: Why is this not returned as the native type? */
544 ZVAL_STR(result, zend_strpprintf_unchecked(0, "%.8H", php_get_float_from_sqldata(var->sqldata)));
545 break;
546 case SQL_DOUBLE:
547 /* TODO: Why is this not returned as the native type? */
548 ZVAL_STR(result, zend_strpprintf_unchecked(0, "%.16H", php_get_double_from_sqldata(var->sqldata)));
549 break;
550 case SQL_BOOLEAN:
551 ZVAL_BOOL(result, *(FB_BOOLEAN*)var->sqldata);
552 break;
553 case SQL_TYPE_DATE:
554 isc_decode_sql_date((ISC_DATE*)var->sqldata, &t);
555 fmt = S->H->date_format ? S->H->date_format : PDO_FB_DEF_DATE_FMT;
556 if (0) {
557 case SQL_TYPE_TIME:
558 isc_decode_sql_time((ISC_TIME*)var->sqldata, &t);
559 fmt = S->H->time_format ? S->H->time_format : PDO_FB_DEF_TIME_FMT;
560 } else if (0) {
561 case SQL_TIMESTAMP:
562 {
563 ISC_TIMESTAMP timestamp = php_get_isc_timestamp_from_sqldata(var->sqldata);
564 isc_decode_timestamp(&timestamp, &t);
565 }
566 fmt = S->H->timestamp_format ? S->H->timestamp_format : PDO_FB_DEF_TIMESTAMP_FMT;
567 }
568 /* convert the timestamp into a string */
569 char buf[80];
570 size_t len = strftime(buf, sizeof(buf), fmt, &t);
572 break;
573#if FB_API_VER >= 40
574 case SQL_TIME_TZ: {
575 ISC_TIME_TZ time = php_get_isc_time_tz_from_sqldata(var->sqldata);
576 return get_formatted_time_tz(stmt, &time, result);
577 }
578 case SQL_TIMESTAMP_TZ: {
579 ISC_TIMESTAMP_TZ ts = php_get_isc_timestamp_tz_from_sqldata(var->sqldata);
580 return get_formatted_timestamp_tz(stmt, &ts, result);
581 }
582#endif
583 case SQL_BLOB: {
584 ISC_QUAD quad = php_get_isc_quad_from_sqldata(var->sqldata);
585 return php_firebird_fetch_blob(stmt, colno, result, &quad);
586 }
587 }
588 }
589 }
590 return 1;
591}
592
593static int php_firebird_bind_blob(pdo_stmt_t *stmt, ISC_QUAD *blob_id, zval *param)
594{
597 isc_blob_handle h = PDO_FIREBIRD_HANDLE_INITIALIZER;
598 zval data;
599 zend_ulong put_cnt = 0, rem_cnt;
600 unsigned short chunk_size;
601 int result = 1;
602
603 if (isc_create_blob(H->isc_status, &H->db, &H->tr, &h, blob_id)) {
605 return 0;
606 }
607
608 if (Z_TYPE_P(param) != IS_STRING) {
610 } else {
611 ZVAL_COPY_VALUE(&data, param);
612 }
613
614 for (rem_cnt = Z_STRLEN(data); rem_cnt > 0; rem_cnt -= chunk_size) {
615 chunk_size = rem_cnt > USHRT_MAX ? USHRT_MAX : (unsigned short)rem_cnt;
616 if (isc_put_segment(H->isc_status, &h, chunk_size, &Z_STRVAL(data)[put_cnt])) {
618 result = 0;
619 break;
620 }
621 put_cnt += chunk_size;
622 }
623
624 if (Z_TYPE_P(param) != IS_STRING) {
625 zval_ptr_dtor_str(&data);
626 }
627
628 if (isc_close_blob(H->isc_status, &h)) {
630 return 0;
631 }
632 return result;
633}
634
635static int pdo_firebird_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, /* {{{ */
636 enum pdo_param_event event_type)
637{
639 XSQLDA *sqlda = param->is_param ? S->in_sqlda : &S->out_sqlda;
640 XSQLVAR *var;
641
642 if (event_type == PDO_PARAM_EVT_FREE) { /* not used */
643 return 1;
644 }
645
646 if (!sqlda || param->paramno >= sqlda->sqld) {
647 const char *msg = "Invalid parameter index";
648 php_firebird_error_stmt_with_info(stmt, "HY093", strlen("HY093"), msg, strlen(msg));
649 return 0;
650 }
651 if (param->is_param && param->paramno == -1) {
652 zval *index;
653
654 /* try to determine the index by looking in the named_params hash */
655 if ((index = zend_hash_find(S->named_params, param->name)) != NULL) {
656 param->paramno = Z_LVAL_P(index);
657 } else {
658 /* ... or by looking in the input descriptor */
659 int i;
660
661 for (i = 0; i < sqlda->sqld; ++i) {
662 XSQLVAR *var = &sqlda->sqlvar[i];
663
664 if ((var->aliasname_length && !strncasecmp(ZSTR_VAL(param->name), var->aliasname,
665 min(ZSTR_LEN(param->name), var->aliasname_length)))
666 || (var->sqlname_length && !strncasecmp(ZSTR_VAL(param->name), var->sqlname,
667 min(ZSTR_LEN(param->name), var->sqlname_length)))) {
668 param->paramno = i;
669 break;
670 }
671 }
672 if (i >= sqlda->sqld) {
673 const char *msg = "Invalid parameter name";
674 php_firebird_error_stmt_with_info(stmt, "HY093", strlen("HY093"), msg, strlen(msg));
675 return 0;
676 }
677 }
678 }
679
680 var = &sqlda->sqlvar[param->paramno];
681
682 switch (event_type) {
683 zval *parameter;
684
686 if (param->is_param) {
687 /* allocate the parameter */
688 if (var->sqlind) {
689 efree(var->sqlind);
690 }
691 var->sqlind = (void*)emalloc(var->sqllen + 2*sizeof(short));
692 var->sqldata = &((char*)var->sqlind)[sizeof(short)];
693 }
694 break;
695
697 if (!param->is_param) {
698 break;
699 }
700
701 *var->sqlind = 0;
702 if (Z_ISREF(param->parameter)) {
703 parameter = Z_REFVAL(param->parameter);
704 } else {
705 parameter = &param->parameter;
706 }
707
708 if (Z_TYPE_P(parameter) == IS_RESOURCE) {
709 php_stream *stm = NULL;
710
711 php_stream_from_zval_no_verify(stm, parameter);
712 if (stm) {
714 zval_ptr_dtor(parameter);
715 ZVAL_STR(parameter, mem ? mem : ZSTR_EMPTY_ALLOC());
716 } else {
717 pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource");
718 return 0;
719 }
720 }
721
722 switch (var->sqltype & ~1) {
723 case SQL_ARRAY:
724 {
725 const char *msg = "Cannot bind to array field";
726 php_firebird_error_stmt_with_info(stmt, "HY000", strlen("HY000"), msg, strlen(msg));
727 }
728 return 0;
729
730 case SQL_BLOB: {
731 if (Z_TYPE_P(parameter) == IS_NULL) {
732 /* Check if field allow NULL values */
733 if (~var->sqltype & 1) {
734 const char *msg = "Parameter requires non-null value";
735 php_firebird_error_stmt_with_info(stmt, "HY105", strlen("HY105"), msg, strlen(msg));
736 return 0;
737 }
738 *var->sqlind = -1;
739 return 1;
740 }
741 ISC_QUAD quad = php_get_isc_quad_from_sqldata(var->sqldata);
742 if (php_firebird_bind_blob(stmt, &quad, parameter) != 0) {
743 memcpy(var->sqldata, &quad, sizeof(quad));
744 return 1;
745 }
746 return 0;
747 }
748 }
749
750 /* keep native BOOLEAN type */
751 if ((var->sqltype & ~1) == SQL_BOOLEAN) {
752 switch (Z_TYPE_P(parameter)) {
753 case IS_LONG:
754 case IS_DOUBLE:
755 case IS_TRUE:
756 case IS_FALSE:
757 *(FB_BOOLEAN*)var->sqldata = zend_is_true(parameter) ? FB_TRUE : FB_FALSE;
758 break;
759 case IS_STRING:
760 {
761 zend_long lval;
762 double dval;
763
764 if (Z_STRLEN_P(parameter) == 0) {
765 *(FB_BOOLEAN*)var->sqldata = FB_FALSE;
766 break;
767 }
768
769 switch (is_numeric_string(Z_STRVAL_P(parameter), Z_STRLEN_P(parameter), &lval, &dval, 0)) {
770 case IS_LONG:
771 *(FB_BOOLEAN*)var->sqldata = (lval != 0) ? FB_TRUE : FB_FALSE;
772 break;
773 case IS_DOUBLE:
774 *(FB_BOOLEAN*)var->sqldata = (dval != 0) ? FB_TRUE : FB_FALSE;
775 break;
776 default:
777 if (!zend_binary_strncasecmp(Z_STRVAL_P(parameter), Z_STRLEN_P(parameter), "true", 4, 4)) {
778 *(FB_BOOLEAN*)var->sqldata = FB_TRUE;
779 } else if (!zend_binary_strncasecmp(Z_STRVAL_P(parameter), Z_STRLEN_P(parameter), "false", 5, 5)) {
780 *(FB_BOOLEAN*)var->sqldata = FB_FALSE;
781 } else {
782 const char *msg = "Cannot convert string to boolean";
783 php_firebird_error_stmt_with_info(stmt, "HY105", strlen("HY105"), msg, strlen(msg));
784 return 0;
785 }
786
787 }
788 }
789 break;
790 case IS_NULL:
791 *var->sqlind = -1;
792 break;
793 default:
794 {
795 const char *msg = "Binding arrays/objects is not supported";
796 php_firebird_error_stmt_with_info(stmt, "HY105", strlen("HY105"), msg, strlen(msg));
797 }
798 return 0;
799 }
800 break;
801 }
802
803 /* check if a NULL should be inserted */
804 switch (Z_TYPE_P(parameter)) {
805 int force_null;
806
807 case IS_LONG:
808 /* keep the allow-NULL flag */
809 var->sqltype = (sizeof(zend_long) == 8 ? SQL_INT64 : SQL_LONG) | (var->sqltype & 1);
810 var->sqldata = (void*)&Z_LVAL_P(parameter);
811 var->sqllen = sizeof(zend_long);
812 break;
813 case IS_DOUBLE:
814 /* keep the allow-NULL flag */
815 var->sqltype = SQL_DOUBLE | (var->sqltype & 1);
816 var->sqldata = (void*)&Z_DVAL_P(parameter);
817 var->sqllen = sizeof(double);
818 break;
819 case IS_STRING:
820 force_null = 0;
821
822 /* for these types, an empty string can be handled like a NULL value */
823 switch (var->sqltype & ~1) {
824 case SQL_SHORT:
825 case SQL_LONG:
826 case SQL_INT64:
827 case SQL_FLOAT:
828 case SQL_DOUBLE:
829 case SQL_TIMESTAMP:
830 case SQL_TYPE_DATE:
831 case SQL_TYPE_TIME:
832#if FB_API_VER >= 40
833 case SQL_INT128:
834 case SQL_DEC16:
835 case SQL_DEC34:
836 case SQL_TIMESTAMP_TZ:
837 case SQL_TIME_TZ:
838#endif
839 force_null = (Z_STRLEN_P(parameter) == 0);
840 }
841 if (!force_null) {
842 /* keep the allow-NULL flag */
843 var->sqltype = SQL_TEXT | (var->sqltype & 1);
844 var->sqldata = Z_STRVAL_P(parameter);
845 var->sqllen = Z_STRLEN_P(parameter);
846 break;
847 }
849 case IS_NULL:
850 /* complain if this field doesn't allow NULL values */
851 if (~var->sqltype & 1) {
852 const char *msg = "Parameter requires non-null value";
853 php_firebird_error_stmt_with_info(stmt, "HY105", strlen("HY105"), msg, strlen(msg));
854 return 0;
855 }
856 *var->sqlind = -1;
857 break;
858 default:
859 {
860 const char *msg = "Binding arrays/objects is not supported";
861 php_firebird_error_stmt_with_info(stmt, "HY105", strlen("HY105"), msg, strlen(msg));
862 }
863 return 0;
864 }
865 break;
866
868 if (param->paramno == -1) {
869 return 0;
870 }
871 if (param->is_param) {
872 break;
873 }
874 if (Z_ISREF(param->parameter)) {
875 parameter = Z_REFVAL(param->parameter);
876 } else {
877 parameter = &param->parameter;
878 }
879 zval_ptr_dtor(parameter);
880 ZVAL_NULL(parameter);
881 return pdo_firebird_stmt_get_col(stmt, param->paramno, parameter, NULL);
882 default:
883 ;
884 }
885 return 1;
886}
887/* }}} */
888
889static int pdo_firebird_stmt_set_attribute(pdo_stmt_t *stmt, zend_long attr, zval *val) /* {{{ */
890{
892
893 switch (attr) {
894 default:
895 return 0;
897 if (!try_convert_to_string(val)) {
898 return 0;
899 }
900
901 if (isc_dsql_set_cursor_name(S->H->isc_status, &S->stmt, Z_STRVAL_P(val),0)) {
903 return 0;
904 }
905 strlcpy(S->name, Z_STRVAL_P(val), sizeof(S->name));
906 break;
907 }
908 return 1;
909}
910/* }}} */
911
912static int pdo_firebird_stmt_get_attribute(pdo_stmt_t *stmt, zend_long attr, zval *val) /* {{{ */
913{
915
916 switch (attr) {
917 default:
918 return 0;
920 if (*S->name) {
921 ZVAL_STRING(val, S->name);
922 } else {
923 ZVAL_NULL(val);
924 }
925 break;
926 }
927 return 1;
928}
929/* }}} */
930
931static int pdo_firebird_stmt_cursor_closer(pdo_stmt_t *stmt) /* {{{ */
932{
934
935 /* close the statement handle */
936 if ((*S->name || S->cursor_open) && isc_dsql_free_statement(S->H->isc_status, &S->stmt, DSQL_close)) {
938 return 0;
939 }
940 *S->name = 0;
941 S->cursor_open = 0;
942 return 1;
943}
944/* }}} */
945
946
947const struct pdo_stmt_methods firebird_stmt_methods = { /* {{{ */
948 pdo_firebird_stmt_dtor,
949 pdo_firebird_stmt_execute,
950 pdo_firebird_stmt_fetch,
951 pdo_firebird_stmt_describe,
952 pdo_firebird_stmt_get_col,
953 pdo_firebird_stmt_param_hook,
954 pdo_firebird_stmt_set_attribute,
955 pdo_firebird_stmt_get_attribute,
956 pdo_firebird_stmt_get_column_meta,
957 NULL, /* next_rowset_func */
958 pdo_firebird_stmt_cursor_closer
959};
960/* }}} */
size_t len
Definition apprentice.c:174
sizeof(Countable|array $value, int $mode=COUNT_NORMAL)
stat(string $filename)
error($message)
Definition ext_skel.php:22
zend_ffi_type * type
Definition ffi.c:3812
zend_long n
Definition ffi.c:4979
memcpy(ptr1, ptr2, size)
new_type attr
Definition ffi.c:4364
zval * val
Definition ffi.c:4262
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
bool php_firebird_commit_transaction(pdo_dbh_t *dbh, bool retain)
const struct pdo_stmt_methods firebird_stmt_methods
#define READ_AND_RETURN_USING_MEMCPY(type, sqldata)
zend_long offset
#define NULL
Definition gdcache.h:45
#define S(s, l, r)
Definition hash_gost.c:121
sprintf("0x%X", $numelems)
#define H(x, y, z)
Definition md5.c:146
const SQL_TYPE_TIME
const SQL_TIMESTAMP
const SQL_DOUBLE
const SQL_FLOAT
const SQL_TYPE_DATE
#define memmove(a, b, c)
void pdo_raise_impl_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, pdo_error_type sqlstate, const char *supp)
Definition pdo_dbh.c:68
ISC_DATE fb_encode_date(unsigned year, unsigned month, unsigned day)
ISC_TIME fb_encode_time(unsigned hours, unsigned minutes, unsigned seconds, unsigned fractions)
#define strlcpy
Definition php.h:159
strftime(string $format, ?int $timestamp=null)
time()
struct _pdo_stmt_t pdo_stmt_t
pdo_param_type
@ PDO_PARAM_BOOL
@ PDO_PARAM_INT
@ PDO_PARAM_STR
pdo_param_event
@ PDO_PARAM_EVT_ALLOC
@ PDO_PARAM_EVT_EXEC_PRE
@ PDO_PARAM_EVT_FETCH_POST
@ PDO_PARAM_EVT_FREE
pdo_fetch_orientation
@ PDO_ATTR_CURSOR_NAME
#define PDO_FIREBIRD_HANDLE_INITIALIZER
#define PDO_FB_DEF_DATE_FMT
#define php_firebird_error_stmt(s)
#define PDO_FB_SQLDA_VERSION
#define PDO_FB_DEF_TIMESTAMP_FMT
#define LL_MASK
#define PDO_FB_DEF_TIME_FMT
#define php_firebird_error_stmt_with_info(s, e, el, m, ml)
#define const_cast(s)
#define LL_LIT(lit)
#define min(a, b)
struct _php_stream php_stream
Definition php_streams.h:96
#define PHP_STREAM_COPY_ALL
#define php_stream_copy_to_mem(src, maxlen, persistent)
#define php_stream_from_zval_no_verify(xstr, pzval)
char * msg
Definition phpdbg.h:289
zend_constant * data
zend_ulong precision
zend_string * name
ZEND_API zend_string * zend_strpprintf(size_t max_len, const char *format,...)
Definition zend.c:353
ZEND_API zend_string * zend_strpprintf_unchecked(size_t max_len, const char *format,...)
Definition zend.c:365
#define ZVAL_STRING(z, s)
Definition zend_API.h:956
#define ZVAL_STRINGL(z, s, l)
Definition zend_API.h:952
#define ZVAL_STRINGL_FAST(z, s, l)
Definition zend_API.h:983
#define array_init(arg)
Definition zend_API.h:537
#define ecalloc(nmemb, size)
Definition zend_alloc.h:158
#define efree(ptr)
Definition zend_alloc.h:155
#define FREE_HASHTABLE(ht)
Definition zend_alloc.h:234
#define emalloc(size)
Definition zend_alloc.h:151
struct _zval_struct zval
strlen(string $string)
#define strncasecmp(s1, s2, n)
#define EG(v)
ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht)
Definition zend_hash.c:1727
ZEND_API zval *ZEND_FASTCALL zend_hash_find(const HashTable *ht, zend_string *key)
Definition zend_hash.c:2668
int32_t zend_long
Definition zend_long.h:42
uint32_t zend_ulong
Definition zend_long.h:43
struct _zend_string zend_string
#define IS_OBJ_VALID(o)
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op)
ZEND_API zend_string *ZEND_FASTCALL zval_get_string_func(zval *op)
ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length)
#define ZEND_FALLTHROUGH
#define zend_always_inline
#define EMPTY_SWITCH_DEFAULT_CASE()
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_MAX_LEN
#define ZSTR_EMPTY_ALLOC()
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
#define dval(x)
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define IS_TRUE
Definition zend_types.h:603
#define ZVAL_STR(z, s)
#define IS_FALSE
Definition zend_types.h:602
#define Z_STRVAL_P(zval_p)
Definition zend_types.h:975
#define ZVAL_NULL(z)
#define ZVAL_LONG(z, l)
#define IS_STRING
Definition zend_types.h:606
#define IS_RESOURCE
Definition zend_types.h:609
#define IS_OBJ_FREE_CALLED
Definition zend_types.h:829
#define IS_DOUBLE
Definition zend_types.h:605
#define Z_ISUNDEF(zval)
Definition zend_types.h:956
#define Z_REFVAL(zval)
#define Z_STRLEN_P(zval_p)
Definition zend_types.h:978
#define IS_NULL
Definition zend_types.h:601
#define Z_STRVAL(zval)
Definition zend_types.h:974
#define Z_STRLEN(zval)
Definition zend_types.h:977
#define IS_LONG
Definition zend_types.h:604
#define Z_OBJ_HANDLE(zval)
Definition zend_types.h:998
#define Z_ISREF(zval)
Definition zend_types.h:953
#define Z_DVAL_P(zval_p)
Definition zend_types.h:969
#define Z_LVAL_P(zval_p)
Definition zend_types.h:966
#define ZVAL_COPY_VALUE(z, v)
#define OBJ_FLAGS(obj)
Definition zend_types.h:831
#define ZVAL_BOOL(z, b)
#define Z_OBJ(zval)
Definition zend_types.h:989
ZEND_API void zval_ptr_dtor(zval *zval_ptr)
zval retval
zval * return_value
bool result