oci_driver.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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: Wez Furlong <wez@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "php.h"
  22. #include "php_ini.h"
  23. #include "ext/standard/info.h"
  24. #include "pdo/php_pdo.h"
  25. #include "pdo/php_pdo_driver.h"
  26. #include "php_pdo_oci.h"
  27. #include "php_pdo_oci_int.h"
  28. #include "Zend/zend_exceptions.h"
  29. static inline ub4 pdo_oci_sanitize_prefetch(long prefetch);
  30. static int pdo_oci_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info) /* {{{ */
  31. {
  32. pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
  33. pdo_oci_error_info *einfo;
  34. einfo = &H->einfo;
  35. if (stmt) {
  36. pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
  37. if (S->einfo.errmsg) {
  38. einfo = &S->einfo;
  39. }
  40. }
  41. if (einfo->errcode) {
  42. add_next_index_long(info, einfo->errcode);
  43. add_next_index_string(info, einfo->errmsg);
  44. }
  45. return 1;
  46. }
  47. /* }}} */
  48. ub4 _oci_error(OCIError *err, pdo_dbh_t *dbh, pdo_stmt_t *stmt, char *what, sword status, int isinit, const char *file, int line) /* {{{ */
  49. {
  50. text errbuf[1024] = "<<Unknown>>";
  51. char tmp_buf[2048];
  52. pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
  53. pdo_oci_error_info *einfo;
  54. pdo_oci_stmt *S = NULL;
  55. pdo_error_type *pdo_err = &dbh->error_code;
  56. if (stmt) {
  57. S = (pdo_oci_stmt*)stmt->driver_data;
  58. einfo = &S->einfo;
  59. pdo_err = &stmt->error_code;
  60. }
  61. else {
  62. einfo = &H->einfo;
  63. }
  64. if (einfo->errmsg) {
  65. pefree(einfo->errmsg, dbh->is_persistent);
  66. }
  67. einfo->errmsg = NULL;
  68. einfo->errcode = 0;
  69. einfo->file = file;
  70. einfo->line = line;
  71. if (isinit) { /* Initialization error */
  72. strcpy(*pdo_err, "HY000");
  73. slprintf(tmp_buf, sizeof(tmp_buf), "%s (%s:%d)", what, file, line);
  74. einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
  75. }
  76. else {
  77. switch (status) {
  78. case OCI_SUCCESS:
  79. strcpy(*pdo_err, "00000");
  80. break;
  81. case OCI_ERROR:
  82. OCIErrorGet(err, (ub4)1, NULL, &einfo->errcode, errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
  83. slprintf(tmp_buf, sizeof(tmp_buf), "%s: %s (%s:%d)", what, errbuf, file, line);
  84. einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
  85. break;
  86. case OCI_SUCCESS_WITH_INFO:
  87. OCIErrorGet(err, (ub4)1, NULL, &einfo->errcode, errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
  88. slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_SUCCESS_WITH_INFO: %s (%s:%d)", what, errbuf, file, line);
  89. einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
  90. break;
  91. case OCI_NEED_DATA:
  92. slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_NEED_DATA (%s:%d)", what, file, line);
  93. einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
  94. break;
  95. case OCI_NO_DATA:
  96. slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_NO_DATA (%s:%d)", what, file, line);
  97. einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
  98. break;
  99. case OCI_INVALID_HANDLE:
  100. slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_INVALID_HANDLE (%s:%d)", what, file, line);
  101. einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
  102. break;
  103. case OCI_STILL_EXECUTING:
  104. slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_STILL_EXECUTING (%s:%d)", what, file, line);
  105. einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
  106. break;
  107. case OCI_CONTINUE:
  108. slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_CONTINUE (%s:%d)", what, file, line);
  109. einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
  110. break;
  111. }
  112. if (einfo->errcode) {
  113. switch (einfo->errcode) {
  114. case 1013: /* user requested cancel of current operation */
  115. zend_bailout();
  116. break;
  117. case 12154: /* ORA-12154: TNS:could not resolve service name */
  118. strcpy(*pdo_err, "42S02");
  119. break;
  120. case 22: /* ORA-00022: invalid session id */
  121. case 378:
  122. case 602:
  123. case 603:
  124. case 604:
  125. case 609:
  126. case 1012: /* ORA-01012: */
  127. case 1033:
  128. case 1041:
  129. case 1043:
  130. case 1089:
  131. case 1090:
  132. case 1092:
  133. case 3113: /* ORA-03133: end of file on communication channel */
  134. case 3114:
  135. case 3122:
  136. case 3135:
  137. case 12153:
  138. case 27146:
  139. case 28511:
  140. /* consider the connection closed */
  141. dbh->is_closed = 1;
  142. H->attached = 0;
  143. strcpy(*pdo_err, "01002"); /* FIXME */
  144. break;
  145. default:
  146. strcpy(*pdo_err, "HY000");
  147. }
  148. }
  149. if (stmt) {
  150. /* always propagate the error code back up to the dbh,
  151. * so that we can catch the error information when execute
  152. * is called via query. See Bug #33707 */
  153. if (H->einfo.errmsg) {
  154. pefree(H->einfo.errmsg, dbh->is_persistent);
  155. }
  156. H->einfo = *einfo;
  157. H->einfo.errmsg = einfo->errmsg ? pestrdup(einfo->errmsg, dbh->is_persistent) : NULL;
  158. strcpy(dbh->error_code, stmt->error_code);
  159. }
  160. }
  161. /* little mini hack so that we can use this code from the dbh ctor */
  162. if (!dbh->methods) {
  163. zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode, "SQLSTATE[%s]: %s", *pdo_err, einfo->errmsg);
  164. }
  165. return einfo->errcode;
  166. }
  167. /* }}} */
  168. static int oci_handle_closer(pdo_dbh_t *dbh) /* {{{ */
  169. {
  170. pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
  171. if (H->svc) {
  172. /* rollback any outstanding work */
  173. OCITransRollback(H->svc, H->err, 0);
  174. }
  175. if (H->session) {
  176. OCIHandleFree(H->session, OCI_HTYPE_SESSION);
  177. H->session = NULL;
  178. }
  179. if (H->svc) {
  180. OCIHandleFree(H->svc, OCI_HTYPE_SVCCTX);
  181. H->svc = NULL;
  182. }
  183. if (H->server && H->attached) {
  184. H->last_err = OCIServerDetach(H->server, H->err, OCI_DEFAULT);
  185. if (H->last_err) {
  186. oci_drv_error("OCIServerDetach");
  187. }
  188. H->attached = 0;
  189. }
  190. if (H->server) {
  191. OCIHandleFree(H->server, OCI_HTYPE_SERVER);
  192. H->server = NULL;
  193. }
  194. if (H->err) {
  195. OCIHandleFree(H->err, OCI_HTYPE_ERROR);
  196. H->err = NULL;
  197. }
  198. if (H->charset && H->env) {
  199. OCIHandleFree(H->env, OCI_HTYPE_ENV);
  200. H->env = NULL;
  201. }
  202. if (H->einfo.errmsg) {
  203. pefree(H->einfo.errmsg, dbh->is_persistent);
  204. H->einfo.errmsg = NULL;
  205. }
  206. pefree(H, dbh->is_persistent);
  207. return 0;
  208. }
  209. /* }}} */
  210. static int oci_handle_preparer(pdo_dbh_t *dbh, const char *sql, size_t sql_len, pdo_stmt_t *stmt, zval *driver_options) /* {{{ */
  211. {
  212. pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
  213. pdo_oci_stmt *S = ecalloc(1, sizeof(*S));
  214. ub4 prefetch;
  215. char *nsql = NULL;
  216. size_t nsql_len = 0;
  217. int ret;
  218. #if HAVE_OCISTMTFETCH2
  219. S->exec_type = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR,
  220. PDO_CURSOR_FWDONLY) == PDO_CURSOR_SCROLL ?
  221. OCI_STMT_SCROLLABLE_READONLY : OCI_DEFAULT;
  222. #else
  223. S->exec_type = OCI_DEFAULT;
  224. #endif
  225. S->H = H;
  226. stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
  227. ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len);
  228. if (ret == 1) {
  229. /* query was re-written */
  230. sql = nsql;
  231. sql_len = nsql_len;
  232. } else if (ret == -1) {
  233. /* couldn't grok it */
  234. strcpy(dbh->error_code, stmt->error_code);
  235. efree(S);
  236. return 0;
  237. }
  238. /* create an OCI statement handle */
  239. OCIHandleAlloc(H->env, (dvoid*)&S->stmt, OCI_HTYPE_STMT, 0, NULL);
  240. /* and our own private error handle */
  241. OCIHandleAlloc(H->env, (dvoid*)&S->err, OCI_HTYPE_ERROR, 0, NULL);
  242. if (sql_len) {
  243. H->last_err = OCIStmtPrepare(S->stmt, H->err, (text*)sql, (ub4) sql_len, OCI_NTV_SYNTAX, OCI_DEFAULT);
  244. if (nsql) {
  245. efree(nsql);
  246. nsql = NULL;
  247. }
  248. if (H->last_err) {
  249. H->last_err = oci_drv_error("OCIStmtPrepare");
  250. OCIHandleFree(S->stmt, OCI_HTYPE_STMT);
  251. OCIHandleFree(S->err, OCI_HTYPE_ERROR);
  252. efree(S);
  253. return 0;
  254. }
  255. }
  256. prefetch = H->prefetch; /* Note 0 is allowed so in future REF CURSORs can be used & then passed with no row loss*/
  257. H->last_err = OCIAttrSet(S->stmt, OCI_HTYPE_STMT, &prefetch, 0,
  258. OCI_ATTR_PREFETCH_ROWS, H->err);
  259. if (!H->last_err) {
  260. prefetch *= PDO_OCI_PREFETCH_ROWSIZE;
  261. H->last_err = OCIAttrSet(S->stmt, OCI_HTYPE_STMT, &prefetch, 0,
  262. OCI_ATTR_PREFETCH_MEMORY, H->err);
  263. }
  264. stmt->driver_data = S;
  265. stmt->methods = &oci_stmt_methods;
  266. if (nsql) {
  267. efree(nsql);
  268. nsql = NULL;
  269. }
  270. return 1;
  271. }
  272. /* }}} */
  273. static zend_long oci_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len) /* {{{ */
  274. {
  275. pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
  276. OCIStmt *stmt;
  277. ub2 stmt_type;
  278. ub4 rowcount;
  279. int ret = -1;
  280. OCIHandleAlloc(H->env, (dvoid*)&stmt, OCI_HTYPE_STMT, 0, NULL);
  281. H->last_err = OCIStmtPrepare(stmt, H->err, (text*)sql, (ub4) sql_len, OCI_NTV_SYNTAX, OCI_DEFAULT);
  282. if (H->last_err) {
  283. H->last_err = oci_drv_error("OCIStmtPrepare");
  284. OCIHandleFree(stmt, OCI_HTYPE_STMT);
  285. return -1;
  286. }
  287. H->last_err = OCIAttrGet(stmt, OCI_HTYPE_STMT, &stmt_type, 0, OCI_ATTR_STMT_TYPE, H->err);
  288. if (stmt_type == OCI_STMT_SELECT) {
  289. /* invalid usage; cancel it */
  290. OCIHandleFree(stmt, OCI_HTYPE_STMT);
  291. php_error_docref(NULL, E_WARNING, "issuing a SELECT query here is invalid");
  292. return -1;
  293. }
  294. /* now we are good to go */
  295. H->last_err = OCIStmtExecute(H->svc, stmt, H->err, 1, 0, NULL, NULL,
  296. (dbh->auto_commit && !dbh->in_txn) ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT);
  297. if (H->last_err) {
  298. H->last_err = oci_drv_error("OCIStmtExecute");
  299. } else {
  300. /* return the number of affected rows */
  301. H->last_err = OCIAttrGet(stmt, OCI_HTYPE_STMT, &rowcount, 0, OCI_ATTR_ROW_COUNT, H->err);
  302. ret = rowcount;
  303. }
  304. OCIHandleFree(stmt, OCI_HTYPE_STMT);
  305. return ret;
  306. }
  307. /* }}} */
  308. static int oci_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype ) /* {{{ */
  309. {
  310. int qcount = 0;
  311. char const *cu, *l, *r;
  312. char *c;
  313. if (!unquotedlen) {
  314. *quotedlen = 2;
  315. *quoted = emalloc(*quotedlen+1);
  316. strcpy(*quoted, "''");
  317. return 1;
  318. }
  319. /* count single quotes */
  320. for (cu = unquoted; (cu = strchr(cu,'\'')); qcount++, cu++)
  321. ; /* empty loop */
  322. *quotedlen = unquotedlen + qcount + 2;
  323. *quoted = c = emalloc(*quotedlen+1);
  324. *c++ = '\'';
  325. /* foreach (chunk that ends in a quote) */
  326. for (l = unquoted; (r = strchr(l,'\'')); l = r+1) {
  327. strncpy(c, l, r-l+1);
  328. c += (r-l+1);
  329. *c++ = '\''; /* add second quote */
  330. }
  331. /* Copy remainder and add enclosing quote */
  332. strncpy(c, l, *quotedlen-(c-*quoted)-1);
  333. (*quoted)[*quotedlen-1] = '\'';
  334. (*quoted)[*quotedlen] = '\0';
  335. return 1;
  336. }
  337. /* }}} */
  338. static int oci_handle_begin(pdo_dbh_t *dbh) /* {{{ */
  339. {
  340. /* with Oracle, there is nothing special to be done */
  341. return 1;
  342. }
  343. /* }}} */
  344. static int oci_handle_commit(pdo_dbh_t *dbh) /* {{{ */
  345. {
  346. pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
  347. H->last_err = OCITransCommit(H->svc, H->err, 0);
  348. if (H->last_err) {
  349. H->last_err = oci_drv_error("OCITransCommit");
  350. return 0;
  351. }
  352. return 1;
  353. }
  354. /* }}} */
  355. static int oci_handle_rollback(pdo_dbh_t *dbh) /* {{{ */
  356. {
  357. pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
  358. H->last_err = OCITransRollback(H->svc, H->err, 0);
  359. if (H->last_err) {
  360. H->last_err = oci_drv_error("OCITransRollback");
  361. return 0;
  362. }
  363. return 1;
  364. }
  365. /* }}} */
  366. static int oci_handle_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val) /* {{{ */
  367. {
  368. zend_long lval = zval_get_long(val);
  369. pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
  370. switch (attr) {
  371. case PDO_ATTR_AUTOCOMMIT:
  372. {
  373. if (dbh->in_txn) {
  374. /* Assume they want to commit whatever is outstanding */
  375. H->last_err = OCITransCommit(H->svc, H->err, 0);
  376. if (H->last_err) {
  377. H->last_err = oci_drv_error("OCITransCommit");
  378. return 0;
  379. }
  380. dbh->in_txn = 0;
  381. }
  382. dbh->auto_commit = (unsigned int)lval? 1 : 0;
  383. return 1;
  384. }
  385. case PDO_ATTR_PREFETCH:
  386. {
  387. H->prefetch = pdo_oci_sanitize_prefetch(lval);
  388. return 1;
  389. }
  390. case PDO_OCI_ATTR_ACTION:
  391. {
  392. #if (OCI_MAJOR_VERSION >= 10)
  393. zend_string *action = zval_get_string(val);
  394. H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
  395. (dvoid *) ZSTR_VAL(action), (ub4) ZSTR_LEN(action),
  396. OCI_ATTR_ACTION, H->err);
  397. if (H->last_err) {
  398. oci_drv_error("OCIAttrSet: OCI_ATTR_ACTION");
  399. return 0;
  400. }
  401. return 1;
  402. #else
  403. oci_drv_error("Unsupported attribute type");
  404. return 0;
  405. #endif
  406. }
  407. case PDO_OCI_ATTR_CLIENT_INFO:
  408. {
  409. #if (OCI_MAJOR_VERSION >= 10)
  410. zend_string *client_info = zval_get_string(val);
  411. H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
  412. (dvoid *) ZSTR_VAL(client_info), (ub4) ZSTR_LEN(client_info),
  413. OCI_ATTR_CLIENT_INFO, H->err);
  414. if (H->last_err) {
  415. oci_drv_error("OCIAttrSet: OCI_ATTR_CLIENT_INFO");
  416. return 0;
  417. }
  418. return 1;
  419. #else
  420. oci_drv_error("Unsupported attribute type");
  421. return 0;
  422. #endif
  423. }
  424. case PDO_OCI_ATTR_CLIENT_IDENTIFIER:
  425. {
  426. #if (OCI_MAJOR_VERSION >= 10)
  427. zend_string *identifier = zval_get_string(val);
  428. H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
  429. (dvoid *) ZSTR_VAL(identifier), (ub4) ZSTR_LEN(identifier),
  430. OCI_ATTR_CLIENT_IDENTIFIER, H->err);
  431. if (H->last_err) {
  432. oci_drv_error("OCIAttrSet: OCI_ATTR_CLIENT_IDENTIFIER");
  433. return 0;
  434. }
  435. return 1;
  436. #else
  437. oci_drv_error("Unsupported attribute type");
  438. return 0;
  439. #endif
  440. }
  441. case PDO_OCI_ATTR_MODULE:
  442. {
  443. #if (OCI_MAJOR_VERSION >= 10)
  444. zend_string *module = zval_get_string(val);
  445. H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
  446. (dvoid *) ZSTR_VAL(module), (ub4) ZSTR_LEN(module),
  447. OCI_ATTR_MODULE, H->err);
  448. if (H->last_err) {
  449. oci_drv_error("OCIAttrSet: OCI_ATTR_MODULE");
  450. return 0;
  451. }
  452. return 1;
  453. #else
  454. oci_drv_error("Unsupported attribute type");
  455. return 0;
  456. #endif
  457. }
  458. default:
  459. return 0;
  460. }
  461. }
  462. /* }}} */
  463. static int oci_handle_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value) /* {{{ */
  464. {
  465. pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
  466. switch (attr) {
  467. case PDO_ATTR_SERVER_VERSION:
  468. case PDO_ATTR_SERVER_INFO:
  469. {
  470. text infostr[512];
  471. char verstr[15];
  472. ub4 vernum;
  473. if (OCIServerRelease(H->svc, H->err, infostr, (ub4)sizeof(infostr), (ub1)OCI_HTYPE_SVCCTX, &vernum))
  474. {
  475. ZVAL_STRING(return_value, "<<Unknown>>");
  476. } else {
  477. if (attr == PDO_ATTR_SERVER_INFO) {
  478. ZVAL_STRING(return_value, (char *)infostr);
  479. } else {
  480. slprintf(verstr, sizeof(verstr), "%d.%d.%d.%d.%d",
  481. (int)((vernum>>24) & 0xFF), /* version number */
  482. (int)((vernum>>20) & 0x0F), /* release number*/
  483. (int)((vernum>>12) & 0xFF), /* update number */
  484. (int)((vernum>>8) & 0x0F), /* port release number */
  485. (int)((vernum>>0) & 0xFF)); /* port update number */
  486. ZVAL_STRING(return_value, verstr);
  487. }
  488. }
  489. return TRUE;
  490. }
  491. case PDO_ATTR_CLIENT_VERSION:
  492. {
  493. #if OCI_MAJOR_VERSION > 10 || (OCI_MAJOR_VERSION == 10 && OCI_MINOR_VERSION >= 2)
  494. /* Run time client version */
  495. sword major, minor, update, patch, port_update;
  496. char verstr[15];
  497. OCIClientVersion(&major, &minor, &update, &patch, &port_update);
  498. slprintf(verstr, sizeof(verstr), "%d.%d.%d.%d.%d", major, minor, update, patch, port_update);
  499. ZVAL_STRING(return_value, verstr);
  500. #elif defined(PHP_PDO_OCI_CLIENT_VERSION)
  501. /* Compile time client version */
  502. ZVAL_STRING(return_value, PHP_PDO_OCI_CLIENT_VERSION);
  503. #else
  504. return FALSE;
  505. #endif /* Check for OCIClientVersion() support */
  506. return TRUE;
  507. }
  508. case PDO_ATTR_AUTOCOMMIT:
  509. ZVAL_BOOL(return_value, dbh->auto_commit);
  510. return TRUE;
  511. case PDO_ATTR_PREFETCH:
  512. ZVAL_LONG(return_value, H->prefetch);
  513. return TRUE;
  514. default:
  515. return FALSE;
  516. }
  517. return FALSE;
  518. }
  519. /* }}} */
  520. static int pdo_oci_check_liveness(pdo_dbh_t *dbh) /* {{{ */
  521. {
  522. pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
  523. sb4 error_code = 0;
  524. #if (!((OCI_MAJOR_VERSION > 10) || ((OCI_MAJOR_VERSION == 10) && (OCI_MINOR_VERSION >= 2))))
  525. char version[256];
  526. #endif
  527. /* TODO move attached check to PDO level */
  528. if (H->attached == 0) {
  529. return FAILURE;
  530. }
  531. /* TODO add persistent_timeout check at PDO level */
  532. /* Use OCIPing instead of OCIServerVersion. If OCIPing returns ORA-1010 (invalid OCI operation)
  533. * such as from Pre-10.1 servers, the error is still from the server and we would have
  534. * successfully performed a roundtrip and validated the connection. Use OCIServerVersion for
  535. * Pre-10.2 clients
  536. */
  537. #if ((OCI_MAJOR_VERSION > 10) || ((OCI_MAJOR_VERSION == 10) && (OCI_MINOR_VERSION >= 2))) /* OCIPing available 10.2 onwards */
  538. H->last_err = OCIPing (H->svc, H->err, OCI_DEFAULT);
  539. #else
  540. /* use good old OCIServerVersion() */
  541. H->last_err = OCIServerVersion (H->svc, H->err, (text *)version, sizeof(version), OCI_HTYPE_SVCCTX);
  542. #endif
  543. if (H->last_err == OCI_SUCCESS) {
  544. return SUCCESS;
  545. }
  546. OCIErrorGet (H->err, (ub4)1, NULL, &error_code, NULL, 0, OCI_HTYPE_ERROR);
  547. if (error_code == 1010) {
  548. return SUCCESS;
  549. }
  550. return FAILURE;
  551. }
  552. /* }}} */
  553. static const struct pdo_dbh_methods oci_methods = {
  554. oci_handle_closer,
  555. oci_handle_preparer,
  556. oci_handle_doer,
  557. oci_handle_quoter,
  558. oci_handle_begin,
  559. oci_handle_commit,
  560. oci_handle_rollback,
  561. oci_handle_set_attribute,
  562. NULL,
  563. pdo_oci_fetch_error_func,
  564. oci_handle_get_attribute,
  565. pdo_oci_check_liveness, /* check_liveness */
  566. NULL /* get_driver_methods */
  567. };
  568. static int pdo_oci_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
  569. {
  570. pdo_oci_db_handle *H;
  571. int i, ret = 0;
  572. struct pdo_data_src_parser vars[] = {
  573. { "charset", NULL, 0 },
  574. { "dbname", "", 0 }
  575. };
  576. php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 2);
  577. H = pecalloc(1, sizeof(*H), dbh->is_persistent);
  578. dbh->driver_data = H;
  579. dbh->skip_param_evt =
  580. 1 << PDO_PARAM_EVT_FETCH_PRE |
  581. 1 << PDO_PARAM_EVT_FETCH_POST |
  582. 1 << PDO_PARAM_EVT_NORMALIZE;
  583. H->prefetch = PDO_OCI_PREFETCH_DEFAULT;
  584. /* allocate an environment */
  585. #if HAVE_OCIENVNLSCREATE
  586. if (vars[0].optval) {
  587. H->charset = OCINlsCharSetNameToId(pdo_oci_Env, (const oratext *)vars[0].optval);
  588. if (!H->charset) {
  589. oci_init_error("OCINlsCharSetNameToId: unknown character set name");
  590. goto cleanup;
  591. } else {
  592. if (OCIEnvNlsCreate(&H->env, PDO_OCI_INIT_MODE, 0, NULL, NULL, NULL, 0, NULL, H->charset, H->charset) != OCI_SUCCESS) {
  593. oci_init_error("OCIEnvNlsCreate: Check the character set is valid and that PHP has access to Oracle libraries and NLS data");
  594. goto cleanup;
  595. }
  596. }
  597. }
  598. #endif
  599. if (H->env == NULL) {
  600. /* use the global environment */
  601. H->env = pdo_oci_Env;
  602. }
  603. /* something to hold errors */
  604. OCIHandleAlloc(H->env, (dvoid **)&H->err, OCI_HTYPE_ERROR, 0, NULL);
  605. /* handle for the server */
  606. OCIHandleAlloc(H->env, (dvoid **)&H->server, OCI_HTYPE_SERVER, 0, NULL);
  607. H->last_err = OCIServerAttach(H->server, H->err, (text*)vars[1].optval,
  608. (sb4) strlen(vars[1].optval), OCI_DEFAULT);
  609. if (H->last_err) {
  610. oci_drv_error("pdo_oci_handle_factory");
  611. goto cleanup;
  612. }
  613. H->attached = 1;
  614. /* create a service context */
  615. H->last_err = OCIHandleAlloc(H->env, (dvoid**)&H->svc, OCI_HTYPE_SVCCTX, 0, NULL);
  616. if (H->last_err) {
  617. oci_drv_error("OCIHandleAlloc: OCI_HTYPE_SVCCTX");
  618. goto cleanup;
  619. }
  620. H->last_err = OCIHandleAlloc(H->env, (dvoid**)&H->session, OCI_HTYPE_SESSION, 0, NULL);
  621. if (H->last_err) {
  622. oci_drv_error("OCIHandleAlloc: OCI_HTYPE_SESSION");
  623. goto cleanup;
  624. }
  625. /* set server handle into service handle */
  626. H->last_err = OCIAttrSet(H->svc, OCI_HTYPE_SVCCTX, H->server, 0, OCI_ATTR_SERVER, H->err);
  627. if (H->last_err) {
  628. oci_drv_error("OCIAttrSet: OCI_ATTR_SERVER");
  629. goto cleanup;
  630. }
  631. /* username */
  632. if (dbh->username) {
  633. H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
  634. dbh->username, (ub4) strlen(dbh->username),
  635. OCI_ATTR_USERNAME, H->err);
  636. if (H->last_err) {
  637. oci_drv_error("OCIAttrSet: OCI_ATTR_USERNAME");
  638. goto cleanup;
  639. }
  640. }
  641. /* password */
  642. if (dbh->password) {
  643. H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
  644. dbh->password, (ub4) strlen(dbh->password),
  645. OCI_ATTR_PASSWORD, H->err);
  646. if (H->last_err) {
  647. oci_drv_error("OCIAttrSet: OCI_ATTR_PASSWORD");
  648. goto cleanup;
  649. }
  650. }
  651. /* Now fire up the session */
  652. H->last_err = OCISessionBegin(H->svc, H->err, H->session, OCI_CRED_RDBMS, OCI_DEFAULT);
  653. if (H->last_err) {
  654. oci_drv_error("OCISessionBegin");
  655. goto cleanup;
  656. }
  657. /* set the server handle into service handle */
  658. H->last_err = OCIAttrSet(H->svc, OCI_HTYPE_SVCCTX, H->session, 0, OCI_ATTR_SESSION, H->err);
  659. if (H->last_err) {
  660. oci_drv_error("OCIAttrSet: OCI_ATTR_SESSION");
  661. goto cleanup;
  662. }
  663. /* Get max character width */
  664. H->last_err = OCINlsNumericInfoGet(H->env, H->err, &H->max_char_width, OCI_NLS_CHARSET_MAXBYTESZ);
  665. if (H->last_err) {
  666. oci_drv_error("OCINlsNumericInfoGet: OCI_NLS_CHARSET_MAXBYTESZ");
  667. goto cleanup;
  668. }
  669. dbh->methods = &oci_methods;
  670. dbh->alloc_own_columns = 1;
  671. dbh->native_case = PDO_CASE_UPPER;
  672. ret = 1;
  673. cleanup:
  674. for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
  675. if (vars[i].freeme) {
  676. efree(vars[i].optval);
  677. }
  678. }
  679. if (!ret) {
  680. oci_handle_closer(dbh);
  681. }
  682. return ret;
  683. }
  684. /* }}} */
  685. const pdo_driver_t pdo_oci_driver = {
  686. PDO_DRIVER_HEADER(oci),
  687. pdo_oci_handle_factory
  688. };
  689. static inline ub4 pdo_oci_sanitize_prefetch(long prefetch) /* {{{ */
  690. {
  691. if (prefetch < 0) {
  692. prefetch = 0;
  693. } else if (prefetch > UB4MAXVAL / PDO_OCI_PREFETCH_ROWSIZE) {
  694. prefetch = PDO_OCI_PREFETCH_DEFAULT;
  695. }
  696. return ((ub4)prefetch);
  697. }
  698. /* }}} */
  699. /*
  700. * Local variables:
  701. * tab-width: 4
  702. * c-basic-offset: 4
  703. * End:
  704. * vim600: noet sw=4 ts=4 fdm=marker
  705. * vim<600: noet sw=4 ts=4
  706. */