firebird_driver.c 18 KB

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