1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) The PHP Group                                          |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: Ard Biesheuvel <abies@php.net>                               |
16   +----------------------------------------------------------------------+
17 */
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #define _GNU_SOURCE
24 
25 #include "php.h"
26 #include "zend_exceptions.h"
27 #include "php_ini.h"
28 #include "ext/standard/info.h"
29 #include "pdo/php_pdo.h"
30 #include "pdo/php_pdo_driver.h"
31 #include "php_pdo_firebird.h"
32 #include "php_pdo_firebird_int.h"
33 
34 static int firebird_alloc_prepare_stmt(pdo_dbh_t*, const char*, size_t, XSQLDA*, isc_stmt_handle*,
35 	HashTable*);
36 
37 /* map driver specific error message to PDO error */
_firebird_error(pdo_dbh_t * dbh,pdo_stmt_t * stmt,char const * file,zend_long line)38 void _firebird_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, char const *file, zend_long line) /* {{{ */
39 {
40 	pdo_error_type *const error_code = stmt ? &stmt->error_code : &dbh->error_code;
41 
42 	strcpy(*error_code, "HY000");
43 }
44 /* }}} */
45 
46 #define RECORD_ERROR(dbh) _firebird_error(dbh, NULL, __FILE__, __LINE__)
47 
48 /* called by PDO to close a db handle */
firebird_handle_closer(pdo_dbh_t * dbh)49 static int firebird_handle_closer(pdo_dbh_t *dbh) /* {{{ */
50 {
51 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
52 
53 	if (dbh->in_txn) {
54 		if (dbh->auto_commit) {
55 			if (isc_commit_transaction(H->isc_status, &H->tr)) {
56 				RECORD_ERROR(dbh);
57 			}
58 		} else {
59 			if (isc_rollback_transaction(H->isc_status, &H->tr)) {
60 				RECORD_ERROR(dbh);
61 			}
62 		}
63 	}
64 
65 	if (isc_detach_database(H->isc_status, &H->db)) {
66 		RECORD_ERROR(dbh);
67 	}
68 
69 	if (H->date_format) {
70 		efree(H->date_format);
71 	}
72 	if (H->time_format) {
73 		efree(H->time_format);
74 	}
75 	if (H->timestamp_format) {
76 		efree(H->timestamp_format);
77 	}
78 
79 	pefree(H, dbh->is_persistent);
80 
81 	return 0;
82 }
83 /* }}} */
84 
85 /* called by PDO to prepare an SQL query */
firebird_handle_preparer(pdo_dbh_t * dbh,const char * sql,size_t sql_len,pdo_stmt_t * stmt,zval * driver_options)86 static int firebird_handle_preparer(pdo_dbh_t *dbh, const char *sql, size_t sql_len, /* {{{ */
87 	pdo_stmt_t *stmt, zval *driver_options)
88 {
89 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
90 	pdo_firebird_stmt *S = NULL;
91 	HashTable *np;
92 
93 	do {
94 		isc_stmt_handle s = PDO_FIREBIRD_HANDLE_INITIALIZER;
95 		XSQLDA num_sqlda;
96 		static char const info[] = { isc_info_sql_stmt_type };
97 		char result[8];
98 
99 		num_sqlda.version = PDO_FB_SQLDA_VERSION;
100 		num_sqlda.sqln = 1;
101 
102 		ALLOC_HASHTABLE(np);
103 		zend_hash_init(np, 8, NULL, NULL, 0);
104 
105 		/* allocate and prepare statement */
106 		if (!firebird_alloc_prepare_stmt(dbh, sql, sql_len, &num_sqlda, &s, np)) {
107 			break;
108 		}
109 
110 		/* allocate a statement handle struct of the right size (struct out_sqlda is inlined) */
111 		S = ecalloc(1, sizeof(*S)-sizeof(XSQLDA) + XSQLDA_LENGTH(num_sqlda.sqld));
112 		S->H = H;
113 		S->stmt = s;
114 		S->fetch_buf = ecalloc(1,sizeof(char*) * num_sqlda.sqld);
115 		S->out_sqlda.version = PDO_FB_SQLDA_VERSION;
116 		S->out_sqlda.sqln = stmt->column_count = num_sqlda.sqld;
117 		S->named_params = np;
118 
119 		/* determine the statement type */
120 		if (isc_dsql_sql_info(H->isc_status, &s, sizeof(info), const_cast(info), sizeof(result),
121 				result)) {
122 			break;
123 		}
124 		S->statement_type = result[3];
125 
126 		/* fill the output sqlda with information about the prepared query */
127 		if (isc_dsql_describe(H->isc_status, &s, PDO_FB_SQLDA_VERSION, &S->out_sqlda)) {
128 			RECORD_ERROR(dbh);
129 			break;
130 		}
131 
132 		/* allocate the input descriptors */
133 		if (isc_dsql_describe_bind(H->isc_status, &s, PDO_FB_SQLDA_VERSION, &num_sqlda)) {
134 			break;
135 		}
136 
137 		if (num_sqlda.sqld) {
138 			S->in_sqlda = ecalloc(1,XSQLDA_LENGTH(num_sqlda.sqld));
139 			S->in_sqlda->version = PDO_FB_SQLDA_VERSION;
140 			S->in_sqlda->sqln = num_sqlda.sqld;
141 
142 			if (isc_dsql_describe_bind(H->isc_status, &s, PDO_FB_SQLDA_VERSION, S->in_sqlda)) {
143 				break;
144 			}
145 		}
146 
147 		stmt->driver_data = S;
148 		stmt->methods = &firebird_stmt_methods;
149 		stmt->supports_placeholders = PDO_PLACEHOLDER_POSITIONAL;
150 
151 		return 1;
152 
153 	} while (0);
154 
155 	RECORD_ERROR(dbh);
156 
157 	zend_hash_destroy(np);
158 	FREE_HASHTABLE(np);
159 
160 	if (S) {
161 		if (S->in_sqlda) {
162 			efree(S->in_sqlda);
163 		}
164 		efree(S);
165 	}
166 
167 	return 0;
168 }
169 /* }}} */
170 
171 /* called by PDO to execute a statement that doesn't produce a result set */
firebird_handle_doer(pdo_dbh_t * dbh,const char * sql,size_t sql_len)172 static zend_long firebird_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len) /* {{{ */
173 {
174 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
175 	isc_stmt_handle stmt = PDO_FIREBIRD_HANDLE_INITIALIZER;
176 	static char const info_count[] = { isc_info_sql_records };
177 	char result[64];
178 	int ret = 0;
179 	XSQLDA in_sqlda, out_sqlda;
180 
181 	/* TODO no placeholders in exec() for now */
182 	in_sqlda.version = out_sqlda.version = PDO_FB_SQLDA_VERSION;
183 	in_sqlda.sqld = out_sqlda.sqld = 0;
184 	out_sqlda.sqln = 1;
185 
186 	/* allocate and prepare statement */
187 	if (!firebird_alloc_prepare_stmt(dbh, sql, sql_len, &out_sqlda, &stmt, 0)) {
188 		return -1;
189 	}
190 
191 	/* execute the statement */
192 	if (isc_dsql_execute2(H->isc_status, &H->tr, &stmt, PDO_FB_SQLDA_VERSION, &in_sqlda, &out_sqlda)) {
193 		RECORD_ERROR(dbh);
194 		ret = -1;
195 		goto free_statement;
196 	}
197 
198 	/* find out how many rows were affected */
199 	if (isc_dsql_sql_info(H->isc_status, &stmt, sizeof(info_count), const_cast(info_count),
200 			sizeof(result),	result)) {
201 		RECORD_ERROR(dbh);
202 		ret = -1;
203 		goto free_statement;
204 	}
205 
206 	if (result[0] == isc_info_sql_records) {
207 		unsigned i = 3, result_size = isc_vax_integer(&result[1],2);
208 
209 		if (result_size > sizeof(result)) {
210 			ret = -1;
211 			goto free_statement;
212 		}
213 		while (result[i] != isc_info_end && i < result_size) {
214 			short len = (short)isc_vax_integer(&result[i+1],2);
215 			/* bail out on bad len */
216 			if (len != 1 && len != 2 && len != 4) {
217 				ret = -1;
218 				goto free_statement;
219 			}
220 			if (result[i] != isc_info_req_select_count) {
221 				ret += isc_vax_integer(&result[i+3],len);
222 			}
223 			i += len+3;
224 		}
225 	}
226 
227 	/* commit if we're in auto_commit mode */
228 	if (dbh->auto_commit && isc_commit_retaining(H->isc_status, &H->tr)) {
229 		RECORD_ERROR(dbh);
230 	}
231 
232 free_statement:
233 
234 	if (isc_dsql_free_statement(H->isc_status, &stmt, DSQL_drop)) {
235 		RECORD_ERROR(dbh);
236 	}
237 
238 	return ret;
239 }
240 /* }}} */
241 
242 /* called by the PDO SQL parser to add quotes to values that are copied into SQL */
firebird_handle_quoter(pdo_dbh_t * dbh,const char * unquoted,size_t unquotedlen,char ** quoted,size_t * quotedlen,enum pdo_param_type paramtype)243 static int firebird_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, /* {{{ */
244 	char **quoted, size_t *quotedlen, enum pdo_param_type paramtype)
245 {
246 	int qcount = 0;
247 	char const *co, *l, *r;
248 	char *c;
249 
250 	if (!unquotedlen) {
251 		*quotedlen = 2;
252 		*quoted = emalloc(*quotedlen+1);
253 		strcpy(*quoted, "''");
254 		return 1;
255 	}
256 
257 	/* Firebird only requires single quotes to be doubled if string lengths are used */
258 	/* count the number of ' characters */
259 	for (co = unquoted; (co = strchr(co,'\'')); qcount++, co++);
260 
261 	*quotedlen = unquotedlen + qcount + 2;
262 	*quoted = c = emalloc(*quotedlen+1);
263 	*c++ = '\'';
264 
265 	/* foreach (chunk that ends in a quote) */
266 	for (l = unquoted; (r = strchr(l,'\'')); l = r+1) {
267 		strncpy(c, l, r-l+1);
268 		c += (r-l+1);
269 		/* add the second quote */
270 		*c++ = '\'';
271 	}
272 
273 	/* copy the remainder */
274 	strncpy(c, l, *quotedlen-(c-*quoted)-1);
275 	(*quoted)[*quotedlen-1] = '\'';
276 	(*quoted)[*quotedlen]   = '\0';
277 
278 	return 1;
279 }
280 /* }}} */
281 
282 /* called by PDO to start a transaction */
firebird_handle_begin(pdo_dbh_t * dbh)283 static int firebird_handle_begin(pdo_dbh_t *dbh) /* {{{ */
284 {
285 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
286 	char tpb[8] = { isc_tpb_version3 }, *ptpb = tpb+1;
287 #if abies_0
288 	if (dbh->transaction_flags & PDO_TRANS_ISOLATION_LEVEL) {
289 		if (dbh->transaction_flags & PDO_TRANS_READ_UNCOMMITTED) {
290 			/* this is a poor fit, but it's all we have */
291 			*ptpb++ = isc_tpb_read_committed;
292 			*ptpb++ = isc_tpb_rec_version;
293 			dbh->transaction_flags &= ~(PDO_TRANS_ISOLATION_LEVEL^PDO_TRANS_READ_UNCOMMITTED);
294 		} else if (dbh->transaction_flags & PDO_TRANS_READ_COMMITTED) {
295 			*ptpb++ = isc_tpb_read_committed;
296 			*ptpb++ = isc_tpb_no_rec_version;
297 			dbh->transaction_flags &= ~(PDO_TRANS_ISOLATION_LEVEL^PDO_TRANS_READ_COMMITTED);
298 		} else if (dbh->transaction_flags & PDO_TRANS_REPEATABLE_READ) {
299 			*ptpb++ = isc_tpb_concurrency;
300 			dbh->transaction_flags &= ~(PDO_TRANS_ISOLATION_LEVEL^PDO_TRANS_REPEATABLE_READ);
301 		} else {
302 			*ptpb++ = isc_tpb_consistency;
303 			dbh->transaction_flags &= ~(PDO_TRANS_ISOLATION_LEVEL^PDO_TRANS_SERIALIZABLE);
304 		}
305 	}
306 
307 	if (dbh->transaction_flags & PDO_TRANS_ACCESS_MODE) {
308 		if (dbh->transaction_flags & PDO_TRANS_READONLY) {
309 			*ptpb++ = isc_tpb_read;
310 			dbh->transaction_flags &= ~(PDO_TRANS_ACCESS_MODE^PDO_TRANS_READONLY);
311 		} else {
312 			*ptpb++ = isc_tpb_write;
313 			dbh->transaction_flags &= ~(PDO_TRANS_ACCESS_MODE^PDO_TRANS_READWRITE);
314 		}
315 	}
316 
317 	if (dbh->transaction_flags & PDO_TRANS_CONFLICT_RESOLUTION) {
318 		if (dbh->transaction_flags & PDO_TRANS_RETRY) {
319 			*ptpb++ = isc_tpb_wait;
320 			dbh->transaction_flags &= ~(PDO_TRANS_CONFLICT_RESOLUTION^PDO_TRANS_RETRY);
321 		} else {
322 			*ptpb++ = isc_tpb_nowait;
323 			dbh->transaction_flags &= ~(PDO_TRANS_CONFLICT_RESOLUTION^PDO_TRANS_ABORT);
324 		}
325 	}
326 #endif
327 	if (isc_start_transaction(H->isc_status, &H->tr, 1, &H->db, (unsigned short)(ptpb-tpb), tpb)) {
328 		RECORD_ERROR(dbh);
329 		return 0;
330 	}
331 	return 1;
332 }
333 /* }}} */
334 
335 /* called by PDO to commit a transaction */
firebird_handle_commit(pdo_dbh_t * dbh)336 static int firebird_handle_commit(pdo_dbh_t *dbh) /* {{{ */
337 {
338 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
339 
340 	if (isc_commit_transaction(H->isc_status, &H->tr)) {
341 		RECORD_ERROR(dbh);
342 		return 0;
343 	}
344 	return 1;
345 }
346 /* }}} */
347 
348 /* called by PDO to rollback a transaction */
firebird_handle_rollback(pdo_dbh_t * dbh)349 static int firebird_handle_rollback(pdo_dbh_t *dbh) /* {{{ */
350 {
351 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
352 
353 	if (isc_rollback_transaction(H->isc_status, &H->tr)) {
354 		RECORD_ERROR(dbh);
355 		return 0;
356 	}
357 	return 1;
358 }
359 /* }}} */
360 
361 /* used by prepare and exec to allocate a statement handle and prepare the SQL */
firebird_alloc_prepare_stmt(pdo_dbh_t * dbh,const char * sql,size_t sql_len,XSQLDA * out_sqlda,isc_stmt_handle * s,HashTable * named_params)362 static int firebird_alloc_prepare_stmt(pdo_dbh_t *dbh, const char *sql, size_t sql_len, /* {{{ */
363 	XSQLDA *out_sqlda, isc_stmt_handle *s, HashTable *named_params)
364 {
365 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
366 	char *c, *new_sql, in_quote, in_param, pname[64], *ppname;
367 	zend_long l, pindex = -1;
368 
369 	/* Firebird allows SQL statements up to 64k, so bail if it doesn't fit */
370 	if (sql_len > 65536) {
371 		strcpy(dbh->error_code, "01004");
372 		return 0;
373 	}
374 
375 	/* start a new transaction implicitly if auto_commit is enabled and no transaction is open */
376 	if (dbh->auto_commit && !dbh->in_txn) {
377 		/* dbh->transaction_flags = PDO_TRANS_READ_UNCOMMITTED; */
378 
379 		if (!firebird_handle_begin(dbh)) {
380 			return 0;
381 		}
382 		dbh->in_txn = 1;
383 	}
384 
385 	/* allocate the statement */
386 	if (isc_dsql_allocate_statement(H->isc_status, &H->db, s)) {
387 		RECORD_ERROR(dbh);
388 		return 0;
389 	}
390 
391 	/* in order to support named params, which Firebird itself doesn't,
392 	   we need to replace :foo by ?, and store the name we just replaced */
393 	new_sql = c = emalloc(sql_len+1);
394 
395 	for (l = in_quote = in_param = 0; l <= sql_len; ++l) {
396 		if ( !(in_quote ^= (sql[l] == '\''))) {
397 			if (!in_param) {
398 				switch (sql[l]) {
399 					case ':':
400 						in_param = 1;
401 						ppname = pname;
402 						*ppname++ = sql[l];
403 					case '?':
404 						*c++ = '?';
405 						++pindex;
406 					continue;
407 				}
408 			} else {
409                                 if ((in_param &= ((sql[l] >= 'A' && sql[l] <= 'Z') || (sql[l] >= 'a' && sql[l] <= 'z')
410                                         || (sql[l] >= '0' && sql[l] <= '9') || sql[l] == '_' || sql[l] == '-'))) {
411 
412 
413 					*ppname++ = sql[l];
414 					continue;
415 				} else {
416 					*ppname++ = 0;
417 					if (named_params) {
418 						zval tmp;
419 						ZVAL_LONG(&tmp, pindex);
420 						zend_hash_str_update(named_params, pname, (unsigned int)(ppname - pname - 1), &tmp);
421 					}
422 				}
423 			}
424 		}
425 		*c++ = sql[l];
426 	}
427 
428 	/* prepare the statement */
429 	if (isc_dsql_prepare(H->isc_status, &H->tr, s, 0, new_sql, H->sql_dialect, out_sqlda)) {
430 		RECORD_ERROR(dbh);
431 		efree(new_sql);
432 		return 0;
433 	}
434 
435 	efree(new_sql);
436 	return 1;
437 }
438 /* }}} */
439 
440 /* called by PDO to set a driver-specific dbh attribute */
firebird_handle_set_attribute(pdo_dbh_t * dbh,zend_long attr,zval * val)441 static int firebird_handle_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val) /* {{{ */
442 {
443 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
444 
445 	switch (attr) {
446 		case PDO_ATTR_AUTOCOMMIT:
447 			{
448 				zend_bool bval = zval_get_long(val)? 1 : 0;
449 
450 				/* ignore if the new value equals the old one */
451 				if (dbh->auto_commit ^ bval) {
452 					if (dbh->in_txn) {
453 						if (bval) {
454 							/* turning on auto_commit with an open transaction is illegal, because
455 							   we won't know what to do with it */
456 							H->last_app_error = "Cannot enable auto-commit while a transaction is already open";
457 							return 0;
458 						} else {
459 							/* close the transaction */
460 							if (!firebird_handle_commit(dbh)) {
461 								break;
462 							}
463 							dbh->in_txn = 0;
464 						}
465 					}
466 					dbh->auto_commit = bval;
467 				}
468 			}
469 			return 1;
470 
471 		case PDO_ATTR_FETCH_TABLE_NAMES:
472 			H->fetch_table_names = zval_get_long(val)? 1 : 0;
473 			return 1;
474 
475 		case PDO_FB_ATTR_DATE_FORMAT:
476 			{
477 				zend_string *str = zval_try_get_string(val);
478 				if (UNEXPECTED(!str)) {
479 					return 0;
480 				}
481 				if (H->date_format) {
482 					efree(H->date_format);
483 				}
484 				spprintf(&H->date_format, 0, "%s", ZSTR_VAL(str));
485 				zend_string_release_ex(str, 0);
486 			}
487 			return 1;
488 
489 		case PDO_FB_ATTR_TIME_FORMAT:
490 			{
491 				zend_string *str = zval_try_get_string(val);
492 				if (UNEXPECTED(!str)) {
493 					return 0;
494 				}
495 				if (H->time_format) {
496 					efree(H->time_format);
497 				}
498 				spprintf(&H->time_format, 0, "%s", ZSTR_VAL(str));
499 				zend_string_release_ex(str, 0);
500 			}
501 			return 1;
502 
503 		case PDO_FB_ATTR_TIMESTAMP_FORMAT:
504 			{
505 				zend_string *str = zval_try_get_string(val);
506 				if (UNEXPECTED(!str)) {
507 					return 0;
508 				}
509 				if (H->timestamp_format) {
510 					efree(H->timestamp_format);
511 				}
512 				spprintf(&H->timestamp_format, 0, "%s", ZSTR_VAL(str));
513 				zend_string_release_ex(str, 0);
514 			}
515 			return 1;
516 	}
517 	return 0;
518 }
519 /* }}} */
520 
521 #define INFO_BUF_LEN 512
522 
523 /* callback to used to report database server info */
firebird_info_cb(void * arg,char const * s)524 static void firebird_info_cb(void *arg, char const *s) /* {{{ */
525 {
526 	if (arg) {
527 		if (*(char*)arg) { /* second call */
528 			strlcat(arg, " ", INFO_BUF_LEN);
529 		}
530 		strlcat(arg, s, INFO_BUF_LEN);
531 	}
532 }
533 /* }}} */
534 
535 /* called by PDO to get a driver-specific dbh attribute */
firebird_handle_get_attribute(pdo_dbh_t * dbh,zend_long attr,zval * val)536 static int firebird_handle_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val) /* {{{ */
537 {
538 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
539 
540 	switch (attr) {
541 		char tmp[INFO_BUF_LEN];
542 
543 		case PDO_ATTR_AUTOCOMMIT:
544 			ZVAL_LONG(val,dbh->auto_commit);
545 			return 1;
546 
547 		case PDO_ATTR_CONNECTION_STATUS:
548 			ZVAL_BOOL(val, !isc_version(&H->db, firebird_info_cb, NULL));
549 			return 1;
550 
551 		case PDO_ATTR_CLIENT_VERSION: {
552 #if defined(__GNUC__) || defined(PHP_WIN32)
553 			info_func_t info_func = NULL;
554 #ifdef __GNUC__
555 			info_func = (info_func_t)dlsym(RTLD_DEFAULT, "isc_get_client_version");
556 #else
557 			HMODULE l = GetModuleHandle("fbclient");
558 
559 			if (!l) {
560 				break;
561 			}
562 			info_func = (info_func_t)GetProcAddress(l, "isc_get_client_version");
563 #endif
564 			if (info_func) {
565 				info_func(tmp);
566 				ZVAL_STRING(val, tmp);
567 			}
568 #else
569 			ZVAL_NULL(val);
570 #endif
571 			}
572 			return 1;
573 
574 		case PDO_ATTR_SERVER_VERSION:
575 		case PDO_ATTR_SERVER_INFO:
576 			*tmp = 0;
577 
578 			if (!isc_version(&H->db, firebird_info_cb, (void*)tmp)) {
579 				ZVAL_STRING(val, tmp);
580 				return 1;
581 			}
582 
583 		case PDO_ATTR_FETCH_TABLE_NAMES:
584 			ZVAL_BOOL(val, H->fetch_table_names);
585 			return 1;
586 	}
587 	return 0;
588 }
589 /* }}} */
590 
591 /* 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)592 static int pdo_firebird_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info) /* {{{ */
593 {
594 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
595 	const ISC_STATUS *s = H->isc_status;
596 	char buf[400];
597 	zend_long i = 0, l, sqlcode = isc_sqlcode(s);
598 
599 	if (sqlcode) {
600 		add_next_index_long(info, sqlcode);
601 
602 		while ((sizeof(buf)>(i+2))&&(l = fb_interpret(&buf[i],(sizeof(buf)-i-2),&s))) {
603 			i += l;
604 			strcpy(&buf[i++], " ");
605 		}
606 		add_next_index_string(info, buf);
607 	} else if (H->last_app_error) {
608 		add_next_index_long(info, -999);
609 		add_next_index_string(info, const_cast(H->last_app_error));
610 	}
611 	return 1;
612 }
613 /* }}} */
614 
615 static const struct pdo_dbh_methods firebird_methods = { /* {{{ */
616 	firebird_handle_closer,
617 	firebird_handle_preparer,
618 	firebird_handle_doer,
619 	firebird_handle_quoter,
620 	firebird_handle_begin,
621 	firebird_handle_commit,
622 	firebird_handle_rollback,
623 	firebird_handle_set_attribute,
624 	NULL, /* last_id not supported */
625 	pdo_firebird_fetch_error_func,
626 	firebird_handle_get_attribute,
627 	NULL /* check_liveness */
628 };
629 /* }}} */
630 
631 /* the driver-specific PDO handle constructor */
pdo_firebird_handle_factory(pdo_dbh_t * dbh,zval * driver_options)632 static int pdo_firebird_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
633 {
634 	struct pdo_data_src_parser vars[] = {
635 		{ "dbname", NULL, 0 },
636 		{ "charset",  NULL,	0 },
637 		{ "role", NULL,	0 },
638 		{ "dialect", "3", 0 },
639 		{ "user", NULL, 0 },
640 		{ "password", NULL, 0 }
641 	};
642 	int i, ret = 0;
643 	short buf_len = 256, dpb_len;
644 
645 	pdo_firebird_db_handle *H = dbh->driver_data = pecalloc(1,sizeof(*H),dbh->is_persistent);
646 
647 	php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 6);
648 
649 	if (!dbh->username && vars[4].optval) {
650 		dbh->username = pestrdup(vars[4].optval, dbh->is_persistent);
651 	}
652 
653 	if (!dbh->password && vars[5].optval) {
654 		dbh->password = pestrdup(vars[5].optval, dbh->is_persistent);
655 	}
656 
657 	do {
658 		static char const dpb_flags[] = {
659 			isc_dpb_user_name, isc_dpb_password, isc_dpb_lc_ctype, isc_dpb_sql_role_name };
660 		char const *dpb_values[] = { dbh->username, dbh->password, vars[1].optval, vars[2].optval };
661 		char dpb_buffer[256] = { isc_dpb_version1 }, *dpb;
662 
663 		dpb = dpb_buffer + 1;
664 
665 		/* loop through all the provided arguments and set dpb fields accordingly */
666 		for (i = 0; i < sizeof(dpb_flags); ++i) {
667 			if (dpb_values[i] && buf_len > 0) {
668 				dpb_len = slprintf(dpb, buf_len, "%c%c%s", dpb_flags[i], (unsigned char)strlen(dpb_values[i]),
669 					dpb_values[i]);
670 				dpb += dpb_len;
671 				buf_len -= dpb_len;
672 			}
673 		}
674 
675 		H->sql_dialect = PDO_FB_DIALECT;
676 		if (vars[3].optval) {
677 			H->sql_dialect = atoi(vars[3].optval);
678 		}
679 
680 		/* fire it up baby! */
681 		if (isc_attach_database(H->isc_status, 0, vars[0].optval, &H->db,(short)(dpb-dpb_buffer), dpb_buffer)) {
682 			break;
683 		}
684 
685 		dbh->methods = &firebird_methods;
686 		dbh->native_case = PDO_CASE_UPPER;
687 		dbh->alloc_own_columns = 1;
688 
689 		ret = 1;
690 
691 	} while (0);
692 
693 	for (i = 0; i < sizeof(vars)/sizeof(vars[0]); ++i) {
694 		if (vars[i].freeme) {
695 			efree(vars[i].optval);
696 		}
697 	}
698 
699 	if (!dbh->methods) {
700 		char errmsg[512];
701 		const ISC_STATUS *s = H->isc_status;
702 		fb_interpret(errmsg, sizeof(errmsg),&s);
703 		zend_throw_exception_ex(php_pdo_get_exception(), H->isc_status[1], "SQLSTATE[%s] [%d] %s",
704 				"HY000", H->isc_status[1], errmsg);
705 	}
706 
707 	if (!ret) {
708 		firebird_handle_closer(dbh);
709 	}
710 
711 	return ret;
712 }
713 /* }}} */
714 
715 
716 const pdo_driver_t pdo_firebird_driver = { /* {{{ */
717 	PDO_DRIVER_HEADER(firebird),
718 	pdo_firebird_handle_factory
719 };
720 /* }}} */
721