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