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"
25 #include "ext/pdo/php_pdo_driver.h"
26 #include "php_pdo_firebird.h"
27 #include "php_pdo_firebird_int.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
php_get_isc_int64_from_sqldata(const ISC_SCHAR * sqldata)38 static 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
php_get_isc_long_from_sqldata(const ISC_SCHAR * sqldata)43 static 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
php_get_double_from_sqldata(const ISC_SCHAR * sqldata)48 static zend_always_inline double php_get_double_from_sqldata(const ISC_SCHAR *sqldata)
49 {
50 READ_AND_RETURN_USING_MEMCPY(double, sqldata);
51 }
52
php_get_float_from_sqldata(const ISC_SCHAR * sqldata)53 static zend_always_inline float php_get_float_from_sqldata(const ISC_SCHAR *sqldata)
54 {
55 READ_AND_RETURN_USING_MEMCPY(float, sqldata);
56 }
57
php_get_isc_timestamp_from_sqldata(const ISC_SCHAR * sqldata)58 static 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
php_get_isc_quad_from_sqldata(const ISC_SCHAR * sqldata)63 static 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
php_get_isc_time_tz_from_sqldata(const ISC_SCHAR * sqldata)70 static 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
php_get_isc_timestamp_tz_from_sqldata(const ISC_SCHAR * sqldata)75 static 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 */
get_formatted_time_tz(pdo_stmt_t * stmt,const ISC_TIME_TZ * timeTz,zval * result)81 static int get_formatted_time_tz(pdo_stmt_t *stmt, const ISC_TIME_TZ* timeTz, zval *result)
82 {
83 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
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 */
get_formatted_timestamp_tz(pdo_stmt_t * stmt,const ISC_TIMESTAMP_TZ * timestampTz,zval * result)109 static int get_formatted_timestamp_tz(pdo_stmt_t *stmt, const ISC_TIMESTAMP_TZ* timestampTz, zval *result)
110 {
111 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
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 */
php_firebird_free_sqlda(XSQLDA const * sqlda)141 static 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 */
pdo_firebird_stmt_dtor(pdo_stmt_t * stmt)156 static int pdo_firebird_stmt_dtor(pdo_stmt_t *stmt) /* {{{ */
157 {
158 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
159 int result = 1;
160
161 /* release the statement */
162 if (isc_dsql_free_statement(S->H->isc_status, &S->stmt, DSQL_drop)) {
163 php_firebird_error_stmt(stmt);
164 result = 0;
165 }
166
167 zend_hash_destroy(S->named_params);
168 FREE_HASHTABLE(S->named_params);
169
170 /* clean up the input descriptor */
171 if (S->in_sqlda) {
172 php_firebird_free_sqlda(S->in_sqlda);
173 efree(S->in_sqlda);
174 }
175
176 php_firebird_free_sqlda(&S->out_sqlda);
177 efree(S);
178
179 return result;
180 }
181 /* }}} */
182
183 /* called by PDO to execute a prepared query */
pdo_firebird_stmt_execute(pdo_stmt_t * stmt)184 static int pdo_firebird_stmt_execute(pdo_stmt_t *stmt) /* {{{ */
185 {
186 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
187 pdo_firebird_db_handle *H = S->H;
188 zend_ulong affected_rows = 0;
189 static char info_count[] = {isc_info_sql_records};
190 char result[64];
191
192 do {
193 /* named or open cursors should be closed first */
194 if ((*S->name || S->cursor_open) && isc_dsql_free_statement(H->isc_status, &S->stmt, DSQL_close)) {
195 break;
196 }
197 S->cursor_open = 0;
198
199 /* allocate storage for the output data */
200 if (S->out_sqlda.sqld) {
201 unsigned int i;
202 for (i = 0; i < S->out_sqlda.sqld; i++) {
203 XSQLVAR *var = &S->out_sqlda.sqlvar[i];
204 if (var->sqlind) {
205 efree(var->sqlind);
206 }
207 var->sqlind = (void*)ecalloc(1, var->sqllen + 2 * sizeof(short));
208 var->sqldata = &((char*)var->sqlind)[sizeof(short)];
209 }
210 }
211
212 if (S->statement_type == isc_info_sql_stmt_exec_procedure) {
213 if (isc_dsql_execute2(H->isc_status, &H->tr, &S->stmt, PDO_FB_SQLDA_VERSION, S->in_sqlda, &S->out_sqlda)) {
214 break;
215 }
216 } else if (isc_dsql_execute(H->isc_status, &H->tr, &S->stmt, PDO_FB_SQLDA_VERSION, S->in_sqlda)) {
217 break;
218 }
219
220 /* Determine how many rows have changed. In this case we are
221 * only interested in rows changed, not rows retrieved. That
222 * should be handled by the client when fetching. */
223 stmt->row_count = affected_rows;
224
225 switch (S->statement_type) {
226 case isc_info_sql_stmt_insert:
227 case isc_info_sql_stmt_update:
228 case isc_info_sql_stmt_delete:
229 case isc_info_sql_stmt_exec_procedure:
230 if (isc_dsql_sql_info(H->isc_status, &S->stmt, sizeof ( info_count),
231 info_count, sizeof(result), result)) {
232 break;
233 }
234 if (result[0] == isc_info_sql_records) {
235 unsigned i = 3, result_size = isc_vax_integer(&result[1], 2);
236 if (result_size > sizeof(result)) {
237 goto error;
238 }
239 while (result[i] != isc_info_end && i < result_size) {
240 short len = (short) isc_vax_integer(&result[i + 1], 2);
241 if (len != 1 && len != 2 && len != 4) {
242 goto error;
243 }
244 if (result[i] != isc_info_req_select_count) {
245 affected_rows += isc_vax_integer(&result[i + 3], len);
246 }
247 i += len + 3;
248 }
249 stmt->row_count = affected_rows;
250 }
251 /* TODO Dead code or assert one of the previous cases are hit? */
252 default:
253 ;
254 }
255
256 if (stmt->dbh->auto_commit && !S->H->in_manually_txn && !php_firebird_commit_transaction(stmt->dbh, /* retain */ true)) {
257 break;
258 }
259
260 *S->name = 0;
261 S->cursor_open = S->out_sqlda.sqln && (S->statement_type != isc_info_sql_stmt_exec_procedure);
262 S->exhausted = !S->out_sqlda.sqln; /* There are data to fetch */
263
264 return 1;
265 } while (0);
266
267 error:
268 php_firebird_error_stmt(stmt);
269
270 return 0;
271 }
272 /* }}} */
273
274 /* called by PDO to fetch the next row from a statement */
pdo_firebird_stmt_fetch(pdo_stmt_t * stmt,enum pdo_fetch_orientation ori,zend_long offset)275 static int pdo_firebird_stmt_fetch(pdo_stmt_t *stmt, /* {{{ */
276 enum pdo_fetch_orientation ori, zend_long offset)
277 {
278 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
279 pdo_firebird_db_handle *H = S->H;
280
281 if (!stmt->executed) {
282 const char *msg = "Cannot fetch from a closed cursor";
283 php_firebird_error_stmt_with_info(stmt, "HY000", strlen("HY000"), msg, strlen(msg));
284 } else if (!S->exhausted) {
285 if (S->statement_type == isc_info_sql_stmt_exec_procedure) {
286 stmt->row_count = 1;
287 S->exhausted = 1;
288 return 1;
289 }
290 if (isc_dsql_fetch(H->isc_status, &S->stmt, PDO_FB_SQLDA_VERSION, &S->out_sqlda)) {
291 if (H->isc_status[0] && H->isc_status[1]) {
292 php_firebird_error_stmt(stmt);
293 }
294 S->exhausted = 1;
295 return 0;
296 }
297 stmt->row_count++;
298 return 1;
299 }
300 return 0;
301 }
302 /* }}} */
303
304 /* called by PDO to retrieve information about the fields being returned */
pdo_firebird_stmt_describe(pdo_stmt_t * stmt,int colno)305 static int pdo_firebird_stmt_describe(pdo_stmt_t *stmt, int colno) /* {{{ */
306 {
307 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
308 struct pdo_column_data *col = &stmt->columns[colno];
309 XSQLVAR *var = &S->out_sqlda.sqlvar[colno];
310 int colname_len;
311 char *cp;
312
313 if ((var->sqltype & ~1) == SQL_TEXT) {
314 var->sqltype = SQL_VARYING | (var->sqltype & 1);
315 }
316 colname_len = (S->H->fetch_table_names && var->relname_length)
317 ? (var->aliasname_length + var->relname_length + 1)
318 : (var->aliasname_length);
319 col->precision = -var->sqlscale;
320 col->maxlen = var->sqllen;
321 col->name = zend_string_alloc(colname_len, 0);
322 cp = ZSTR_VAL(col->name);
323 if (colname_len > var->aliasname_length) {
324 memmove(cp, var->relname, var->relname_length);
325 cp += var->relname_length;
326 *cp++ = '.';
327 }
328 memmove(cp, var->aliasname, var->aliasname_length);
329 *(cp+var->aliasname_length) = '\0';
330
331 return 1;
332 }
333 /* }}} */
334
pdo_firebird_stmt_get_column_meta(pdo_stmt_t * stmt,zend_long colno,zval * return_value)335 static int pdo_firebird_stmt_get_column_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value)
336 {
337 pdo_firebird_stmt *S = (pdo_firebird_stmt *) stmt->driver_data;
338 XSQLVAR *var = &S->out_sqlda.sqlvar[colno];
339
340 enum pdo_param_type param_type;
341 if (var->sqlscale < 0) {
342 param_type = PDO_PARAM_STR;
343 } else {
344 switch (var->sqltype & ~1) {
345 case SQL_SHORT:
346 case SQL_LONG:
347 #if SIZEOF_ZEND_LONG >= 8
348 case SQL_INT64:
349 #endif
350 param_type = PDO_PARAM_INT;
351 break;
352 case SQL_BOOLEAN:
353 param_type = PDO_PARAM_BOOL;
354 break;
355 default:
356 param_type = PDO_PARAM_STR;
357 break;
358 }
359 }
360
361 array_init(return_value);
362 add_assoc_long(return_value, "pdo_type", param_type);
363 return 1;
364 }
365
366 /* fetch a blob into a fetch buffer */
php_firebird_fetch_blob(pdo_stmt_t * stmt,int colno,zval * result,ISC_QUAD * blob_id)367 static int php_firebird_fetch_blob(pdo_stmt_t *stmt, int colno, zval *result, ISC_QUAD *blob_id)
368 {
369 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
370 pdo_firebird_db_handle *H = S->H;
371 isc_blob_handle blobh = PDO_FIREBIRD_HANDLE_INITIALIZER;
372 char const bl_item = isc_info_blob_total_length;
373 char bl_info[20];
374 unsigned short i;
375 int retval = 0;
376 size_t len = 0;
377
378 if (isc_open_blob(H->isc_status, &H->db, &H->tr, &blobh, blob_id)) {
379 php_firebird_error_stmt(stmt);
380 return 0;
381 }
382
383 if (isc_blob_info(H->isc_status, &blobh, 1, const_cast(&bl_item),
384 sizeof(bl_info), bl_info)) {
385 php_firebird_error_stmt(stmt);
386 goto fetch_blob_end;
387 }
388
389 /* find total length of blob's data */
390 for (i = 0; i < sizeof(bl_info); ) {
391 unsigned short item_len;
392 char item = bl_info[i++];
393
394 if (item == isc_info_end || item == isc_info_truncated || item == isc_info_error
395 || i >= sizeof(bl_info)) {
396 const char *msg = "Couldn't determine BLOB size";
397 php_firebird_error_stmt_with_info(stmt, "HY000", strlen("HY000"), msg, strlen(msg));
398 goto fetch_blob_end;
399 }
400
401 item_len = (unsigned short) isc_vax_integer(&bl_info[i], 2);
402
403 if (item == isc_info_blob_total_length) {
404 len = isc_vax_integer(&bl_info[i+2], item_len);
405 break;
406 }
407 i += item_len+2;
408 }
409
410 /* we've found the blob's length, now fetch! */
411
412 if (len) {
413 zend_ulong cur_len;
414 unsigned short seg_len;
415 ISC_STATUS stat;
416 zend_string *str;
417
418 /* prevent overflow */
419 if (len > ZSTR_MAX_LEN) {
420 result = 0;
421 goto fetch_blob_end;
422 }
423
424 str = zend_string_alloc(len, 0);
425
426 for (cur_len = stat = 0; (!stat || stat == isc_segment) && cur_len < len; cur_len += seg_len) {
427
428 unsigned short chunk_size = (len - cur_len) > USHRT_MAX ? USHRT_MAX
429 : (unsigned short)(len - cur_len);
430
431 stat = isc_get_segment(H->isc_status, &blobh, &seg_len, chunk_size, ZSTR_VAL(str) + cur_len);
432 }
433
434 ZSTR_VAL(str)[len] = '\0';
435 ZVAL_STR(result, str);
436
437 if (H->isc_status[0] == 1 && (stat != 0 && stat != isc_segstr_eof && stat != isc_segment)) {
438 const char *msg = "Error reading from BLOB";
439 php_firebird_error_stmt_with_info(stmt, "HY000", strlen("HY000"), msg, strlen(msg));
440 goto fetch_blob_end;
441 }
442 }
443 retval = 1;
444
445 fetch_blob_end:
446 if (isc_close_blob(H->isc_status, &blobh)) {
447 php_firebird_error_stmt(stmt);
448 return 0;
449 }
450 return retval;
451 }
452 /* }}} */
453
pdo_firebird_stmt_get_col(pdo_stmt_t * stmt,int colno,zval * result,enum pdo_param_type * type)454 static int pdo_firebird_stmt_get_col(
455 pdo_stmt_t *stmt, int colno, zval *result, enum pdo_param_type *type)
456 {
457 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
458 XSQLVAR const *var = &S->out_sqlda.sqlvar[colno];
459
460 if (*var->sqlind == -1) {
461 ZVAL_NULL(result);
462 } else {
463 if (var->sqlscale < 0) {
464 static ISC_INT64 const scales[] = { 1, 10, 100, 1000,
465 10000,
466 100000,
467 1000000,
468 10000000,
469 100000000,
470 1000000000,
471 LL_LIT(10000000000),
472 LL_LIT(100000000000),
473 LL_LIT(1000000000000),
474 LL_LIT(10000000000000),
475 LL_LIT(100000000000000),
476 LL_LIT(1000000000000000),
477 LL_LIT(10000000000000000),
478 LL_LIT(100000000000000000),
479 LL_LIT(1000000000000000000)
480 };
481 ISC_INT64 n, f = scales[-var->sqlscale];
482 zend_string *str;
483
484 switch (var->sqltype & ~1) {
485 case SQL_SHORT:
486 n = *(short*)var->sqldata;
487 break;
488 case SQL_LONG:
489 n = php_get_isc_long_from_sqldata(var->sqldata);
490 break;
491 case SQL_INT64:
492 n = php_get_isc_int64_from_sqldata(var->sqldata);
493 break;
494 case SQL_DOUBLE:
495 break;
496 EMPTY_SWITCH_DEFAULT_CASE()
497 }
498
499 if ((var->sqltype & ~1) == SQL_DOUBLE) {
500 str = zend_strpprintf(0, "%.*F", -var->sqlscale, php_get_double_from_sqldata(var->sqldata));
501 } else if (n >= 0) {
502 str = zend_strpprintf(0, "%" LL_MASK "d.%0*" LL_MASK "d",
503 n / f, -var->sqlscale, n % f);
504 } else if (n <= -f) {
505 str = zend_strpprintf(0, "%" LL_MASK "d.%0*" LL_MASK "d",
506 n / f, -var->sqlscale, -n % f);
507 } else {
508 str = zend_strpprintf(0, "-0.%0*" LL_MASK "d", -var->sqlscale, -n % f);
509 }
510 ZVAL_STR(result, str);
511 } else {
512 switch (var->sqltype & ~1) {
513 struct tm t;
514 char *fmt;
515
516 case SQL_VARYING:
517 ZVAL_STRINGL_FAST(result, &var->sqldata[2], *(short*)var->sqldata);
518 break;
519 case SQL_TEXT:
520 ZVAL_STRINGL_FAST(result, var->sqldata, var->sqllen);
521 break;
522 case SQL_SHORT:
523 ZVAL_LONG(result, *(short*)var->sqldata);
524 break;
525 case SQL_LONG:
526 ZVAL_LONG(result, php_get_isc_long_from_sqldata(var->sqldata));
527 break;
528 case SQL_INT64:
529 #if SIZEOF_ZEND_LONG >= 8
530 ZVAL_LONG(result, php_get_isc_int64_from_sqldata(var->sqldata));
531 #else
532 ZVAL_STR(result, zend_strpprintf(0, "%" LL_MASK "d", php_get_isc_int64_from_sqldata(var->sqldata)));
533 #endif
534 break;
535 case SQL_FLOAT:
536 /* TODO: Why is this not returned as the native type? */
537 ZVAL_STR(result, zend_strpprintf_unchecked(0, "%.8H", php_get_float_from_sqldata(var->sqldata)));
538 break;
539 case SQL_DOUBLE:
540 /* TODO: Why is this not returned as the native type? */
541 ZVAL_STR(result, zend_strpprintf_unchecked(0, "%.16H", php_get_double_from_sqldata(var->sqldata)));
542 break;
543 case SQL_BOOLEAN:
544 ZVAL_BOOL(result, *(FB_BOOLEAN*)var->sqldata);
545 break;
546 case SQL_TYPE_DATE:
547 isc_decode_sql_date((ISC_DATE*)var->sqldata, &t);
548 fmt = S->H->date_format ? S->H->date_format : PDO_FB_DEF_DATE_FMT;
549 if (0) {
550 case SQL_TYPE_TIME:
551 isc_decode_sql_time((ISC_TIME*)var->sqldata, &t);
552 fmt = S->H->time_format ? S->H->time_format : PDO_FB_DEF_TIME_FMT;
553 } else if (0) {
554 case SQL_TIMESTAMP:
555 {
556 ISC_TIMESTAMP timestamp = php_get_isc_timestamp_from_sqldata(var->sqldata);
557 isc_decode_timestamp(×tamp, &t);
558 }
559 fmt = S->H->timestamp_format ? S->H->timestamp_format : PDO_FB_DEF_TIMESTAMP_FMT;
560 }
561 /* convert the timestamp into a string */
562 char buf[80];
563 size_t len = strftime(buf, sizeof(buf), fmt, &t);
564 ZVAL_STRINGL(result, buf, len);
565 break;
566 #if FB_API_VER >= 40
567 case SQL_TIME_TZ: {
568 ISC_TIME_TZ time = php_get_isc_time_tz_from_sqldata(var->sqldata);
569 return get_formatted_time_tz(stmt, &time, result);
570 }
571 case SQL_TIMESTAMP_TZ: {
572 ISC_TIMESTAMP_TZ ts = php_get_isc_timestamp_tz_from_sqldata(var->sqldata);
573 return get_formatted_timestamp_tz(stmt, &ts, result);
574 }
575 #endif
576 case SQL_BLOB: {
577 ISC_QUAD quad = php_get_isc_quad_from_sqldata(var->sqldata);
578 return php_firebird_fetch_blob(stmt, colno, result, &quad);
579 }
580 }
581 }
582 }
583 return 1;
584 }
585
php_firebird_bind_blob(pdo_stmt_t * stmt,ISC_QUAD * blob_id,zval * param)586 static int php_firebird_bind_blob(pdo_stmt_t *stmt, ISC_QUAD *blob_id, zval *param)
587 {
588 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
589 pdo_firebird_db_handle *H = S->H;
590 isc_blob_handle h = PDO_FIREBIRD_HANDLE_INITIALIZER;
591 zval data;
592 zend_ulong put_cnt = 0, rem_cnt;
593 unsigned short chunk_size;
594 int result = 1;
595
596 if (isc_create_blob(H->isc_status, &H->db, &H->tr, &h, blob_id)) {
597 php_firebird_error_stmt(stmt);
598 return 0;
599 }
600
601 if (Z_TYPE_P(param) != IS_STRING) {
602 ZVAL_STR(&data, zval_get_string_func(param));
603 } else {
604 ZVAL_COPY_VALUE(&data, param);
605 }
606
607 for (rem_cnt = Z_STRLEN(data); rem_cnt > 0; rem_cnt -= chunk_size) {
608 chunk_size = rem_cnt > USHRT_MAX ? USHRT_MAX : (unsigned short)rem_cnt;
609 if (isc_put_segment(H->isc_status, &h, chunk_size, &Z_STRVAL(data)[put_cnt])) {
610 php_firebird_error_stmt(stmt);
611 result = 0;
612 break;
613 }
614 put_cnt += chunk_size;
615 }
616
617 if (Z_TYPE_P(param) != IS_STRING) {
618 zval_ptr_dtor_str(&data);
619 }
620
621 if (isc_close_blob(H->isc_status, &h)) {
622 php_firebird_error_stmt(stmt);
623 return 0;
624 }
625 return result;
626 }
627
pdo_firebird_stmt_param_hook(pdo_stmt_t * stmt,struct pdo_bound_param_data * param,enum pdo_param_event event_type)628 static int pdo_firebird_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, /* {{{ */
629 enum pdo_param_event event_type)
630 {
631 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
632 XSQLDA *sqlda = param->is_param ? S->in_sqlda : &S->out_sqlda;
633 XSQLVAR *var;
634
635 if (event_type == PDO_PARAM_EVT_FREE) { /* not used */
636 return 1;
637 }
638
639 if (!sqlda || param->paramno >= sqlda->sqld) {
640 const char *msg = "Invalid parameter index";
641 php_firebird_error_stmt_with_info(stmt, "HY093", strlen("HY093"), msg, strlen(msg));
642 return 0;
643 }
644 if (param->is_param && param->paramno == -1) {
645 zval *index;
646
647 /* try to determine the index by looking in the named_params hash */
648 if ((index = zend_hash_find(S->named_params, param->name)) != NULL) {
649 param->paramno = Z_LVAL_P(index);
650 } else {
651 /* ... or by looking in the input descriptor */
652 int i;
653
654 for (i = 0; i < sqlda->sqld; ++i) {
655 XSQLVAR *var = &sqlda->sqlvar[i];
656
657 if ((var->aliasname_length && !strncasecmp(ZSTR_VAL(param->name), var->aliasname,
658 min(ZSTR_LEN(param->name), var->aliasname_length)))
659 || (var->sqlname_length && !strncasecmp(ZSTR_VAL(param->name), var->sqlname,
660 min(ZSTR_LEN(param->name), var->sqlname_length)))) {
661 param->paramno = i;
662 break;
663 }
664 }
665 if (i >= sqlda->sqld) {
666 const char *msg = "Invalid parameter name";
667 php_firebird_error_stmt_with_info(stmt, "HY093", strlen("HY093"), msg, strlen(msg));
668 return 0;
669 }
670 }
671 }
672
673 var = &sqlda->sqlvar[param->paramno];
674
675 switch (event_type) {
676 zval *parameter;
677
678 case PDO_PARAM_EVT_ALLOC:
679 if (param->is_param) {
680 /* allocate the parameter */
681 if (var->sqlind) {
682 efree(var->sqlind);
683 }
684 var->sqlind = (void*)emalloc(var->sqllen + 2*sizeof(short));
685 var->sqldata = &((char*)var->sqlind)[sizeof(short)];
686 }
687 break;
688
689 case PDO_PARAM_EVT_EXEC_PRE:
690 if (!param->is_param) {
691 break;
692 }
693
694 *var->sqlind = 0;
695 if (Z_ISREF(param->parameter)) {
696 parameter = Z_REFVAL(param->parameter);
697 } else {
698 parameter = ¶m->parameter;
699 }
700
701 if (Z_TYPE_P(parameter) == IS_RESOURCE) {
702 php_stream *stm = NULL;
703
704 php_stream_from_zval_no_verify(stm, parameter);
705 if (stm) {
706 zend_string *mem = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
707 zval_ptr_dtor(parameter);
708 ZVAL_STR(parameter, mem ? mem : ZSTR_EMPTY_ALLOC());
709 } else {
710 pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource");
711 return 0;
712 }
713 }
714
715 switch (var->sqltype & ~1) {
716 case SQL_ARRAY:
717 {
718 const char *msg = "Cannot bind to array field";
719 php_firebird_error_stmt_with_info(stmt, "HY000", strlen("HY000"), msg, strlen(msg));
720 }
721 return 0;
722
723 case SQL_BLOB: {
724 if (Z_TYPE_P(parameter) == IS_NULL) {
725 /* Check if field allow NULL values */
726 if (~var->sqltype & 1) {
727 const char *msg = "Parameter requires non-null value";
728 php_firebird_error_stmt_with_info(stmt, "HY105", strlen("HY105"), msg, strlen(msg));
729 return 0;
730 }
731 *var->sqlind = -1;
732 return 1;
733 }
734 ISC_QUAD quad = php_get_isc_quad_from_sqldata(var->sqldata);
735 if (php_firebird_bind_blob(stmt, &quad, parameter) != 0) {
736 memcpy(var->sqldata, &quad, sizeof(quad));
737 return 1;
738 }
739 return 0;
740 }
741 }
742
743 /* keep native BOOLEAN type */
744 if ((var->sqltype & ~1) == SQL_BOOLEAN) {
745 switch (Z_TYPE_P(parameter)) {
746 case IS_LONG:
747 case IS_DOUBLE:
748 case IS_TRUE:
749 case IS_FALSE:
750 *(FB_BOOLEAN*)var->sqldata = zend_is_true(parameter) ? FB_TRUE : FB_FALSE;
751 break;
752 case IS_STRING:
753 {
754 zend_long lval;
755 double dval;
756
757 if (Z_STRLEN_P(parameter) == 0) {
758 *(FB_BOOLEAN*)var->sqldata = FB_FALSE;
759 break;
760 }
761
762 switch (is_numeric_string(Z_STRVAL_P(parameter), Z_STRLEN_P(parameter), &lval, &dval, 0)) {
763 case IS_LONG:
764 *(FB_BOOLEAN*)var->sqldata = (lval != 0) ? FB_TRUE : FB_FALSE;
765 break;
766 case IS_DOUBLE:
767 *(FB_BOOLEAN*)var->sqldata = (dval != 0) ? FB_TRUE : FB_FALSE;
768 break;
769 default:
770 if (!zend_binary_strncasecmp(Z_STRVAL_P(parameter), Z_STRLEN_P(parameter), "true", 4, 4)) {
771 *(FB_BOOLEAN*)var->sqldata = FB_TRUE;
772 } else if (!zend_binary_strncasecmp(Z_STRVAL_P(parameter), Z_STRLEN_P(parameter), "false", 5, 5)) {
773 *(FB_BOOLEAN*)var->sqldata = FB_FALSE;
774 } else {
775 const char *msg = "Cannot convert string to boolean";
776 php_firebird_error_stmt_with_info(stmt, "HY105", strlen("HY105"), msg, strlen(msg));
777 return 0;
778 }
779
780 }
781 }
782 break;
783 case IS_NULL:
784 *var->sqlind = -1;
785 break;
786 default:
787 {
788 const char *msg = "Binding arrays/objects is not supported";
789 php_firebird_error_stmt_with_info(stmt, "HY105", strlen("HY105"), msg, strlen(msg));
790 }
791 return 0;
792 }
793 break;
794 }
795
796 /* check if a NULL should be inserted */
797 switch (Z_TYPE_P(parameter)) {
798 int force_null;
799
800 case IS_LONG:
801 /* keep the allow-NULL flag */
802 var->sqltype = (sizeof(zend_long) == 8 ? SQL_INT64 : SQL_LONG) | (var->sqltype & 1);
803 var->sqldata = (void*)&Z_LVAL_P(parameter);
804 var->sqllen = sizeof(zend_long);
805 break;
806 case IS_DOUBLE:
807 /* keep the allow-NULL flag */
808 var->sqltype = SQL_DOUBLE | (var->sqltype & 1);
809 var->sqldata = (void*)&Z_DVAL_P(parameter);
810 var->sqllen = sizeof(double);
811 break;
812 case IS_STRING:
813 force_null = 0;
814
815 /* for these types, an empty string can be handled like a NULL value */
816 switch (var->sqltype & ~1) {
817 case SQL_SHORT:
818 case SQL_LONG:
819 case SQL_INT64:
820 case SQL_FLOAT:
821 case SQL_DOUBLE:
822 case SQL_TIMESTAMP:
823 case SQL_TYPE_DATE:
824 case SQL_TYPE_TIME:
825 #if FB_API_VER >= 40
826 case SQL_INT128:
827 case SQL_DEC16:
828 case SQL_DEC34:
829 case SQL_TIMESTAMP_TZ:
830 case SQL_TIME_TZ:
831 #endif
832 force_null = (Z_STRLEN_P(parameter) == 0);
833 }
834 if (!force_null) {
835 /* keep the allow-NULL flag */
836 var->sqltype = SQL_TEXT | (var->sqltype & 1);
837 var->sqldata = Z_STRVAL_P(parameter);
838 var->sqllen = Z_STRLEN_P(parameter);
839 break;
840 }
841 ZEND_FALLTHROUGH;
842 case IS_NULL:
843 /* complain if this field doesn't allow NULL values */
844 if (~var->sqltype & 1) {
845 const char *msg = "Parameter requires non-null value";
846 php_firebird_error_stmt_with_info(stmt, "HY105", strlen("HY105"), msg, strlen(msg));
847 return 0;
848 }
849 *var->sqlind = -1;
850 break;
851 default:
852 {
853 const char *msg = "Binding arrays/objects is not supported";
854 php_firebird_error_stmt_with_info(stmt, "HY105", strlen("HY105"), msg, strlen(msg));
855 }
856 return 0;
857 }
858 break;
859
860 case PDO_PARAM_EVT_FETCH_POST:
861 if (param->paramno == -1) {
862 return 0;
863 }
864 if (param->is_param) {
865 break;
866 }
867 if (Z_ISREF(param->parameter)) {
868 parameter = Z_REFVAL(param->parameter);
869 } else {
870 parameter = ¶m->parameter;
871 }
872 zval_ptr_dtor(parameter);
873 ZVAL_NULL(parameter);
874 return pdo_firebird_stmt_get_col(stmt, param->paramno, parameter, NULL);
875 default:
876 ;
877 }
878 return 1;
879 }
880 /* }}} */
881
pdo_firebird_stmt_set_attribute(pdo_stmt_t * stmt,zend_long attr,zval * val)882 static int pdo_firebird_stmt_set_attribute(pdo_stmt_t *stmt, zend_long attr, zval *val) /* {{{ */
883 {
884 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
885
886 switch (attr) {
887 default:
888 return 0;
889 case PDO_ATTR_CURSOR_NAME: {
890 zend_string *str_val = zval_try_get_string(val);
891 if (str_val == NULL) {
892 return 0;
893 }
894 // TODO Check cursor name does not have null bytes?
895 if (ZSTR_LEN(str_val) >= sizeof(S->name)) {
896 zend_value_error("Cursor name must not be longer than %zu bytes", sizeof(S->name) - 1);
897 zend_string_release(str_val);
898 return 0;
899 }
900
901 if (isc_dsql_set_cursor_name(S->H->isc_status, &S->stmt, ZSTR_VAL(str_val), 0)) {
902 php_firebird_error_stmt(stmt);
903 zend_string_release(str_val);
904 return 0;
905 }
906 /* Include trailing nul byte */
907 memcpy(S->name, ZSTR_VAL(str_val), ZSTR_LEN(str_val) + 1);
908 zend_string_release(str_val);
909 break;
910 }
911 }
912 return 1;
913 }
914 /* }}} */
915
pdo_firebird_stmt_get_attribute(pdo_stmt_t * stmt,zend_long attr,zval * val)916 static int pdo_firebird_stmt_get_attribute(pdo_stmt_t *stmt, zend_long attr, zval *val) /* {{{ */
917 {
918 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
919
920 switch (attr) {
921 default:
922 return 0;
923 case PDO_ATTR_CURSOR_NAME:
924 if (*S->name) {
925 ZVAL_STRING(val, S->name);
926 } else {
927 ZVAL_NULL(val);
928 }
929 break;
930 }
931 return 1;
932 }
933 /* }}} */
934
pdo_firebird_stmt_cursor_closer(pdo_stmt_t * stmt)935 static int pdo_firebird_stmt_cursor_closer(pdo_stmt_t *stmt) /* {{{ */
936 {
937 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
938
939 /* close the statement handle */
940 if ((*S->name || S->cursor_open) && isc_dsql_free_statement(S->H->isc_status, &S->stmt, DSQL_close)) {
941 php_firebird_error_stmt(stmt);
942 return 0;
943 }
944 *S->name = 0;
945 S->cursor_open = 0;
946 return 1;
947 }
948 /* }}} */
949
950
951 const struct pdo_stmt_methods firebird_stmt_methods = { /* {{{ */
952 pdo_firebird_stmt_dtor,
953 pdo_firebird_stmt_execute,
954 pdo_firebird_stmt_fetch,
955 pdo_firebird_stmt_describe,
956 pdo_firebird_stmt_get_col,
957 pdo_firebird_stmt_param_hook,
958 pdo_firebird_stmt_set_attribute,
959 pdo_firebird_stmt_get_attribute,
960 pdo_firebird_stmt_get_column_meta,
961 NULL, /* next_rowset_func */
962 pdo_firebird_stmt_cursor_closer
963 };
964 /* }}} */
965