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 		while (result[i] != isc_info_end && i < result_size) {
210 			short len = (short)isc_vax_integer(&result[i+1],2);
211 			if (result[i] != isc_info_req_select_count) {
212 				ret += isc_vax_integer(&result[i+3],len);
213 			}
214 			i += len+3;
215 		}
216 	}
217 
218 	/* commit if we're in auto_commit mode */
219 	if (dbh->auto_commit && isc_commit_retaining(H->isc_status, &H->tr)) {
220 		RECORD_ERROR(dbh);
221 	}
222 
223 free_statement:
224 
225 	if (isc_dsql_free_statement(H->isc_status, &stmt, DSQL_drop)) {
226 		RECORD_ERROR(dbh);
227 	}
228 
229 	return ret;
230 }
231 /* }}} */
232 
233 /* 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)234 static int firebird_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, /* {{{ */
235 	char **quoted, size_t *quotedlen, enum pdo_param_type paramtype)
236 {
237 	int qcount = 0;
238 	char const *co, *l, *r;
239 	char *c;
240 
241 	if (!unquotedlen) {
242 		*quotedlen = 2;
243 		*quoted = emalloc(*quotedlen+1);
244 		strcpy(*quoted, "''");
245 		return 1;
246 	}
247 
248 	/* Firebird only requires single quotes to be doubled if string lengths are used */
249 	/* count the number of ' characters */
250 	for (co = unquoted; (co = strchr(co,'\'')); qcount++, co++);
251 
252 	*quotedlen = unquotedlen + qcount + 2;
253 	*quoted = c = emalloc(*quotedlen+1);
254 	*c++ = '\'';
255 
256 	/* foreach (chunk that ends in a quote) */
257 	for (l = unquoted; (r = strchr(l,'\'')); l = r+1) {
258 		strncpy(c, l, r-l+1);
259 		c += (r-l+1);
260 		/* add the second quote */
261 		*c++ = '\'';
262 	}
263 
264 	/* copy the remainder */
265 	strncpy(c, l, *quotedlen-(c-*quoted)-1);
266 	(*quoted)[*quotedlen-1] = '\'';
267 	(*quoted)[*quotedlen]   = '\0';
268 
269 	return 1;
270 }
271 /* }}} */
272 
273 /* called by PDO to start a transaction */
firebird_handle_begin(pdo_dbh_t *dbh)274 static int firebird_handle_begin(pdo_dbh_t *dbh) /* {{{ */
275 {
276 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
277 	char tpb[8] = { isc_tpb_version3 }, *ptpb = tpb+1;
278 #if abies_0
279 	if (dbh->transaction_flags & PDO_TRANS_ISOLATION_LEVEL) {
280 		if (dbh->transaction_flags & PDO_TRANS_READ_UNCOMMITTED) {
281 			/* this is a poor fit, but it's all we have */
282 			*ptpb++ = isc_tpb_read_committed;
283 			*ptpb++ = isc_tpb_rec_version;
284 			dbh->transaction_flags &= ~(PDO_TRANS_ISOLATION_LEVEL^PDO_TRANS_READ_UNCOMMITTED);
285 		} else if (dbh->transaction_flags & PDO_TRANS_READ_COMMITTED) {
286 			*ptpb++ = isc_tpb_read_committed;
287 			*ptpb++ = isc_tpb_no_rec_version;
288 			dbh->transaction_flags &= ~(PDO_TRANS_ISOLATION_LEVEL^PDO_TRANS_READ_COMMITTED);
289 		} else if (dbh->transaction_flags & PDO_TRANS_REPEATABLE_READ) {
290 			*ptpb++ = isc_tpb_concurrency;
291 			dbh->transaction_flags &= ~(PDO_TRANS_ISOLATION_LEVEL^PDO_TRANS_REPEATABLE_READ);
292 		} else {
293 			*ptpb++ = isc_tpb_consistency;
294 			dbh->transaction_flags &= ~(PDO_TRANS_ISOLATION_LEVEL^PDO_TRANS_SERIALIZABLE);
295 		}
296 	}
297 
298 	if (dbh->transaction_flags & PDO_TRANS_ACCESS_MODE) {
299 		if (dbh->transaction_flags & PDO_TRANS_READONLY) {
300 			*ptpb++ = isc_tpb_read;
301 			dbh->transaction_flags &= ~(PDO_TRANS_ACCESS_MODE^PDO_TRANS_READONLY);
302 		} else {
303 			*ptpb++ = isc_tpb_write;
304 			dbh->transaction_flags &= ~(PDO_TRANS_ACCESS_MODE^PDO_TRANS_READWRITE);
305 		}
306 	}
307 
308 	if (dbh->transaction_flags & PDO_TRANS_CONFLICT_RESOLUTION) {
309 		if (dbh->transaction_flags & PDO_TRANS_RETRY) {
310 			*ptpb++ = isc_tpb_wait;
311 			dbh->transaction_flags &= ~(PDO_TRANS_CONFLICT_RESOLUTION^PDO_TRANS_RETRY);
312 		} else {
313 			*ptpb++ = isc_tpb_nowait;
314 			dbh->transaction_flags &= ~(PDO_TRANS_CONFLICT_RESOLUTION^PDO_TRANS_ABORT);
315 		}
316 	}
317 #endif
318 	if (isc_start_transaction(H->isc_status, &H->tr, 1, &H->db, (unsigned short)(ptpb-tpb), tpb)) {
319 		RECORD_ERROR(dbh);
320 		return 0;
321 	}
322 	return 1;
323 }
324 /* }}} */
325 
326 /* called by PDO to commit a transaction */
firebird_handle_commit(pdo_dbh_t *dbh)327 static int firebird_handle_commit(pdo_dbh_t *dbh) /* {{{ */
328 {
329 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
330 
331 	if (isc_commit_transaction(H->isc_status, &H->tr)) {
332 		RECORD_ERROR(dbh);
333 		return 0;
334 	}
335 	return 1;
336 }
337 /* }}} */
338 
339 /* called by PDO to rollback a transaction */
firebird_handle_rollback(pdo_dbh_t *dbh)340 static int firebird_handle_rollback(pdo_dbh_t *dbh) /* {{{ */
341 {
342 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
343 
344 	if (isc_rollback_transaction(H->isc_status, &H->tr)) {
345 		RECORD_ERROR(dbh);
346 		return 0;
347 	}
348 	return 1;
349 }
350 /* }}} */
351 
352 /* 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)353 static int firebird_alloc_prepare_stmt(pdo_dbh_t *dbh, const char *sql, size_t sql_len, /* {{{ */
354 	XSQLDA *out_sqlda, isc_stmt_handle *s, HashTable *named_params)
355 {
356 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
357 	char *c, *new_sql, in_quote, in_param, pname[64], *ppname;
358 	zend_long l, pindex = -1;
359 
360 	/* Firebird allows SQL statements up to 64k, so bail if it doesn't fit */
361 	if (sql_len > 65536) {
362 		strcpy(dbh->error_code, "01004");
363 		return 0;
364 	}
365 
366 	/* start a new transaction implicitly if auto_commit is enabled and no transaction is open */
367 	if (dbh->auto_commit && !dbh->in_txn) {
368 		/* dbh->transaction_flags = PDO_TRANS_READ_UNCOMMITTED; */
369 
370 		if (!firebird_handle_begin(dbh)) {
371 			return 0;
372 		}
373 		dbh->in_txn = 1;
374 	}
375 
376 	/* allocate the statement */
377 	if (isc_dsql_allocate_statement(H->isc_status, &H->db, s)) {
378 		RECORD_ERROR(dbh);
379 		return 0;
380 	}
381 
382 	/* in order to support named params, which Firebird itself doesn't,
383 	   we need to replace :foo by ?, and store the name we just replaced */
384 	new_sql = c = emalloc(sql_len+1);
385 
386 	for (l = in_quote = in_param = 0; l <= sql_len; ++l) {
387 		if ( !(in_quote ^= (sql[l] == '\''))) {
388 			if (!in_param) {
389 				switch (sql[l]) {
390 					case ':':
391 						in_param = 1;
392 						ppname = pname;
393 						*ppname++ = sql[l];
394 					case '?':
395 						*c++ = '?';
396 						++pindex;
397 					continue;
398 				}
399 			} else {
400                                 if ((in_param &= ((sql[l] >= 'A' && sql[l] <= 'Z') || (sql[l] >= 'a' && sql[l] <= 'z')
401                                         || (sql[l] >= '0' && sql[l] <= '9') || sql[l] == '_' || sql[l] == '-'))) {
402 
403 
404 					*ppname++ = sql[l];
405 					continue;
406 				} else {
407 					*ppname++ = 0;
408 					if (named_params) {
409 						zval tmp;
410 						ZVAL_LONG(&tmp, pindex);
411 						zend_hash_str_update(named_params, pname, (unsigned int)(ppname - pname - 1), &tmp);
412 					}
413 				}
414 			}
415 		}
416 		*c++ = sql[l];
417 	}
418 
419 	/* prepare the statement */
420 	if (isc_dsql_prepare(H->isc_status, &H->tr, s, 0, new_sql, H->sql_dialect, out_sqlda)) {
421 		RECORD_ERROR(dbh);
422 		efree(new_sql);
423 		return 0;
424 	}
425 
426 	efree(new_sql);
427 	return 1;
428 }
429 /* }}} */
430 
431 /* called by PDO to set a driver-specific dbh attribute */
firebird_handle_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)432 static int firebird_handle_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val) /* {{{ */
433 {
434 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
435 
436 	switch (attr) {
437 		case PDO_ATTR_AUTOCOMMIT:
438 			{
439 				zend_bool bval = zval_get_long(val)? 1 : 0;
440 
441 				/* ignore if the new value equals the old one */
442 				if (dbh->auto_commit ^ bval) {
443 					if (dbh->in_txn) {
444 						if (bval) {
445 							/* turning on auto_commit with an open transaction is illegal, because
446 							   we won't know what to do with it */
447 							H->last_app_error = "Cannot enable auto-commit while a transaction is already open";
448 							return 0;
449 						} else {
450 							/* close the transaction */
451 							if (!firebird_handle_commit(dbh)) {
452 								break;
453 							}
454 							dbh->in_txn = 0;
455 						}
456 					}
457 					dbh->auto_commit = bval;
458 				}
459 			}
460 			return 1;
461 
462 		case PDO_ATTR_FETCH_TABLE_NAMES:
463 			H->fetch_table_names = zval_get_long(val)? 1 : 0;
464 			return 1;
465 
466 		case PDO_FB_ATTR_DATE_FORMAT:
467 			{
468 				zend_string *str = zval_try_get_string(val);
469 				if (UNEXPECTED(!str)) {
470 					return 0;
471 				}
472 				if (H->date_format) {
473 					efree(H->date_format);
474 				}
475 				spprintf(&H->date_format, 0, "%s", ZSTR_VAL(str));
476 				zend_string_release_ex(str, 0);
477 			}
478 			return 1;
479 
480 		case PDO_FB_ATTR_TIME_FORMAT:
481 			{
482 				zend_string *str = zval_try_get_string(val);
483 				if (UNEXPECTED(!str)) {
484 					return 0;
485 				}
486 				if (H->time_format) {
487 					efree(H->time_format);
488 				}
489 				spprintf(&H->time_format, 0, "%s", ZSTR_VAL(str));
490 				zend_string_release_ex(str, 0);
491 			}
492 			return 1;
493 
494 		case PDO_FB_ATTR_TIMESTAMP_FORMAT:
495 			{
496 				zend_string *str = zval_try_get_string(val);
497 				if (UNEXPECTED(!str)) {
498 					return 0;
499 				}
500 				if (H->timestamp_format) {
501 					efree(H->timestamp_format);
502 				}
503 				spprintf(&H->timestamp_format, 0, "%s", ZSTR_VAL(str));
504 				zend_string_release_ex(str, 0);
505 			}
506 			return 1;
507 	}
508 	return 0;
509 }
510 /* }}} */
511 
512 /* callback to used to report database server info */
firebird_info_cb(void *arg, char const *s)513 static void firebird_info_cb(void *arg, char const *s) /* {{{ */
514 {
515 	if (arg) {
516 		if (*(char*)arg) { /* second call */
517 			strcat(arg, " ");
518 		}
519 		strcat(arg, s);
520 	}
521 }
522 /* }}} */
523 
524 /* called by PDO to get a driver-specific dbh attribute */
firebird_handle_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)525 static int firebird_handle_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val) /* {{{ */
526 {
527 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
528 
529 	switch (attr) {
530 		char tmp[512];
531 
532 		case PDO_ATTR_AUTOCOMMIT:
533 			ZVAL_LONG(val,dbh->auto_commit);
534 			return 1;
535 
536 		case PDO_ATTR_CONNECTION_STATUS:
537 			ZVAL_BOOL(val, !isc_version(&H->db, firebird_info_cb, NULL));
538 			return 1;
539 
540 		case PDO_ATTR_CLIENT_VERSION: {
541 #if defined(__GNUC__) || defined(PHP_WIN32)
542 			info_func_t info_func = NULL;
543 #ifdef __GNUC__
544 			info_func = (info_func_t)dlsym(RTLD_DEFAULT, "isc_get_client_version");
545 #else
546 			HMODULE l = GetModuleHandle("fbclient");
547 
548 			if (!l) {
549 				break;
550 			}
551 			info_func = (info_func_t)GetProcAddress(l, "isc_get_client_version");
552 #endif
553 			if (info_func) {
554 				info_func(tmp);
555 				ZVAL_STRING(val, tmp);
556 			}
557 #else
558 			ZVAL_NULL(val);
559 #endif
560 			}
561 			return 1;
562 
563 		case PDO_ATTR_SERVER_VERSION:
564 		case PDO_ATTR_SERVER_INFO:
565 			*tmp = 0;
566 
567 			if (!isc_version(&H->db, firebird_info_cb, (void*)tmp)) {
568 				ZVAL_STRING(val, tmp);
569 				return 1;
570 			}
571 
572 		case PDO_ATTR_FETCH_TABLE_NAMES:
573 			ZVAL_BOOL(val, H->fetch_table_names);
574 			return 1;
575 	}
576 	return 0;
577 }
578 /* }}} */
579 
580 /* 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)581 static int pdo_firebird_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info) /* {{{ */
582 {
583 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
584 	const ISC_STATUS *s = H->isc_status;
585 	char buf[400];
586 	zend_long i = 0, l, sqlcode = isc_sqlcode(s);
587 
588 	if (sqlcode) {
589 		add_next_index_long(info, sqlcode);
590 
591 		while ((sizeof(buf)>(i+2))&&(l = fb_interpret(&buf[i],(sizeof(buf)-i-2),&s))) {
592 			i += l;
593 			strcpy(&buf[i++], " ");
594 		}
595 		add_next_index_string(info, buf);
596 	} else if (H->last_app_error) {
597 		add_next_index_long(info, -999);
598 		add_next_index_string(info, const_cast(H->last_app_error));
599 	}
600 	return 1;
601 }
602 /* }}} */
603 
604 static const struct pdo_dbh_methods firebird_methods = { /* {{{ */
605 	firebird_handle_closer,
606 	firebird_handle_preparer,
607 	firebird_handle_doer,
608 	firebird_handle_quoter,
609 	firebird_handle_begin,
610 	firebird_handle_commit,
611 	firebird_handle_rollback,
612 	firebird_handle_set_attribute,
613 	NULL, /* last_id not supported */
614 	pdo_firebird_fetch_error_func,
615 	firebird_handle_get_attribute,
616 	NULL /* check_liveness */
617 };
618 /* }}} */
619 
620 /* the driver-specific PDO handle constructor */
pdo_firebird_handle_factory(pdo_dbh_t *dbh, zval *driver_options)621 static int pdo_firebird_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
622 {
623 	struct pdo_data_src_parser vars[] = {
624 		{ "dbname", NULL, 0 },
625 		{ "charset",  NULL,	0 },
626 		{ "role", NULL,	0 },
627 		{ "dialect", "3", 0 },
628 		{ "user", NULL, 0 },
629 		{ "password", NULL, 0 }
630 	};
631 	int i, ret = 0;
632 	short buf_len = 256, dpb_len;
633 
634 	pdo_firebird_db_handle *H = dbh->driver_data = pecalloc(1,sizeof(*H),dbh->is_persistent);
635 
636 	php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 6);
637 
638 	if (!dbh->username && vars[4].optval) {
639 		dbh->username = pestrdup(vars[4].optval, dbh->is_persistent);
640 	}
641 
642 	if (!dbh->password && vars[5].optval) {
643 		dbh->password = pestrdup(vars[5].optval, dbh->is_persistent);
644 	}
645 
646 	do {
647 		static char const dpb_flags[] = {
648 			isc_dpb_user_name, isc_dpb_password, isc_dpb_lc_ctype, isc_dpb_sql_role_name };
649 		char const *dpb_values[] = { dbh->username, dbh->password, vars[1].optval, vars[2].optval };
650 		char dpb_buffer[256] = { isc_dpb_version1 }, *dpb;
651 
652 		dpb = dpb_buffer + 1;
653 
654 		/* loop through all the provided arguments and set dpb fields accordingly */
655 		for (i = 0; i < sizeof(dpb_flags); ++i) {
656 			if (dpb_values[i] && buf_len > 0) {
657 				dpb_len = slprintf(dpb, buf_len, "%c%c%s", dpb_flags[i], (unsigned char)strlen(dpb_values[i]),
658 					dpb_values[i]);
659 				dpb += dpb_len;
660 				buf_len -= dpb_len;
661 			}
662 		}
663 
664 		H->sql_dialect = PDO_FB_DIALECT;
665 		if (vars[3].optval) {
666 			H->sql_dialect = atoi(vars[3].optval);
667 		}
668 
669 		/* fire it up baby! */
670 		if (isc_attach_database(H->isc_status, 0, vars[0].optval, &H->db,(short)(dpb-dpb_buffer), dpb_buffer)) {
671 			break;
672 		}
673 
674 		dbh->methods = &firebird_methods;
675 		dbh->native_case = PDO_CASE_UPPER;
676 		dbh->alloc_own_columns = 1;
677 
678 		ret = 1;
679 
680 	} while (0);
681 
682 	for (i = 0; i < sizeof(vars)/sizeof(vars[0]); ++i) {
683 		if (vars[i].freeme) {
684 			efree(vars[i].optval);
685 		}
686 	}
687 
688 	if (!dbh->methods) {
689 		char errmsg[512];
690 		const ISC_STATUS *s = H->isc_status;
691 		fb_interpret(errmsg, sizeof(errmsg),&s);
692 		zend_throw_exception_ex(php_pdo_get_exception(), H->isc_status[1], "SQLSTATE[%s] [%d] %s",
693 				"HY000", H->isc_status[1], errmsg);
694 	}
695 
696 	if (!ret) {
697 		firebird_handle_closer(dbh);
698 	}
699 
700 	return ret;
701 }
702 /* }}} */
703 
704 
705 const pdo_driver_t pdo_firebird_driver = { /* {{{ */
706 	PDO_DRIVER_HEADER(firebird),
707 	pdo_firebird_handle_factory
708 };
709 /* }}} */
710