1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2018 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, PDO_FB_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_get_string(val);
478 				if (H->date_format) {
479 					efree(H->date_format);
480 				}
481 				spprintf(&H->date_format, 0, "%s", ZSTR_VAL(str));
482 				zend_string_release_ex(str, 0);
483 			}
484 			return 1;
485 
486 		case PDO_FB_ATTR_TIME_FORMAT:
487 			{
488 				zend_string *str = zval_get_string(val);
489 				if (H->time_format) {
490 					efree(H->time_format);
491 				}
492 				spprintf(&H->time_format, 0, "%s", ZSTR_VAL(str));
493 				zend_string_release_ex(str, 0);
494 			}
495 			return 1;
496 
497 		case PDO_FB_ATTR_TIMESTAMP_FORMAT:
498 			{
499 				zend_string *str = zval_get_string(val);
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 #define INFO_BUF_LEN 512
513 
514 /* callback to used to report database server info */
firebird_info_cb(void * arg,char const * s)515 static void firebird_info_cb(void *arg, char const *s) /* {{{ */
516 {
517 	if (arg) {
518 		if (*(char*)arg) { /* second call */
519 			strlcat(arg, " ", INFO_BUF_LEN);
520 		}
521 		strlcat(arg, s, INFO_BUF_LEN);
522 	}
523 }
524 /* }}} */
525 
526 /* called by PDO to get a driver-specific dbh attribute */
firebird_handle_get_attribute(pdo_dbh_t * dbh,zend_long attr,zval * val)527 static int firebird_handle_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val) /* {{{ */
528 {
529 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
530 
531 	switch (attr) {
532 		char tmp[INFO_BUF_LEN];
533 
534 		case PDO_ATTR_AUTOCOMMIT:
535 			ZVAL_LONG(val,dbh->auto_commit);
536 			return 1;
537 
538 		case PDO_ATTR_CONNECTION_STATUS:
539 			ZVAL_BOOL(val, !isc_version(&H->db, firebird_info_cb, NULL));
540 			return 1;
541 
542 		case PDO_ATTR_CLIENT_VERSION: {
543 #if defined(__GNUC__) || defined(PHP_WIN32)
544 			info_func_t info_func = NULL;
545 #ifdef __GNUC__
546 			info_func = (info_func_t)dlsym(RTLD_DEFAULT, "isc_get_client_version");
547 #else
548 			HMODULE l = GetModuleHandle("fbclient");
549 
550 			if (!l) {
551 				break;
552 			}
553 			info_func = (info_func_t)GetProcAddress(l, "isc_get_client_version");
554 #endif
555 			if (info_func) {
556 				info_func(tmp);
557 				ZVAL_STRING(val, tmp);
558 			}
559 #else
560 			ZVAL_NULL(val);
561 #endif
562 			}
563 			return 1;
564 
565 		case PDO_ATTR_SERVER_VERSION:
566 		case PDO_ATTR_SERVER_INFO:
567 			*tmp = 0;
568 
569 			if (!isc_version(&H->db, firebird_info_cb, (void*)tmp)) {
570 				ZVAL_STRING(val, tmp);
571 				return 1;
572 			}
573 
574 		case PDO_ATTR_FETCH_TABLE_NAMES:
575 			ZVAL_BOOL(val, H->fetch_table_names);
576 			return 1;
577 	}
578 	return 0;
579 }
580 /* }}} */
581 
582 /* 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)583 static int pdo_firebird_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info) /* {{{ */
584 {
585 	pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
586 	const ISC_STATUS *s = H->isc_status;
587 	char buf[400];
588 	zend_long i = 0, l, sqlcode = isc_sqlcode(s);
589 
590 	if (sqlcode) {
591 		add_next_index_long(info, sqlcode);
592 
593 		while ((sizeof(buf)>(i+2))&&(l = fb_interpret(&buf[i],(sizeof(buf)-i-2),&s))) {
594 			i += l;
595 			strcpy(&buf[i++], " ");
596 		}
597 		add_next_index_string(info, buf);
598 	} else if (H->last_app_error) {
599 		add_next_index_long(info, -999);
600 		add_next_index_string(info, const_cast(H->last_app_error));
601 	}
602 	return 1;
603 }
604 /* }}} */
605 
606 static const struct pdo_dbh_methods firebird_methods = { /* {{{ */
607 	firebird_handle_closer,
608 	firebird_handle_preparer,
609 	firebird_handle_doer,
610 	firebird_handle_quoter,
611 	firebird_handle_begin,
612 	firebird_handle_commit,
613 	firebird_handle_rollback,
614 	firebird_handle_set_attribute,
615 	NULL, /* last_id not supported */
616 	pdo_firebird_fetch_error_func,
617 	firebird_handle_get_attribute,
618 	NULL /* check_liveness */
619 };
620 /* }}} */
621 
622 /* the driver-specific PDO handle constructor */
pdo_firebird_handle_factory(pdo_dbh_t * dbh,zval * driver_options)623 static int pdo_firebird_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
624 {
625 	struct pdo_data_src_parser vars[] = {
626 		{ "dbname", NULL, 0 },
627 		{ "charset",  NULL,	0 },
628 		{ "role", NULL,	0 }
629 	};
630 	int i, ret = 0;
631 	short buf_len = 256, dpb_len;
632 
633 	pdo_firebird_db_handle *H = dbh->driver_data = pecalloc(1,sizeof(*H),dbh->is_persistent);
634 
635 	php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 3);
636 
637 	do {
638 		static char const dpb_flags[] = {
639 			isc_dpb_user_name, isc_dpb_password, isc_dpb_lc_ctype, isc_dpb_sql_role_name };
640 		char const *dpb_values[] = { dbh->username, dbh->password, vars[1].optval, vars[2].optval };
641 		char dpb_buffer[256] = { isc_dpb_version1 }, *dpb;
642 
643 		dpb = dpb_buffer + 1;
644 
645 		/* loop through all the provided arguments and set dpb fields accordingly */
646 		for (i = 0; i < sizeof(dpb_flags); ++i) {
647 			if (dpb_values[i] && buf_len > 0) {
648 				dpb_len = slprintf(dpb, buf_len, "%c%c%s", dpb_flags[i], (unsigned char)strlen(dpb_values[i]),
649 					dpb_values[i]);
650 				dpb += dpb_len;
651 				buf_len -= dpb_len;
652 			}
653 		}
654 
655 		/* fire it up baby! */
656 		if (isc_attach_database(H->isc_status, 0, vars[0].optval, &H->db,(short)(dpb-dpb_buffer), dpb_buffer)) {
657 			break;
658 		}
659 
660 		dbh->methods = &firebird_methods;
661 		dbh->native_case = PDO_CASE_UPPER;
662 		dbh->alloc_own_columns = 1;
663 
664 		ret = 1;
665 
666 	} while (0);
667 
668 	for (i = 0; i < sizeof(vars)/sizeof(vars[0]); ++i) {
669 		if (vars[i].freeme) {
670 			efree(vars[i].optval);
671 		}
672 	}
673 
674 	if (!dbh->methods) {
675 		char errmsg[512];
676 		const ISC_STATUS *s = H->isc_status;
677 		fb_interpret(errmsg, sizeof(errmsg),&s);
678 		zend_throw_exception_ex(php_pdo_get_exception(), H->isc_status[1], "SQLSTATE[%s] [%d] %s",
679 				"HY000", H->isc_status[1], errmsg);
680 	}
681 
682 	if (!ret) {
683 		firebird_handle_closer(dbh);
684 	}
685 
686 	return ret;
687 }
688 /* }}} */
689 
690 
691 const pdo_driver_t pdo_firebird_driver = { /* {{{ */
692 	PDO_DRIVER_HEADER(firebird),
693 	pdo_firebird_handle_factory
694 };
695 /* }}} */
696 
697 /*
698  * Local variables:
699  * tab-width: 4
700  * c-basic-offset: 4
701  * End:
702  * vim600: noet sw=4 ts=4 fdm=marker
703  * vim<600: noet sw=4 ts=4
704  */
705