PageRenderTime 61ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/odbc/php_odbc.c

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