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 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE
23 #endif
24
25 #include "php.h"
26 #include "zend_exceptions.h"
27 #include "php_ini.h"
28 #include "ext/standard/info.h"
29 #include "ext/pdo/php_pdo.h"
30 #include "ext/pdo/php_pdo_driver.h"
31 #include "php_pdo_firebird.h"
32 #include "php_pdo_firebird_int.h"
33 #include "pdo_firebird_utils.h"
34
35 static int php_firebird_alloc_prepare_stmt(pdo_dbh_t*, const zend_string*, XSQLDA*, isc_stmt_handle*,
36 HashTable*);
37 static bool php_firebird_rollback_transaction(pdo_dbh_t *dbh);
38
39 const char CHR_LETTER = 1;
40 const char CHR_DIGIT = 2;
41 const char CHR_IDENT = 4;
42 const char CHR_QUOTE = 8;
43 const char CHR_WHITE = 16;
44 const char CHR_HEX = 32;
45 const char CHR_INTRODUCER = 64;
46
47 static const char classes_array[] = {
48 /* 000 */ 0,
49 /* 001 */ 0,
50 /* 002 */ 0,
51 /* 003 */ 0,
52 /* 004 */ 0,
53 /* 005 */ 0,
54 /* 006 */ 0,
55 /* 007 */ 0,
56 /* 008 */ 0,
57 /* 009 */ 16, /* CHR_WHITE */
58 /* 010 */ 16, /* CHR_WHITE */
59 /* 011 */ 0,
60 /* 012 */ 0,
61 /* 013 */ 16, /* CHR_WHITE */
62 /* 014 */ 0,
63 /* 015 */ 0,
64 /* 016 */ 0,
65 /* 017 */ 0,
66 /* 018 */ 0,
67 /* 019 */ 0,
68 /* 020 */ 0,
69 /* 021 */ 0,
70 /* 022 */ 0,
71 /* 023 */ 0,
72 /* 024 */ 0,
73 /* 025 */ 0,
74 /* 026 */ 0,
75 /* 027 */ 0,
76 /* 028 */ 0,
77 /* 029 */ 0,
78 /* 030 */ 0,
79 /* 031 */ 0,
80 /* 032 */ 16, /* CHR_WHITE */
81 /* 033 ! */ 0,
82 /* 034 " */ 8, /* CHR_QUOTE */
83 /* 035 # */ 0,
84 /* 036 $ */ 4, /* CHR_IDENT */
85 /* 037 % */ 0,
86 /* 038 & */ 0,
87 /* 039 ' */ 8, /* CHR_QUOTE */
88 /* 040 ( */ 0,
89 /* 041 ) */ 0,
90 /* 042 * */ 0,
91 /* 043 + */ 0,
92 /* 044 , */ 0,
93 /* 045 - */ 0,
94 /* 046 . */ 0,
95 /* 047 / */ 0,
96 /* 048 0 */ 38, /* CHR_DIGIT | CHR_IDENT | CHR_HEX */
97 /* 049 1 */ 38, /* CHR_DIGIT | CHR_IDENT | CHR_HEX */
98 /* 050 2 */ 38, /* CHR_DIGIT | CHR_IDENT | CHR_HEX */
99 /* 051 3 */ 38, /* CHR_DIGIT | CHR_IDENT | CHR_HEX */
100 /* 052 4 */ 38, /* CHR_DIGIT | CHR_IDENT | CHR_HEX */
101 /* 053 5 */ 38, /* CHR_DIGIT | CHR_IDENT | CHR_HEX */
102 /* 054 6 */ 38, /* CHR_DIGIT | CHR_IDENT | CHR_HEX */
103 /* 055 7 */ 38, /* CHR_DIGIT | CHR_IDENT | CHR_HEX */
104 /* 056 8 */ 38, /* CHR_DIGIT | CHR_IDENT | CHR_HEX */
105 /* 057 9 */ 38, /* CHR_DIGIT | CHR_IDENT | CHR_HEX */
106 /* 058 : */ 0,
107 /* 059 ; */ 0,
108 /* 060 < */ 0,
109 /* 061 = */ 0,
110 /* 062 > */ 0,
111 /* 063 ? */ 0,
112 /* 064 @ */ 0,
113 /* 065 A */ 37, /* CHR_LETTER | CHR_IDENT | CHR_HEX */
114 /* 066 B */ 37, /* CHR_LETTER | CHR_IDENT | CHR_HEX */
115 /* 067 C */ 37, /* CHR_LETTER | CHR_IDENT | CHR_HEX */
116 /* 068 D */ 37, /* CHR_LETTER | CHR_IDENT | CHR_HEX */
117 /* 069 E */ 37, /* CHR_LETTER | CHR_IDENT | CHR_HEX */
118 /* 070 F */ 37, /* CHR_LETTER | CHR_IDENT | CHR_HEX */
119 /* 071 G */ 5, /* CHR_LETTER | CHR_IDENT */
120 /* 072 H */ 5, /* CHR_LETTER | CHR_IDENT */
121 /* 073 I */ 5, /* CHR_LETTER | CHR_IDENT */
122 /* 074 J */ 5, /* CHR_LETTER | CHR_IDENT */
123 /* 075 K */ 5, /* CHR_LETTER | CHR_IDENT */
124 /* 076 L */ 5, /* CHR_LETTER | CHR_IDENT */
125 /* 077 M */ 5, /* CHR_LETTER | CHR_IDENT */
126 /* 078 N */ 5, /* CHR_LETTER | CHR_IDENT */
127 /* 079 O */ 5, /* CHR_LETTER | CHR_IDENT */
128 /* 080 P */ 5, /* CHR_LETTER | CHR_IDENT */
129 /* 081 Q */ 5, /* CHR_LETTER | CHR_IDENT */
130 /* 082 R */ 5, /* CHR_LETTER | CHR_IDENT */
131 /* 083 S */ 5, /* CHR_LETTER | CHR_IDENT */
132 /* 084 T */ 5, /* CHR_LETTER | CHR_IDENT */
133 /* 085 U */ 5, /* CHR_LETTER | CHR_IDENT */
134 /* 086 V */ 5, /* CHR_LETTER | CHR_IDENT */
135 /* 087 W */ 5, /* CHR_LETTER | CHR_IDENT */
136 /* 088 X */ 5, /* CHR_LETTER | CHR_IDENT */
137 /* 089 Y */ 5, /* CHR_LETTER | CHR_IDENT */
138 /* 090 Z */ 5, /* CHR_LETTER | CHR_IDENT */
139 /* 091 [ */ 0,
140 /* 092 \ */ 0,
141 /* 093 ] */ 0,
142 /* 094 ^ */ 0,
143 /* 095 _ */ 68, /* CHR_IDENT | CHR_INTRODUCER */
144 /* 096 ` */ 0,
145 /* 097 a */ 37, /* CHR_LETTER | CHR_IDENT | CHR_HEX */
146 /* 098 b */ 37, /* CHR_LETTER | CHR_IDENT | CHR_HEX */
147 /* 099 c */ 37, /* CHR_LETTER | CHR_IDENT | CHR_HEX */
148 /* 100 d */ 37, /* CHR_LETTER | CHR_IDENT | CHR_HEX */
149 /* 101 e */ 37, /* CHR_LETTER | CHR_IDENT | CHR_HEX */
150 /* 102 f */ 37, /* CHR_LETTER | CHR_IDENT | CHR_HEX */
151 /* 103 g */ 5, /* CHR_LETTER | CHR_IDENT */
152 /* 104 h */ 5, /* CHR_LETTER | CHR_IDENT */
153 /* 105 i */ 5, /* CHR_LETTER | CHR_IDENT */
154 /* 106 j */ 5, /* CHR_LETTER | CHR_IDENT */
155 /* 107 k */ 5, /* CHR_LETTER | CHR_IDENT */
156 /* 108 l */ 5, /* CHR_LETTER | CHR_IDENT */
157 /* 109 m */ 5, /* CHR_LETTER | CHR_IDENT */
158 /* 110 n */ 5, /* CHR_LETTER | CHR_IDENT */
159 /* 111 o */ 5, /* CHR_LETTER | CHR_IDENT */
160 /* 112 p */ 5, /* CHR_LETTER | CHR_IDENT */
161 /* 113 q */ 5, /* CHR_LETTER | CHR_IDENT */
162 /* 114 r */ 5, /* CHR_LETTER | CHR_IDENT */
163 /* 115 s */ 5, /* CHR_LETTER | CHR_IDENT */
164 /* 116 t */ 5, /* CHR_LETTER | CHR_IDENT */
165 /* 117 u */ 5, /* CHR_LETTER | CHR_IDENT */
166 /* 118 v */ 5, /* CHR_LETTER | CHR_IDENT */
167 /* 119 w */ 5, /* CHR_LETTER | CHR_IDENT */
168 /* 120 x */ 5, /* CHR_LETTER | CHR_IDENT */
169 /* 121 y */ 5, /* CHR_LETTER | CHR_IDENT */
170 /* 122 z */ 5, /* CHR_LETTER | CHR_IDENT */
171 /* 123 { */ 5, /* CHR_LETTER | CHR_IDENT */
172 /* 124 | */ 0,
173 /* 125 } */ 5, /* CHR_LETTER | CHR_IDENT */
174 /* 126 ~ */ 0,
175 /* 127 */ 0
176 };
177
php_firebird_classes(char idx)178 static inline char php_firebird_classes(char idx)
179 {
180 unsigned char uidx = (unsigned char) idx;
181 if (uidx > 127) return 0;
182 return classes_array[uidx];
183 }
184
185 typedef enum {
186 ttNone,
187 ttWhite,
188 ttComment,
189 ttBrokenComment,
190 ttString,
191 ttParamMark,
192 ttIdent,
193 ttOther
194 } FbTokenType;
195
php_firebird_get_token(const char ** begin,const char * end)196 static FbTokenType php_firebird_get_token(const char** begin, const char* end)
197 {
198 FbTokenType ret = ttNone;
199 const char* p = *begin;
200
201 char c = *p++;
202 switch (c)
203 {
204 case ':':
205 case '?':
206 ret = ttParamMark;
207 break;
208
209 case '\'':
210 case '"':
211 while (p < end)
212 {
213 if (*p++ == c)
214 {
215 ret = ttString;
216 break;
217 }
218 }
219 break;
220
221 case '/':
222 if (p < end && *p == '*')
223 {
224 ret = ttBrokenComment;
225 p++;
226 while (p < end)
227 {
228 if (*p++ == '*' && p < end && *p == '/')
229 {
230 p++;
231 ret = ttComment;
232 break;
233 }
234 }
235 }
236 else {
237 ret = ttOther;
238 }
239 break;
240
241 case '-':
242 if (p < end && *p == '-')
243 {
244 while (++p < end)
245 {
246 if (*p == '\r')
247 {
248 p++;
249 if (p < end && *p == '\n')
250 p++;
251 break;
252 }
253 else if (*p == '\n')
254 break;
255 }
256
257 ret = ttComment;
258 }
259 else
260 ret = ttOther;
261 break;
262
263 default:
264 if (php_firebird_classes(c) & CHR_DIGIT)
265 {
266 while (p < end && (php_firebird_classes(*p) & CHR_DIGIT))
267 p++;
268 ret = ttOther;
269 }
270 else if (php_firebird_classes(c) & CHR_IDENT)
271 {
272 while (p < end && (php_firebird_classes(*p) & CHR_IDENT))
273 p++;
274 ret = ttIdent;
275 }
276 else if (php_firebird_classes(c) & CHR_WHITE)
277 {
278 while (p < end && (php_firebird_classes(*p) & CHR_WHITE))
279 p++;
280 ret = ttWhite;
281 }
282 else
283 {
284 while (p < end && !(php_firebird_classes(*p) & (CHR_DIGIT | CHR_IDENT | CHR_WHITE)) &&
285 (*p != '/') && (*p != '-') && (*p != ':') && (*p != '?') &&
286 (*p != '\'') && (*p != '"'))
287 {
288 p++;
289 }
290 ret = ttOther;
291 }
292 }
293
294 *begin = p;
295 return ret;
296 }
297
php_firebird_preprocess(const zend_string * sql,char * sql_out,HashTable * named_params)298 static int php_firebird_preprocess(const zend_string* sql, char* sql_out, HashTable* named_params)
299 {
300 bool passAsIs = 1, execBlock = 0;
301 zend_long pindex = -1;
302 char pname[254], ident[253], ident2[253];
303 unsigned int l;
304 const char* p = ZSTR_VAL(sql), * end = ZSTR_VAL(sql) + ZSTR_LEN(sql);
305 const char* start = p;
306 FbTokenType tok = php_firebird_get_token(&p, end);
307
308 const char* i = start;
309 while (p < end && (tok == ttComment || tok == ttWhite))
310 {
311 i = p;
312 tok = php_firebird_get_token(&p, end);
313 }
314
315 if (p >= end || tok != ttIdent)
316 {
317 /* Execute statement preprocess SQL error */
318 /* Statement expected */
319 return 0;
320 }
321 /* skip leading comments ?? */
322 start = i;
323 l = p - i;
324 /* check the length of the identifier */
325 /* in Firebird 4.0 it is 63 characters, in previous versions 31 bytes */
326 if (l > 252) {
327 return 0;
328 }
329 strncpy(ident, i, l);
330 ident[l] = '\0';
331 if (!strcasecmp(ident, "EXECUTE"))
332 {
333 /* For EXECUTE PROCEDURE and EXECUTE BLOCK statements, named parameters must be processed. */
334 /* However, in EXECUTE BLOCK this is done in a special way. */
335 const char* i2 = p;
336 tok = php_firebird_get_token(&p, end);
337 while (p < end && (tok == ttComment || tok == ttWhite))
338 {
339 i2 = p;
340 tok = php_firebird_get_token(&p, end);
341 }
342 if (p >= end || tok != ttIdent)
343 {
344 /* Execute statement preprocess SQL error */
345 /* Statement expected */
346 return 0;
347 }
348 l = p - i2;
349 /* check the length of the identifier */
350 /* in Firebird 4.0 it is 63 characters, in previous versions 31 bytes */
351 if (l > 252) {
352 return 0;
353 }
354 strncpy(ident2, i2, l);
355 ident2[l] = '\0';
356 execBlock = !strcasecmp(ident2, "BLOCK");
357 passAsIs = 0;
358 }
359 else
360 {
361 /* Named parameters must be processed in the INSERT, UPDATE, DELETE, MERGE statements. */
362 /* If CTEs are present in the query, they begin with the WITH keyword. */
363 passAsIs = strcasecmp(ident, "INSERT") && strcasecmp(ident, "UPDATE") &&
364 strcasecmp(ident, "DELETE") && strcasecmp(ident, "MERGE") &&
365 strcasecmp(ident, "SELECT") && strcasecmp(ident, "WITH");
366 }
367
368 if (passAsIs)
369 {
370 strcpy(sql_out, ZSTR_VAL(sql));
371 return 1;
372 }
373
374 strncat(sql_out, start, p - start);
375
376 while (p < end)
377 {
378 start = p;
379 tok = php_firebird_get_token(&p, end);
380 switch (tok)
381 {
382 case ttParamMark:
383 tok = php_firebird_get_token(&p, end);
384 if (tok == ttIdent /*|| tok == ttString*/)
385 {
386 ++pindex;
387 l = p - start;
388 /* check the length of the identifier */
389 /* in Firebird 4.0 it is 63 characters, in previous versions 31 bytes */
390 /* + symbol ":" */
391 if (l > 253) {
392 return 0;
393 }
394 strncpy(pname, start, l);
395 pname[l] = '\0';
396
397 if (named_params) {
398 zval tmp;
399 ZVAL_LONG(&tmp, pindex);
400 zend_hash_str_update(named_params, pname, l, &tmp);
401 }
402
403 strcat(sql_out, "?");
404 }
405 else
406 {
407 if (strncmp(start, "?", 1)) {
408 /* Execute statement preprocess SQL error */
409 /* Parameter name expected */
410 return 0;
411 }
412 ++pindex;
413 strncat(sql_out, start, p - start);
414 }
415 break;
416
417 case ttIdent:
418 if (execBlock)
419 {
420 /* In the EXECUTE BLOCK statement, processing must be */
421 /* carried out up to the keyword AS. */
422 l = p - start;
423 /* check the length of the identifier */
424 /* in Firebird 4.0 it is 63 characters, in previous versions 31 bytes */
425 if (l > 252) {
426 return 0;
427 }
428 strncpy(ident, start, l);
429 ident[l] = '\0';
430 if (!strcasecmp(ident, "AS"))
431 {
432 strncat(sql_out, start, end - start);
433 return 1;
434 }
435 }
436 /* TODO Check this is correct? */
437 ZEND_FALLTHROUGH;
438
439 case ttWhite:
440 case ttComment:
441 case ttString:
442 case ttOther:
443 strncat(sql_out, start, p - start);
444 break;
445
446 case ttBrokenComment:
447 {
448 /* Execute statement preprocess SQL error */
449 /* Unclosed comment found near ''@1'' */
450 return 0;
451 }
452 break;
453
454
455 case ttNone:
456 /* Execute statement preprocess SQL error */
457 return 0;
458 break;
459 }
460 }
461 return 1;
462 }
463
464 #if FB_API_VER >= 40
465 /* set coercing a data type */
set_coercing_output_data_types(XSQLDA * sqlda)466 static void set_coercing_output_data_types(XSQLDA* sqlda)
467 {
468 /* Data types introduced in Firebird 4.0 are difficult to process using the Firebird Legacy API. */
469 /* These data types include DECFLOAT(16), DECFLOAT(34), INT128 (NUMERIC/DECIMAL(38, x)). */
470 /* In any case, at this data types can only be mapped to strings. */
471 /* This function allows you to ensure minimal performance of queries if they contain columns of the above types. */
472 unsigned int i;
473 short dtype;
474 short nullable;
475 XSQLVAR* var;
476 unsigned fb_client_version = fb_get_client_version();
477 unsigned fb_client_major_version = (fb_client_version >> 8) & 0xFF;
478 for (i=0, var = sqlda->sqlvar; i < sqlda->sqld; i++, var++) {
479 dtype = (var->sqltype & ~1); /* drop flag bit */
480 nullable = (var->sqltype & 1);
481 switch(dtype) {
482 case SQL_INT128:
483 var->sqltype = SQL_VARYING + nullable;
484 var->sqllen = 46;
485 var->sqlscale = 0;
486 break;
487
488 case SQL_DEC16:
489 var->sqltype = SQL_VARYING + nullable;
490 var->sqllen = 24;
491 break;
492
493 case SQL_DEC34:
494 var->sqltype = SQL_VARYING + nullable;
495 var->sqllen = 43;
496 break;
497
498 case SQL_TIMESTAMP_TZ:
499 if (fb_client_major_version < 4) {
500 /* If the client version is below 4.0, then it is impossible to handle time zones natively, */
501 /* so we convert these types to a string. */
502 var->sqltype = SQL_VARYING + nullable;
503 var->sqllen = 58;
504 }
505 break;
506
507 case SQL_TIME_TZ:
508 if (fb_client_major_version < 4) {
509 /* If the client version is below 4.0, then it is impossible to handle time zones natively, */
510 /* so we convert these types to a string. */
511 var->sqltype = SQL_VARYING + nullable;
512 var->sqllen = 46;
513 }
514 break;
515
516 default:
517 break;
518 }
519 }
520 }
521 #endif
522
523 /* map driver specific error message to PDO error */
php_firebird_set_error(pdo_dbh_t * dbh,pdo_stmt_t * stmt,const char * state,const size_t state_len,const char * msg,const size_t msg_len)524 void php_firebird_set_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *state, const size_t state_len,
525 const char *msg, const size_t msg_len) /* {{{ */
526 {
527 pdo_error_type *const error_code = stmt ? &stmt->error_code : &dbh->error_code;
528 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
529 pdo_firebird_error_info *einfo = &H->einfo;
530 int sqlcode = -999;
531
532 if (einfo->errmsg) {
533 pefree(einfo->errmsg, dbh->is_persistent);
534 einfo->errmsg = NULL;
535 einfo->errmsg_length = 0;
536 }
537
538 if (H->isc_status[0] == 1 && H->isc_status[1] > 0) {
539 char buf[512];
540 size_t buf_size = sizeof(buf), read_len = 0;
541 ssize_t tmp_len;
542 const ISC_STATUS *s = H->isc_status;
543 sqlcode = isc_sqlcode(s);
544
545 while ((buf_size > (read_len + 1)) && (tmp_len = fb_interpret(&buf[read_len], (buf_size - read_len - 1), &s)) && tmp_len > 0) {
546 read_len += tmp_len;
547 buf[read_len++] = ' ';
548 }
549
550 /* remove last space */
551 if (read_len) {
552 buf[read_len--] = '\0';
553 }
554
555 einfo->errmsg_length = read_len;
556 einfo->errmsg = pestrndup(buf, read_len, dbh->is_persistent);
557
558 char sqlstate[sizeof(pdo_error_type)];
559 fb_sqlstate(sqlstate, H->isc_status);
560 if (strlen(sqlstate) < sizeof(pdo_error_type)) {
561 strcpy(*error_code, sqlstate);
562 goto end;
563 }
564 } else if (msg && msg_len) {
565 einfo->errmsg_length = msg_len;
566 einfo->errmsg = pestrndup(msg, einfo->errmsg_length, dbh->is_persistent);
567 }
568
569 if (state && state_len && state_len < sizeof(pdo_error_type)) {
570 memcpy(*error_code, state, state_len + 1);
571 } else {
572 memcpy(*error_code, "HY000", sizeof("HY000"));
573 }
574
575 end:
576 einfo->sqlcode = sqlcode;
577 if (!dbh->methods) {
578 pdo_throw_exception(0, einfo->errmsg, error_code);
579 }
580 }
581 /* }}} */
582
583 /* called by PDO to close a db handle */
firebird_handle_closer(pdo_dbh_t * dbh)584 static void firebird_handle_closer(pdo_dbh_t *dbh) /* {{{ */
585 {
586 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
587
588 if (H->tr) {
589 if (dbh->auto_commit) {
590 php_firebird_commit_transaction(dbh, /* retain */ false);
591 } else {
592 php_firebird_rollback_transaction(dbh);
593 }
594 }
595 H->in_manually_txn = 0;
596
597 if (isc_detach_database(H->isc_status, &H->db)) {
598 php_firebird_error(dbh);
599 }
600
601 if (H->date_format) {
602 efree(H->date_format);
603 }
604 if (H->time_format) {
605 efree(H->time_format);
606 }
607 if (H->timestamp_format) {
608 efree(H->timestamp_format);
609 }
610
611 if (H->einfo.errmsg) {
612 pefree(H->einfo.errmsg, dbh->is_persistent);
613 H->einfo.errmsg = NULL;
614 }
615
616 pefree(H, dbh->is_persistent);
617 }
618 /* }}} */
619
620 /* called by PDO to prepare an SQL query */
firebird_handle_preparer(pdo_dbh_t * dbh,zend_string * sql,pdo_stmt_t * stmt,zval * driver_options)621 static bool firebird_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, /* {{{ */
622 pdo_stmt_t *stmt, zval *driver_options)
623 {
624 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
625 pdo_firebird_stmt *S = NULL;
626 HashTable *np;
627
628 do {
629 isc_stmt_handle s = PDO_FIREBIRD_HANDLE_INITIALIZER;
630 XSQLDA num_sqlda;
631 static char const info[] = { isc_info_sql_stmt_type };
632 char result[8];
633
634 num_sqlda.version = PDO_FB_SQLDA_VERSION;
635 num_sqlda.sqln = 1;
636
637 ALLOC_HASHTABLE(np);
638 zend_hash_init(np, 8, NULL, NULL, 0);
639
640 /* allocate and prepare statement */
641 if (!php_firebird_alloc_prepare_stmt(dbh, sql, &num_sqlda, &s, np)) {
642 break;
643 }
644
645 /* allocate a statement handle struct of the right size (struct out_sqlda is inlined) */
646 S = ecalloc(1, sizeof(*S)-sizeof(XSQLDA) + XSQLDA_LENGTH(num_sqlda.sqld));
647 S->H = H;
648 S->stmt = s;
649 S->out_sqlda.version = PDO_FB_SQLDA_VERSION;
650 S->out_sqlda.sqln = stmt->column_count = num_sqlda.sqld;
651 S->named_params = np;
652
653 /* determine the statement type */
654 if (isc_dsql_sql_info(H->isc_status, &s, sizeof(info), const_cast(info), sizeof(result),
655 result)) {
656 break;
657 }
658 S->statement_type = result[3];
659
660 /* fill the output sqlda with information about the prepared query */
661 if (isc_dsql_describe(H->isc_status, &s, PDO_FB_SQLDA_VERSION, &S->out_sqlda)) {
662 php_firebird_error(dbh);
663 break;
664 }
665
666 #if FB_API_VER >= 40
667 /* set coercing a data type */
668 set_coercing_output_data_types(&S->out_sqlda);
669 #endif
670
671 /* allocate the input descriptors */
672 if (isc_dsql_describe_bind(H->isc_status, &s, PDO_FB_SQLDA_VERSION, &num_sqlda)) {
673 break;
674 }
675
676 if (num_sqlda.sqld) {
677 S->in_sqlda = ecalloc(1,XSQLDA_LENGTH(num_sqlda.sqld));
678 S->in_sqlda->version = PDO_FB_SQLDA_VERSION;
679 S->in_sqlda->sqln = num_sqlda.sqld;
680
681 if (isc_dsql_describe_bind(H->isc_status, &s, PDO_FB_SQLDA_VERSION, S->in_sqlda)) {
682 break;
683 }
684
685 /* make all parameters nullable */
686 unsigned int i;
687 XSQLVAR* var;
688 for (i = 0, var = S->in_sqlda->sqlvar; i < S->in_sqlda->sqld; i++, var++) {
689 /* The low bit of sqltype indicates that the parameter can take a NULL value */
690 var->sqltype |= 1;
691 }
692 }
693
694 stmt->driver_data = S;
695 stmt->methods = &firebird_stmt_methods;
696 stmt->supports_placeholders = PDO_PLACEHOLDER_POSITIONAL;
697
698 return true;
699
700 } while (0);
701
702 php_firebird_error(dbh);
703
704 zend_hash_destroy(np);
705 FREE_HASHTABLE(np);
706
707 if (S) {
708 if (S->in_sqlda) {
709 efree(S->in_sqlda);
710 }
711 efree(S);
712 }
713
714 return false;
715 }
716 /* }}} */
717
718 /* called by PDO to execute a statement that doesn't produce a result set */
firebird_handle_doer(pdo_dbh_t * dbh,const zend_string * sql)719 static zend_long firebird_handle_doer(pdo_dbh_t *dbh, const zend_string *sql) /* {{{ */
720 {
721 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
722 isc_stmt_handle stmt = PDO_FIREBIRD_HANDLE_INITIALIZER;
723 static char const info_count[] = { isc_info_sql_records };
724 char result[64];
725 int ret = 0;
726 XSQLDA in_sqlda, out_sqlda;
727
728 /* TODO no placeholders in exec() for now */
729 in_sqlda.version = out_sqlda.version = PDO_FB_SQLDA_VERSION;
730 in_sqlda.sqld = out_sqlda.sqld = 0;
731 out_sqlda.sqln = 1;
732
733 /* allocate and prepare statement */
734 if (!php_firebird_alloc_prepare_stmt(dbh, sql, &out_sqlda, &stmt, 0)) {
735 return -1;
736 }
737
738 /* execute the statement */
739 if (isc_dsql_execute2(H->isc_status, &H->tr, &stmt, PDO_FB_SQLDA_VERSION, &in_sqlda, &out_sqlda)) {
740 php_firebird_error(dbh);
741 ret = -1;
742 goto free_statement;
743 }
744
745 /* find out how many rows were affected */
746 if (isc_dsql_sql_info(H->isc_status, &stmt, sizeof(info_count), const_cast(info_count),
747 sizeof(result), result)) {
748 php_firebird_error(dbh);
749 ret = -1;
750 goto free_statement;
751 }
752
753 if (result[0] == isc_info_sql_records) {
754 unsigned i = 3, result_size = isc_vax_integer(&result[1],2);
755
756 if (result_size > sizeof(result)) {
757 ret = -1;
758 goto free_statement;
759 }
760 while (result[i] != isc_info_end && i < result_size) {
761 short len = (short)isc_vax_integer(&result[i+1],2);
762 /* bail out on bad len */
763 if (len != 1 && len != 2 && len != 4) {
764 ret = -1;
765 goto free_statement;
766 }
767 if (result[i] != isc_info_req_select_count) {
768 ret += isc_vax_integer(&result[i+3],len);
769 }
770 i += len+3;
771 }
772 }
773
774 if (dbh->auto_commit && !H->in_manually_txn) {
775 if (!php_firebird_commit_transaction(dbh, /* retain */ true)) {
776 ret = -1;
777 }
778 }
779
780 free_statement:
781
782 if (isc_dsql_free_statement(H->isc_status, &stmt, DSQL_drop)) {
783 php_firebird_error(dbh);
784 }
785
786 return ret;
787 }
788 /* }}} */
789
790 /* called by the PDO SQL parser to add quotes to values that are copied into SQL */
firebird_handle_quoter(pdo_dbh_t * dbh,const zend_string * unquoted,enum pdo_param_type paramtype)791 static zend_string* firebird_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
792 {
793 size_t qcount = 0;
794 char const *co, *l, *r;
795 char *c;
796 size_t quotedlen;
797 zend_string *quoted_str;
798
799 if (ZSTR_LEN(unquoted) == 0) {
800 return ZSTR_INIT_LITERAL("''", 0);
801 }
802
803 /* Firebird only requires single quotes to be doubled if string lengths are used */
804 /* count the number of ' characters */
805 for (co = ZSTR_VAL(unquoted); (co = strchr(co,'\'')); qcount++, co++);
806
807 if (UNEXPECTED(ZSTR_LEN(unquoted) + 2 > ZSTR_MAX_LEN - qcount)) {
808 return NULL;
809 }
810
811 quotedlen = ZSTR_LEN(unquoted) + qcount + 2;
812 quoted_str = zend_string_alloc(quotedlen, 0);
813 c = ZSTR_VAL(quoted_str);
814 *c++ = '\'';
815
816 /* foreach (chunk that ends in a quote) */
817 for (l = ZSTR_VAL(unquoted); (r = strchr(l,'\'')); l = r+1) {
818 strncpy(c, l, r-l+1);
819 c += (r-l+1);
820 /* add the second quote */
821 *c++ = '\'';
822 }
823
824 /* copy the remainder */
825 strncpy(c, l, quotedlen-(c-ZSTR_VAL(quoted_str))-1);
826 ZSTR_VAL(quoted_str)[quotedlen-1] = '\'';
827 ZSTR_VAL(quoted_str)[quotedlen] = '\0';
828
829 return quoted_str;
830 }
831 /* }}} */
832
833 /* php_firebird_begin_transaction */
php_firebird_begin_transaction(pdo_dbh_t * dbh,bool is_auto_commit_txn)834 static bool php_firebird_begin_transaction(pdo_dbh_t *dbh, bool is_auto_commit_txn) /* {{{ */
835 {
836 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
837
838 /* isc_xxx are all 1 byte. */
839 char tpb[4] = { isc_tpb_version3 };
840 size_t tpb_size;
841
842 /* access mode. writable or readonly */
843 tpb[1] = H->is_writable_txn ? isc_tpb_write : isc_tpb_read;
844
845 if (is_auto_commit_txn) {
846 /*
847 * In autocommit mode, we need to always read the latest information, so we set `read committed`.
848 */
849 tpb[2] = isc_tpb_read_committed;
850 /* Ignore indeterminate data from other transactions. This option only required with `read committed`. */
851 tpb[3] = isc_tpb_rec_version;
852 tpb_size = 4;
853 } else {
854 switch (H->txn_isolation_level) {
855 /*
856 * firebird's `read committed` has the option to wait until other transactions
857 * commit or rollback if there is indeterminate data.
858 * Introducing too many configuration values at once can cause confusion, so
859 * we don't support in PDO that feature yet.
860 */
861 case PDO_FB_READ_COMMITTED:
862 tpb[2] = isc_tpb_read_committed;
863 /* Ignore indeterminate data from other transactions. This option only required with `read committed`. */
864 tpb[3] = isc_tpb_rec_version;
865 tpb_size = 4;
866 break;
867
868 case PDO_FB_SERIALIZABLE:
869 tpb[2] = isc_tpb_consistency;
870 tpb_size = 3;
871 break;
872
873 case PDO_FB_REPEATABLE_READ:
874 default:
875 tpb[2] = isc_tpb_concurrency;
876 tpb_size = 3;
877 break;
878 }
879 }
880
881 if (isc_start_transaction(H->isc_status, &H->tr, 1, &H->db, tpb_size, tpb)) {
882 php_firebird_error(dbh);
883 return false;
884 }
885 return true;
886 }
887 /* }}} */
888
889 /* called by PDO to start a transaction */
firebird_handle_manually_begin(pdo_dbh_t * dbh)890 static bool firebird_handle_manually_begin(pdo_dbh_t *dbh) /* {{{ */
891 {
892 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
893
894 /**
895 * If in autocommit mode and in transaction, we will need to close the transaction once.
896 */
897 if (dbh->auto_commit && H->tr) {
898 if (!php_firebird_commit_transaction(dbh, /* retain */ false)) {
899 return false;
900 }
901 }
902
903 if (!php_firebird_begin_transaction(dbh, /* auto commit mode */ false)) {
904 return false;
905 }
906 H->in_manually_txn = 1;
907 return true;
908 }
909 /* }}} */
910
911 /* php_firebird_commit_transaction */
php_firebird_commit_transaction(pdo_dbh_t * dbh,bool retain)912 bool php_firebird_commit_transaction(pdo_dbh_t *dbh, bool retain) /* {{{ */
913 {
914 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
915
916 /**
917 * `retaining` keeps the transaction open without closing it.
918 *
919 * firebird needs to always have a transaction open to emulate autocommit mode,
920 * and in autocommit mode it keeps the transaction open.
921 *
922 * Same as close and then begin again, but use retain to save overhead.
923 */
924 if (retain) {
925 if (isc_commit_retaining(H->isc_status, &H->tr)) {
926 php_firebird_error(dbh);
927 return false;
928 }
929 } else {
930 if (isc_commit_transaction(H->isc_status, &H->tr)) {
931 php_firebird_error(dbh);
932 return false;
933 }
934 }
935 return true;
936 }
937 /* }}} */
938
939 /* called by PDO to commit a transaction */
firebird_handle_manually_commit(pdo_dbh_t * dbh)940 static bool firebird_handle_manually_commit(pdo_dbh_t *dbh) /* {{{ */
941 {
942 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
943 if (!php_firebird_commit_transaction(dbh, /*release*/ false)) {
944 return false;
945 }
946
947 /**
948 * If in autocommit mode, begin the transaction again
949 * Reopen instead of retain because isolation level may change
950 */
951 if (dbh->auto_commit) {
952 if (!php_firebird_begin_transaction(dbh, /* auto commit mode */ true)) {
953 return false;
954 }
955 }
956 H->in_manually_txn = 0;
957 return true;
958 }
959 /* }}} */
960
961 /* php_firebird_rollback_transaction */
php_firebird_rollback_transaction(pdo_dbh_t * dbh)962 static bool php_firebird_rollback_transaction(pdo_dbh_t *dbh) /* {{{ */
963 {
964 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
965
966 if (isc_rollback_transaction(H->isc_status, &H->tr)) {
967 php_firebird_error(dbh);
968 return false;
969 }
970 return true;
971 }
972 /* }}} */
973
974 /* called by PDO to rollback a transaction */
firebird_handle_manually_rollback(pdo_dbh_t * dbh)975 static bool firebird_handle_manually_rollback(pdo_dbh_t *dbh) /* {{{ */
976 {
977 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
978
979 if (!php_firebird_rollback_transaction(dbh)) {
980 return false;
981 }
982
983 /**
984 * If in autocommit mode, begin the transaction again
985 * Reopen instead of retain because isolation level may change
986 */
987 if (dbh->auto_commit) {
988 if (!php_firebird_begin_transaction(dbh, /* auto commit mode */ true)) {
989 return false;
990 }
991 }
992 H->in_manually_txn = 0;
993 return true;
994 }
995 /* }}} */
996
997 /* used by prepare and exec to allocate a statement handle and prepare the SQL */
php_firebird_alloc_prepare_stmt(pdo_dbh_t * dbh,const zend_string * sql,XSQLDA * out_sqlda,isc_stmt_handle * s,HashTable * named_params)998 static int php_firebird_alloc_prepare_stmt(pdo_dbh_t *dbh, const zend_string *sql,
999 XSQLDA *out_sqlda, isc_stmt_handle *s, HashTable *named_params)
1000 {
1001 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
1002 char *new_sql;
1003
1004 /* allocate the statement */
1005 if (isc_dsql_allocate_statement(H->isc_status, &H->db, s)) {
1006 php_firebird_error(dbh);
1007 return 0;
1008 }
1009
1010 /* in order to support named params, which Firebird itself doesn't,
1011 we need to replace :foo by ?, and store the name we just replaced */
1012 new_sql = emalloc(ZSTR_LEN(sql)+1);
1013 new_sql[0] = '\0';
1014 if (!php_firebird_preprocess(sql, new_sql, named_params)) {
1015 php_firebird_error_with_info(dbh, "07000", strlen("07000"), NULL, 0);
1016 efree(new_sql);
1017 return 0;
1018 }
1019
1020 /* prepare the statement */
1021 if (isc_dsql_prepare(H->isc_status, &H->tr, s, 0, new_sql, H->sql_dialect, out_sqlda)) {
1022 php_firebird_error(dbh);
1023 efree(new_sql);
1024 return 0;
1025 }
1026
1027 efree(new_sql);
1028 return 1;
1029 }
1030
1031 /* called by PDO to set a driver-specific dbh attribute */
pdo_firebird_set_attribute(pdo_dbh_t * dbh,zend_long attr,zval * val)1032 static bool pdo_firebird_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val) /* {{{ */
1033 {
1034 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
1035 bool bval;
1036 zend_long lval;
1037
1038 switch (attr) {
1039 case PDO_ATTR_AUTOCOMMIT:
1040 {
1041 if (!pdo_get_bool_param(&bval, val)) {
1042 return false;
1043 }
1044
1045 if (H->in_manually_txn) {
1046 /* change auto commit mode with an open transaction is illegal, because
1047 we won't know what to do with it */
1048 pdo_raise_impl_error(dbh, NULL, "HY000", "Cannot change autocommit mode while a transaction is already open");
1049 return false;
1050 }
1051
1052 /* ignore if the new value equals the old one */
1053 if (dbh->auto_commit ^ bval) {
1054 if (bval) {
1055 /*
1056 * change to auto commit mode.
1057 * If the transaction is not started, start it.
1058 */
1059 if (!H->tr) {
1060 if (!php_firebird_begin_transaction(dbh, /* auto commit mode */ true)) {
1061 return false;
1062 }
1063 }
1064 } else {
1065 /*
1066 * change to not auto commit mode.
1067 * close the transaction if exists.
1068 */
1069 if (H->tr) {
1070 if (!php_firebird_commit_transaction(dbh, /* retain */ false)) {
1071 return false;
1072 }
1073 }
1074 }
1075 dbh->auto_commit = bval;
1076 }
1077 }
1078 return true;
1079
1080 case PDO_ATTR_FETCH_TABLE_NAMES:
1081 if (!pdo_get_bool_param(&bval, val)) {
1082 return false;
1083 }
1084 H->fetch_table_names = bval;
1085 return true;
1086
1087 case PDO_FB_ATTR_DATE_FORMAT:
1088 {
1089 zend_string *str = zval_try_get_string(val);
1090 if (UNEXPECTED(!str)) {
1091 return false;
1092 }
1093 if (H->date_format) {
1094 efree(H->date_format);
1095 }
1096 spprintf(&H->date_format, 0, "%s", ZSTR_VAL(str));
1097 zend_string_release_ex(str, 0);
1098 }
1099 return true;
1100
1101 case PDO_FB_ATTR_TIME_FORMAT:
1102 {
1103 zend_string *str = zval_try_get_string(val);
1104 if (UNEXPECTED(!str)) {
1105 return false;
1106 }
1107 if (H->time_format) {
1108 efree(H->time_format);
1109 }
1110 spprintf(&H->time_format, 0, "%s", ZSTR_VAL(str));
1111 zend_string_release_ex(str, 0);
1112 }
1113 return true;
1114
1115 case PDO_FB_ATTR_TIMESTAMP_FORMAT:
1116 {
1117 zend_string *str = zval_try_get_string(val);
1118 if (UNEXPECTED(!str)) {
1119 return false;
1120 }
1121 if (H->timestamp_format) {
1122 efree(H->timestamp_format);
1123 }
1124 spprintf(&H->timestamp_format, 0, "%s", ZSTR_VAL(str));
1125 zend_string_release_ex(str, 0);
1126 }
1127 return true;
1128
1129 case PDO_FB_TRANSACTION_ISOLATION_LEVEL:
1130 {
1131 if (!pdo_get_long_param(&lval, val)) {
1132 return false;
1133 }
1134
1135 if (H->in_manually_txn) {
1136 pdo_raise_impl_error(dbh, NULL, "HY000", "Cannot change transaction isolation level while a transaction is already open");
1137 return false;
1138 }
1139
1140 /* ignore if the new value equals the old one */
1141 if (H->txn_isolation_level != lval) {
1142 if (lval == PDO_FB_READ_COMMITTED ||
1143 lval == PDO_FB_REPEATABLE_READ ||
1144 lval == PDO_FB_SERIALIZABLE
1145 ) {
1146 /*
1147 * Autocommit mode is always read-committed, so this setting is used the next time
1148 * a manual transaction starts. Therefore, there is no need to immediately reopen the transaction.
1149 */
1150 H->txn_isolation_level = lval;
1151 } else {
1152 zend_value_error("Pdo\\Firebird::TRANSACTION_ISOLATION_LEVEL must be a valid transaction isolation level "
1153 "(Pdo\\Firebird::READ_COMMITTED, Pdo\\Firebird::REPEATABLE_READ, or Pdo\\Firebird::SERIALIZABLE)");
1154 return false;
1155 }
1156 }
1157 }
1158 return true;
1159
1160 case PDO_FB_WRITABLE_TRANSACTION:
1161 {
1162 if (!pdo_get_bool_param(&bval, val)) {
1163 return false;
1164 }
1165
1166 if (H->in_manually_txn) {
1167 pdo_raise_impl_error(dbh, NULL, "HY000", "Cannot change access mode while a transaction is already open");
1168 return false;
1169 }
1170
1171 /* ignore if the new value equals the old one */
1172 if (H->is_writable_txn != bval) {
1173 H->is_writable_txn = bval;
1174 if (dbh->auto_commit) {
1175 if (H->tr) {
1176 if (!php_firebird_commit_transaction(dbh, /* retain */ false)) {
1177 /* In case of error, revert the setting */
1178 H->is_writable_txn = !bval;
1179 return false;
1180 }
1181 }
1182 if (!php_firebird_begin_transaction(dbh, /* auto commit mode */ true)) {
1183 /* In case of error, revert the setting */
1184 H->is_writable_txn = !bval;
1185 return false;
1186 }
1187 }
1188 }
1189 }
1190 return true;
1191 }
1192 return false;
1193 }
1194 /* }}} */
1195
1196 #define INFO_BUF_LEN 512
1197
1198 /* callback to used to report database server info */
php_firebird_info_cb(void * arg,char const * s)1199 static void php_firebird_info_cb(void *arg, char const *s) /* {{{ */
1200 {
1201 if (arg) {
1202 if (*(char*)arg) { /* second call */
1203 strlcat(arg, " ", INFO_BUF_LEN);
1204 }
1205 strlcat(arg, s, INFO_BUF_LEN);
1206 }
1207 }
1208 /* }}} */
1209
1210 /* called by PDO to get a driver-specific dbh attribute */
pdo_firebird_get_attribute(pdo_dbh_t * dbh,zend_long attr,zval * val)1211 static int pdo_firebird_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val) /* {{{ */
1212 {
1213 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
1214
1215 switch (attr) {
1216 char tmp[INFO_BUF_LEN];
1217
1218 case PDO_ATTR_AUTOCOMMIT:
1219 ZVAL_BOOL(val,dbh->auto_commit);
1220 return 1;
1221
1222 case PDO_ATTR_CONNECTION_STATUS:
1223 ZVAL_BOOL(val, !isc_version(&H->db, php_firebird_info_cb, NULL));
1224 return 1;
1225
1226 case PDO_ATTR_CLIENT_VERSION:
1227 isc_get_client_version(tmp);
1228 ZVAL_STRING(val, tmp);
1229 return 1;
1230
1231 case PDO_ATTR_SERVER_VERSION:
1232 case PDO_ATTR_SERVER_INFO:
1233 *tmp = 0;
1234
1235 if (!isc_version(&H->db, php_firebird_info_cb, (void*)tmp)) {
1236 ZVAL_STRING(val, tmp);
1237 return 1;
1238 }
1239 return -1;
1240
1241 case PDO_ATTR_FETCH_TABLE_NAMES:
1242 ZVAL_BOOL(val, H->fetch_table_names);
1243 return 1;
1244
1245 case PDO_FB_ATTR_DATE_FORMAT:
1246 ZVAL_STRING(val, H->date_format ? H->date_format : PDO_FB_DEF_DATE_FMT);
1247 return 1;
1248
1249 case PDO_FB_ATTR_TIME_FORMAT:
1250 ZVAL_STRING(val, H->time_format ? H->time_format : PDO_FB_DEF_TIME_FMT);
1251 return 1;
1252
1253 case PDO_FB_ATTR_TIMESTAMP_FORMAT:
1254 ZVAL_STRING(val, H->timestamp_format ? H->timestamp_format : PDO_FB_DEF_TIMESTAMP_FMT);
1255 return 1;
1256
1257 case PDO_FB_TRANSACTION_ISOLATION_LEVEL:
1258 ZVAL_LONG(val, H->txn_isolation_level);
1259 return 1;
1260
1261 case PDO_FB_WRITABLE_TRANSACTION:
1262 ZVAL_BOOL(val, H->is_writable_txn);
1263 return 1;
1264 }
1265 return 0;
1266 }
1267 /* }}} */
1268
1269 /* called by PDO to check liveness */
pdo_firebird_check_liveness(pdo_dbh_t * dbh)1270 static zend_result pdo_firebird_check_liveness(pdo_dbh_t *dbh) /* {{{ */
1271 {
1272 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
1273
1274 /* fb_ping return 0 if the connection is alive */
1275 return fb_ping(H->isc_status, &H->db) ? FAILURE : SUCCESS;
1276 }
1277 /* }}} */
1278
1279 /* called by PDO to retrieve driver-specific information about an error that has occurred */
pdo_firebird_fetch_error_func(pdo_dbh_t * dbh,pdo_stmt_t * stmt,zval * info)1280 static void pdo_firebird_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info) /* {{{ */
1281 {
1282 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
1283 if (H->einfo.sqlcode != IS_NULL) {
1284 add_next_index_long(info, H->einfo.sqlcode);
1285 }
1286 if (H->einfo.errmsg && H->einfo.errmsg_length) {
1287 add_next_index_stringl(info, H->einfo.errmsg, H->einfo.errmsg_length);
1288 }
1289 }
1290 /* }}} */
1291
1292 /* {{{ firebird_in_manually_transaction */
pdo_firebird_in_manually_transaction(pdo_dbh_t * dbh)1293 static bool pdo_firebird_in_manually_transaction(pdo_dbh_t *dbh)
1294 {
1295 /**
1296 * we can tell if a transaction exists now by checking H->tr,
1297 * but which will always be true in autocommit mode.
1298 * So this function checks if there is currently a "manually begun transaction".
1299 */
1300 pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
1301 return H->in_manually_txn;
1302 }
1303 /* }}} */
1304
1305 static const struct pdo_dbh_methods firebird_methods = { /* {{{ */
1306 firebird_handle_closer,
1307 firebird_handle_preparer,
1308 firebird_handle_doer,
1309 firebird_handle_quoter,
1310 firebird_handle_manually_begin,
1311 firebird_handle_manually_commit,
1312 firebird_handle_manually_rollback,
1313 pdo_firebird_set_attribute,
1314 NULL, /* last_id not supported */
1315 pdo_firebird_fetch_error_func,
1316 pdo_firebird_get_attribute,
1317 pdo_firebird_check_liveness,
1318 NULL, /* get driver methods */
1319 NULL, /* request shutdown */
1320 pdo_firebird_in_manually_transaction,
1321 NULL, /* get gc */
1322 NULL /* scanner */
1323 };
1324 /* }}} */
1325
1326 /* the driver-specific PDO handle constructor */
pdo_firebird_handle_factory(pdo_dbh_t * dbh,zval * driver_options)1327 static int pdo_firebird_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
1328 {
1329 struct pdo_data_src_parser vars[] = {
1330 { "dbname", NULL, 0 },
1331 { "charset", NULL, 0 },
1332 { "role", NULL, 0 },
1333 { "dialect", "3", 0 },
1334 { "user", NULL, 0 },
1335 { "password", NULL, 0 }
1336 };
1337 int i, ret = 0;
1338 short buf_len = 256, dpb_len;
1339
1340 pdo_firebird_db_handle *H = dbh->driver_data = pecalloc(1,sizeof(*H),dbh->is_persistent);
1341
1342 php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 6);
1343
1344 if (!dbh->username && vars[4].optval) {
1345 dbh->username = pestrdup(vars[4].optval, dbh->is_persistent);
1346 }
1347
1348 if (!dbh->password && vars[5].optval) {
1349 dbh->password = pestrdup(vars[5].optval, dbh->is_persistent);
1350 }
1351
1352 H->in_manually_txn = 0;
1353 H->is_writable_txn = pdo_attr_lval(driver_options, PDO_FB_WRITABLE_TRANSACTION, 1);
1354 zend_long txn_isolation_level = pdo_attr_lval(driver_options, PDO_FB_TRANSACTION_ISOLATION_LEVEL, PDO_FB_REPEATABLE_READ);
1355 if (txn_isolation_level == PDO_FB_READ_COMMITTED ||
1356 txn_isolation_level == PDO_FB_REPEATABLE_READ ||
1357 txn_isolation_level == PDO_FB_SERIALIZABLE
1358 ) {
1359 H->txn_isolation_level = txn_isolation_level;
1360 } else {
1361 zend_value_error("Pdo\\Firebird::TRANSACTION_ISOLATION_LEVEL must be a valid transaction isolation level "
1362 "(Pdo\\Firebird::READ_COMMITTED, Pdo\\Firebird::REPEATABLE_READ, or Pdo\\Firebird::SERIALIZABLE)");
1363 ret = 0;
1364 }
1365
1366 do {
1367 static char const dpb_flags[] = {
1368 isc_dpb_user_name, isc_dpb_password, isc_dpb_lc_ctype, isc_dpb_sql_role_name };
1369 char const *dpb_values[] = { dbh->username, dbh->password, vars[1].optval, vars[2].optval };
1370 char dpb_buffer[256] = { isc_dpb_version1 }, *dpb;
1371
1372 dpb = dpb_buffer + 1;
1373
1374 /* loop through all the provided arguments and set dpb fields accordingly */
1375 for (i = 0; i < sizeof(dpb_flags); ++i) {
1376 if (dpb_values[i] && buf_len > 0) {
1377 dpb_len = slprintf(dpb, buf_len, "%c%c%s", dpb_flags[i], (unsigned char)strlen(dpb_values[i]),
1378 dpb_values[i]);
1379 dpb += dpb_len;
1380 buf_len -= dpb_len;
1381 }
1382 }
1383
1384 H->sql_dialect = PDO_FB_DIALECT;
1385 if (vars[3].optval) {
1386 H->sql_dialect = atoi(vars[3].optval);
1387 }
1388
1389 /* fire it up baby! */
1390 if (isc_attach_database(H->isc_status, 0, vars[0].optval, &H->db,(short)(dpb-dpb_buffer), dpb_buffer)) {
1391 break;
1392 }
1393
1394 dbh->methods = &firebird_methods;
1395 dbh->native_case = PDO_CASE_UPPER;
1396 dbh->alloc_own_columns = 1;
1397
1398 ret = 1;
1399
1400 } while (0);
1401
1402 for (i = 0; i < sizeof(vars)/sizeof(vars[0]); ++i) {
1403 if (vars[i].freeme) {
1404 efree(vars[i].optval);
1405 }
1406 }
1407
1408 if (!dbh->methods) {
1409 char errmsg[512];
1410 const ISC_STATUS *s = H->isc_status;
1411 fb_interpret(errmsg, sizeof(errmsg),&s);
1412 zend_throw_exception_ex(php_pdo_get_exception(), H->isc_status[1], "SQLSTATE[%s] [%" PRIiPTR "] %s",
1413 "HY000", H->isc_status[1], errmsg);
1414 }
1415
1416 if (dbh->auto_commit && !H->tr) {
1417 ret = php_firebird_begin_transaction(dbh, /* auto commit mode */ true);
1418 }
1419
1420 if (!ret) {
1421 firebird_handle_closer(dbh);
1422 }
1423
1424 return ret;
1425 }
1426 /* }}} */
1427
1428
1429 const pdo_driver_t pdo_firebird_driver = { /* {{{ */
1430 PDO_DRIVER_HEADER(firebird),
1431 pdo_firebird_handle_factory
1432 };
1433 /* }}} */
1434