PageRenderTime 73ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/odbc/php_odbc.c

http://github.com/php/php-src
C | 3539 lines | 2685 code | 487 blank | 367 comment | 832 complexity | 6ad8240556832136423a4e6529a1196a MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | http://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Stig Sæther Bakken <ssb@php.net> |
  14. | Andreas Karajannis <Andreas.Karajannis@gmd.de> |
  15. | Frank M. Kromann <frank@kromann.info> Support for DB/2 CLI |
  16. | Kevin N. Shallow <kshallow@tampabay.rr.com> |
  17. | Daniel R. Kalowsky <kalowsky@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "php.h"
  24. #include "php_globals.h"
  25. #include "ext/standard/info.h"
  26. #include "ext/standard/php_string.h"
  27. #include "ext/standard/php_standard.h"
  28. #include "php_odbc.h"
  29. #include "php_odbc_includes.h"
  30. #include "php_globals.h"
  31. #include "odbc_arginfo.h"
  32. #if HAVE_UODBC
  33. #include <fcntl.h>
  34. #include "ext/standard/head.h"
  35. #include "php_ini.h"
  36. #ifdef PHP_WIN32
  37. #include <winsock2.h>
  38. #define ODBC_TYPE "Win32"
  39. #define PHP_ODBC_TYPE ODBC_TYPE
  40. #endif
  41. /*
  42. * not defined elsewhere
  43. */
  44. #ifndef TRUE
  45. #define TRUE 1
  46. #define FALSE 0
  47. #endif
  48. void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent);
  49. static int le_result, le_conn, le_pconn;
  50. #define SAFE_SQL_NTS(n) ((SQLSMALLINT) ((n)?(SQL_NTS):0))
  51. PHP_ODBC_API ZEND_DECLARE_MODULE_GLOBALS(odbc)
  52. static PHP_GINIT_FUNCTION(odbc);
  53. /* {{{ odbc_module_entry
  54. */
  55. zend_module_entry odbc_module_entry = {
  56. STANDARD_MODULE_HEADER,
  57. "odbc",
  58. ext_functions,
  59. PHP_MINIT(odbc),
  60. PHP_MSHUTDOWN(odbc),
  61. PHP_RINIT(odbc),
  62. PHP_RSHUTDOWN(odbc),
  63. PHP_MINFO(odbc),
  64. PHP_ODBC_VERSION,
  65. PHP_MODULE_GLOBALS(odbc),
  66. PHP_GINIT(odbc),
  67. NULL,
  68. NULL,
  69. STANDARD_MODULE_PROPERTIES_EX
  70. };
  71. /* }}} */
  72. #ifdef COMPILE_DL_ODBC
  73. #ifdef ZTS
  74. ZEND_TSRMLS_CACHE_DEFINE()
  75. #endif
  76. ZEND_GET_MODULE(odbc)
  77. #endif
  78. /* {{{ _free_odbc_result
  79. */
  80. static void _free_odbc_result(zend_resource *rsrc)
  81. {
  82. odbc_result *res = (odbc_result *)rsrc->ptr;
  83. int i;
  84. if (res) {
  85. if (res->values) {
  86. for(i = 0; i < res->numcols; i++) {
  87. if (res->values[i].value)
  88. efree(res->values[i].value);
  89. }
  90. efree(res->values);
  91. res->values = NULL;
  92. }
  93. /* If aborted via timer expiration, don't try to call any unixODBC function */
  94. if (res->stmt && !(PG(connection_status) & PHP_CONNECTION_TIMEOUT)) {
  95. #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30) || defined(HAVE_SOLID_35)
  96. SQLTransact(res->conn_ptr->henv, res->conn_ptr->hdbc,
  97. (SQLUSMALLINT) SQL_COMMIT);
  98. #endif
  99. SQLFreeStmt(res->stmt,SQL_DROP);
  100. /* We don't want the connection to be closed after the last statement has been closed
  101. * Connections will be closed on shutdown
  102. * zend_list_delete(res->conn_ptr->id);
  103. */
  104. }
  105. if (res->param_info) {
  106. efree(res->param_info);
  107. }
  108. efree(res);
  109. }
  110. }
  111. /* }}} */
  112. /* {{{ safe_odbc_disconnect
  113. * disconnect, and if it fails, then issue a rollback for any pending transaction (lurcher)
  114. */
  115. static void safe_odbc_disconnect( void *handle )
  116. {
  117. int ret;
  118. ret = SQLDisconnect( handle );
  119. if ( ret == SQL_ERROR )
  120. {
  121. SQLTransact( NULL, handle, SQL_ROLLBACK );
  122. SQLDisconnect( handle );
  123. }
  124. }
  125. /* }}} */
  126. /* {{{ _close_odbc_conn
  127. */
  128. static void _close_odbc_conn(zend_resource *rsrc)
  129. {
  130. zend_resource *p;
  131. odbc_result *res;
  132. odbc_connection *conn = (odbc_connection *)rsrc->ptr;
  133. ZEND_HASH_FOREACH_PTR(&EG(regular_list), p) {
  134. if (p->ptr && (p->type == le_result)) {
  135. res = (odbc_result *)p->ptr;
  136. if (res->conn_ptr == conn) {
  137. zend_list_close(p);
  138. }
  139. }
  140. } ZEND_HASH_FOREACH_END();
  141. /* If aborted via timer expiration, don't try to call any unixODBC function */
  142. if (!(PG(connection_status) & PHP_CONNECTION_TIMEOUT)) {
  143. safe_odbc_disconnect(conn->hdbc);
  144. SQLFreeConnect(conn->hdbc);
  145. SQLFreeEnv(conn->henv);
  146. }
  147. efree(conn);
  148. ODBCG(num_links)--;
  149. }
  150. /* }}} */
  151. /* {{{ void _close_odbc_pconn
  152. */
  153. static void _close_odbc_pconn(zend_resource *rsrc)
  154. {
  155. zend_resource *p;
  156. odbc_result *res;
  157. odbc_connection *conn = (odbc_connection *)rsrc->ptr;
  158. ZEND_HASH_FOREACH_PTR(&EG(regular_list), p) {
  159. if (p->ptr && (p->type == le_result)) {
  160. res = (odbc_result *)p->ptr;
  161. if (res->conn_ptr == conn) {
  162. zend_list_close(p);
  163. }
  164. }
  165. } ZEND_HASH_FOREACH_END();
  166. /* If aborted via timer expiration, don't try to call any unixODBC function */
  167. if (!(PG(connection_status) & PHP_CONNECTION_TIMEOUT)) {
  168. safe_odbc_disconnect(conn->hdbc);
  169. SQLFreeConnect(conn->hdbc);
  170. SQLFreeEnv(conn->henv);
  171. }
  172. free(conn);
  173. ODBCG(num_links)--;
  174. ODBCG(num_persistent)--;
  175. }
  176. /* }}} */
  177. /* {{{ PHP_INI_DISP(display_link_nums)
  178. */
  179. static PHP_INI_DISP(display_link_nums)
  180. {
  181. char *value;
  182. if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
  183. value = ZSTR_VAL(ini_entry->orig_value);
  184. } else if (ini_entry->value) {
  185. value = ZSTR_VAL(ini_entry->value);
  186. } else {
  187. value = NULL;
  188. }
  189. if (value) {
  190. if (atoi(value) == -1) {
  191. PUTS("Unlimited");
  192. } else {
  193. php_printf("%s", value);
  194. }
  195. }
  196. }
  197. /* }}} */
  198. /* {{{ PHP_INI_DISP(display_defPW)
  199. */
  200. static PHP_INI_DISP(display_defPW)
  201. {
  202. char *value;
  203. if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
  204. value = ZSTR_VAL(ini_entry->orig_value);
  205. } else if (ini_entry->value) {
  206. value = ZSTR_VAL(ini_entry->value);
  207. } else {
  208. value = NULL;
  209. }
  210. if (value) {
  211. #if PHP_DEBUG
  212. php_printf("%s", value);
  213. #else
  214. PUTS("********");
  215. #endif
  216. } else {
  217. if (PG(html_errors)) {
  218. PUTS("<i>no value</i>");
  219. } else {
  220. PUTS("no value");
  221. }
  222. }
  223. }
  224. /* }}} */
  225. /* {{{ PHP_INI_DISP(display_binmode)
  226. */
  227. static PHP_INI_DISP(display_binmode)
  228. {
  229. char *value;
  230. if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
  231. value = ZSTR_VAL(ini_entry->orig_value);
  232. } else if (ini_entry->value) {
  233. value = ZSTR_VAL(ini_entry->value);
  234. } else {
  235. value = NULL;
  236. }
  237. if (value) {
  238. switch(atoi(value)) {
  239. case 0:
  240. PUTS("passthru");
  241. break;
  242. case 1:
  243. PUTS("return as is");
  244. break;
  245. case 2:
  246. PUTS("return as char");
  247. break;
  248. }
  249. }
  250. }
  251. /* }}} */
  252. /* {{{ PHP_INI_DISP(display_lrl)
  253. */
  254. static PHP_INI_DISP(display_lrl)
  255. {
  256. char *value;
  257. if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
  258. value = ZSTR_VAL(ini_entry->orig_value);
  259. } else if (ini_entry->value) {
  260. value = ZSTR_VAL(ini_entry->value);
  261. } else {
  262. value = NULL;
  263. }
  264. if (value) {
  265. if (atoi(value) <= 0) {
  266. PUTS("Passthru");
  267. } else {
  268. php_printf("return up to %s bytes", value);
  269. }
  270. }
  271. }
  272. /* }}} */
  273. /* {{{ PHP_INI_DISP(display_cursortype)
  274. */
  275. static PHP_INI_DISP(display_cursortype)
  276. {
  277. char *value;
  278. if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
  279. value = ZSTR_VAL(ini_entry->orig_value);
  280. } else if (ini_entry->value) {
  281. value = ZSTR_VAL(ini_entry->value);
  282. } else {
  283. value = NULL;
  284. }
  285. if (value) {
  286. switch (atoi (value))
  287. {
  288. case SQL_CURSOR_FORWARD_ONLY:
  289. PUTS ("Forward Only cursor");
  290. break;
  291. case SQL_CURSOR_STATIC:
  292. PUTS ("Static cursor");
  293. break;
  294. case SQL_CURSOR_KEYSET_DRIVEN:
  295. PUTS ("Keyset driven cursor");
  296. break;
  297. case SQL_CURSOR_DYNAMIC:
  298. PUTS ("Dynamic cursor");
  299. break;
  300. default:
  301. php_printf("Unknown cursor model %s", value);
  302. break;
  303. }
  304. }
  305. }
  306. /* }}} */
  307. /* {{{ PHP_INI_BEGIN
  308. */
  309. PHP_INI_BEGIN()
  310. STD_PHP_INI_BOOLEAN("odbc.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateLong,
  311. allow_persistent, zend_odbc_globals, odbc_globals)
  312. STD_PHP_INI_ENTRY_EX("odbc.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateLong,
  313. max_persistent, zend_odbc_globals, odbc_globals, display_link_nums)
  314. STD_PHP_INI_ENTRY_EX("odbc.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong,
  315. max_links, zend_odbc_globals, odbc_globals, display_link_nums)
  316. STD_PHP_INI_ENTRY("odbc.default_db", NULL, PHP_INI_ALL, OnUpdateString,
  317. defDB, zend_odbc_globals, odbc_globals)
  318. STD_PHP_INI_ENTRY("odbc.default_user", NULL, PHP_INI_ALL, OnUpdateString,
  319. defUser, zend_odbc_globals, odbc_globals)
  320. STD_PHP_INI_ENTRY_EX("odbc.default_pw", NULL, PHP_INI_ALL, OnUpdateString,
  321. defPW, zend_odbc_globals, odbc_globals, display_defPW)
  322. STD_PHP_INI_ENTRY_EX("odbc.defaultlrl", "4096", PHP_INI_ALL, OnUpdateLong,
  323. defaultlrl, zend_odbc_globals, odbc_globals, display_lrl)
  324. STD_PHP_INI_ENTRY_EX("odbc.defaultbinmode", "1", PHP_INI_ALL, OnUpdateLong,
  325. defaultbinmode, zend_odbc_globals, odbc_globals, display_binmode)
  326. STD_PHP_INI_BOOLEAN("odbc.check_persistent", "1", PHP_INI_SYSTEM, OnUpdateLong,
  327. check_persistent, zend_odbc_globals, odbc_globals)
  328. STD_PHP_INI_ENTRY_EX("odbc.default_cursortype", "3", PHP_INI_ALL, OnUpdateLong,
  329. default_cursortype, zend_odbc_globals, odbc_globals, display_cursortype)
  330. PHP_INI_END()
  331. /* }}} */
  332. static PHP_GINIT_FUNCTION(odbc)
  333. {
  334. #if defined(COMPILE_DL_ODBC) && defined(ZTS)
  335. ZEND_TSRMLS_CACHE_UPDATE();
  336. #endif
  337. odbc_globals->num_persistent = 0;
  338. }
  339. /* {{{ PHP_MINIT_FUNCTION */
  340. PHP_MINIT_FUNCTION(odbc)
  341. {
  342. #ifdef SQLANY_BUG
  343. ODBC_SQL_CONN_T foobar;
  344. RETCODE rc;
  345. #endif
  346. REGISTER_INI_ENTRIES();
  347. le_result = zend_register_list_destructors_ex(_free_odbc_result, NULL, "odbc result", module_number);
  348. le_conn = zend_register_list_destructors_ex(_close_odbc_conn, NULL, "odbc link", module_number);
  349. le_pconn = zend_register_list_destructors_ex(NULL, _close_odbc_pconn, "odbc link persistent", module_number);
  350. odbc_module_entry.type = type;
  351. REGISTER_STRING_CONSTANT("ODBC_TYPE", PHP_ODBC_TYPE, CONST_CS | CONST_PERSISTENT);
  352. REGISTER_LONG_CONSTANT("ODBC_BINMODE_PASSTHRU", 0, CONST_CS | CONST_PERSISTENT);
  353. REGISTER_LONG_CONSTANT("ODBC_BINMODE_RETURN", 1, CONST_CS | CONST_PERSISTENT);
  354. REGISTER_LONG_CONSTANT("ODBC_BINMODE_CONVERT", 2, CONST_CS | CONST_PERSISTENT);
  355. /* Define Constants for options
  356. these Constants are defined in <sqlext.h>
  357. */
  358. REGISTER_LONG_CONSTANT("SQL_ODBC_CURSORS", SQL_ODBC_CURSORS, CONST_PERSISTENT | CONST_CS);
  359. REGISTER_LONG_CONSTANT("SQL_CUR_USE_DRIVER", SQL_CUR_USE_DRIVER, CONST_PERSISTENT | CONST_CS);
  360. REGISTER_LONG_CONSTANT("SQL_CUR_USE_IF_NEEDED", SQL_CUR_USE_IF_NEEDED, CONST_PERSISTENT | CONST_CS);
  361. REGISTER_LONG_CONSTANT("SQL_CUR_USE_ODBC", SQL_CUR_USE_ODBC, CONST_PERSISTENT | CONST_CS);
  362. REGISTER_LONG_CONSTANT("SQL_CONCURRENCY", SQL_CONCURRENCY, CONST_PERSISTENT | CONST_CS);
  363. REGISTER_LONG_CONSTANT("SQL_CONCUR_READ_ONLY", SQL_CONCUR_READ_ONLY, CONST_PERSISTENT | CONST_CS);
  364. REGISTER_LONG_CONSTANT("SQL_CONCUR_LOCK", SQL_CONCUR_LOCK, CONST_PERSISTENT | CONST_CS);
  365. REGISTER_LONG_CONSTANT("SQL_CONCUR_ROWVER", SQL_CONCUR_ROWVER, CONST_PERSISTENT | CONST_CS);
  366. REGISTER_LONG_CONSTANT("SQL_CONCUR_VALUES", SQL_CONCUR_VALUES, CONST_PERSISTENT | CONST_CS);
  367. REGISTER_LONG_CONSTANT("SQL_CURSOR_TYPE", SQL_CURSOR_TYPE, CONST_PERSISTENT | CONST_CS);
  368. REGISTER_LONG_CONSTANT("SQL_CURSOR_FORWARD_ONLY", SQL_CURSOR_FORWARD_ONLY, CONST_PERSISTENT | CONST_CS);
  369. REGISTER_LONG_CONSTANT("SQL_CURSOR_KEYSET_DRIVEN", SQL_CURSOR_KEYSET_DRIVEN, CONST_PERSISTENT | CONST_CS);
  370. REGISTER_LONG_CONSTANT("SQL_CURSOR_DYNAMIC", SQL_CURSOR_DYNAMIC, CONST_PERSISTENT | CONST_CS);
  371. REGISTER_LONG_CONSTANT("SQL_CURSOR_STATIC", SQL_CURSOR_STATIC, CONST_PERSISTENT | CONST_CS);
  372. REGISTER_LONG_CONSTANT("SQL_KEYSET_SIZE", SQL_KEYSET_SIZE, CONST_PERSISTENT | CONST_CS);
  373. /* these are for the Data Source type */
  374. REGISTER_LONG_CONSTANT("SQL_FETCH_FIRST", SQL_FETCH_FIRST, CONST_PERSISTENT | CONST_CS);
  375. REGISTER_LONG_CONSTANT("SQL_FETCH_NEXT", SQL_FETCH_NEXT, CONST_PERSISTENT | CONST_CS);
  376. /*
  377. * register the standard data types
  378. */
  379. REGISTER_LONG_CONSTANT("SQL_CHAR", SQL_CHAR, CONST_PERSISTENT | CONST_CS);
  380. REGISTER_LONG_CONSTANT("SQL_VARCHAR", SQL_VARCHAR, CONST_PERSISTENT | CONST_CS);
  381. REGISTER_LONG_CONSTANT("SQL_LONGVARCHAR", SQL_LONGVARCHAR, CONST_PERSISTENT | CONST_CS);
  382. REGISTER_LONG_CONSTANT("SQL_DECIMAL", SQL_DECIMAL, CONST_PERSISTENT | CONST_CS);
  383. REGISTER_LONG_CONSTANT("SQL_NUMERIC", SQL_NUMERIC, CONST_PERSISTENT | CONST_CS);
  384. REGISTER_LONG_CONSTANT("SQL_BIT", SQL_BIT, CONST_PERSISTENT | CONST_CS);
  385. REGISTER_LONG_CONSTANT("SQL_TINYINT", SQL_TINYINT, CONST_PERSISTENT | CONST_CS);
  386. REGISTER_LONG_CONSTANT("SQL_SMALLINT", SQL_SMALLINT, CONST_PERSISTENT | CONST_CS);
  387. REGISTER_LONG_CONSTANT("SQL_INTEGER", SQL_INTEGER, CONST_PERSISTENT | CONST_CS);
  388. REGISTER_LONG_CONSTANT("SQL_BIGINT", SQL_BIGINT, CONST_PERSISTENT | CONST_CS);
  389. REGISTER_LONG_CONSTANT("SQL_REAL", SQL_REAL, CONST_PERSISTENT | CONST_CS);
  390. REGISTER_LONG_CONSTANT("SQL_FLOAT", SQL_FLOAT, CONST_PERSISTENT | CONST_CS);
  391. REGISTER_LONG_CONSTANT("SQL_DOUBLE", SQL_DOUBLE, CONST_PERSISTENT | CONST_CS);
  392. REGISTER_LONG_CONSTANT("SQL_BINARY", SQL_BINARY, CONST_PERSISTENT | CONST_CS);
  393. REGISTER_LONG_CONSTANT("SQL_VARBINARY", SQL_VARBINARY, CONST_PERSISTENT | CONST_CS);
  394. REGISTER_LONG_CONSTANT("SQL_LONGVARBINARY", SQL_LONGVARBINARY, CONST_PERSISTENT | CONST_CS);
  395. REGISTER_LONG_CONSTANT("SQL_DATE", SQL_DATE, CONST_PERSISTENT | CONST_CS);
  396. REGISTER_LONG_CONSTANT("SQL_TIME", SQL_TIME, CONST_PERSISTENT | CONST_CS);
  397. REGISTER_LONG_CONSTANT("SQL_TIMESTAMP", SQL_TIMESTAMP, CONST_PERSISTENT | CONST_CS);
  398. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  399. REGISTER_LONG_CONSTANT("SQL_TYPE_DATE", SQL_TYPE_DATE, CONST_PERSISTENT | CONST_CS);
  400. REGISTER_LONG_CONSTANT("SQL_TYPE_TIME", SQL_TYPE_TIME, CONST_PERSISTENT | CONST_CS);
  401. REGISTER_LONG_CONSTANT("SQL_TYPE_TIMESTAMP", SQL_TYPE_TIMESTAMP, CONST_PERSISTENT | CONST_CS);
  402. REGISTER_LONG_CONSTANT("SQL_WCHAR", SQL_WCHAR, CONST_PERSISTENT | CONST_CS);
  403. REGISTER_LONG_CONSTANT("SQL_WVARCHAR", SQL_WVARCHAR, CONST_PERSISTENT | CONST_CS);
  404. REGISTER_LONG_CONSTANT("SQL_WLONGVARCHAR", SQL_WLONGVARCHAR, CONST_PERSISTENT | CONST_CS);
  405. /*
  406. * SQLSpecialColumns values
  407. */
  408. REGISTER_LONG_CONSTANT("SQL_BEST_ROWID", SQL_BEST_ROWID, CONST_PERSISTENT | CONST_CS);
  409. REGISTER_LONG_CONSTANT("SQL_ROWVER", SQL_ROWVER, CONST_PERSISTENT | CONST_CS);
  410. REGISTER_LONG_CONSTANT("SQL_SCOPE_CURROW", SQL_SCOPE_CURROW, CONST_PERSISTENT | CONST_CS);
  411. REGISTER_LONG_CONSTANT("SQL_SCOPE_TRANSACTION", SQL_SCOPE_TRANSACTION, CONST_PERSISTENT | CONST_CS);
  412. REGISTER_LONG_CONSTANT("SQL_SCOPE_SESSION", SQL_SCOPE_SESSION, CONST_PERSISTENT | CONST_CS);
  413. REGISTER_LONG_CONSTANT("SQL_NO_NULLS", SQL_NO_NULLS, CONST_PERSISTENT | CONST_CS);
  414. REGISTER_LONG_CONSTANT("SQL_NULLABLE", SQL_NULLABLE, CONST_PERSISTENT | CONST_CS);
  415. /*
  416. * SQLStatistics values
  417. */
  418. REGISTER_LONG_CONSTANT("SQL_INDEX_UNIQUE", SQL_INDEX_UNIQUE, CONST_PERSISTENT | CONST_CS);
  419. REGISTER_LONG_CONSTANT("SQL_INDEX_ALL", SQL_INDEX_ALL, CONST_PERSISTENT | CONST_CS);
  420. REGISTER_LONG_CONSTANT("SQL_ENSURE", SQL_ENSURE, CONST_PERSISTENT | CONST_CS);
  421. REGISTER_LONG_CONSTANT("SQL_QUICK", SQL_QUICK, CONST_PERSISTENT | CONST_CS);
  422. #endif
  423. #if defined(HAVE_IBMDB2) && defined(_AIX)
  424. /* atexit() handler in the DB2/AIX library segfaults in PHP CLI */
  425. /* DB2NOEXITLIST env variable prevents DB2 from invoking atexit() */
  426. putenv("DB2NOEXITLIST=TRUE");
  427. #endif
  428. return SUCCESS;
  429. }
  430. /* }}} */
  431. /* {{{ PHP_RINIT_FUNCTION */
  432. PHP_RINIT_FUNCTION(odbc)
  433. {
  434. ODBCG(defConn) = -1;
  435. ODBCG(num_links) = ODBCG(num_persistent);
  436. memset(ODBCG(laststate), '\0', 6);
  437. memset(ODBCG(lasterrormsg), '\0', SQL_MAX_MESSAGE_LENGTH);
  438. return SUCCESS;
  439. }
  440. /* }}} */
  441. /* {{{ PHP_RSHUTDOWN_FUNCTION */
  442. PHP_RSHUTDOWN_FUNCTION(odbc)
  443. {
  444. return SUCCESS;
  445. }
  446. /* }}} */
  447. /* {{{ PHP_MSHUTDOWN_FUNCTION */
  448. PHP_MSHUTDOWN_FUNCTION(odbc)
  449. {
  450. UNREGISTER_INI_ENTRIES();
  451. return SUCCESS;
  452. }
  453. /* }}} */
  454. /* {{{ PHP_MINFO_FUNCTION */
  455. PHP_MINFO_FUNCTION(odbc)
  456. {
  457. char buf[32];
  458. php_info_print_table_start();
  459. php_info_print_table_header(2, "ODBC Support", "enabled");
  460. snprintf(buf, sizeof(buf), ZEND_LONG_FMT, ODBCG(num_persistent));
  461. php_info_print_table_row(2, "Active Persistent Links", buf);
  462. snprintf(buf, sizeof(buf), ZEND_LONG_FMT, ODBCG(num_links));
  463. php_info_print_table_row(2, "Active Links", buf);
  464. php_info_print_table_row(2, "ODBC library", PHP_ODBC_TYPE);
  465. #ifdef ODBCVER
  466. snprintf(buf, sizeof(buf), "0x%0.4x", ODBCVER);
  467. php_info_print_table_row(2, "ODBCVER", buf);
  468. #endif
  469. #ifndef PHP_WIN32
  470. php_info_print_table_row(2, "ODBC_CFLAGS", PHP_ODBC_CFLAGS);
  471. php_info_print_table_row(2, "ODBC_LFLAGS", PHP_ODBC_LFLAGS);
  472. php_info_print_table_row(2, "ODBC_LIBS", PHP_ODBC_LIBS);
  473. #endif
  474. php_info_print_table_end();
  475. DISPLAY_INI_ENTRIES();
  476. }
  477. /* }}} */
  478. /* {{{ odbc_sql_error */
  479. void odbc_sql_error(ODBC_SQL_ERROR_PARAMS)
  480. {
  481. SQLINTEGER error; /* Not used */
  482. SQLSMALLINT errormsgsize; /* Not used */
  483. RETCODE rc;
  484. ODBC_SQL_ENV_T henv;
  485. ODBC_SQL_CONN_T conn;
  486. if (conn_resource) {
  487. henv = conn_resource->henv;
  488. conn = conn_resource->hdbc;
  489. } else {
  490. henv = SQL_NULL_HENV;
  491. conn = SQL_NULL_HDBC;
  492. }
  493. /* This leads to an endless loop in many drivers!
  494. *
  495. while(henv != SQL_NULL_HENV){
  496. do {
  497. */
  498. rc = SQLError(henv, conn, stmt, ODBCG(laststate), &error, ODBCG(lasterrormsg), sizeof(ODBCG(lasterrormsg))-1, &errormsgsize);
  499. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  500. snprintf(ODBCG(laststate), sizeof(ODBCG(laststate)), "HY000");
  501. snprintf(ODBCG(lasterrormsg), sizeof(ODBCG(lasterrormsg)), "Failed to fetch error message");
  502. }
  503. if (conn_resource) {
  504. memcpy(conn_resource->laststate, ODBCG(laststate), sizeof(ODBCG(laststate)));
  505. memcpy(conn_resource->lasterrormsg, ODBCG(lasterrormsg), sizeof(ODBCG(lasterrormsg)));
  506. }
  507. if (func) {
  508. php_error_docref(NULL, E_WARNING, "SQL error: %s, SQL state %s in %s", ODBCG(lasterrormsg), ODBCG(laststate), func);
  509. } else {
  510. php_error_docref(NULL, E_WARNING, "SQL error: %s, SQL state %s", ODBCG(lasterrormsg), ODBCG(laststate));
  511. }
  512. /*
  513. } while (SQL_SUCCEEDED(rc));
  514. }
  515. */
  516. }
  517. /* }}} */
  518. /* {{{ php_odbc_fetch_attribs */
  519. void php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAMETERS, int mode)
  520. {
  521. odbc_result *result;
  522. zval *pv_res;
  523. zend_long flag;
  524. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &pv_res, &flag) == FAILURE) {
  525. RETURN_THROWS();
  526. }
  527. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  528. RETURN_THROWS();
  529. }
  530. if (mode) {
  531. result->longreadlen = flag;
  532. } else {
  533. result->binmode = flag;
  534. }
  535. RETURN_TRUE;
  536. }
  537. /* }}} */
  538. /* {{{ odbc_bindcols */
  539. int odbc_bindcols(odbc_result *result)
  540. {
  541. RETCODE rc;
  542. int i;
  543. SQLSMALLINT colnamelen; /* Not used */
  544. SQLLEN displaysize;
  545. SQLUSMALLINT colfieldid;
  546. int charextraalloc;
  547. result->values = (odbc_result_value *) safe_emalloc(sizeof(odbc_result_value), result->numcols, 0);
  548. result->longreadlen = ODBCG(defaultlrl);
  549. result->binmode = ODBCG(defaultbinmode);
  550. for(i = 0; i < result->numcols; i++) {
  551. charextraalloc = 0;
  552. colfieldid = SQL_COLUMN_DISPLAY_SIZE;
  553. rc = PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(i+1), PHP_ODBC_SQL_DESC_NAME,
  554. result->values[i].name, sizeof(result->values[i].name), &colnamelen, 0);
  555. result->values[i].coltype = 0;
  556. rc = PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(i+1), SQL_COLUMN_TYPE,
  557. NULL, 0, NULL, &result->values[i].coltype);
  558. /* Don't bind LONG / BINARY columns, so that fetch behaviour can
  559. * be controlled by odbc_binmode() / odbc_longreadlen()
  560. */
  561. switch(result->values[i].coltype) {
  562. case SQL_BINARY:
  563. case SQL_VARBINARY:
  564. case SQL_LONGVARBINARY:
  565. case SQL_LONGVARCHAR:
  566. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  567. case SQL_WLONGVARCHAR:
  568. #endif
  569. result->values[i].value = NULL;
  570. break;
  571. #ifdef HAVE_ADABAS
  572. case SQL_TIMESTAMP:
  573. result->values[i].value = (char *)emalloc(27);
  574. SQLBindCol(result->stmt, (SQLUSMALLINT)(i+1), SQL_C_CHAR, result->values[i].value,
  575. 27, &result->values[i].vallen);
  576. break;
  577. #endif /* HAVE_ADABAS */
  578. case SQL_CHAR:
  579. case SQL_VARCHAR:
  580. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  581. case SQL_WCHAR:
  582. case SQL_WVARCHAR:
  583. colfieldid = SQL_DESC_OCTET_LENGTH;
  584. #else
  585. charextraalloc = 1;
  586. #endif
  587. default:
  588. rc = PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(i+1), colfieldid,
  589. NULL, 0, NULL, &displaysize);
  590. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  591. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO && colfieldid == SQL_DESC_OCTET_LENGTH) {
  592. SQLINTEGER err;
  593. SQLCHAR errtxt[128];
  594. SQLCHAR state[6];
  595. memset(errtxt, '\0', 128);
  596. memset(state, '\0', 6);
  597. if (SQL_SUCCESS == SQLGetDiagRec(SQL_HANDLE_STMT, result->stmt, 1, state, &err, errtxt, 128, NULL)) {
  598. errtxt[127] = '\0';
  599. state[5] = '\0';
  600. php_error_docref(NULL, E_WARNING, "SQLColAttribute can't handle SQL_DESC_OCTET_LENGTH: [%s] %s", state, errtxt);
  601. }
  602. /* This is a quirk for ODBC 2.0 compatibility for broken driver implementations.
  603. */
  604. charextraalloc = 1;
  605. rc = SQLColAttributes(result->stmt, (SQLUSMALLINT)(i+1), SQL_COLUMN_DISPLAY_SIZE,
  606. NULL, 0, NULL, &displaysize);
  607. }
  608. /* Workaround for drivers that report NVARCHAR(MAX) columns as SQL_WVARCHAR with size 0 (bug #69975) */
  609. if (result->values[i].coltype == SQL_WVARCHAR && displaysize == 0) {
  610. result->values[i].coltype = SQL_WLONGVARCHAR;
  611. result->values[i].value = NULL;
  612. break;
  613. }
  614. #endif
  615. /* Workaround for drivers that report VARCHAR(MAX) columns as SQL_VARCHAR (bug #73725) */
  616. if (SQL_VARCHAR == result->values[i].coltype && displaysize == 0) {
  617. result->values[i].coltype = SQL_LONGVARCHAR;
  618. result->values[i].value = NULL;
  619. break;
  620. }
  621. /* Workaround for Oracle ODBC Driver bug (#50162) when fetching TIMESTAMP column */
  622. if (result->values[i].coltype == SQL_TIMESTAMP) {
  623. displaysize += 3;
  624. }
  625. if (charextraalloc) {
  626. /* Since we don't know the exact # of bytes, allocate extra */
  627. displaysize *= 4;
  628. }
  629. result->values[i].value = (char *)emalloc(displaysize + 1);
  630. rc = SQLBindCol(result->stmt, (SQLUSMALLINT)(i+1), SQL_C_CHAR, result->values[i].value,
  631. displaysize + 1, &result->values[i].vallen);
  632. break;
  633. }
  634. }
  635. return 1;
  636. }
  637. /* }}} */
  638. /* {{{ odbc_transact */
  639. void odbc_transact(INTERNAL_FUNCTION_PARAMETERS, int type)
  640. {
  641. odbc_connection *conn;
  642. RETCODE rc;
  643. zval *pv_conn;
  644. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_conn) == FAILURE) {
  645. RETURN_THROWS();
  646. }
  647. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  648. RETURN_THROWS();
  649. }
  650. rc = SQLTransact(conn->henv, conn->hdbc, (SQLUSMALLINT)((type)?SQL_COMMIT:SQL_ROLLBACK));
  651. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  652. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLTransact");
  653. RETURN_FALSE;
  654. }
  655. RETURN_TRUE;
  656. }
  657. /* }}} */
  658. /* {{{ _close_pconn_with_res */
  659. static int _close_pconn_with_res(zend_resource *le, zend_resource *res)
  660. {
  661. if (le->type == le_pconn && (((odbc_connection *)(le->ptr))->res == res)){
  662. return 1;
  663. }else{
  664. return 0;
  665. }
  666. }
  667. /* }}} */
  668. /* {{{ odbc_column_lengths */
  669. void odbc_column_lengths(INTERNAL_FUNCTION_PARAMETERS, int type)
  670. {
  671. odbc_result *result;
  672. #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30)
  673. /* this seems to be necessary for Solid2.3 ( tested by
  674. * tammy@synchronis.com) and Solid 3.0 (tested by eric@terra.telemediair.nl)
  675. * Solid does not seem to declare a SQLINTEGER, but it does declare a
  676. * SQL_INTEGER which does not work (despite being the same type as a SDWORD.
  677. * Solid 3.5 does not have this issue.
  678. */
  679. SDWORD len;
  680. #else
  681. SQLLEN len;
  682. #endif
  683. zval *pv_res;
  684. zend_long pv_num;
  685. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &pv_res, &pv_num) == FAILURE) {
  686. RETURN_THROWS();
  687. }
  688. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  689. RETURN_THROWS();
  690. }
  691. if (result->numcols == 0) {
  692. php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
  693. RETURN_FALSE;
  694. }
  695. if (pv_num > result->numcols) {
  696. php_error_docref(NULL, E_WARNING, "Field index larger than number of fields");
  697. RETURN_FALSE;
  698. }
  699. if (pv_num < 1) {
  700. php_error_docref(NULL, E_WARNING, "Field numbering starts at 1");
  701. RETURN_FALSE;
  702. }
  703. PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)pv_num, (SQLUSMALLINT) (type?SQL_COLUMN_SCALE:SQL_COLUMN_PRECISION), NULL, 0, NULL, &len);
  704. RETURN_LONG(len);
  705. }
  706. /* }}} */
  707. /* Main User Functions */
  708. /* {{{ proto void odbc_close_all(void)
  709. Close all ODBC connections */
  710. PHP_FUNCTION(odbc_close_all)
  711. {
  712. zend_resource *p;
  713. if (zend_parse_parameters_none() == FAILURE) {
  714. RETURN_THROWS();
  715. }
  716. /* Loop through list and close all statements */
  717. ZEND_HASH_FOREACH_PTR(&EG(regular_list), p) {
  718. if (p->ptr && (p->type == le_result)) {
  719. zend_list_close(p);
  720. }
  721. } ZEND_HASH_FOREACH_END();
  722. /* Second loop through list, now close all connections */
  723. ZEND_HASH_FOREACH_PTR(&EG(regular_list), p) {
  724. if (p->ptr) {
  725. if (p->type == le_conn){
  726. zend_list_close(p);
  727. } else if (p->type == le_pconn){
  728. zend_list_close(p);
  729. /* Delete the persistent connection */
  730. zend_hash_apply_with_argument(&EG(persistent_list),
  731. (apply_func_arg_t) _close_pconn_with_res, (void *)p);
  732. }
  733. }
  734. } ZEND_HASH_FOREACH_END();
  735. }
  736. /* }}} */
  737. /* {{{ proto bool odbc_binmode(int result_id, int mode)
  738. Handle binary column data */
  739. PHP_FUNCTION(odbc_binmode)
  740. {
  741. php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  742. }
  743. /* }}} */
  744. /* {{{ proto bool odbc_longreadlen(int result_id, int length)
  745. Handle LONG columns */
  746. PHP_FUNCTION(odbc_longreadlen)
  747. {
  748. php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  749. }
  750. /* }}} */
  751. /* {{{ proto resource odbc_prepare(resource connection_id, string query)
  752. Prepares a statement for execution */
  753. PHP_FUNCTION(odbc_prepare)
  754. {
  755. zval *pv_conn;
  756. char *query;
  757. size_t query_len;
  758. odbc_result *result = NULL;
  759. odbc_connection *conn;
  760. RETCODE rc;
  761. int i;
  762. #ifdef HAVE_SQL_EXTENDED_FETCH
  763. SQLUINTEGER scrollopts;
  764. #endif
  765. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &pv_conn, &query, &query_len) == FAILURE) {
  766. RETURN_THROWS();
  767. }
  768. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  769. RETURN_THROWS();
  770. }
  771. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  772. result->numparams = 0;
  773. result->param_info = NULL;
  774. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  775. if (rc == SQL_INVALID_HANDLE) {
  776. efree(result);
  777. php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  778. RETURN_FALSE;
  779. }
  780. if (rc == SQL_ERROR) {
  781. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  782. efree(result);
  783. RETURN_FALSE;
  784. }
  785. #ifdef HAVE_SQL_EXTENDED_FETCH
  786. /* Solid doesn't have ExtendedFetch, if DriverManager is used, get Info,
  787. whether Driver supports ExtendedFetch */
  788. rc = SQLGetInfo(conn->hdbc, SQL_FETCH_DIRECTION, (void *) &scrollopts, sizeof(scrollopts), NULL);
  789. if (rc == SQL_SUCCESS) {
  790. if ((result->fetch_abs = (scrollopts & SQL_FD_FETCH_ABSOLUTE))) {
  791. /* Try to set CURSOR_TYPE to dynamic. Driver will replace this with other
  792. type if not possible.
  793. */
  794. SQLSetStmtOption(result->stmt, SQL_CURSOR_TYPE, ODBCG(default_cursortype));
  795. }
  796. } else {
  797. result->fetch_abs = 0;
  798. }
  799. #endif
  800. rc = SQLPrepare(result->stmt, query, SQL_NTS);
  801. switch (rc) {
  802. case SQL_SUCCESS:
  803. break;
  804. case SQL_SUCCESS_WITH_INFO:
  805. odbc_sql_error(conn, result->stmt, "SQLPrepare");
  806. break;
  807. default:
  808. odbc_sql_error(conn, result->stmt, "SQLPrepare");
  809. RETURN_FALSE;
  810. }
  811. SQLNumParams(result->stmt, &(result->numparams));
  812. SQLNumResultCols(result->stmt, &(result->numcols));
  813. if (result->numcols > 0) {
  814. if (!odbc_bindcols(result)) {
  815. efree(result);
  816. RETURN_FALSE;
  817. }
  818. } else {
  819. result->values = NULL;
  820. }
  821. Z_ADDREF_P(pv_conn);
  822. result->conn_ptr = conn;
  823. result->fetched = 0;
  824. result->param_info = (odbc_param_info *) safe_emalloc(sizeof(odbc_param_info), result->numparams, 0);
  825. for (i=0;i<result->numparams;i++) {
  826. rc = SQLDescribeParam(result->stmt, (SQLUSMALLINT)(i+1), &result->param_info[i].sqltype, &result->param_info[i].precision,
  827. &result->param_info[i].scale, &result->param_info[i].nullable);
  828. if (rc == SQL_ERROR) {
  829. odbc_sql_error(result->conn_ptr, result->stmt, "SQLDescribeParameter");
  830. SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
  831. efree(result->param_info);
  832. efree(result);
  833. RETURN_FALSE;
  834. }
  835. }
  836. RETURN_RES(zend_register_resource(result, le_result));
  837. }
  838. /* }}} */
  839. /*
  840. * Execute prepared SQL statement. Supports only input parameters.
  841. */
  842. /* {{{ proto bool odbc_execute(resource result_id [, array parameters_array])
  843. Execute a prepared statement */
  844. PHP_FUNCTION(odbc_execute)
  845. {
  846. zval *pv_res, *pv_param_arr, *tmp;
  847. typedef struct params_t {
  848. SQLLEN vallen;
  849. int fp;
  850. } params_t;
  851. params_t *params = NULL;
  852. char *filename;
  853. unsigned char otype;
  854. SQLSMALLINT ctype;
  855. odbc_result *result;
  856. int numArgs = ZEND_NUM_ARGS(), i, ne;
  857. RETCODE rc;
  858. if (zend_parse_parameters(numArgs, "r|a", &pv_res, &pv_param_arr) == FAILURE) {
  859. RETURN_THROWS();
  860. }
  861. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  862. RETURN_THROWS();
  863. }
  864. /* XXX check for already bound parameters*/
  865. if (result->numparams > 0 && numArgs == 1) {
  866. php_error_docref(NULL, E_WARNING, "No parameters to SQL statement given");
  867. RETURN_FALSE;
  868. }
  869. if (result->numparams > 0) {
  870. if ((ne = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr))) < result->numparams) {
  871. php_error_docref(NULL, E_WARNING,"Not enough parameters (%d should be %d) given", ne, result->numparams);
  872. RETURN_FALSE;
  873. }
  874. zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr));
  875. params = (params_t *)safe_emalloc(sizeof(params_t), result->numparams, 0);
  876. for(i = 0; i < result->numparams; i++) {
  877. params[i].fp = -1;
  878. }
  879. for(i = 1; i <= result->numparams; i++) {
  880. if ((tmp = zend_hash_get_current_data(Z_ARRVAL_P(pv_param_arr))) == NULL) {
  881. php_error_docref(NULL, E_WARNING,"Error getting parameter");
  882. SQLFreeStmt(result->stmt,SQL_RESET_PARAMS);
  883. for (i = 0; i < result->numparams; i++) {
  884. if (params[i].fp != -1) {
  885. close(params[i].fp);
  886. }
  887. }
  888. efree(params);
  889. RETURN_FALSE;
  890. }
  891. otype = Z_TYPE_P(tmp);
  892. if (!try_convert_to_string(tmp)) {
  893. SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
  894. for (i = 0; i < result->numparams; i++) {
  895. if (params[i].fp != -1) {
  896. close(params[i].fp);
  897. }
  898. }
  899. efree(params);
  900. RETURN_THROWS();
  901. }
  902. params[i-1].vallen = Z_STRLEN_P(tmp);
  903. params[i-1].fp = -1;
  904. if (IS_SQL_BINARY(result->param_info[i-1].sqltype)) {
  905. ctype = SQL_C_BINARY;
  906. } else {
  907. ctype = SQL_C_CHAR;
  908. }
  909. if (Z_STRLEN_P(tmp) > 2 &&
  910. Z_STRVAL_P(tmp)[0] == '\'' &&
  911. Z_STRVAL_P(tmp)[Z_STRLEN_P(tmp) - 1] == '\'') {
  912. if (CHECK_ZVAL_NULL_PATH(tmp)) {
  913. RETURN_FALSE;
  914. }
  915. filename = estrndup(&Z_STRVAL_P(tmp)[1], Z_STRLEN_P(tmp) - 2);
  916. filename[strlen(filename)] = '\0';
  917. /* Check the basedir */
  918. if (php_check_open_basedir(filename)) {
  919. efree(filename);
  920. SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
  921. for (i = 0; i < result->numparams; i++) {
  922. if (params[i].fp != -1) {
  923. close(params[i].fp);
  924. }
  925. }
  926. efree(params);
  927. RETURN_FALSE;
  928. }
  929. if ((params[i-1].fp = open(filename,O_RDONLY)) == -1) {
  930. php_error_docref(NULL, E_WARNING,"Can't open file %s", filename);
  931. SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
  932. for (i = 0; i < result->numparams; i++) {
  933. if (params[i].fp != -1) {
  934. close(params[i].fp);
  935. }
  936. }
  937. efree(params);
  938. efree(filename);
  939. RETURN_FALSE;
  940. }
  941. efree(filename);
  942. params[i-1].vallen = SQL_LEN_DATA_AT_EXEC(0);
  943. rc = SQLBindParameter(result->stmt, (SQLUSMALLINT)i, SQL_PARAM_INPUT,
  944. ctype, result->param_info[i-1].sqltype, result->param_info[i-1].precision, result->param_info[i-1].scale,
  945. (void *)(intptr_t)params[i-1].fp, 0,
  946. &params[i-1].vallen);
  947. } else {
  948. #ifdef HAVE_DBMAKER
  949. precision = params[i-1].vallen;
  950. #endif
  951. if (otype == IS_NULL) {
  952. params[i-1].vallen = SQL_NULL_DATA;
  953. }
  954. rc = SQLBindParameter(result->stmt, (SQLUSMALLINT)i, SQL_PARAM_INPUT,
  955. ctype, result->param_info[i-1].sqltype, result->param_info[i-1].precision, result->param_info[i-1].scale,
  956. Z_STRVAL_P(tmp), 0,
  957. &params[i-1].vallen);
  958. }
  959. if (rc == SQL_ERROR) {
  960. odbc_sql_error(result->conn_ptr, result->stmt, "SQLBindParameter");
  961. SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
  962. for (i = 0; i < result->numparams; i++) {
  963. if (params[i].fp != -1) {
  964. close(params[i].fp);
  965. }
  966. }
  967. efree(params);
  968. RETURN_FALSE;
  969. }
  970. zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr));
  971. }
  972. }
  973. /* Close cursor, needed for doing multiple selects */
  974. rc = SQLFreeStmt(result->stmt, SQL_CLOSE);
  975. if (rc == SQL_ERROR) {
  976. odbc_sql_error(result->conn_ptr, result->stmt, "SQLFreeStmt");
  977. }
  978. rc = SQLExecute(result->stmt);
  979. result->fetched = 0;
  980. if (rc == SQL_NEED_DATA) {
  981. char buf[4096];
  982. int fp, nbytes;
  983. while (rc == SQL_NEED_DATA) {
  984. rc = SQLParamData(result->stmt, (void*)&fp);
  985. if (rc == SQL_NEED_DATA) {
  986. while ((nbytes = read(fp, &buf, 4096)) > 0) {
  987. SQLPutData(result->stmt, (void*)&buf, nbytes);
  988. }
  989. }
  990. }
  991. } else {
  992. switch (rc) {
  993. case SQL_SUCCESS:
  994. break;
  995. case SQL_NO_DATA_FOUND:
  996. case SQL_SUCCESS_WITH_INFO:
  997. odbc_sql_error(result->conn_ptr, result->stmt, "SQLExecute");
  998. break;
  999. default:
  1000. odbc_sql_error(result->conn_ptr, result->stmt, "SQLExecute");
  1001. RETVAL_FALSE;
  1002. }
  1003. }
  1004. if (result->numparams > 0) {
  1005. SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
  1006. for(i = 0; i < result->numparams; i++) {
  1007. if (params[i].fp != -1) {
  1008. close(params[i].fp);
  1009. }
  1010. }
  1011. efree(params);
  1012. }
  1013. if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO || rc == SQL_NO_DATA_FOUND) {
  1014. RETVAL_TRUE;
  1015. }
  1016. if (result->numcols == 0) {
  1017. SQLNumResultCols(result->stmt, &(result->numcols));
  1018. if (result->numcols > 0) {
  1019. if (!odbc_bindcols(result)) {
  1020. efree(result);
  1021. RETVAL_FALSE;
  1022. }
  1023. } else {
  1024. result->values = NULL;
  1025. }
  1026. }
  1027. }
  1028. /* }}} */
  1029. /* {{{ proto string odbc_cursor(resource result_id)
  1030. Get cursor name */
  1031. PHP_FUNCTION(odbc_cursor)
  1032. {
  1033. zval *pv_res;
  1034. SQLUSMALLINT max_len;
  1035. SQLSMALLINT len;
  1036. char *cursorname;
  1037. odbc_result *result;
  1038. RETCODE rc;
  1039. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
  1040. RETURN_THROWS();
  1041. }
  1042. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  1043. RETURN_THROWS();
  1044. }
  1045. rc = SQLGetInfo(result->conn_ptr->hdbc,SQL_MAX_CURSOR_NAME_LEN, (void *)&max_len,sizeof(max_len),&len);
  1046. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1047. RETURN_FALSE;
  1048. }
  1049. if (max_len > 0) {
  1050. cursorname = emalloc(max_len + 1);
  1051. rc = SQLGetCursorName(result->stmt,cursorname,(SQLSMALLINT)max_len,&len);
  1052. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1053. char state[6]; /* Not used */
  1054. SQLINTEGER error; /* Not used */
  1055. char errormsg[SQL_MAX_MESSAGE_LENGTH];
  1056. SQLSMALLINT errormsgsize; /* Not used */
  1057. SQLError( result->conn_ptr->henv, result->conn_ptr->hdbc,
  1058. result->stmt, state, &error, errormsg,
  1059. sizeof(errormsg)-1, &errormsgsize);
  1060. if (!strncmp(state,"S1015",5)) {
  1061. snprintf(cursorname, max_len+1, "php_curs_" ZEND_ULONG_FMT, (zend_ulong)result->stmt);
  1062. if (SQLSetCursorName(result->stmt,cursorname,SQL_NTS) != SQL_SUCCESS) {
  1063. odbc_sql_error(result->conn_ptr, result->stmt, "SQLSetCursorName");
  1064. RETVAL_FALSE;
  1065. } else {
  1066. RETVAL_STRING(cursorname);
  1067. }
  1068. } else {
  1069. php_error_docref(NULL, E_WARNING, "SQL error: %s, SQL state %s", errormsg, state);
  1070. RETVAL_FALSE;
  1071. }
  1072. } else {
  1073. RETVAL_STRING(cursorname);
  1074. }
  1075. efree(cursorname);
  1076. } else {
  1077. RETVAL_FALSE;
  1078. }
  1079. }
  1080. /* }}} */
  1081. #ifdef HAVE_SQLDATASOURCES
  1082. /* {{{ proto array odbc_data_source(resource connection_id, int fetch_type)
  1083. Return information about the currently connected data source */
  1084. PHP_FUNCTION(odbc_data_source)
  1085. {
  1086. zval *zv_conn;
  1087. zend_long zv_fetch_type;
  1088. RETCODE rc = 0; /* assume all is good */
  1089. odbc_connection *conn;
  1090. UCHAR server_name[100], desc[200];
  1091. SQLSMALLINT len1=0, len2=0, fetch_type;
  1092. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zv_conn, &zv_fetch_type) == FAILURE) {
  1093. RETURN_THROWS();
  1094. }
  1095. fetch_type = (SQLSMALLINT) zv_fetch_type;
  1096. if (!(fetch_type == SQL_FETCH_FIRST || fetch_type == SQL_FETCH_NEXT)) {
  1097. php_error_docref(NULL, E_WARNING, "Invalid fetch type (%d)", fetch_type);
  1098. RETURN_FALSE;
  1099. }
  1100. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(zv_conn), "ODBC-Link", le_conn, le_pconn))) {
  1101. RETURN_THROWS();
  1102. }
  1103. /* now we have the "connection" lets call the DataSource object */
  1104. rc = SQLDataSources(conn->henv,
  1105. fetch_type,
  1106. server_name,
  1107. (SQLSMALLINT)sizeof(server_name),
  1108. &len1,
  1109. desc,
  1110. (SQLSMALLINT)sizeof(desc),
  1111. &len2);
  1112. if (SQL_NO_DATA == rc) {
  1113. /* System has no data sources, no error. Signal it by returning NULL,
  1114. not false. */
  1115. RETURN_NULL();
  1116. } else if (rc != SQL_SUCCESS) {
  1117. /* ummm.... he did it */
  1118. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLDataSources");
  1119. RETURN_FALSE;
  1120. }
  1121. if (len1 == 0 || len2 == 0) {
  1122. /* we have a non-valid entry... so stop the looping */
  1123. RETURN_FALSE;
  1124. }
  1125. array_init(return_value);
  1126. add_assoc_string_ex(return_value, "server", sizeof("server")-1, server_name);
  1127. add_assoc_string_ex(return_value, "description", sizeof("description")-1, desc);
  1128. }
  1129. /* }}} */
  1130. #endif /* HAVE_SQLDATASOURCES */
  1131. /* {{{ proto resource odbc_exec(resource connection_id, string query [, int flags])
  1132. Prepare and execute an SQL statement */
  1133. /* XXX Use flags */
  1134. PHP_FUNCTION(odbc_exec)
  1135. {
  1136. zval *pv_conn;
  1137. zend_long pv_flags;
  1138. char *query;
  1139. size_t query_len;
  1140. odbc_result *result = NULL;
  1141. odbc_connection *conn;
  1142. RETCODE rc;
  1143. #ifdef HAVE_SQL_EXTENDED_FETCH
  1144. SQLUINTEGER scrollopts;
  1145. #endif
  1146. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|l", &pv_conn, &query, &query_len, &pv_flags) == FAILURE) {
  1147. RETURN_THROWS();
  1148. }
  1149. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  1150. RETURN_THROWS();
  1151. }
  1152. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  1153. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  1154. if (rc == SQL_INVALID_HANDLE) {
  1155. php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  1156. efree(result);
  1157. RETURN_FALSE;
  1158. }
  1159. if (rc == SQL_ERROR) {
  1160. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  1161. efree(result);
  1162. RETURN_FALSE;
  1163. }
  1164. #ifdef HAVE_SQL_EXTENDED_FETCH
  1165. /* Solid doesn't have ExtendedFetch, if DriverManager is used, get Info,
  1166. whether Driver supports ExtendedFetch */
  1167. rc = SQLGetInfo(conn->hdbc, SQL_FETCH_DIRECTION, (void *) &scrollopts, sizeof(scrollopts), NULL);
  1168. if (rc == SQL_SUCCESS) {
  1169. if ((result->fetch_abs = (scrollopts & SQL_FD_FETCH_ABSOLUTE))) {
  1170. /* Try to set CURSOR_TYPE to dynamic. Driver will replace this with other
  1171. type if not possible.
  1172. */
  1173. SQLSetStmtOption(result->stmt, SQL_CURSOR_TYPE, ODBCG(default_cursortype));
  1174. }
  1175. } else {
  1176. result->fetch_abs = 0;
  1177. }
  1178. #endif
  1179. rc = SQLExecDirect(result->stmt, query, SQL_NTS);
  1180. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO && rc != SQL_NO_DATA_FOUND) {
  1181. /* XXX FIXME we should really check out SQLSTATE with SQLError
  1182. * in case rc is SQL_SUCCESS_WITH_INFO here.
  1183. */
  1184. odbc_sql_error(conn, result->stmt, "SQLExecDirect");
  1185. SQLFreeStmt(result->stmt, SQL_DROP);
  1186. efree(result);
  1187. RETURN_FALSE;
  1188. }
  1189. SQLNumResultCols(result->stmt, &(result->numcols));
  1190. /* For insert, update etc. cols == 0 */
  1191. if (result->numcols > 0) {
  1192. if (!odbc_bindcols(result)) {
  1193. efree(result);
  1194. RETURN_FALSE;
  1195. }
  1196. } else {
  1197. result->values = NULL;
  1198. }
  1199. Z_ADDREF_P(pv_conn);
  1200. result->conn_ptr = conn;
  1201. result->fetched = 0;
  1202. RETURN_RES(zend_register_resource(result, le_result));
  1203. }
  1204. /* }}} */
  1205. #ifdef PHP_ODBC_HAVE_FETCH_HASH
  1206. #define ODBC_NUM 1
  1207. #define ODBC_OBJECT 2
  1208. /* {{{ php_odbc_fetch_hash */
  1209. static void php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int result_type)
  1210. {
  1211. int i;
  1212. odbc_result *result;
  1213. RETCODE rc;
  1214. SQLSMALLINT sql_c_type;
  1215. char *buf = NULL;
  1216. #ifdef HAVE_SQL_EXTENDED_FETCH
  1217. SQLULEN crow;
  1218. SQLUSMALLINT RowStatus[1];
  1219. SQLLEN rownum;
  1220. zval *pv_res, tmp;
  1221. zend_long pv_row = -1;
  1222. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pv_res, &pv_row) == FAILURE) {
  1223. RETURN_THROWS();
  1224. }
  1225. rownum = pv_row;
  1226. #else
  1227. zval *pv_res, tmp;
  1228. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
  1229. RETURN_THROWS();
  1230. }
  1231. #endif
  1232. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  1233. RETURN_THROWS();
  1234. }
  1235. if (result->numcols == 0) {
  1236. php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
  1237. RETURN_FALSE;
  1238. }
  1239. #ifdef HAVE_SQL_EXTENDED_FETCH
  1240. if (result->fetch_abs) {
  1241. if (rownum > 0) {
  1242. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,rownum,&crow,RowStatus);
  1243. } else {
  1244. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
  1245. }
  1246. } else
  1247. #endif
  1248. rc = SQLFetch(result->stmt);
  1249. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1250. RETURN_FALSE;
  1251. }
  1252. array_init(return_value);
  1253. #ifdef HAVE_SQL_EXTENDED_FETCH
  1254. if (rownum > 0 && result->fetch_abs)
  1255. result->fetched = rownum;
  1256. else
  1257. #endif
  1258. result->fetched++;
  1259. for(i = 0; i < result->numcols; i++) {
  1260. sql_c_type = SQL_C_CHAR;
  1261. switch(result->values[i].coltype) {
  1262. case SQL_BINARY:
  1263. case SQL_VARBINARY:
  1264. case SQL_LONGVARBINARY:
  1265. if (result->binmode <= 0) {
  1266. ZVAL_EMPTY_STRING(&tmp);
  1267. break;
  1268. }
  1269. if (result->binmode == 1) {
  1270. sql_c_type = SQL_C_BINARY;
  1271. }
  1272. case SQL_LONGVARCHAR:
  1273. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  1274. case SQL_WLONGVARCHAR:
  1275. #endif
  1276. if (IS_SQL_LONG(result->values[i].coltype) && result->longreadlen <= 0) {
  1277. ZVAL_EMPTY_STRING(&tmp);
  1278. break;
  1279. }
  1280. if (buf == NULL) {
  1281. buf = emalloc(result->longreadlen + 1);
  1282. }
  1283. rc = SQLGetData(result->stmt, (SQLUSMALLINT)(i + 1), sql_c_type, buf, result->longreadlen + 1, &result->values[i].vallen);
  1284. if (rc == SQL_ERROR) {
  1285. odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
  1286. efree(buf);
  1287. RETURN_FALSE;
  1288. }
  1289. if (rc == SQL_SUCCESS_WITH_INFO) {
  1290. ZVAL_STRINGL(&tmp, buf, result->longreadlen);
  1291. } else if (result->values[i].vallen == SQL_NULL_DATA) {
  1292. ZVAL_NULL(&tmp);
  1293. break;
  1294. } else {
  1295. ZVAL_STRINGL(&tmp, buf, result->values[i].vallen);
  1296. }
  1297. break;
  1298. default:
  1299. if (result->values[i].vallen == SQL_NULL_DATA) {
  1300. ZVAL_NULL(&tmp);
  1301. break;
  1302. }
  1303. ZVAL_STRINGL(&tmp, result->values[i].value, result->values[i].vallen);
  1304. break;
  1305. }
  1306. if (result_type & ODBC_NUM) {
  1307. zend_hash_index_update(Z_ARRVAL_P(return_value), i, &tmp);
  1308. } else {
  1309. if (!*(result->values[i].name) && Z_TYPE(tmp) == IS_STRING) {
  1310. zend_hash_update(Z_ARRVAL_P(return_value), Z_STR(tmp), &tmp);
  1311. } else {
  1312. zend_hash_str_update(Z_ARRVAL_P(return_value), result->values[i].name, strlen(result->values[i].name), &tmp);
  1313. }
  1314. }
  1315. }
  1316. if (buf) {
  1317. efree(buf);
  1318. }
  1319. }
  1320. /* }}} */
  1321. /* {{{ proto object odbc_fetch_object(resource result [, int rownumber])
  1322. Fetch a result row as an object */
  1323. PHP_FUNCTION(odbc_fetch_object)
  1324. {
  1325. php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, ODBC_OBJECT);
  1326. if (Z_TYPE_P(return_value) == IS_ARRAY) {
  1327. object_and_properties_init(return_value, ZEND_STANDARD_CLASS_DEF_PTR, Z_ARRVAL_P(return_value));
  1328. }
  1329. }
  1330. /* }}} */
  1331. /* {{{ proto array odbc_fetch_array(resource result [, int rownumber])
  1332. Fetch a result row as an associative array */
  1333. PHP_FUNCTION(odbc_fetch_array)
  1334. {
  1335. php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, ODBC_OBJECT);
  1336. }
  1337. /* }}} */
  1338. #endif
  1339. /* {{{ proto int odbc_fetch_into(resource result_id, array &result_array [, int rownumber])
  1340. Fetch one result row into an array */
  1341. PHP_FUNCTION(odbc_fetch_into)
  1342. {
  1343. int i;
  1344. odbc_result *result;
  1345. RETCODE rc;
  1346. SQLSMALLINT sql_c_type;
  1347. char *buf = NULL;
  1348. zval *pv_res, *pv_res_arr, tmp;
  1349. #ifdef HAVE_SQL_EXTENDED_FETCH
  1350. zend_long pv_row = 0;
  1351. SQLULEN crow;
  1352. SQLUSMALLINT RowStatus[1];
  1353. SQLLEN rownum = -1;
  1354. #endif /* HAVE_SQL_EXTENDED_FETCH */
  1355. #ifdef HAVE_SQL_EXTENDED_FETCH
  1356. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz|l", &pv_res, &pv_res_arr, &pv_row) == FAILURE) {
  1357. RETURN_THROWS();
  1358. }
  1359. rownum = pv_row;
  1360. #else
  1361. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pv_res, &pv_res_arr) == FAILURE) {
  1362. RETURN_THROWS();
  1363. }
  1364. #endif /* HAVE_SQL_EXTENDED_FETCH */
  1365. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  1366. RETURN_THROWS();
  1367. }
  1368. if (result->numcols == 0) {
  1369. php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
  1370. RETURN_FALSE;
  1371. }
  1372. pv_res_arr = zend_try_array_init(pv_res_arr);
  1373. if (!pv_res_arr) {
  1374. RETURN_THROWS();
  1375. }
  1376. #ifdef HAVE_SQL_EXTENDED_FETCH
  1377. if (result->fetch_abs) {
  1378. if (rownum > 0) {
  1379. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,rownum,&crow,RowStatus);
  1380. } else {
  1381. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
  1382. }
  1383. } else
  1384. #endif
  1385. rc = SQLFetch(result->stmt);
  1386. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1387. RETURN_FALSE;
  1388. }
  1389. #ifdef HAVE_SQL_EXTENDED_FETCH
  1390. if (rownum > 0 && result->fetch_abs)
  1391. result->fetched = rownum;
  1392. else
  1393. #endif
  1394. result->fetched++;
  1395. for(i = 0; i < result->numcols; i++) {
  1396. sql_c_type = SQL_C_CHAR;
  1397. switch(result->values[i].coltype) {
  1398. case SQL_BINARY:
  1399. case SQL_VARBINARY:
  1400. case SQL_LONGVARBINARY:
  1401. if (result->binmode <= 0) {
  1402. ZVAL_EMPTY_STRING(&tmp);
  1403. break;
  1404. }
  1405. if (result->binmode == 1) sql_c_type = SQL_C_BINARY;
  1406. case SQL_LONGVARCHAR:
  1407. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  1408. case SQL_WLONGVARCHAR:
  1409. #endif
  1410. if (IS_SQL_LONG(result->values[i].coltype) && result->longreadlen <= 0) {
  1411. ZVAL_EMPTY_STRING(&tmp);
  1412. break;
  1413. }
  1414. if (buf == NULL) {
  1415. buf = emalloc(result->longreadlen + 1);
  1416. }
  1417. rc = SQLGetData(result->stmt, (SQLUSMALLINT)(i + 1),sql_c_type, buf, result->longreadlen + 1, &result->values[i].vallen);
  1418. if (rc == SQL_ERROR) {
  1419. odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
  1420. efree(buf);
  1421. RETURN_FALSE;
  1422. }
  1423. if (rc == SQL_SUCCESS_WITH_INFO) {
  1424. ZVAL_STRINGL(&tmp, buf, result->longreadlen);
  1425. } else if (result->values[i].vallen == SQL_NULL_DATA) {
  1426. ZVAL_NULL(&tmp);
  1427. break;
  1428. } else {
  1429. ZVAL_STRINGL(&tmp, buf, result->values[i].vallen);
  1430. }
  1431. break;
  1432. default:
  1433. if (result->values[i].vallen == SQL_NULL_DATA) {
  1434. ZVAL_NULL(&tmp);
  1435. break;
  1436. }
  1437. ZVAL_STRINGL(&tmp, result->values[i].value, result->values[i].vallen);
  1438. break;
  1439. }
  1440. zend_hash_index_update(Z_ARRVAL_P(pv_res_arr), i, &tmp);
  1441. }
  1442. if (buf) efree(buf);
  1443. RETURN_LONG(result->numcols);
  1444. }
  1445. /* }}} */
  1446. /* {{{ proto bool solid_fetch_prev(resource result_id)
  1447. */
  1448. #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30) || defined(HAVE_SOLID_35)
  1449. PHP_FUNCTION(solid_fetch_prev)
  1450. {
  1451. odbc_result *result;
  1452. RETCODE rc;
  1453. zval *pv_res;
  1454. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
  1455. RETURN_THROWS();
  1456. }
  1457. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  1458. RETURN_THROWS();
  1459. }
  1460. if (result->numcols == 0) {
  1461. php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
  1462. RETURN_FALSE;
  1463. }
  1464. rc = SQLFetchPrev(result->stmt);
  1465. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1466. RETURN_FALSE;
  1467. }
  1468. if (result->fetched > 1) {
  1469. result->fetched--;
  1470. }
  1471. RETURN_TRUE;
  1472. }
  1473. #endif
  1474. /* }}} */
  1475. /* {{{ proto bool odbc_fetch_row(resource result_id [, int row_number])
  1476. Fetch a row */
  1477. PHP_FUNCTION(odbc_fetch_row)
  1478. {
  1479. SQLLEN rownum;
  1480. odbc_result *result;
  1481. RETCODE rc;
  1482. zval *pv_res;
  1483. zend_long pv_row = 1;
  1484. #ifdef HAVE_SQL_EXTENDED_FETCH
  1485. SQLULEN crow;
  1486. SQLUSMALLINT RowStatus[1];
  1487. #endif
  1488. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pv_res, &pv_row) == FAILURE) {
  1489. RETURN_THROWS();
  1490. }
  1491. rownum = pv_row;
  1492. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  1493. RETURN_THROWS();
  1494. }
  1495. if (result->numcols == 0) {
  1496. php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
  1497. RETURN_FALSE;
  1498. }
  1499. #ifdef HAVE_SQL_EXTENDED_FETCH
  1500. if (result->fetch_abs) {
  1501. if (ZEND_NUM_ARGS() > 1) {
  1502. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,rownum,&crow,RowStatus);
  1503. } else {
  1504. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
  1505. }
  1506. } else
  1507. #endif
  1508. rc = SQLFetch(result->stmt);
  1509. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1510. RETURN_FALSE;
  1511. }
  1512. if (ZEND_NUM_ARGS() > 1) {
  1513. result->fetched = rownum;
  1514. } else {
  1515. result->fetched++;
  1516. }
  1517. RETURN_TRUE;
  1518. }
  1519. /* }}} */
  1520. /* {{{ proto mixed odbc_result(resource result_id, mixed field)
  1521. Get result data */
  1522. PHP_FUNCTION(odbc_result)
  1523. {
  1524. char *field;
  1525. zend_string *field_str;
  1526. int field_ind;
  1527. SQLSMALLINT sql_c_type = SQL_C_CHAR;
  1528. odbc_result *result;
  1529. int i = 0;
  1530. RETCODE rc;
  1531. SQLLEN fieldsize;
  1532. zval *pv_res, *pv_field;
  1533. #ifdef HAVE_SQL_EXTENDED_FETCH
  1534. SQLULEN crow;
  1535. SQLUSMALLINT RowStatus[1];
  1536. #endif
  1537. field_ind = -1;
  1538. field = NULL;
  1539. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pv_res, &pv_field) == FAILURE) {
  1540. RETURN_THROWS();
  1541. }
  1542. if (Z_TYPE_P(pv_field) == IS_STRING) {
  1543. field = Z_STRVAL_P(pv_field);
  1544. } else {
  1545. convert_to_long_ex(pv_field);
  1546. field_ind = Z_LVAL_P(pv_field) - 1;
  1547. }
  1548. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  1549. RETURN_THROWS();
  1550. }
  1551. if ((result->numcols == 0)) {
  1552. php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
  1553. RETURN_FALSE;
  1554. }
  1555. /* get field index if the field parameter was a string */
  1556. if (field != NULL) {
  1557. if (result->values == NULL) {
  1558. php_error_docref(NULL, E_WARNING, "Result set contains no data");
  1559. RETURN_FALSE;
  1560. }
  1561. for(i = 0; i < result->numcols; i++) {
  1562. if (!strcasecmp(result->values[i].name, field)) {
  1563. field_ind = i;
  1564. break;
  1565. }
  1566. }
  1567. if (field_ind < 0) {
  1568. php_error_docref(NULL, E_WARNING, "Field %s not found", field);
  1569. RETURN_FALSE;
  1570. }
  1571. } else {
  1572. /* check for limits of field_ind if the field parameter was an int */
  1573. if (field_ind >= result->numcols || field_ind < 0) {
  1574. php_error_docref(NULL, E_WARNING, "Field index is larger than the number of fields");
  1575. RETURN_FALSE;
  1576. }
  1577. }
  1578. if (result->fetched == 0) {
  1579. /* User forgot to call odbc_fetch_row(), or wants to reload the results, do it now */
  1580. #ifdef HAVE_SQL_EXTENDED_FETCH
  1581. if (result->fetch_abs)
  1582. rc = SQLExtendedFetch(result->stmt, SQL_FETCH_NEXT, 1, &crow,RowStatus);
  1583. else
  1584. #endif
  1585. rc = SQLFetch(result->stmt);
  1586. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1587. RETURN_FALSE;
  1588. }
  1589. result->fetched++;
  1590. }
  1591. switch(result->values[field_ind].coltype) {
  1592. case SQL_BINARY:
  1593. case SQL_VARBINARY:
  1594. case SQL_LONGVARBINARY:
  1595. if (result->binmode <= 1) {
  1596. sql_c_type = SQL_C_BINARY;
  1597. }
  1598. if (result->binmode <= 0) {
  1599. break;
  1600. }
  1601. case SQL_LONGVARCHAR:
  1602. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  1603. case SQL_WLONGVARCHAR:
  1604. #endif
  1605. if (IS_SQL_LONG(result->values[field_ind].coltype)) {
  1606. if (result->longreadlen <= 0) {
  1607. break;
  1608. } else {
  1609. fieldsize = result->longreadlen;
  1610. }
  1611. } else {
  1612. PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(field_ind + 1),
  1613. (SQLUSMALLINT)((sql_c_type == SQL_C_BINARY) ? SQL_COLUMN_LENGTH :
  1614. SQL_COLUMN_DISPLAY_SIZE),
  1615. NULL, 0, NULL, &fieldsize);
  1616. }
  1617. /* For char data, the length of the returned string will be longreadlen - 1 */
  1618. fieldsize = (result->longreadlen <= 0) ? 4096 : result->longreadlen;
  1619. field_str = zend_string_alloc(fieldsize, 0);
  1620. /* SQLGetData will truncate CHAR data to fieldsize - 1 bytes and append \0.
  1621. * For binary data it is truncated to fieldsize bytes.
  1622. */
  1623. rc = SQLGetData(result->stmt, (SQLUSMALLINT)(field_ind + 1), sql_c_type,
  1624. ZSTR_VAL(field_str), fieldsize, &result->values[field_ind].vallen);
  1625. if (rc == SQL_ERROR) {
  1626. odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
  1627. zend_string_efree(field_str);
  1628. RETURN_FALSE;
  1629. }
  1630. if (result->values[field_ind].vallen == SQL_NULL_DATA) {
  1631. zend_string_efree(field_str);
  1632. RETURN_NULL();
  1633. } else if (rc == SQL_NO_DATA_FOUND) {
  1634. zend_string_efree(field_str);
  1635. RETURN_FALSE;
  1636. }
  1637. /* Reduce fieldlen by 1 if we have char data. One day we might
  1638. have binary strings... */
  1639. if ((result->values[field_ind].coltype == SQL_LONGVARCHAR)
  1640. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  1641. || (result->values[field_ind].coltype == SQL_WLONGVARCHAR)
  1642. #endif
  1643. ) {
  1644. fieldsize -= 1;
  1645. }
  1646. /* Don't duplicate result, saves one emalloc.
  1647. For SQL_SUCCESS, the length is in vallen.
  1648. */
  1649. if (rc != SQL_SUCCESS_WITH_INFO) {
  1650. field_str = zend_string_truncate(field_str, result->values[field_ind].vallen, 0);
  1651. }
  1652. RETURN_NEW_STR(field_str);
  1653. break;
  1654. default:
  1655. if (result->values[field_ind].vallen == SQL_NULL_DATA) {
  1656. RETURN_NULL();
  1657. } else {
  1658. RETURN_STRINGL(result->values[field_ind].value, result->values[field_ind].vallen);
  1659. }
  1660. break;
  1661. }
  1662. /* If we come here, output unbound LONG and/or BINARY column data to the client */
  1663. /* We emalloc 1 byte more for SQL_C_CHAR (trailing \0) */
  1664. fieldsize = (sql_c_type == SQL_C_CHAR) ? 4096 : 4095;
  1665. field = emalloc(fieldsize);
  1666. /* Call SQLGetData() until SQL_SUCCESS is returned */
  1667. while(1) {
  1668. rc = SQLGetData(result->stmt, (SQLUSMALLINT)(field_ind + 1),sql_c_type, field, fieldsize, &result->values[field_ind].vallen);
  1669. if (rc == SQL_ERROR) {
  1670. odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
  1671. efree(field);
  1672. RETURN_FALSE;
  1673. }
  1674. if (result->values[field_ind].vallen == SQL_NULL_DATA) {
  1675. efree(field);
  1676. RETURN_NULL();
  1677. }
  1678. /* chop the trailing \0 by outputting only 4095 bytes */
  1679. PHPWRITE(field,(rc == SQL_SUCCESS_WITH_INFO) ? 4095 : result->values[field_ind].vallen);
  1680. if (rc == SQL_SUCCESS) { /* no more data avail */
  1681. efree(field);
  1682. RETURN_TRUE;
  1683. }
  1684. }
  1685. RETURN_TRUE;
  1686. }
  1687. /* }}} */
  1688. /* {{{ proto int odbc_result_all(resource result_id [, string format])
  1689. Print result as HTML table */
  1690. PHP_FUNCTION(odbc_result_all)
  1691. {
  1692. char *buf = NULL;
  1693. odbc_result *result;
  1694. RETCODE rc;
  1695. zval *pv_res;
  1696. char *pv_format = NULL;
  1697. size_t i, pv_format_len = 0;
  1698. SQLSMALLINT sql_c_type;
  1699. #ifdef HAVE_SQL_EXTENDED_FETCH
  1700. SQLULEN crow;
  1701. SQLUSMALLINT RowStatus[1];
  1702. #endif
  1703. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|s", &pv_res, &pv_format, &pv_format_len) == FAILURE) {
  1704. RETURN_THROWS();
  1705. }
  1706. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  1707. RETURN_THROWS();
  1708. }
  1709. if (result->numcols == 0) {
  1710. php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
  1711. RETURN_FALSE;
  1712. }
  1713. #ifdef HAVE_SQL_EXTENDED_FETCH
  1714. if (result->fetch_abs)
  1715. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
  1716. else
  1717. #endif
  1718. rc = SQLFetch(result->stmt);
  1719. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1720. php_printf("<h2>No rows found</h2>\n");
  1721. RETURN_LONG(0);
  1722. }
  1723. /* Start table tag */
  1724. if (ZEND_NUM_ARGS() == 1) {
  1725. php_printf("<table><tr>");
  1726. } else {
  1727. php_printf("<table %s ><tr>", pv_format);
  1728. }
  1729. for (i = 0; i < result->numcols; i++) {
  1730. php_printf("<th>%s</th>", result->values[i].name);
  1731. }
  1732. php_printf("</tr>\n");
  1733. while(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) {
  1734. result->fetched++;
  1735. php_printf("<tr>");
  1736. for(i = 0; i < result->numcols; i++) {
  1737. sql_c_type = SQL_C_CHAR;
  1738. switch(result->values[i].coltype) {
  1739. case SQL_BINARY:
  1740. case SQL_VARBINARY:
  1741. case SQL_LONGVARBINARY:
  1742. if (result->binmode <= 0) {
  1743. php_printf("<td>Not printable</td>");
  1744. break;
  1745. }
  1746. if (result->binmode <= 1) sql_c_type = SQL_C_BINARY;
  1747. case SQL_LONGVARCHAR:
  1748. #if defined(ODBCVER) && (ODBCVER >= 0x0300)
  1749. case SQL_WLONGVARCHAR:
  1750. #endif
  1751. if (IS_SQL_LONG(result->values[i].coltype) &&
  1752. result->longreadlen <= 0) {
  1753. php_printf("<td>Not printable</td>");
  1754. break;
  1755. }
  1756. if (buf == NULL) {
  1757. buf = emalloc(result->longreadlen);
  1758. }
  1759. rc = SQLGetData(result->stmt, (SQLUSMALLINT)(i + 1),sql_c_type, buf, result->longreadlen, &result->values[i].vallen);
  1760. php_printf("<td>");
  1761. if (rc == SQL_ERROR) {
  1762. odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
  1763. php_printf("</td></tr></table>");
  1764. efree(buf);
  1765. RETURN_FALSE;
  1766. }
  1767. if (rc == SQL_SUCCESS_WITH_INFO) {
  1768. PHPWRITE(buf, result->longreadlen);
  1769. } else if (result->values[i].vallen == SQL_NULL_DATA) {
  1770. php_printf("<td>NULL</td>");
  1771. break;
  1772. } else {
  1773. PHPWRITE(buf, result->values[i].vallen);
  1774. }
  1775. php_printf("</td>");
  1776. break;
  1777. default:
  1778. if (result->values[i].vallen == SQL_NULL_DATA) {
  1779. php_printf("<td>NULL</td>");
  1780. } else {
  1781. php_printf("<td>%s</td>", result->values[i].value);
  1782. }
  1783. break;
  1784. }
  1785. }
  1786. php_printf("</tr>\n");
  1787. #ifdef HAVE_SQL_EXTENDED_FETCH
  1788. if (result->fetch_abs)
  1789. rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
  1790. else
  1791. #endif
  1792. rc = SQLFetch(result->stmt);
  1793. }
  1794. php_printf("</table>\n");
  1795. if (buf) efree(buf);
  1796. RETURN_LONG(result->fetched);
  1797. }
  1798. /* }}} */
  1799. /* {{{ proto bool odbc_free_result(resource result_id)
  1800. Free resources associated with a result */
  1801. PHP_FUNCTION(odbc_free_result)
  1802. {
  1803. zval *pv_res;
  1804. odbc_result *result;
  1805. int i;
  1806. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
  1807. RETURN_THROWS();
  1808. }
  1809. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  1810. RETURN_THROWS();
  1811. }
  1812. if (result->values) {
  1813. for (i = 0; i < result->numcols; i++) {
  1814. if (result->values[i].value) {
  1815. efree(result->values[i].value);
  1816. }
  1817. }
  1818. efree(result->values);
  1819. result->values = NULL;
  1820. }
  1821. zend_list_close(Z_RES_P(pv_res));
  1822. RETURN_TRUE;
  1823. }
  1824. /* }}} */
  1825. /* {{{ proto resource odbc_connect(string DSN, string user, string password [, int cursor_option])
  1826. Connect to a datasource */
  1827. PHP_FUNCTION(odbc_connect)
  1828. {
  1829. odbc_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  1830. }
  1831. /* }}} */
  1832. /* {{{ proto resource odbc_pconnect(string DSN, string user, string password [, int cursor_option])
  1833. Establish a persistent connection to a datasource */
  1834. PHP_FUNCTION(odbc_pconnect)
  1835. {
  1836. odbc_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  1837. }
  1838. /* }}} */
  1839. /* {{{ odbc_sqlconnect */
  1840. int odbc_sqlconnect(odbc_connection **conn, char *db, char *uid, char *pwd, int cur_opt, int persistent)
  1841. {
  1842. RETCODE rc;
  1843. *conn = (odbc_connection *)pemalloc(sizeof(odbc_connection), persistent);
  1844. memset(*conn, 0, sizeof(odbc_connection));
  1845. (*conn)->persistent = persistent;
  1846. SQLAllocEnv(&((*conn)->henv));
  1847. SQLAllocConnect((*conn)->henv, &((*conn)->hdbc));
  1848. #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30)
  1849. SQLSetConnectOption((*conn)->hdbc, SQL_TRANSLATE_OPTION,
  1850. SQL_SOLID_XLATOPT_NOCNV);
  1851. #endif
  1852. #ifdef HAVE_OPENLINK
  1853. {
  1854. char dsnbuf[1024];
  1855. short dsnbuflen;
  1856. rc = SQLDriverConnect((*conn)->hdbc, NULL, db, SQL_NTS, dsnbuf, sizeof(dsnbuf) - 1, &dsnbuflen, SQL_DRIVER_NOPROMPT);
  1857. }
  1858. #else
  1859. if (cur_opt != SQL_CUR_DEFAULT) {
  1860. rc = SQLSetConnectOption((*conn)->hdbc, SQL_ODBC_CURSORS, cur_opt);
  1861. if (rc != SQL_SUCCESS) { /* && rc != SQL_SUCCESS_WITH_INFO ? */
  1862. odbc_sql_error(*conn, SQL_NULL_HSTMT, "SQLSetConnectOption");
  1863. SQLFreeConnect((*conn)->hdbc);
  1864. pefree(*conn, persistent);
  1865. return FALSE;
  1866. }
  1867. }
  1868. /* Possible fix for bug #10250
  1869. * Needs testing on UnixODBC < 2.0.5 though. */
  1870. #if defined(HAVE_EMPRESS) || defined(HAVE_UNIXODBC) || defined(PHP_WIN32) || defined (HAVE_IODBC)
  1871. /* * Uncomment the line above, and comment line below to fully test
  1872. * #ifdef HAVE_EMPRESS */
  1873. {
  1874. int direct = 0;
  1875. char dsnbuf[1024];
  1876. short dsnbuflen;
  1877. char *ldb = 0;
  1878. int ldb_len = 0;
  1879. if (strstr((char*)db, ";")) {
  1880. direct = 1;
  1881. if (uid && !strstr ((char*)db, "uid") && !strstr((char*)db, "UID")) {
  1882. spprintf(&ldb, 0, "%s;UID=%s;PWD=%s", db, uid, pwd);
  1883. } else {
  1884. ldb_len = strlen(db)+1;
  1885. ldb = (char*) emalloc(ldb_len);
  1886. memcpy(ldb, db, ldb_len);
  1887. }
  1888. }
  1889. if (direct) {
  1890. rc = SQLDriverConnect((*conn)->hdbc, NULL, ldb, strlen(ldb), dsnbuf, sizeof(dsnbuf) - 1, &dsnbuflen, SQL_DRIVER_NOPROMPT);
  1891. } else {
  1892. rc = SQLConnect((*conn)->hdbc, db, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
  1893. }
  1894. if (ldb) {
  1895. efree(ldb);
  1896. }
  1897. }
  1898. #else
  1899. rc = SQLConnect((*conn)->hdbc, db, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
  1900. #endif
  1901. #endif
  1902. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  1903. odbc_sql_error(*conn, SQL_NULL_HSTMT, "SQLConnect");
  1904. SQLFreeConnect((*conn)->hdbc);
  1905. pefree((*conn), persistent);
  1906. return FALSE;
  1907. }
  1908. /* (*conn)->open = 1;*/
  1909. return TRUE;
  1910. }
  1911. /* }}} */
  1912. /* Persistent connections: two list-types le_pconn, le_conn and a plist
  1913. * where hashed connection info is stored together with index pointer to
  1914. * the actual link of type le_pconn in the list. Only persistent
  1915. * connections get hashed up. Normal connections use existing pconnections.
  1916. * Maybe this has to change with regard to transactions on pconnections?
  1917. * Possibly set autocommit to on on request shutdown.
  1918. *
  1919. * We do have to hash non-persistent connections, and reuse connections.
  1920. * In the case where two connects were being made, without closing the first
  1921. * connect, access violations were occurring. This is because some of the
  1922. * "globals" in this module should actually be per-connection variables. I
  1923. * simply fixed things to get them working for now. Shane
  1924. */
  1925. /* {{{ odbc_do_connect */
  1926. void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
  1927. {
  1928. char *db, *uid, *pwd;
  1929. size_t db_len, uid_len, pwd_len;
  1930. zend_long pv_opt = SQL_CUR_DEFAULT;
  1931. odbc_connection *db_conn;
  1932. char *hashed_details;
  1933. int hashed_len, cur_opt;
  1934. /* Now an optional 4th parameter specifying the cursor type
  1935. * defaulting to the cursors default
  1936. */
  1937. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|l", &db, &db_len, &uid, &uid_len, &pwd, &pwd_len, &pv_opt) == FAILURE) {
  1938. RETURN_THROWS();
  1939. }
  1940. cur_opt = pv_opt;
  1941. if (ZEND_NUM_ARGS() > 3) {
  1942. /* Confirm the cur_opt range */
  1943. if (! (cur_opt == SQL_CUR_USE_IF_NEEDED ||
  1944. cur_opt == SQL_CUR_USE_ODBC ||
  1945. cur_opt == SQL_CUR_USE_DRIVER ||
  1946. cur_opt == SQL_CUR_DEFAULT) ) {
  1947. php_error_docref(NULL, E_WARNING, "Invalid Cursor type (%d)", cur_opt);
  1948. RETURN_FALSE;
  1949. }
  1950. }
  1951. if (ODBCG(allow_persistent) <= 0) {
  1952. persistent = 0;
  1953. }
  1954. hashed_len = spprintf(&hashed_details, 0, "%s_%s_%s_%s_%d", ODBC_TYPE, db, uid, pwd, cur_opt);
  1955. /* FIXME the idea of checking to see if our connection is already persistent
  1956. is good, but it adds a lot of overhead to non-persistent connections. We
  1957. should look and see if we can fix that somehow */
  1958. /* try to find if we already have this link in our persistent list,
  1959. * no matter if it is to be persistent or not
  1960. */
  1961. try_and_get_another_connection:
  1962. if (persistent) {
  1963. zend_resource *le;
  1964. /* the link is not in the persistent list */
  1965. if ((le = zend_hash_str_find_ptr(&EG(persistent_list), hashed_details, hashed_len)) == NULL) {
  1966. if (ODBCG(max_links) != -1 && ODBCG(num_links) >= ODBCG(max_links)) {
  1967. php_error_docref(NULL, E_WARNING, "Too many open links (%ld)", ODBCG(num_links));
  1968. efree(hashed_details);
  1969. RETURN_FALSE;
  1970. }
  1971. if (ODBCG(max_persistent) != -1 && ODBCG(num_persistent) >= ODBCG(max_persistent)) {
  1972. php_error_docref(NULL, E_WARNING,"Too many open persistent links (%ld)", ODBCG(num_persistent));
  1973. efree(hashed_details);
  1974. RETURN_FALSE;
  1975. }
  1976. if (!odbc_sqlconnect(&db_conn, db, uid, pwd, cur_opt, 1)) {
  1977. efree(hashed_details);
  1978. RETURN_FALSE;
  1979. }
  1980. if (zend_register_persistent_resource(hashed_details, hashed_len, db_conn, le_pconn) == NULL) {
  1981. free(db_conn);
  1982. efree(hashed_details);
  1983. RETURN_FALSE;
  1984. }
  1985. ODBCG(num_persistent)++;
  1986. ODBCG(num_links)++;
  1987. db_conn->res = zend_register_resource(db_conn, le_pconn);
  1988. RETVAL_RES(db_conn->res);
  1989. } else { /* found connection */
  1990. if (le->type != le_pconn) {
  1991. RETURN_FALSE;
  1992. }
  1993. /*
  1994. * check to see if the connection is still valid
  1995. */
  1996. db_conn = (odbc_connection *)le->ptr;
  1997. /*
  1998. * check to see if the connection is still in place (lurcher)
  1999. */
  2000. if(ODBCG(check_persistent)){
  2001. RETCODE ret;
  2002. UCHAR d_name[32];
  2003. SQLSMALLINT len;
  2004. ret = SQLGetInfo(db_conn->hdbc,
  2005. SQL_DATA_SOURCE_READ_ONLY,
  2006. d_name, sizeof(d_name), &len);
  2007. if(ret != SQL_SUCCESS || len == 0) {
  2008. zend_hash_str_del(&EG(persistent_list), hashed_details, hashed_len);
  2009. /* Commented out to fix a possible double closure error
  2010. * when working with persistent connections as submitted by
  2011. * bug #15758
  2012. *
  2013. * safe_odbc_disconnect(db_conn->hdbc);
  2014. * SQLFreeConnect(db_conn->hdbc);
  2015. */
  2016. goto try_and_get_another_connection;
  2017. }
  2018. }
  2019. }
  2020. db_conn->res = zend_register_resource(db_conn, le_pconn);
  2021. RETVAL_RES(db_conn->res);
  2022. } else { /* non persistent */
  2023. zend_resource *index_ptr, new_index_ptr;
  2024. if ((index_ptr = zend_hash_str_find_ptr(&EG(regular_list), hashed_details, hashed_len)) != NULL) {
  2025. zend_ulong conn_id;
  2026. zend_resource *p;
  2027. if (index_ptr->type != le_index_ptr) {
  2028. RETURN_FALSE;
  2029. }
  2030. conn_id = (zend_ulong)index_ptr->ptr;
  2031. p = zend_hash_index_find_ptr(&EG(regular_list), conn_id); /* check if the connection is still there */
  2032. if (p && p->ptr && (p->type == le_conn || p->type == le_pconn)) {
  2033. GC_ADDREF(p);
  2034. RETVAL_RES(p);
  2035. efree(hashed_details);
  2036. return;
  2037. } else {
  2038. zend_hash_str_del(&EG(regular_list), hashed_details, hashed_len);
  2039. }
  2040. }
  2041. if (ODBCG(max_links) != -1 && ODBCG(num_links) >= ODBCG(max_links)) {
  2042. php_error_docref(NULL, E_WARNING,"Too many open connections (%ld)",ODBCG(num_links));
  2043. efree(hashed_details);
  2044. RETURN_FALSE;
  2045. }
  2046. if (!odbc_sqlconnect(&db_conn, db, uid, pwd, cur_opt, 0)) {
  2047. efree(hashed_details);
  2048. RETURN_FALSE;
  2049. }
  2050. db_conn->res = zend_register_resource(db_conn, le_conn);
  2051. RETVAL_RES(db_conn->res);
  2052. new_index_ptr.ptr = (void *)(zend_uintptr_t)Z_RES_HANDLE_P(return_value);
  2053. new_index_ptr.type = le_index_ptr;
  2054. zend_hash_str_update_mem(&EG(regular_list), hashed_details, hashed_len, (void *) &new_index_ptr,
  2055. sizeof(zend_resource));
  2056. ODBCG(num_links)++;
  2057. }
  2058. efree(hashed_details);
  2059. }
  2060. /* }}} */
  2061. /* {{{ proto void odbc_close(resource connection_id)
  2062. Close an ODBC connection */
  2063. PHP_FUNCTION(odbc_close)
  2064. {
  2065. zval *pv_conn;
  2066. zend_resource *p;
  2067. odbc_connection *conn;
  2068. odbc_result *res;
  2069. int is_pconn = 0;
  2070. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_conn) == FAILURE) {
  2071. RETURN_THROWS();
  2072. }
  2073. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  2074. RETURN_THROWS();
  2075. }
  2076. if (Z_RES_P(pv_conn)->type == le_pconn) {
  2077. is_pconn = 1;
  2078. }
  2079. ZEND_HASH_FOREACH_PTR(&EG(regular_list), p) {
  2080. if (p->ptr && (p->type == le_result)) {
  2081. res = (odbc_result *)p->ptr;
  2082. if (res->conn_ptr == conn) {
  2083. zend_list_close(p);
  2084. }
  2085. }
  2086. } ZEND_HASH_FOREACH_END();
  2087. zend_list_close(Z_RES_P(pv_conn));
  2088. if(is_pconn){
  2089. zend_hash_apply_with_argument(&EG(persistent_list), (apply_func_arg_t) _close_pconn_with_res, (void *) Z_RES_P(pv_conn));
  2090. }
  2091. }
  2092. /* }}} */
  2093. /* {{{ proto int odbc_num_rows(resource result_id)
  2094. Get number of rows in a result */
  2095. PHP_FUNCTION(odbc_num_rows)
  2096. {
  2097. odbc_result *result;
  2098. SQLLEN rows;
  2099. zval *pv_res;
  2100. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
  2101. RETURN_THROWS();
  2102. }
  2103. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  2104. RETURN_THROWS();
  2105. }
  2106. SQLRowCount(result->stmt, &rows);
  2107. RETURN_LONG(rows);
  2108. }
  2109. /* }}} */
  2110. #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30)
  2111. /* {{{ proto bool odbc_next_result(resource result_id)
  2112. Checks if multiple results are available */
  2113. PHP_FUNCTION(odbc_next_result)
  2114. {
  2115. odbc_result *result;
  2116. zval *pv_res;
  2117. int rc, i;
  2118. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
  2119. RETURN_THROWS();
  2120. }
  2121. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  2122. RETURN_THROWS();
  2123. }
  2124. if (result->values) {
  2125. for(i = 0; i < result->numcols; i++) {
  2126. if (result->values[i].value) {
  2127. efree(result->values[i].value);
  2128. }
  2129. }
  2130. efree(result->values);
  2131. result->values = NULL;
  2132. }
  2133. result->fetched = 0;
  2134. rc = SQLMoreResults(result->stmt);
  2135. if (rc == SQL_SUCCESS_WITH_INFO || rc == SQL_SUCCESS) {
  2136. rc = SQLFreeStmt(result->stmt, SQL_UNBIND);
  2137. SQLNumParams(result->stmt, &(result->numparams));
  2138. SQLNumResultCols(result->stmt, &(result->numcols));
  2139. if (result->numcols > 0) {
  2140. if (!odbc_bindcols(result)) {
  2141. efree(result);
  2142. RETVAL_FALSE;
  2143. }
  2144. } else {
  2145. result->values = NULL;
  2146. }
  2147. RETURN_TRUE;
  2148. } else if (rc == SQL_NO_DATA_FOUND) {
  2149. RETURN_FALSE;
  2150. } else {
  2151. odbc_sql_error(result->conn_ptr, result->stmt, "SQLMoreResults");
  2152. RETURN_FALSE;
  2153. }
  2154. }
  2155. /* }}} */
  2156. #endif
  2157. /* {{{ proto int odbc_num_fields(resource result_id)
  2158. Get number of columns in a result */
  2159. PHP_FUNCTION(odbc_num_fields)
  2160. {
  2161. odbc_result *result;
  2162. zval *pv_res;
  2163. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
  2164. RETURN_THROWS();
  2165. }
  2166. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  2167. RETURN_THROWS();
  2168. }
  2169. RETURN_LONG(result->numcols);
  2170. }
  2171. /* }}} */
  2172. /* {{{ proto string odbc_field_name(resource result_id, int field_number)
  2173. Get a column name */
  2174. PHP_FUNCTION(odbc_field_name)
  2175. {
  2176. odbc_result *result;
  2177. zval *pv_res;
  2178. zend_long pv_num;
  2179. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &pv_res, &pv_num) == FAILURE) {
  2180. RETURN_THROWS();
  2181. }
  2182. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  2183. RETURN_THROWS();
  2184. }
  2185. if (result->numcols == 0) {
  2186. php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
  2187. RETURN_FALSE;
  2188. }
  2189. if (pv_num > result->numcols) {
  2190. php_error_docref(NULL, E_WARNING, "Field index larger than number of fields");
  2191. RETURN_FALSE;
  2192. }
  2193. if (pv_num < 1) {
  2194. php_error_docref(NULL, E_WARNING, "Field numbering starts at 1");
  2195. RETURN_FALSE;
  2196. }
  2197. RETURN_STRING(result->values[pv_num - 1].name);
  2198. }
  2199. /* }}} */
  2200. /* {{{ proto string odbc_field_type(resource result_id, int field_number)
  2201. Get the datatype of a column */
  2202. PHP_FUNCTION(odbc_field_type)
  2203. {
  2204. odbc_result *result;
  2205. char tmp[32];
  2206. SQLSMALLINT tmplen;
  2207. zval *pv_res;
  2208. zend_long pv_num;
  2209. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &pv_res, &pv_num) == FAILURE) {
  2210. RETURN_THROWS();
  2211. }
  2212. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  2213. RETURN_THROWS();
  2214. }
  2215. if (result->numcols == 0) {
  2216. php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
  2217. RETURN_FALSE;
  2218. }
  2219. if (pv_num > result->numcols) {
  2220. php_error_docref(NULL, E_WARNING, "Field index larger than number of fields");
  2221. RETURN_FALSE;
  2222. }
  2223. if (pv_num < 1) {
  2224. php_error_docref(NULL, E_WARNING, "Field numbering starts at 1");
  2225. RETURN_FALSE;
  2226. }
  2227. PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)pv_num, SQL_COLUMN_TYPE_NAME, tmp, 31, &tmplen, NULL);
  2228. RETURN_STRING(tmp);
  2229. }
  2230. /* }}} */
  2231. /* {{{ proto int odbc_field_len(resource result_id, int field_number)
  2232. Get the length (precision) of a column */
  2233. PHP_FUNCTION(odbc_field_len)
  2234. {
  2235. odbc_column_lengths(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2236. }
  2237. /* }}} */
  2238. /* {{{ proto int odbc_field_scale(resource result_id, int field_number)
  2239. Get the scale of a column */
  2240. PHP_FUNCTION(odbc_field_scale)
  2241. {
  2242. odbc_column_lengths(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2243. }
  2244. /* }}} */
  2245. /* {{{ proto int odbc_field_num(resource result_id, string field_name)
  2246. Return column number */
  2247. PHP_FUNCTION(odbc_field_num)
  2248. {
  2249. char *fname;
  2250. size_t i, field_ind, fname_len;
  2251. odbc_result *result;
  2252. zval *pv_res;
  2253. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &pv_res, &fname, &fname_len) == FAILURE) {
  2254. RETURN_THROWS();
  2255. }
  2256. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
  2257. RETURN_THROWS();
  2258. }
  2259. if (result->numcols == 0) {
  2260. php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
  2261. RETURN_FALSE;
  2262. }
  2263. field_ind = -1;
  2264. for(i = 0; i < result->numcols; i++) {
  2265. if (strcasecmp(result->values[i].name, fname) == 0) {
  2266. field_ind = i + 1;
  2267. }
  2268. }
  2269. if (field_ind == -1) {
  2270. RETURN_FALSE;
  2271. }
  2272. RETURN_LONG(field_ind);
  2273. }
  2274. /* }}} */
  2275. /* {{{ proto mixed odbc_autocommit(resource connection_id [, int OnOff])
  2276. Toggle autocommit mode or get status */
  2277. /* There can be problems with pconnections!*/
  2278. PHP_FUNCTION(odbc_autocommit)
  2279. {
  2280. odbc_connection *conn;
  2281. RETCODE rc;
  2282. zval *pv_conn;
  2283. zend_long pv_onoff = 0;
  2284. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pv_conn, &pv_onoff) == FAILURE) {
  2285. RETURN_THROWS();
  2286. }
  2287. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  2288. RETURN_THROWS();
  2289. }
  2290. if (ZEND_NUM_ARGS() > 1) {
  2291. rc = SQLSetConnectOption(conn->hdbc, SQL_AUTOCOMMIT, (pv_onoff) ? SQL_AUTOCOMMIT_ON : SQL_AUTOCOMMIT_OFF);
  2292. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  2293. odbc_sql_error(conn, SQL_NULL_HSTMT, "Set autocommit");
  2294. RETURN_FALSE;
  2295. }
  2296. RETVAL_TRUE;
  2297. } else {
  2298. SQLINTEGER status;
  2299. rc = SQLGetConnectOption(conn->hdbc, SQL_AUTOCOMMIT, (PTR)&status);
  2300. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  2301. odbc_sql_error(conn, SQL_NULL_HSTMT, "Get commit status");
  2302. RETURN_FALSE;
  2303. }
  2304. RETVAL_LONG((zend_long)status);
  2305. }
  2306. }
  2307. /* }}} */
  2308. /* {{{ proto bool odbc_commit(resource connection_id)
  2309. Commit an ODBC transaction */
  2310. PHP_FUNCTION(odbc_commit)
  2311. {
  2312. odbc_transact(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2313. }
  2314. /* }}} */
  2315. /* {{{ proto bool odbc_rollback(resource connection_id)
  2316. Rollback a transaction */
  2317. PHP_FUNCTION(odbc_rollback)
  2318. {
  2319. odbc_transact(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2320. }
  2321. /* }}} */
  2322. /* {{{ php_odbc_lasterror */
  2323. static void php_odbc_lasterror(INTERNAL_FUNCTION_PARAMETERS, int mode)
  2324. {
  2325. odbc_connection *conn;
  2326. zval *pv_handle;
  2327. char *ret;
  2328. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|r", &pv_handle) == FAILURE) {
  2329. RETURN_THROWS();
  2330. }
  2331. if (ZEND_NUM_ARGS() == 1) {
  2332. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_handle), "ODBC-Link", le_conn, le_pconn))) {
  2333. RETURN_THROWS();
  2334. }
  2335. if (mode == 0) {
  2336. ret = conn->laststate;
  2337. } else {
  2338. ret = conn->lasterrormsg;
  2339. }
  2340. } else {
  2341. if (mode == 0) {
  2342. ret = ODBCG(laststate);
  2343. } else {
  2344. ret = ODBCG(lasterrormsg);
  2345. }
  2346. }
  2347. RETURN_STRING(ret);
  2348. }
  2349. /* }}} */
  2350. /* {{{ proto string odbc_error([resource connection_id])
  2351. Get the last error code */
  2352. PHP_FUNCTION(odbc_error)
  2353. {
  2354. php_odbc_lasterror(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  2355. }
  2356. /* }}} */
  2357. /* {{{ proto string odbc_errormsg([resource connection_id])
  2358. Get the last error message */
  2359. PHP_FUNCTION(odbc_errormsg)
  2360. {
  2361. php_odbc_lasterror(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  2362. }
  2363. /* }}} */
  2364. /* {{{ proto bool odbc_setoption(resource conn_id|result_id, int which, int option, int value)
  2365. Sets connection or statement options */
  2366. /* This one has to be used carefully. We can't allow to set connection options for
  2367. persistent connections. I think that SetStmtOption is of little use, since most
  2368. of those can only be specified before preparing/executing statements.
  2369. On the other hand, they can be made connection wide default through SetConnectOption
  2370. - but will be overridden by calls to SetStmtOption() in odbc_prepare/odbc_do
  2371. */
  2372. PHP_FUNCTION(odbc_setoption)
  2373. {
  2374. odbc_connection *conn;
  2375. odbc_result *result;
  2376. RETCODE rc;
  2377. zval *pv_handle;
  2378. zend_long pv_which, pv_opt, pv_val;
  2379. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlll", &pv_handle, &pv_which, &pv_opt, &pv_val) == FAILURE) {
  2380. RETURN_THROWS();
  2381. }
  2382. switch (pv_which) {
  2383. case 1: /* SQLSetConnectOption */
  2384. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_handle), "ODBC-Link", le_conn, le_pconn))) {
  2385. RETURN_THROWS();
  2386. }
  2387. if (conn->persistent) {
  2388. php_error_docref(NULL, E_WARNING, "Unable to set option for persistent connection");
  2389. RETURN_FALSE;
  2390. }
  2391. rc = SQLSetConnectOption(conn->hdbc, (unsigned short) pv_opt, pv_val);
  2392. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  2393. odbc_sql_error(conn, SQL_NULL_HSTMT, "SetConnectOption");
  2394. RETURN_FALSE;
  2395. }
  2396. break;
  2397. case 2: /* SQLSetStmtOption */
  2398. if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_handle), "ODBC result", le_result)) == NULL) {
  2399. RETURN_THROWS();
  2400. }
  2401. rc = SQLSetStmtOption(result->stmt, (unsigned short) pv_opt, pv_val);
  2402. if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
  2403. odbc_sql_error(result->conn_ptr, result->stmt, "SetStmtOption");
  2404. RETURN_FALSE;
  2405. }
  2406. break;
  2407. default:
  2408. php_error_docref(NULL, E_WARNING, "Unknown option type");
  2409. RETURN_FALSE;
  2410. break;
  2411. }
  2412. RETURN_TRUE;
  2413. }
  2414. /* }}} */
  2415. /*
  2416. * metadata functions
  2417. */
  2418. /* {{{ proto resource odbc_tables(resource connection_id [, string qualifier [, string owner [, string name [, string table_types]]]])
  2419. Call the SQLTables function */
  2420. PHP_FUNCTION(odbc_tables)
  2421. {
  2422. zval *pv_conn;
  2423. odbc_result *result = NULL;
  2424. odbc_connection *conn;
  2425. char *cat = NULL, *schema = NULL, *table = NULL, *type = NULL;
  2426. size_t cat_len = 0, schema_len = 0, table_len = 0, type_len = 0;
  2427. RETCODE rc;
  2428. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|s!sss", &pv_conn, &cat, &cat_len, &schema, &schema_len,
  2429. &table, &table_len, &type, &type_len) == FAILURE) {
  2430. RETURN_THROWS();
  2431. }
  2432. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  2433. RETURN_THROWS();
  2434. }
  2435. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  2436. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  2437. if (rc == SQL_INVALID_HANDLE) {
  2438. efree(result);
  2439. php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  2440. RETURN_FALSE;
  2441. }
  2442. if (rc == SQL_ERROR) {
  2443. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  2444. efree(result);
  2445. RETURN_FALSE;
  2446. }
  2447. /* This hack is needed to access table information in Access databases (fmk) */
  2448. if (table && table_len && schema && schema_len == 0) {
  2449. schema = NULL;
  2450. }
  2451. rc = SQLTables(result->stmt,
  2452. cat, SAFE_SQL_NTS(cat),
  2453. schema, SAFE_SQL_NTS(schema),
  2454. table, SAFE_SQL_NTS(table),
  2455. type, SAFE_SQL_NTS(type));
  2456. if (rc == SQL_ERROR) {
  2457. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLTables");
  2458. efree(result);
  2459. RETURN_FALSE;
  2460. }
  2461. result->numparams = 0;
  2462. SQLNumResultCols(result->stmt, &(result->numcols));
  2463. if (result->numcols > 0) {
  2464. if (!odbc_bindcols(result)) {
  2465. efree(result);
  2466. RETURN_FALSE;
  2467. }
  2468. } else {
  2469. result->values = NULL;
  2470. }
  2471. result->conn_ptr = conn;
  2472. result->fetched = 0;
  2473. RETURN_RES(zend_register_resource(result, le_result));
  2474. }
  2475. /* }}} */
  2476. /* {{{ proto resource odbc_columns(resource connection_id [, string qualifier [, string owner [, string table_name [, string column_name]]]])
  2477. Returns a result identifier that can be used to fetch a list of column names in specified tables */
  2478. PHP_FUNCTION(odbc_columns)
  2479. {
  2480. zval *pv_conn;
  2481. odbc_result *result = NULL;
  2482. odbc_connection *conn;
  2483. char *cat = NULL, *schema = NULL, *table = NULL, *column = NULL;
  2484. size_t cat_len = 0, schema_len = 0, table_len = 0, column_len = 0;
  2485. RETCODE rc;
  2486. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|s!sss", &pv_conn, &cat, &cat_len, &schema, &schema_len,
  2487. &table, &table_len, &column, &column_len) == FAILURE) {
  2488. RETURN_THROWS();
  2489. }
  2490. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  2491. RETURN_THROWS();
  2492. }
  2493. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  2494. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  2495. if (rc == SQL_INVALID_HANDLE) {
  2496. efree(result);
  2497. php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  2498. RETURN_FALSE;
  2499. }
  2500. if (rc == SQL_ERROR) {
  2501. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  2502. efree(result);
  2503. RETURN_FALSE;
  2504. }
  2505. /*
  2506. * Needed to make MS Access happy
  2507. */
  2508. if (table && table_len && schema && schema_len == 0) {
  2509. schema = NULL;
  2510. }
  2511. rc = SQLColumns(result->stmt,
  2512. cat, (SQLSMALLINT) cat_len,
  2513. schema, (SQLSMALLINT) schema_len,
  2514. table, (SQLSMALLINT) table_len,
  2515. column, (SQLSMALLINT) column_len);
  2516. if (rc == SQL_ERROR) {
  2517. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLColumns");
  2518. efree(result);
  2519. RETURN_FALSE;
  2520. }
  2521. result->numparams = 0;
  2522. SQLNumResultCols(result->stmt, &(result->numcols));
  2523. if (result->numcols > 0) {
  2524. if (!odbc_bindcols(result)) {
  2525. efree(result);
  2526. RETURN_FALSE;
  2527. }
  2528. } else {
  2529. result->values = NULL;
  2530. }
  2531. result->conn_ptr = conn;
  2532. result->fetched = 0;
  2533. RETURN_RES(zend_register_resource(result, le_result));
  2534. }
  2535. /* }}} */
  2536. #if !defined(HAVE_DBMAKER) && !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35)
  2537. /* {{{ proto resource odbc_columnprivileges(resource connection_id, string catalog, string schema, string table, string column)
  2538. Returns a result identifier that can be used to fetch a list of columns and associated privileges for the specified table */
  2539. PHP_FUNCTION(odbc_columnprivileges)
  2540. {
  2541. zval *pv_conn;
  2542. odbc_result *result = NULL;
  2543. odbc_connection *conn;
  2544. char *cat = NULL, *schema, *table, *column;
  2545. size_t cat_len = 0, schema_len, table_len, column_len;
  2546. RETCODE rc;
  2547. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs!sss", &pv_conn, &cat, &cat_len, &schema, &schema_len,
  2548. &table, &table_len, &column, &column_len) == FAILURE) {
  2549. RETURN_THROWS();
  2550. }
  2551. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  2552. RETURN_THROWS();
  2553. }
  2554. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  2555. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  2556. if (rc == SQL_INVALID_HANDLE) {
  2557. efree(result);
  2558. php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  2559. RETURN_FALSE;
  2560. }
  2561. if (rc == SQL_ERROR) {
  2562. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  2563. efree(result);
  2564. RETURN_FALSE;
  2565. }
  2566. rc = SQLColumnPrivileges(result->stmt,
  2567. cat, SAFE_SQL_NTS(cat),
  2568. schema, SAFE_SQL_NTS(schema),
  2569. table, SAFE_SQL_NTS(table),
  2570. column, SAFE_SQL_NTS(column));
  2571. if (rc == SQL_ERROR) {
  2572. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLColumnPrivileges");
  2573. efree(result);
  2574. RETURN_FALSE;
  2575. }
  2576. result->numparams = 0;
  2577. SQLNumResultCols(result->stmt, &(result->numcols));
  2578. if (result->numcols > 0) {
  2579. if (!odbc_bindcols(result)) {
  2580. efree(result);
  2581. RETURN_FALSE;
  2582. }
  2583. } else {
  2584. result->values = NULL;
  2585. }
  2586. result->conn_ptr = conn;
  2587. result->fetched = 0;
  2588. RETURN_RES(zend_register_resource(result, le_result));
  2589. }
  2590. /* }}} */
  2591. #endif /* HAVE_DBMAKER || HAVE_SOLID*/
  2592. #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35)
  2593. /* {{{ proto resource odbc_foreignkeys(resource connection_id, string pk_qualifier, string pk_owner, string pk_table, string fk_qualifier, string fk_owner, string fk_table)
  2594. Returns a result identifier to either a list of foreign keys in the specified table or a list of foreign keys in other tables that refer to the primary key in the specified table */
  2595. PHP_FUNCTION(odbc_foreignkeys)
  2596. {
  2597. zval *pv_conn;
  2598. odbc_result *result = NULL;
  2599. odbc_connection *conn;
  2600. char *pcat = NULL, *pschema, *ptable, *fcat, *fschema, *ftable;
  2601. size_t pcat_len = 0, pschema_len, ptable_len, fcat_len, fschema_len, ftable_len;
  2602. RETCODE rc;
  2603. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs!sssss", &pv_conn, &pcat, &pcat_len, &pschema, &pschema_len,
  2604. &ptable, &ptable_len, &fcat, &fcat_len, &fschema, &fschema_len, &ftable, &ftable_len) == FAILURE) {
  2605. RETURN_THROWS();
  2606. }
  2607. #if defined(HAVE_DBMAKER) || defined(HAVE_IBMDB2)
  2608. #define EMPTY_TO_NULL(xstr) \
  2609. if ((int)strlen((xstr)) == 0) (xstr) = NULL
  2610. EMPTY_TO_NULL(pcat);
  2611. EMPTY_TO_NULL(pschema);
  2612. EMPTY_TO_NULL(ptable);
  2613. EMPTY_TO_NULL(fcat);
  2614. EMPTY_TO_NULL(fschema);
  2615. EMPTY_TO_NULL(ftable);
  2616. #endif
  2617. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  2618. RETURN_THROWS();
  2619. }
  2620. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  2621. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  2622. if (rc == SQL_INVALID_HANDLE) {
  2623. efree(result);
  2624. php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  2625. RETURN_FALSE;
  2626. }
  2627. if (rc == SQL_ERROR) {
  2628. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  2629. efree(result);
  2630. RETURN_FALSE;
  2631. }
  2632. rc = SQLForeignKeys(result->stmt,
  2633. pcat, SAFE_SQL_NTS(pcat),
  2634. pschema, SAFE_SQL_NTS(pschema),
  2635. ptable, SAFE_SQL_NTS(ptable),
  2636. fcat, SAFE_SQL_NTS(fcat),
  2637. fschema, SAFE_SQL_NTS(fschema),
  2638. ftable, SAFE_SQL_NTS(ftable) );
  2639. if (rc == SQL_ERROR) {
  2640. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLForeignKeys");
  2641. efree(result);
  2642. RETURN_FALSE;
  2643. }
  2644. result->numparams = 0;
  2645. SQLNumResultCols(result->stmt, &(result->numcols));
  2646. if (result->numcols > 0) {
  2647. if (!odbc_bindcols(result)) {
  2648. efree(result);
  2649. RETURN_FALSE;
  2650. }
  2651. } else {
  2652. result->values = NULL;
  2653. }
  2654. result->conn_ptr = conn;
  2655. result->fetched = 0;
  2656. RETURN_RES(zend_register_resource(result, le_result));
  2657. }
  2658. /* }}} */
  2659. #endif /* HAVE_SOLID */
  2660. /* {{{ proto resource odbc_gettypeinfo(resource connection_id [, int data_type])
  2661. Returns a result identifier containing information about data types supported by the data source */
  2662. PHP_FUNCTION(odbc_gettypeinfo)
  2663. {
  2664. zval *pv_conn;
  2665. zend_long pv_data_type = SQL_ALL_TYPES;
  2666. odbc_result *result = NULL;
  2667. odbc_connection *conn;
  2668. RETCODE rc;
  2669. SQLSMALLINT data_type;
  2670. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pv_conn, &pv_data_type) == FAILURE) {
  2671. RETURN_THROWS();
  2672. }
  2673. data_type = (SQLSMALLINT) pv_data_type;
  2674. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  2675. RETURN_THROWS();
  2676. }
  2677. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  2678. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  2679. if (rc == SQL_INVALID_HANDLE) {
  2680. efree(result);
  2681. php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  2682. RETURN_FALSE;
  2683. }
  2684. if (rc == SQL_ERROR) {
  2685. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  2686. efree(result);
  2687. RETURN_FALSE;
  2688. }
  2689. rc = SQLGetTypeInfo(result->stmt, data_type );
  2690. if (rc == SQL_ERROR) {
  2691. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLGetTypeInfo");
  2692. efree(result);
  2693. RETURN_FALSE;
  2694. }
  2695. result->numparams = 0;
  2696. SQLNumResultCols(result->stmt, &(result->numcols));
  2697. if (result->numcols > 0) {
  2698. if (!odbc_bindcols(result)) {
  2699. efree(result);
  2700. RETURN_FALSE;
  2701. }
  2702. } else {
  2703. result->values = NULL;
  2704. }
  2705. result->conn_ptr = conn;
  2706. result->fetched = 0;
  2707. RETURN_RES(zend_register_resource(result, le_result));
  2708. }
  2709. /* }}} */
  2710. /* {{{ proto resource odbc_primarykeys(resource connection_id, string qualifier, string owner, string table)
  2711. Returns a result identifier listing the column names that comprise the primary key for a table */
  2712. PHP_FUNCTION(odbc_primarykeys)
  2713. {
  2714. zval *pv_conn;
  2715. odbc_result *result = NULL;
  2716. odbc_connection *conn;
  2717. char *cat = NULL, *schema = NULL, *table = NULL;
  2718. size_t cat_len = 0, schema_len, table_len;
  2719. RETCODE rc;
  2720. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs!ss", &pv_conn, &cat, &cat_len, &schema, &schema_len, &table, &table_len) == FAILURE) {
  2721. RETURN_THROWS();
  2722. }
  2723. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  2724. RETURN_THROWS();
  2725. }
  2726. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  2727. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  2728. if (rc == SQL_INVALID_HANDLE) {
  2729. efree(result);
  2730. php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  2731. RETURN_FALSE;
  2732. }
  2733. if (rc == SQL_ERROR) {
  2734. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  2735. efree(result);
  2736. RETURN_FALSE;
  2737. }
  2738. rc = SQLPrimaryKeys(result->stmt,
  2739. cat, SAFE_SQL_NTS(cat),
  2740. schema, SAFE_SQL_NTS(schema),
  2741. table, SAFE_SQL_NTS(table) );
  2742. if (rc == SQL_ERROR) {
  2743. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLPrimaryKeys");
  2744. efree(result);
  2745. RETURN_FALSE;
  2746. }
  2747. result->numparams = 0;
  2748. SQLNumResultCols(result->stmt, &(result->numcols));
  2749. if (result->numcols > 0) {
  2750. if (!odbc_bindcols(result)) {
  2751. efree(result);
  2752. RETURN_FALSE;
  2753. }
  2754. } else {
  2755. result->values = NULL;
  2756. }
  2757. result->conn_ptr = conn;
  2758. result->fetched = 0;
  2759. RETURN_RES(zend_register_resource(result, le_result));
  2760. }
  2761. /* }}} */
  2762. #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35)
  2763. /* {{{ proto resource odbc_procedurecolumns(resource connection_id [, string qualifier, string owner, string proc, string column])
  2764. Returns a result identifier containing the list of input and output parameters, as well as the columns that make up the result set for the specified procedures */
  2765. PHP_FUNCTION(odbc_procedurecolumns)
  2766. {
  2767. zval *pv_conn;
  2768. odbc_result *result = NULL;
  2769. odbc_connection *conn;
  2770. char *cat = NULL, *schema = NULL, *proc = NULL, *col = NULL;
  2771. size_t cat_len = 0, schema_len = 0, proc_len = 0, col_len = 0;
  2772. RETCODE rc;
  2773. if (ZEND_NUM_ARGS() != 1 && ZEND_NUM_ARGS() != 5) {
  2774. WRONG_PARAM_COUNT;
  2775. }
  2776. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|s!sss", &pv_conn, &cat, &cat_len, &schema, &schema_len,
  2777. &proc, &proc_len, &col, &col_len) == FAILURE) {
  2778. RETURN_THROWS();
  2779. }
  2780. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  2781. RETURN_THROWS();
  2782. }
  2783. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  2784. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  2785. if (rc == SQL_INVALID_HANDLE) {
  2786. efree(result);
  2787. php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  2788. RETURN_FALSE;
  2789. }
  2790. if (rc == SQL_ERROR) {
  2791. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  2792. efree(result);
  2793. RETURN_FALSE;
  2794. }
  2795. rc = SQLProcedureColumns(result->stmt,
  2796. cat, SAFE_SQL_NTS(cat),
  2797. schema, SAFE_SQL_NTS(schema),
  2798. proc, SAFE_SQL_NTS(proc),
  2799. col, SAFE_SQL_NTS(col) );
  2800. if (rc == SQL_ERROR) {
  2801. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLProcedureColumns");
  2802. efree(result);
  2803. RETURN_FALSE;
  2804. }
  2805. result->numparams = 0;
  2806. SQLNumResultCols(result->stmt, &(result->numcols));
  2807. if (result->numcols > 0) {
  2808. if (!odbc_bindcols(result)) {
  2809. efree(result);
  2810. RETURN_FALSE;
  2811. }
  2812. } else {
  2813. result->values = NULL;
  2814. }
  2815. result->conn_ptr = conn;
  2816. result->fetched = 0;
  2817. RETURN_RES(zend_register_resource(result, le_result));
  2818. }
  2819. /* }}} */
  2820. #endif /* HAVE_SOLID */
  2821. #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35)
  2822. /* {{{ proto resource odbc_procedures(resource connection_id [, string qualifier, string owner, string name])
  2823. Returns a result identifier containing the list of procedure names in a datasource */
  2824. PHP_FUNCTION(odbc_procedures)
  2825. {
  2826. zval *pv_conn;
  2827. odbc_result *result = NULL;
  2828. odbc_connection *conn;
  2829. char *cat = NULL, *schema = NULL, *proc = NULL;
  2830. size_t cat_len = 0, schema_len = 0, proc_len = 0;
  2831. RETCODE rc;
  2832. if (ZEND_NUM_ARGS() != 1 && ZEND_NUM_ARGS() != 4) {
  2833. WRONG_PARAM_COUNT;
  2834. }
  2835. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|s!ss", &pv_conn, &cat, &cat_len, &schema, &schema_len, &proc, &proc_len) == FAILURE) {
  2836. RETURN_THROWS();
  2837. }
  2838. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  2839. RETURN_THROWS();
  2840. }
  2841. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  2842. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  2843. if (rc == SQL_INVALID_HANDLE) {
  2844. efree(result);
  2845. php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  2846. RETURN_FALSE;
  2847. }
  2848. if (rc == SQL_ERROR) {
  2849. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  2850. efree(result);
  2851. RETURN_FALSE;
  2852. }
  2853. rc = SQLProcedures(result->stmt,
  2854. cat, SAFE_SQL_NTS(cat),
  2855. schema, SAFE_SQL_NTS(schema),
  2856. proc, SAFE_SQL_NTS(proc) );
  2857. if (rc == SQL_ERROR) {
  2858. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLProcedures");
  2859. efree(result);
  2860. RETURN_FALSE;
  2861. }
  2862. result->numparams = 0;
  2863. SQLNumResultCols(result->stmt, &(result->numcols));
  2864. if (result->numcols > 0) {
  2865. if (!odbc_bindcols(result)) {
  2866. efree(result);
  2867. RETURN_FALSE;
  2868. }
  2869. } else {
  2870. result->values = NULL;
  2871. }
  2872. result->conn_ptr = conn;
  2873. result->fetched = 0;
  2874. RETURN_RES(zend_register_resource(result, le_result));
  2875. }
  2876. /* }}} */
  2877. #endif /* HAVE_SOLID */
  2878. /* {{{ proto resource odbc_specialcolumns(resource connection_id, int type, string qualifier, string owner, string table, int scope, int nullable)
  2879. Returns a result identifier containing either the optimal set of columns that uniquely identifies a row in the table or columns that are automatically updated when any value in the row is updated by a transaction */
  2880. PHP_FUNCTION(odbc_specialcolumns)
  2881. {
  2882. zval *pv_conn;
  2883. zend_long vtype, vscope, vnullable;
  2884. odbc_result *result = NULL;
  2885. odbc_connection *conn;
  2886. char *cat = NULL, *schema = NULL, *name = NULL;
  2887. size_t cat_len = 0, schema_len, name_len;
  2888. SQLUSMALLINT type, scope, nullable;
  2889. RETCODE rc;
  2890. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rls!ssl", &pv_conn, &vtype, &cat, &cat_len, &schema, &schema_len,
  2891. &name, &name_len, &vscope, &vnullable) == FAILURE) {
  2892. RETURN_THROWS();
  2893. }
  2894. type = (SQLUSMALLINT) vtype;
  2895. scope = (SQLUSMALLINT) vscope;
  2896. nullable = (SQLUSMALLINT) vnullable;
  2897. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  2898. RETURN_THROWS();
  2899. }
  2900. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  2901. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  2902. if (rc == SQL_INVALID_HANDLE) {
  2903. efree(result);
  2904. php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  2905. RETURN_FALSE;
  2906. }
  2907. if (rc == SQL_ERROR) {
  2908. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  2909. efree(result);
  2910. RETURN_FALSE;
  2911. }
  2912. rc = SQLSpecialColumns(result->stmt,
  2913. type,
  2914. cat, SAFE_SQL_NTS(cat),
  2915. schema, SAFE_SQL_NTS(schema),
  2916. name, SAFE_SQL_NTS(name),
  2917. scope,
  2918. nullable);
  2919. if (rc == SQL_ERROR) {
  2920. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLSpecialColumns");
  2921. efree(result);
  2922. RETURN_FALSE;
  2923. }
  2924. result->numparams = 0;
  2925. SQLNumResultCols(result->stmt, &(result->numcols));
  2926. if (result->numcols > 0) {
  2927. if (!odbc_bindcols(result)) {
  2928. efree(result);
  2929. RETURN_FALSE;
  2930. }
  2931. } else {
  2932. result->values = NULL;
  2933. }
  2934. result->conn_ptr = conn;
  2935. result->fetched = 0;
  2936. RETURN_RES(zend_register_resource(result, le_result));
  2937. }
  2938. /* }}} */
  2939. /* {{{ proto resource odbc_statistics(resource connection_id, string qualifier, string owner, string name, int unique, int accuracy)
  2940. Returns a result identifier that contains statistics about a single table and the indexes associated with the table */
  2941. PHP_FUNCTION(odbc_statistics)
  2942. {
  2943. zval *pv_conn;
  2944. zend_long vunique, vreserved;
  2945. odbc_result *result = NULL;
  2946. odbc_connection *conn;
  2947. char *cat = NULL, *schema, *name;
  2948. size_t cat_len = 0, schema_len, name_len;
  2949. SQLUSMALLINT unique, reserved;
  2950. RETCODE rc;
  2951. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs!ssll", &pv_conn, &cat, &cat_len, &schema, &schema_len,
  2952. &name, &name_len, &vunique, &vreserved) == FAILURE) {
  2953. RETURN_THROWS();
  2954. }
  2955. unique = (SQLUSMALLINT) vunique;
  2956. reserved = (SQLUSMALLINT) vreserved;
  2957. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  2958. RETURN_THROWS();
  2959. }
  2960. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  2961. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  2962. if (rc == SQL_INVALID_HANDLE) {
  2963. efree(result);
  2964. php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  2965. RETURN_FALSE;
  2966. }
  2967. if (rc == SQL_ERROR) {
  2968. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  2969. efree(result);
  2970. RETURN_FALSE;
  2971. }
  2972. rc = SQLStatistics(result->stmt,
  2973. cat, SAFE_SQL_NTS(cat),
  2974. schema, SAFE_SQL_NTS(schema),
  2975. name, SAFE_SQL_NTS(name),
  2976. unique,
  2977. reserved);
  2978. if (rc == SQL_ERROR) {
  2979. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLStatistics");
  2980. efree(result);
  2981. RETURN_FALSE;
  2982. }
  2983. result->numparams = 0;
  2984. SQLNumResultCols(result->stmt, &(result->numcols));
  2985. if (result->numcols > 0) {
  2986. if (!odbc_bindcols(result)) {
  2987. efree(result);
  2988. RETURN_FALSE;
  2989. }
  2990. } else {
  2991. result->values = NULL;
  2992. }
  2993. result->conn_ptr = conn;
  2994. result->fetched = 0;
  2995. RETURN_RES(zend_register_resource(result, le_result));
  2996. }
  2997. /* }}} */
  2998. #if !defined(HAVE_DBMAKER) && !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35)
  2999. /* {{{ proto resource odbc_tableprivileges(resource connection_id, string qualifier, string owner, string name)
  3000. Returns a result identifier containing a list of tables and the privileges associated with each table */
  3001. PHP_FUNCTION(odbc_tableprivileges)
  3002. {
  3003. zval *pv_conn;
  3004. odbc_result *result = NULL;
  3005. odbc_connection *conn;
  3006. char *cat = NULL, *schema = NULL, *table = NULL;
  3007. size_t cat_len = 0, schema_len, table_len;
  3008. RETCODE rc;
  3009. if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs!ss", &pv_conn, &cat, &cat_len, &schema, &schema_len, &table, &table_len) == FAILURE) {
  3010. RETURN_THROWS();
  3011. }
  3012. if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
  3013. RETURN_THROWS();
  3014. }
  3015. result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
  3016. rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
  3017. if (rc == SQL_INVALID_HANDLE) {
  3018. efree(result);
  3019. php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
  3020. RETURN_FALSE;
  3021. }
  3022. if (rc == SQL_ERROR) {
  3023. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
  3024. efree(result);
  3025. RETURN_FALSE;
  3026. }
  3027. rc = SQLTablePrivileges(result->stmt,
  3028. cat, SAFE_SQL_NTS(cat),
  3029. schema, SAFE_SQL_NTS(schema),
  3030. table, SAFE_SQL_NTS(table));
  3031. if (rc == SQL_ERROR) {
  3032. odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLTablePrivileges");
  3033. efree(result);
  3034. RETURN_FALSE;
  3035. }
  3036. result->numparams = 0;
  3037. SQLNumResultCols(result->stmt, &(result->numcols));
  3038. if (result->numcols > 0) {
  3039. if (!odbc_bindcols(result)) {
  3040. efree(result);
  3041. RETURN_FALSE;
  3042. }
  3043. } else {
  3044. result->values = NULL;
  3045. }
  3046. result->conn_ptr = conn;
  3047. result->fetched = 0;
  3048. RETURN_RES(zend_register_resource(result, le_result));
  3049. }
  3050. /* }}} */
  3051. #endif /* HAVE_DBMAKER */
  3052. #endif /* HAVE_UODBC */