firebird_driver.c 19 KB

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