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