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 "pdo/php_pdo.h"
25 #include "pdo/php_pdo_driver.h"
26 #include "php_pdo_firebird.h"
27 #include "php_pdo_firebird_int.h"
28
29 #include <time.h>
30
31 #define RECORD_ERROR(stmt) _firebird_error(NULL, stmt, __FILE__, __LINE__)
32
33 #define READ_AND_RETURN_USING_MEMCPY(type, sqldata) do { \
34 type ret; \
35 memcpy(&ret, sqldata, sizeof(ret)); \
36 return ret; \
37 } while (0);
38
get_isc_int64_from_sqldata(const ISC_SCHAR * sqldata)39 static zend_always_inline ISC_INT64 get_isc_int64_from_sqldata(const ISC_SCHAR *sqldata)
40 {
41 READ_AND_RETURN_USING_MEMCPY(ISC_INT64, sqldata);
42 }
43
get_isc_long_from_sqldata(const ISC_SCHAR * sqldata)44 static zend_always_inline ISC_LONG get_isc_long_from_sqldata(const ISC_SCHAR *sqldata)
45 {
46 READ_AND_RETURN_USING_MEMCPY(ISC_LONG, sqldata);
47 }
48
get_double_from_sqldata(const ISC_SCHAR * sqldata)49 static zend_always_inline double get_double_from_sqldata(const ISC_SCHAR *sqldata)
50 {
51 READ_AND_RETURN_USING_MEMCPY(double, sqldata);
52 }
53
get_isc_timestamp_from_sqldata(const ISC_SCHAR * sqldata)54 static zend_always_inline ISC_TIMESTAMP get_isc_timestamp_from_sqldata(const ISC_SCHAR *sqldata)
55 {
56 READ_AND_RETURN_USING_MEMCPY(ISC_TIMESTAMP, sqldata);
57 }
58
get_isc_quad_from_sqldata(const ISC_SCHAR * sqldata)59 static zend_always_inline ISC_QUAD get_isc_quad_from_sqldata(const ISC_SCHAR *sqldata)
60 {
61 READ_AND_RETURN_USING_MEMCPY(ISC_QUAD, sqldata);
62 }
63
64 /* free the allocated space for passing field values to the db and back */
free_sqlda(XSQLDA const * sqlda)65 static void free_sqlda(XSQLDA const *sqlda) /* {{{ */
66 {
67 int i;
68
69 for (i = 0; i < sqlda->sqld; ++i) {
70 XSQLVAR const *var = &sqlda->sqlvar[i];
71
72 if (var->sqlind) {
73 efree(var->sqlind);
74 }
75 }
76 }
77 /* }}} */
78
79 /* called by PDO to clean up a statement handle */
firebird_stmt_dtor(pdo_stmt_t * stmt)80 static int firebird_stmt_dtor(pdo_stmt_t *stmt) /* {{{ */
81 {
82 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
83 int result = 1;
84
85 /* release the statement */
86 if (isc_dsql_free_statement(S->H->isc_status, &S->stmt, DSQL_drop)) {
87 RECORD_ERROR(stmt);
88 result = 0;
89 }
90
91 zend_hash_destroy(S->named_params);
92 FREE_HASHTABLE(S->named_params);
93
94 /* clean up the input descriptor */
95 if (S->in_sqlda) {
96 free_sqlda(S->in_sqlda);
97 efree(S->in_sqlda);
98 }
99
100 free_sqlda(&S->out_sqlda);
101 efree(S);
102
103 return result;
104 }
105 /* }}} */
106
107 /* called by PDO to execute a prepared query */
firebird_stmt_execute(pdo_stmt_t * stmt)108 static int firebird_stmt_execute(pdo_stmt_t *stmt) /* {{{ */
109 {
110 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
111 pdo_firebird_db_handle *H = S->H;
112 zend_ulong affected_rows = 0;
113 static char info_count[] = {isc_info_sql_records};
114 char result[64];
115
116 do {
117 /* named or open cursors should be closed first */
118 if ((*S->name || S->cursor_open) && isc_dsql_free_statement(H->isc_status, &S->stmt, DSQL_close)) {
119 break;
120 }
121 S->cursor_open = 0;
122
123 /* allocate storage for the output data */
124 if (S->out_sqlda.sqld) {
125 unsigned int i;
126 for (i = 0; i < S->out_sqlda.sqld; i++) {
127 XSQLVAR *var = &S->out_sqlda.sqlvar[i];
128 if (var->sqlind) {
129 efree(var->sqlind);
130 }
131 var->sqlind = (void*)ecalloc(1, var->sqllen + 2 * sizeof(short));
132 var->sqldata = &((char*)var->sqlind)[sizeof(short)];
133 }
134 }
135
136 if (S->statement_type == isc_info_sql_stmt_exec_procedure) {
137 if (isc_dsql_execute2(H->isc_status, &H->tr, &S->stmt, PDO_FB_SQLDA_VERSION, S->in_sqlda, &S->out_sqlda)) {
138 break;
139 }
140 } else if (isc_dsql_execute(H->isc_status, &H->tr, &S->stmt, PDO_FB_SQLDA_VERSION, S->in_sqlda)) {
141 break;
142 }
143
144 /* Determine how many rows have changed. In this case we are
145 * only interested in rows changed, not rows retrieved. That
146 * should be handled by the client when fetching. */
147 stmt->row_count = affected_rows;
148
149 switch (S->statement_type) {
150 case isc_info_sql_stmt_insert:
151 case isc_info_sql_stmt_update:
152 case isc_info_sql_stmt_delete:
153 case isc_info_sql_stmt_exec_procedure:
154 if (isc_dsql_sql_info(H->isc_status, &S->stmt, sizeof ( info_count),
155 info_count, sizeof(result), result)) {
156 break;
157 }
158 if (result[0] == isc_info_sql_records) {
159 unsigned i = 3, result_size = isc_vax_integer(&result[1], 2);
160 if (result_size > sizeof(result)) {
161 goto error;
162 }
163 while (result[i] != isc_info_end && i < result_size) {
164 short len = (short) isc_vax_integer(&result[i + 1], 2);
165 if (len != 1 && len != 2 && len != 4) {
166 goto error;
167 }
168 if (result[i] != isc_info_req_select_count) {
169 affected_rows += isc_vax_integer(&result[i + 3], len);
170 }
171 i += len + 3;
172 }
173 stmt->row_count = affected_rows;
174 }
175 /* TODO Dead code or assert one of the previous cases are hit? */
176 default:
177 ;
178 }
179
180 /* commit? */
181 if (stmt->dbh->auto_commit && isc_commit_retaining(H->isc_status, &H->tr)) {
182 break;
183 }
184
185 *S->name = 0;
186 S->cursor_open = S->out_sqlda.sqln && (S->statement_type != isc_info_sql_stmt_exec_procedure);
187 S->exhausted = !S->out_sqlda.sqln; /* There are data to fetch */
188
189 return 1;
190 } while (0);
191
192 error:
193 RECORD_ERROR(stmt);
194
195 return 0;
196 }
197 /* }}} */
198
199 /* called by PDO to fetch the next row from a statement */
firebird_stmt_fetch(pdo_stmt_t * stmt,enum pdo_fetch_orientation ori,zend_long offset)200 static int firebird_stmt_fetch(pdo_stmt_t *stmt, /* {{{ */
201 enum pdo_fetch_orientation ori, zend_long offset)
202 {
203 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
204 pdo_firebird_db_handle *H = S->H;
205
206 if (!stmt->executed) {
207 strcpy(stmt->error_code, "HY000");
208 H->last_app_error = "Cannot fetch from a closed cursor";
209 } else if (!S->exhausted) {
210 if (S->statement_type == isc_info_sql_stmt_exec_procedure) {
211 stmt->row_count = 1;
212 S->exhausted = 1;
213 return 1;
214 }
215 if (isc_dsql_fetch(H->isc_status, &S->stmt, PDO_FB_SQLDA_VERSION, &S->out_sqlda)) {
216 if (H->isc_status[0] && H->isc_status[1]) {
217 RECORD_ERROR(stmt);
218 }
219 S->exhausted = 1;
220 return 0;
221 }
222 stmt->row_count++;
223 return 1;
224 }
225 return 0;
226 }
227 /* }}} */
228
229 /* called by PDO to retrieve information about the fields being returned */
firebird_stmt_describe(pdo_stmt_t * stmt,int colno)230 static int firebird_stmt_describe(pdo_stmt_t *stmt, int colno) /* {{{ */
231 {
232 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
233 struct pdo_column_data *col = &stmt->columns[colno];
234 XSQLVAR *var = &S->out_sqlda.sqlvar[colno];
235 int colname_len;
236 char *cp;
237
238 colname_len = (S->H->fetch_table_names && var->relname_length)
239 ? (var->aliasname_length + var->relname_length + 1)
240 : (var->aliasname_length);
241 col->precision = -var->sqlscale;
242 col->maxlen = var->sqllen;
243 col->name = zend_string_alloc(colname_len, 0);
244 cp = ZSTR_VAL(col->name);
245 if (colname_len > var->aliasname_length) {
246 memmove(cp, var->relname, var->relname_length);
247 cp += var->relname_length;
248 *cp++ = '.';
249 }
250 memmove(cp, var->aliasname, var->aliasname_length);
251 *(cp+var->aliasname_length) = '\0';
252
253 return 1;
254 }
255 /* }}} */
256
firebird_stmt_get_column_meta(pdo_stmt_t * stmt,zend_long colno,zval * return_value)257 static int firebird_stmt_get_column_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value)
258 {
259 pdo_firebird_stmt *S = (pdo_firebird_stmt *) stmt->driver_data;
260 XSQLVAR *var = &S->out_sqlda.sqlvar[colno];
261
262 enum pdo_param_type param_type;
263 if (var->sqlscale < 0) {
264 param_type = PDO_PARAM_STR;
265 } else {
266 switch (var->sqltype & ~1) {
267 case SQL_SHORT:
268 case SQL_LONG:
269 #if SIZEOF_ZEND_LONG >= 8
270 case SQL_INT64:
271 #endif
272 param_type = PDO_PARAM_INT;
273 break;
274 #ifdef SQL_BOOLEAN
275 case SQL_BOOLEAN:
276 param_type = PDO_PARAM_BOOL;
277 break;
278 #endif
279 default:
280 param_type = PDO_PARAM_STR;
281 break;
282 }
283 }
284
285 array_init(return_value);
286 add_assoc_long(return_value, "pdo_type", param_type);
287 return 1;
288 }
289
290 /* fetch a blob into a fetch buffer */
firebird_fetch_blob(pdo_stmt_t * stmt,int colno,zval * result,ISC_QUAD * blob_id)291 static int firebird_fetch_blob(pdo_stmt_t *stmt, int colno, zval *result, ISC_QUAD *blob_id)
292 {
293 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
294 pdo_firebird_db_handle *H = S->H;
295 isc_blob_handle blobh = PDO_FIREBIRD_HANDLE_INITIALIZER;
296 char const bl_item = isc_info_blob_total_length;
297 char bl_info[20];
298 unsigned short i;
299 int retval = 0;
300 size_t len = 0;
301
302 if (isc_open_blob(H->isc_status, &H->db, &H->tr, &blobh, blob_id)) {
303 RECORD_ERROR(stmt);
304 return 0;
305 }
306
307 if (isc_blob_info(H->isc_status, &blobh, 1, const_cast(&bl_item),
308 sizeof(bl_info), bl_info)) {
309 RECORD_ERROR(stmt);
310 goto fetch_blob_end;
311 }
312
313 /* find total length of blob's data */
314 for (i = 0; i < sizeof(bl_info); ) {
315 unsigned short item_len;
316 char item = bl_info[i++];
317
318 if (item == isc_info_end || item == isc_info_truncated || item == isc_info_error
319 || i >= sizeof(bl_info)) {
320 H->last_app_error = "Couldn't determine BLOB size";
321 goto fetch_blob_end;
322 }
323
324 item_len = (unsigned short) isc_vax_integer(&bl_info[i], 2);
325
326 if (item == isc_info_blob_total_length) {
327 len = isc_vax_integer(&bl_info[i+2], item_len);
328 break;
329 }
330 i += item_len+2;
331 }
332
333 /* we've found the blob's length, now fetch! */
334
335 if (len) {
336 zend_ulong cur_len;
337 unsigned short seg_len;
338 ISC_STATUS stat;
339 zend_string *str;
340
341 /* prevent overflow */
342 if (len > ZSTR_MAX_LEN) {
343 result = 0;
344 goto fetch_blob_end;
345 }
346
347 str = zend_string_alloc(len, 0);
348
349 for (cur_len = stat = 0; (!stat || stat == isc_segment) && cur_len < len; cur_len += seg_len) {
350
351 unsigned short chunk_size = (len - cur_len) > USHRT_MAX ? USHRT_MAX
352 : (unsigned short)(len - cur_len);
353
354 stat = isc_get_segment(H->isc_status, &blobh, &seg_len, chunk_size, ZSTR_VAL(str) + cur_len);
355 }
356
357 ZSTR_VAL(str)[len] = '\0';
358 ZVAL_STR(result, str);
359
360 if (H->isc_status[0] == 1 && (stat != 0 && stat != isc_segstr_eof && stat != isc_segment)) {
361 H->last_app_error = "Error reading from BLOB";
362 goto fetch_blob_end;
363 }
364 }
365 retval = 1;
366
367 fetch_blob_end:
368 if (isc_close_blob(H->isc_status, &blobh)) {
369 RECORD_ERROR(stmt);
370 return 0;
371 }
372 return retval;
373 }
374 /* }}} */
375
firebird_stmt_get_col(pdo_stmt_t * stmt,int colno,zval * result,enum pdo_param_type * type)376 static int firebird_stmt_get_col(
377 pdo_stmt_t *stmt, int colno, zval *result, enum pdo_param_type *type)
378 {
379 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
380 XSQLVAR const *var = &S->out_sqlda.sqlvar[colno];
381
382 if (*var->sqlind == -1) {
383 ZVAL_NULL(result);
384 } else {
385 if (var->sqlscale < 0) {
386 static ISC_INT64 const scales[] = { 1, 10, 100, 1000,
387 10000,
388 100000,
389 1000000,
390 10000000,
391 100000000,
392 1000000000,
393 LL_LIT(10000000000),
394 LL_LIT(100000000000),
395 LL_LIT(1000000000000),
396 LL_LIT(10000000000000),
397 LL_LIT(100000000000000),
398 LL_LIT(1000000000000000),
399 LL_LIT(10000000000000000),
400 LL_LIT(100000000000000000),
401 LL_LIT(1000000000000000000)
402 };
403 ISC_INT64 n, f = scales[-var->sqlscale];
404 zend_string *str;
405
406 switch (var->sqltype & ~1) {
407 case SQL_SHORT:
408 n = *(short*)var->sqldata;
409 break;
410 case SQL_LONG:
411 n = get_isc_long_from_sqldata(var->sqldata);
412 break;
413 case SQL_INT64:
414 n = get_isc_int64_from_sqldata(var->sqldata);
415 break;
416 case SQL_DOUBLE:
417 break;
418 EMPTY_SWITCH_DEFAULT_CASE()
419 }
420
421 if ((var->sqltype & ~1) == SQL_DOUBLE) {
422 str = zend_strpprintf(0, "%.*F", -var->sqlscale, get_double_from_sqldata(var->sqldata));
423 } else if (n >= 0) {
424 str = zend_strpprintf(0, "%" LL_MASK "d.%0*" LL_MASK "d",
425 n / f, -var->sqlscale, n % f);
426 } else if (n <= -f) {
427 str = zend_strpprintf(0, "%" LL_MASK "d.%0*" LL_MASK "d",
428 n / f, -var->sqlscale, -n % f);
429 } else {
430 str = zend_strpprintf(0, "-0.%0*" LL_MASK "d", -var->sqlscale, -n % f);
431 }
432 ZVAL_STR(result, str);
433 } else {
434 switch (var->sqltype & ~1) {
435 struct tm t;
436 char *fmt;
437
438 case SQL_VARYING:
439 ZVAL_STRINGL_FAST(result, &var->sqldata[2], *(short*)var->sqldata);
440 break;
441 case SQL_TEXT:
442 ZVAL_STRINGL_FAST(result, var->sqldata, var->sqllen);
443 break;
444 case SQL_SHORT:
445 ZVAL_LONG(result, *(short*)var->sqldata);
446 break;
447 case SQL_LONG:
448 ZVAL_LONG(result, get_isc_long_from_sqldata(var->sqldata));
449 break;
450 case SQL_INT64:
451 #if SIZEOF_ZEND_LONG >= 8
452 ZVAL_LONG(result, get_isc_int64_from_sqldata(var->sqldata));
453 #else
454 ZVAL_STR(result, zend_strpprintf(0, "%" LL_MASK "d", get_isc_int64_from_sqldata(var->sqldata)));
455 #endif
456 break;
457 case SQL_FLOAT:
458 /* TODO: Why is this not returned as the native type? */
459 ZVAL_STR(result, zend_strpprintf(0, "%F", *(float*)var->sqldata));
460 break;
461 case SQL_DOUBLE:
462 /* TODO: Why is this not returned as the native type? */
463 ZVAL_STR(result, zend_strpprintf(0, "%F", get_double_from_sqldata(var->sqldata)));
464 break;
465 #ifdef SQL_BOOLEAN
466 case SQL_BOOLEAN:
467 ZVAL_BOOL(result, *(FB_BOOLEAN*)var->sqldata);
468 break;
469 #endif
470 case SQL_TYPE_DATE:
471 isc_decode_sql_date((ISC_DATE*)var->sqldata, &t);
472 fmt = S->H->date_format ? S->H->date_format : PDO_FB_DEF_DATE_FMT;
473 if (0) {
474 case SQL_TYPE_TIME:
475 isc_decode_sql_time((ISC_TIME*)var->sqldata, &t);
476 fmt = S->H->time_format ? S->H->time_format : PDO_FB_DEF_TIME_FMT;
477 } else if (0) {
478 case SQL_TIMESTAMP:
479 {
480 ISC_TIMESTAMP timestamp = get_isc_timestamp_from_sqldata(var->sqldata);
481 isc_decode_timestamp(×tamp, &t);
482 }
483 fmt = S->H->timestamp_format ? S->H->timestamp_format : PDO_FB_DEF_TIMESTAMP_FMT;
484 }
485 /* convert the timestamp into a string */
486 char buf[80];
487 size_t len = strftime(buf, sizeof(buf), fmt, &t);
488 ZVAL_STRINGL(result, buf, len);
489 break;
490 case SQL_BLOB: {
491 ISC_QUAD quad = get_isc_quad_from_sqldata(var->sqldata);
492 return firebird_fetch_blob(stmt, colno, result, &quad);
493 }
494 }
495 }
496 }
497 return 1;
498 }
499
firebird_bind_blob(pdo_stmt_t * stmt,ISC_QUAD * blob_id,zval * param)500 static int firebird_bind_blob(pdo_stmt_t *stmt, ISC_QUAD *blob_id, zval *param)
501 {
502 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
503 pdo_firebird_db_handle *H = S->H;
504 isc_blob_handle h = PDO_FIREBIRD_HANDLE_INITIALIZER;
505 zval data;
506 zend_ulong put_cnt = 0, rem_cnt;
507 unsigned short chunk_size;
508 int result = 1;
509
510 if (isc_create_blob(H->isc_status, &H->db, &H->tr, &h, blob_id)) {
511 RECORD_ERROR(stmt);
512 return 0;
513 }
514
515 if (Z_TYPE_P(param) != IS_STRING) {
516 ZVAL_STR(&data, zval_get_string_func(param));
517 } else {
518 ZVAL_COPY_VALUE(&data, param);
519 }
520
521 for (rem_cnt = Z_STRLEN(data); rem_cnt > 0; rem_cnt -= chunk_size) {
522 chunk_size = rem_cnt > USHRT_MAX ? USHRT_MAX : (unsigned short)rem_cnt;
523 if (isc_put_segment(H->isc_status, &h, chunk_size, &Z_STRVAL(data)[put_cnt])) {
524 RECORD_ERROR(stmt);
525 result = 0;
526 break;
527 }
528 put_cnt += chunk_size;
529 }
530
531 if (Z_TYPE_P(param) != IS_STRING) {
532 zval_ptr_dtor_str(&data);
533 }
534
535 if (isc_close_blob(H->isc_status, &h)) {
536 RECORD_ERROR(stmt);
537 return 0;
538 }
539 return result;
540 }
541
firebird_stmt_param_hook(pdo_stmt_t * stmt,struct pdo_bound_param_data * param,enum pdo_param_event event_type)542 static int firebird_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, /* {{{ */
543 enum pdo_param_event event_type)
544 {
545 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
546 XSQLDA *sqlda = param->is_param ? S->in_sqlda : &S->out_sqlda;
547 XSQLVAR *var;
548
549 if (event_type == PDO_PARAM_EVT_FREE) { /* not used */
550 return 1;
551 }
552
553 if (!sqlda || param->paramno >= sqlda->sqld) {
554 strcpy(stmt->error_code, "HY093");
555 S->H->last_app_error = "Invalid parameter index";
556 return 0;
557 }
558 if (param->is_param && param->paramno == -1) {
559 zval *index;
560
561 /* try to determine the index by looking in the named_params hash */
562 if ((index = zend_hash_find(S->named_params, param->name)) != NULL) {
563 param->paramno = Z_LVAL_P(index);
564 } else {
565 /* ... or by looking in the input descriptor */
566 int i;
567
568 for (i = 0; i < sqlda->sqld; ++i) {
569 XSQLVAR *var = &sqlda->sqlvar[i];
570
571 if ((var->aliasname_length && !strncasecmp(ZSTR_VAL(param->name), var->aliasname,
572 min(ZSTR_LEN(param->name), var->aliasname_length)))
573 || (var->sqlname_length && !strncasecmp(ZSTR_VAL(param->name), var->sqlname,
574 min(ZSTR_LEN(param->name), var->sqlname_length)))) {
575 param->paramno = i;
576 break;
577 }
578 }
579 if (i >= sqlda->sqld) {
580 strcpy(stmt->error_code, "HY093");
581 S->H->last_app_error = "Invalid parameter name";
582 return 0;
583 }
584 }
585 }
586
587 var = &sqlda->sqlvar[param->paramno];
588
589 switch (event_type) {
590 zval *parameter;
591
592 case PDO_PARAM_EVT_ALLOC:
593 if (param->is_param) {
594 /* allocate the parameter */
595 if (var->sqlind) {
596 efree(var->sqlind);
597 }
598 var->sqlind = (void*)emalloc(var->sqllen + 2*sizeof(short));
599 var->sqldata = &((char*)var->sqlind)[sizeof(short)];
600 }
601 break;
602
603 case PDO_PARAM_EVT_EXEC_PRE:
604 if (!param->is_param) {
605 break;
606 }
607
608 *var->sqlind = 0;
609 if (Z_ISREF(param->parameter)) {
610 parameter = Z_REFVAL(param->parameter);
611 } else {
612 parameter = ¶m->parameter;
613 }
614
615 if (Z_TYPE_P(parameter) == IS_RESOURCE) {
616 php_stream *stm = NULL;
617
618 php_stream_from_zval_no_verify(stm, parameter);
619 if (stm) {
620 zend_string *mem = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
621 zval_ptr_dtor(parameter);
622 ZVAL_STR(parameter, mem ? mem : ZSTR_EMPTY_ALLOC());
623 } else {
624 pdo_raise_impl_error(stmt->dbh, stmt, "HY105", "Expected a stream resource");
625 return 0;
626 }
627 }
628
629 switch (var->sqltype & ~1) {
630 case SQL_ARRAY:
631 strcpy(stmt->error_code, "HY000");
632 S->H->last_app_error = "Cannot bind to array field";
633 return 0;
634
635 case SQL_BLOB: {
636 if (Z_TYPE_P(parameter) == IS_NULL) {
637 /* Check if field allow NULL values */
638 if (~var->sqltype & 1) {
639 strcpy(stmt->error_code, "HY105");
640 S->H->last_app_error = "Parameter requires non-null value";
641 return 0;
642 }
643 *var->sqlind = -1;
644 return 1;
645 }
646 ISC_QUAD quad = get_isc_quad_from_sqldata(var->sqldata);
647 if (firebird_bind_blob(stmt, &quad, parameter) != 0) {
648 memcpy(var->sqldata, &quad, sizeof(quad));
649 return 1;
650 }
651 return 0;
652 }
653 }
654
655 #ifdef SQL_BOOLEAN
656 /* keep native BOOLEAN type */
657 if ((var->sqltype & ~1) == SQL_BOOLEAN) {
658 switch (Z_TYPE_P(parameter)) {
659 case IS_LONG:
660 case IS_DOUBLE:
661 case IS_TRUE:
662 case IS_FALSE:
663 *(FB_BOOLEAN*)var->sqldata = zend_is_true(parameter) ? FB_TRUE : FB_FALSE;
664 break;
665 case IS_STRING:
666 {
667 zend_long lval;
668 double dval;
669
670 if ((Z_STRLEN_P(parameter) == 0)) {
671 *(FB_BOOLEAN*)var->sqldata = FB_FALSE;
672 break;
673 }
674
675 switch (is_numeric_string(Z_STRVAL_P(parameter), Z_STRLEN_P(parameter), &lval, &dval, 0)) {
676 case IS_LONG:
677 *(FB_BOOLEAN*)var->sqldata = (lval != 0) ? FB_TRUE : FB_FALSE;
678 break;
679 case IS_DOUBLE:
680 *(FB_BOOLEAN*)var->sqldata = (dval != 0) ? FB_TRUE : FB_FALSE;
681 break;
682 default:
683 if (!zend_binary_strncasecmp(Z_STRVAL_P(parameter), Z_STRLEN_P(parameter), "true", 4, 4)) {
684 *(FB_BOOLEAN*)var->sqldata = FB_TRUE;
685 } else if (!zend_binary_strncasecmp(Z_STRVAL_P(parameter), Z_STRLEN_P(parameter), "false", 5, 5)) {
686 *(FB_BOOLEAN*)var->sqldata = FB_FALSE;
687 } else {
688 strcpy(stmt->error_code, "HY105");
689 S->H->last_app_error = "Cannot convert string to boolean";
690 return 0;
691 }
692
693 }
694 }
695 break;
696 case IS_NULL:
697 *var->sqlind = -1;
698 break;
699 default:
700 strcpy(stmt->error_code, "HY105");
701 S->H->last_app_error = "Binding arrays/objects is not supported";
702 return 0;
703 }
704 break;
705 }
706 #endif
707
708
709 /* check if a NULL should be inserted */
710 switch (Z_TYPE_P(parameter)) {
711 int force_null;
712
713 case IS_LONG:
714 /* keep the allow-NULL flag */
715 var->sqltype = (sizeof(zend_long) == 8 ? SQL_INT64 : SQL_LONG) | (var->sqltype & 1);
716 var->sqldata = (void*)&Z_LVAL_P(parameter);
717 var->sqllen = sizeof(zend_long);
718 break;
719 case IS_DOUBLE:
720 /* keep the allow-NULL flag */
721 var->sqltype = SQL_DOUBLE | (var->sqltype & 1);
722 var->sqldata = (void*)&Z_DVAL_P(parameter);
723 var->sqllen = sizeof(double);
724 break;
725 case IS_STRING:
726 force_null = 0;
727
728 /* for these types, an empty string can be handled like a NULL value */
729 switch (var->sqltype & ~1) {
730 case SQL_SHORT:
731 case SQL_LONG:
732 case SQL_INT64:
733 case SQL_FLOAT:
734 case SQL_DOUBLE:
735 case SQL_TIMESTAMP:
736 case SQL_TYPE_DATE:
737 case SQL_TYPE_TIME:
738 force_null = (Z_STRLEN_P(parameter) == 0);
739 }
740 if (!force_null) {
741 /* keep the allow-NULL flag */
742 var->sqltype = SQL_TEXT | (var->sqltype & 1);
743 var->sqldata = Z_STRVAL_P(parameter);
744 var->sqllen = Z_STRLEN_P(parameter);
745 break;
746 }
747 ZEND_FALLTHROUGH;
748 case IS_NULL:
749 /* complain if this field doesn't allow NULL values */
750 if (~var->sqltype & 1) {
751 strcpy(stmt->error_code, "HY105");
752 S->H->last_app_error = "Parameter requires non-null value";
753 return 0;
754 }
755 *var->sqlind = -1;
756 break;
757 default:
758 strcpy(stmt->error_code, "HY105");
759 S->H->last_app_error = "Binding arrays/objects is not supported";
760 return 0;
761 }
762 break;
763
764 case PDO_PARAM_EVT_FETCH_POST:
765 if (param->paramno == -1) {
766 return 0;
767 }
768 if (param->is_param) {
769 break;
770 }
771 if (Z_ISREF(param->parameter)) {
772 parameter = Z_REFVAL(param->parameter);
773 } else {
774 parameter = ¶m->parameter;
775 }
776 zval_ptr_dtor(parameter);
777 ZVAL_NULL(parameter);
778 return firebird_stmt_get_col(stmt, param->paramno, parameter, NULL);
779 default:
780 ;
781 }
782 return 1;
783 }
784 /* }}} */
785
firebird_stmt_set_attribute(pdo_stmt_t * stmt,zend_long attr,zval * val)786 static int firebird_stmt_set_attribute(pdo_stmt_t *stmt, zend_long attr, zval *val) /* {{{ */
787 {
788 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
789
790 switch (attr) {
791 default:
792 return 0;
793 case PDO_ATTR_CURSOR_NAME:
794 if (!try_convert_to_string(val)) {
795 return 0;
796 }
797
798 if (isc_dsql_set_cursor_name(S->H->isc_status, &S->stmt, Z_STRVAL_P(val),0)) {
799 RECORD_ERROR(stmt);
800 return 0;
801 }
802 strlcpy(S->name, Z_STRVAL_P(val), sizeof(S->name));
803 break;
804 }
805 return 1;
806 }
807 /* }}} */
808
firebird_stmt_get_attribute(pdo_stmt_t * stmt,zend_long attr,zval * val)809 static int firebird_stmt_get_attribute(pdo_stmt_t *stmt, zend_long attr, zval *val) /* {{{ */
810 {
811 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
812
813 switch (attr) {
814 default:
815 return 0;
816 case PDO_ATTR_CURSOR_NAME:
817 if (*S->name) {
818 ZVAL_STRING(val, S->name);
819 } else {
820 ZVAL_NULL(val);
821 }
822 break;
823 }
824 return 1;
825 }
826 /* }}} */
827
firebird_stmt_cursor_closer(pdo_stmt_t * stmt)828 static int firebird_stmt_cursor_closer(pdo_stmt_t *stmt) /* {{{ */
829 {
830 pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
831
832 /* close the statement handle */
833 if ((*S->name || S->cursor_open) && isc_dsql_free_statement(S->H->isc_status, &S->stmt, DSQL_close)) {
834 RECORD_ERROR(stmt);
835 return 0;
836 }
837 *S->name = 0;
838 S->cursor_open = 0;
839 return 1;
840 }
841 /* }}} */
842
843
844 const struct pdo_stmt_methods firebird_stmt_methods = { /* {{{ */
845 firebird_stmt_dtor,
846 firebird_stmt_execute,
847 firebird_stmt_fetch,
848 firebird_stmt_describe,
849 firebird_stmt_get_col,
850 firebird_stmt_param_hook,
851 firebird_stmt_set_attribute,
852 firebird_stmt_get_attribute,
853 firebird_stmt_get_column_meta,
854 NULL, /* next_rowset_func */
855 firebird_stmt_cursor_closer
856 };
857 /* }}} */
858