dblib_driver.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. | Frank M. Kromann <frank@kromann.info> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. #include "php.h"
  23. #include "php_ini.h"
  24. #include "ext/standard/info.h"
  25. #include "pdo/php_pdo.h"
  26. #include "pdo/php_pdo_driver.h"
  27. #include "php_pdo_dblib.h"
  28. #include "php_pdo_dblib_int.h"
  29. #include "zend_exceptions.h"
  30. /* Cache of the server supported datatypes, initialized in handle_factory */
  31. zval* pdo_dblib_datatypes;
  32. static int dblib_fetch_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info)
  33. {
  34. pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
  35. pdo_dblib_err *einfo = &H->err;
  36. pdo_dblib_stmt *S = NULL;
  37. char *message;
  38. char *msg;
  39. if (stmt) {
  40. S = (pdo_dblib_stmt*)stmt->driver_data;
  41. einfo = &S->err;
  42. }
  43. if (einfo->lastmsg) {
  44. msg = einfo->lastmsg;
  45. } else if (DBLIB_G(err).lastmsg) {
  46. msg = DBLIB_G(err).lastmsg;
  47. DBLIB_G(err).lastmsg = NULL;
  48. } else {
  49. msg = einfo->dberrstr;
  50. }
  51. /* don't return anything if there's nothing to return */
  52. if (msg == NULL && einfo->dberr == 0 && einfo->oserr == 0 && einfo->severity == 0) {
  53. return 0;
  54. }
  55. spprintf(&message, 0, "%s [%d] (severity %d) [%s]",
  56. msg, einfo->dberr, einfo->severity, stmt ? stmt->active_query_string : "");
  57. add_next_index_long(info, einfo->dberr);
  58. add_next_index_string(info, message);
  59. efree(message);
  60. add_next_index_long(info, einfo->oserr);
  61. add_next_index_long(info, einfo->severity);
  62. if (einfo->oserrstr) {
  63. add_next_index_string(info, einfo->oserrstr);
  64. }
  65. return 1;
  66. }
  67. static int dblib_handle_closer(pdo_dbh_t *dbh)
  68. {
  69. pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
  70. if (H) {
  71. pdo_dblib_err_dtor(&H->err);
  72. if (H->link) {
  73. dbclose(H->link);
  74. H->link = NULL;
  75. }
  76. if (H->login) {
  77. dbfreelogin(H->login);
  78. H->login = NULL;
  79. }
  80. pefree(H, dbh->is_persistent);
  81. dbh->driver_data = NULL;
  82. }
  83. return 0;
  84. }
  85. static int dblib_handle_preparer(pdo_dbh_t *dbh, const char *sql, size_t sql_len, pdo_stmt_t *stmt, zval *driver_options)
  86. {
  87. pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
  88. pdo_dblib_stmt *S = ecalloc(1, sizeof(*S));
  89. S->H = H;
  90. stmt->driver_data = S;
  91. stmt->methods = &dblib_stmt_methods;
  92. stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
  93. S->computed_column_name_count = 0;
  94. S->err.sqlstate = stmt->error_code;
  95. return 1;
  96. }
  97. static zend_long dblib_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
  98. {
  99. pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
  100. RETCODE ret, resret;
  101. dbsetuserdata(H->link, (BYTE*)&H->err);
  102. if (FAIL == dbcmd(H->link, sql)) {
  103. return -1;
  104. }
  105. if (FAIL == dbsqlexec(H->link)) {
  106. return -1;
  107. }
  108. resret = dbresults(H->link);
  109. if (resret == FAIL) {
  110. return -1;
  111. }
  112. ret = dbnextrow(H->link);
  113. if (ret == FAIL) {
  114. return -1;
  115. }
  116. if (dbnumcols(H->link) <= 0) {
  117. return DBCOUNT(H->link);
  118. }
  119. /* throw away any rows it might have returned */
  120. dbcanquery(H->link);
  121. return DBCOUNT(H->link);
  122. }
  123. static int dblib_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype)
  124. {
  125. pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
  126. zend_bool use_national_character_set = 0;
  127. size_t i;
  128. char * q;
  129. *quotedlen = 0;
  130. if (H->assume_national_character_set_strings) {
  131. use_national_character_set = 1;
  132. }
  133. if ((paramtype & PDO_PARAM_STR_NATL) == PDO_PARAM_STR_NATL) {
  134. use_national_character_set = 1;
  135. }
  136. if ((paramtype & PDO_PARAM_STR_CHAR) == PDO_PARAM_STR_CHAR) {
  137. use_national_character_set = 0;
  138. }
  139. /* Detect quoted length, adding extra char for doubled single quotes */
  140. for (i = 0; i < unquotedlen; i++) {
  141. if (unquoted[i] == '\'') ++*quotedlen;
  142. ++*quotedlen;
  143. }
  144. *quotedlen += 2; /* +2 for opening, closing quotes */
  145. if (use_national_character_set) {
  146. ++*quotedlen; /* N prefix */
  147. }
  148. q = *quoted = emalloc(*quotedlen + 1); /* Add byte for terminal null */
  149. if (use_national_character_set) {
  150. *q++ = 'N';
  151. }
  152. *q++ = '\'';
  153. for (i = 0; i < unquotedlen; i++) {
  154. if (unquoted[i] == '\'') {
  155. *q++ = '\'';
  156. *q++ = '\'';
  157. } else {
  158. *q++ = unquoted[i];
  159. }
  160. }
  161. *q++ = '\'';
  162. *q = 0;
  163. return 1;
  164. }
  165. static int pdo_dblib_transaction_cmd(const char *cmd, pdo_dbh_t *dbh)
  166. {
  167. pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
  168. if (FAIL == dbcmd(H->link, cmd)) {
  169. return 0;
  170. }
  171. if (FAIL == dbsqlexec(H->link)) {
  172. return 0;
  173. }
  174. return 1;
  175. }
  176. static int dblib_handle_begin(pdo_dbh_t *dbh)
  177. {
  178. return pdo_dblib_transaction_cmd("BEGIN TRANSACTION", dbh);
  179. }
  180. static int dblib_handle_commit(pdo_dbh_t *dbh)
  181. {
  182. return pdo_dblib_transaction_cmd("COMMIT TRANSACTION", dbh);
  183. }
  184. static int dblib_handle_rollback(pdo_dbh_t *dbh)
  185. {
  186. return pdo_dblib_transaction_cmd("ROLLBACK TRANSACTION", dbh);
  187. }
  188. char *dblib_handle_last_id(pdo_dbh_t *dbh, const char *name, size_t *len)
  189. {
  190. pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
  191. RETCODE ret;
  192. char *id = NULL;
  193. /*
  194. * Would use scope_identity() but it's not implemented on Sybase
  195. */
  196. if (FAIL == dbcmd(H->link, "SELECT @@IDENTITY")) {
  197. return NULL;
  198. }
  199. if (FAIL == dbsqlexec(H->link)) {
  200. return NULL;
  201. }
  202. ret = dbresults(H->link);
  203. if (ret == FAIL || ret == NO_MORE_RESULTS) {
  204. dbcancel(H->link);
  205. return NULL;
  206. }
  207. ret = dbnextrow(H->link);
  208. if (ret == FAIL || ret == NO_MORE_ROWS) {
  209. dbcancel(H->link);
  210. return NULL;
  211. }
  212. if (dbdatlen(H->link, 1) == 0) {
  213. dbcancel(H->link);
  214. return NULL;
  215. }
  216. id = emalloc(32);
  217. *len = dbconvert(NULL, (dbcoltype(H->link, 1)) , (dbdata(H->link, 1)) , (dbdatlen(H->link, 1)), SQLCHAR, (BYTE *)id, (DBINT)-1);
  218. dbcancel(H->link);
  219. return id;
  220. }
  221. static int dblib_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
  222. {
  223. pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
  224. switch(attr) {
  225. case PDO_ATTR_DEFAULT_STR_PARAM:
  226. H->assume_national_character_set_strings = zval_get_long(val) == PDO_PARAM_STR_NATL ? 1 : 0;
  227. return 1;
  228. case PDO_ATTR_TIMEOUT:
  229. case PDO_DBLIB_ATTR_QUERY_TIMEOUT:
  230. return SUCCEED == dbsettime(zval_get_long(val)) ? 1 : 0;
  231. case PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER:
  232. H->stringify_uniqueidentifier = zval_get_long(val);
  233. return 1;
  234. case PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS:
  235. H->skip_empty_rowsets = zval_is_true(val);
  236. return 1;
  237. case PDO_DBLIB_ATTR_DATETIME_CONVERT:
  238. H->datetime_convert = zval_get_long(val);
  239. return 1;
  240. default:
  241. return 0;
  242. }
  243. }
  244. static void dblib_get_tds_version(zval *return_value, int tds)
  245. {
  246. switch (tds) {
  247. case DBTDS_2_0:
  248. ZVAL_STRING(return_value, "2.0");
  249. break;
  250. case DBTDS_3_4:
  251. ZVAL_STRING(return_value, "3.4");
  252. break;
  253. case DBTDS_4_0:
  254. ZVAL_STRING(return_value, "4.0");
  255. break;
  256. case DBTDS_4_2:
  257. ZVAL_STRING(return_value, "4.2");
  258. break;
  259. case DBTDS_4_6:
  260. ZVAL_STRING(return_value, "4.6");
  261. break;
  262. case DBTDS_4_9_5:
  263. ZVAL_STRING(return_value, "4.9.5");
  264. break;
  265. case DBTDS_5_0:
  266. ZVAL_STRING(return_value, "5.0");
  267. break;
  268. #ifdef DBTDS_7_0
  269. case DBTDS_7_0:
  270. ZVAL_STRING(return_value, "7.0");
  271. break;
  272. #endif
  273. #ifdef DBTDS_7_1
  274. case DBTDS_7_1:
  275. ZVAL_STRING(return_value, "7.1");
  276. break;
  277. #endif
  278. #ifdef DBTDS_7_2
  279. case DBTDS_7_2:
  280. ZVAL_STRING(return_value, "7.2");
  281. break;
  282. #endif
  283. #ifdef DBTDS_7_3
  284. case DBTDS_7_3:
  285. ZVAL_STRING(return_value, "7.3");
  286. break;
  287. #endif
  288. #ifdef DBTDS_7_4
  289. case DBTDS_7_4:
  290. ZVAL_STRING(return_value, "7.4");
  291. break;
  292. #endif
  293. default:
  294. ZVAL_FALSE(return_value);
  295. break;
  296. }
  297. }
  298. static int dblib_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
  299. {
  300. pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
  301. switch (attr) {
  302. case PDO_ATTR_DEFAULT_STR_PARAM:
  303. ZVAL_LONG(return_value, H->assume_national_character_set_strings ? PDO_PARAM_STR_NATL : PDO_PARAM_STR_CHAR);
  304. break;
  305. case PDO_ATTR_EMULATE_PREPARES:
  306. /* this is the only option available, but expose it so common tests and whatever else can introspect */
  307. ZVAL_TRUE(return_value);
  308. break;
  309. case PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER:
  310. ZVAL_BOOL(return_value, H->stringify_uniqueidentifier);
  311. break;
  312. case PDO_DBLIB_ATTR_VERSION:
  313. ZVAL_STRING(return_value, dbversion());
  314. break;
  315. case PDO_DBLIB_ATTR_TDS_VERSION:
  316. dblib_get_tds_version(return_value, dbtds(H->link));
  317. break;
  318. case PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS:
  319. ZVAL_BOOL(return_value, H->skip_empty_rowsets);
  320. break;
  321. case PDO_DBLIB_ATTR_DATETIME_CONVERT:
  322. ZVAL_BOOL(return_value, H->datetime_convert);
  323. break;
  324. default:
  325. return 0;
  326. }
  327. return 1;
  328. }
  329. static const struct pdo_dbh_methods dblib_methods = {
  330. dblib_handle_closer,
  331. dblib_handle_preparer,
  332. dblib_handle_doer,
  333. dblib_handle_quoter,
  334. dblib_handle_begin, /* begin */
  335. dblib_handle_commit, /* commit */
  336. dblib_handle_rollback, /* rollback */
  337. dblib_set_attr, /*set attr */
  338. dblib_handle_last_id, /* last insert id */
  339. dblib_fetch_error, /* fetch error */
  340. dblib_get_attribute, /* get attr */
  341. NULL, /* check liveness */
  342. NULL, /* get driver methods */
  343. NULL, /* request shutdown */
  344. NULL /* in transaction */
  345. };
  346. static int pdo_dblib_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
  347. {
  348. pdo_dblib_db_handle *H;
  349. int i, nvars, nvers, ret = 0;
  350. const pdo_dblib_keyval tdsver[] = {
  351. {"4.2",DBVERSION_42}
  352. ,{"4.6",DBVERSION_46}
  353. ,{"5.0",DBVERSION_70} /* FIXME: This does not work with Sybase, but environ will */
  354. ,{"6.0",DBVERSION_70}
  355. ,{"7.0",DBVERSION_70}
  356. #ifdef DBVERSION_71
  357. ,{"7.1",DBVERSION_71}
  358. #endif
  359. #ifdef DBVERSION_72
  360. ,{"7.2",DBVERSION_72}
  361. ,{"8.0",DBVERSION_72}
  362. #endif
  363. #ifdef DBVERSION_73
  364. ,{"7.3",DBVERSION_73}
  365. #endif
  366. #ifdef DBVERSION_74
  367. ,{"7.4",DBVERSION_74}
  368. #endif
  369. ,{"10.0",DBVERSION_100}
  370. ,{"auto",0} /* Only works with FreeTDS. Other drivers will bork */
  371. };
  372. struct pdo_data_src_parser vars[] = {
  373. { "charset", NULL, 0 }
  374. ,{ "appname", "PHP " PDO_DBLIB_FLAVOUR, 0 }
  375. ,{ "host", "127.0.0.1", 0 }
  376. ,{ "dbname", NULL, 0 }
  377. ,{ "secure", NULL, 0 } /* DBSETLSECURE */
  378. ,{ "version", NULL, 0 } /* DBSETLVERSION */
  379. };
  380. nvars = sizeof(vars)/sizeof(vars[0]);
  381. nvers = sizeof(tdsver)/sizeof(tdsver[0]);
  382. php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, nvars);
  383. H = pecalloc(1, sizeof(*H), dbh->is_persistent);
  384. H->login = dblogin();
  385. H->err.sqlstate = dbh->error_code;
  386. H->assume_national_character_set_strings = 0;
  387. H->stringify_uniqueidentifier = 0;
  388. H->skip_empty_rowsets = 0;
  389. H->datetime_convert = 0;
  390. if (!H->login) {
  391. goto cleanup;
  392. }
  393. if (driver_options) {
  394. int connect_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_CONNECTION_TIMEOUT, -1);
  395. int query_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_QUERY_TIMEOUT, -1);
  396. int timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
  397. if (connect_timeout == -1) {
  398. connect_timeout = timeout;
  399. }
  400. if (query_timeout == -1) {
  401. query_timeout = timeout;
  402. }
  403. dbsetlogintime(connect_timeout); /* Connection/Login Timeout */
  404. dbsettime(query_timeout); /* Statement Timeout */
  405. H->assume_national_character_set_strings = pdo_attr_lval(driver_options, PDO_ATTR_DEFAULT_STR_PARAM, 0) == PDO_PARAM_STR_NATL ? 1 : 0;
  406. H->stringify_uniqueidentifier = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER, 0);
  407. H->skip_empty_rowsets = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS, 0);
  408. H->datetime_convert = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_DATETIME_CONVERT, 0);
  409. }
  410. DBERRHANDLE(H->login, (EHANDLEFUNC) pdo_dblib_error_handler);
  411. DBMSGHANDLE(H->login, (MHANDLEFUNC) pdo_dblib_msg_handler);
  412. if(vars[5].optval) {
  413. for(i=0;i<nvers;i++) {
  414. if(strcmp(vars[5].optval,tdsver[i].key) == 0) {
  415. if(FAIL==dbsetlversion(H->login, tdsver[i].value)) {
  416. pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Failed to set version specified in connection string.");
  417. goto cleanup;
  418. }
  419. break;
  420. }
  421. }
  422. if (i==nvers) {
  423. printf("Invalid version '%s'\n", vars[5].optval);
  424. pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Invalid version specified in connection string.");
  425. goto cleanup; /* unknown version specified */
  426. }
  427. }
  428. if (dbh->username) {
  429. if(FAIL == DBSETLUSER(H->login, dbh->username)) {
  430. goto cleanup;
  431. }
  432. }
  433. if (dbh->password) {
  434. if(FAIL == DBSETLPWD(H->login, dbh->password)) {
  435. goto cleanup;
  436. }
  437. }
  438. #if !PHP_DBLIB_IS_MSSQL
  439. if (vars[0].optval) {
  440. DBSETLCHARSET(H->login, vars[0].optval);
  441. }
  442. #endif
  443. DBSETLAPP(H->login, vars[1].optval);
  444. /* DBSETLDBNAME is only available in FreeTDS 0.92 or above */
  445. #ifdef DBSETLDBNAME
  446. if (vars[3].optval) {
  447. if(FAIL == DBSETLDBNAME(H->login, vars[3].optval)) goto cleanup;
  448. }
  449. #endif
  450. H->link = dbopen(H->login, vars[2].optval);
  451. if (!H->link) {
  452. goto cleanup;
  453. }
  454. /*
  455. * FreeTDS < 0.92 does not support the DBSETLDBNAME option
  456. * Send use database here after login (Will not work with SQL Azure)
  457. */
  458. #ifndef DBSETLDBNAME
  459. if (vars[3].optval) {
  460. if(FAIL == dbuse(H->link, vars[3].optval)) goto cleanup;
  461. }
  462. #endif
  463. #if PHP_DBLIB_IS_MSSQL
  464. /* dblib do not return more than this length from text/image */
  465. DBSETOPT(H->link, DBTEXTLIMIT, "2147483647");
  466. #endif
  467. /* limit text/image from network */
  468. DBSETOPT(H->link, DBTEXTSIZE, "2147483647");
  469. /* allow double quoted indentifiers */
  470. DBSETOPT(H->link, DBQUOTEDIDENT, "1");
  471. ret = 1;
  472. dbh->max_escaped_char_length = 2;
  473. dbh->alloc_own_columns = 1;
  474. cleanup:
  475. for (i = 0; i < nvars; i++) {
  476. if (vars[i].freeme) {
  477. efree(vars[i].optval);
  478. }
  479. }
  480. dbh->methods = &dblib_methods;
  481. dbh->driver_data = H;
  482. if (!ret) {
  483. zend_throw_exception_ex(php_pdo_get_exception(), DBLIB_G(err).dberr,
  484. "SQLSTATE[%s] %s (severity %d)",
  485. DBLIB_G(err).sqlstate,
  486. DBLIB_G(err).dberrstr,
  487. DBLIB_G(err).severity);
  488. }
  489. return ret;
  490. }
  491. const pdo_driver_t pdo_dblib_driver = {
  492. #if PDO_DBLIB_IS_MSSQL
  493. PDO_DRIVER_HEADER(mssql),
  494. #elif defined(PHP_WIN32)
  495. #define PDO_DBLIB_IS_SYBASE
  496. PDO_DRIVER_HEADER(sybase),
  497. #else
  498. PDO_DRIVER_HEADER(dblib),
  499. #endif
  500. pdo_dblib_handle_factory
  501. };