pdo_odbc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.0 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_0.txt. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Wez Furlong <wez@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "php.h"
  22. #include "php_ini.h"
  23. #include "ext/standard/info.h"
  24. #include "pdo/php_pdo.h"
  25. #include "pdo/php_pdo_driver.h"
  26. #include "php_pdo_odbc.h"
  27. #include "php_pdo_odbc_int.h"
  28. /* {{{ pdo_odbc_functions[] */
  29. static const zend_function_entry pdo_odbc_functions[] = {
  30. PHP_FE_END
  31. };
  32. /* }}} */
  33. /* {{{ pdo_odbc_deps[] */
  34. static const zend_module_dep pdo_odbc_deps[] = {
  35. ZEND_MOD_REQUIRED("pdo")
  36. ZEND_MOD_END
  37. };
  38. /* }}} */
  39. /* {{{ pdo_odbc_module_entry */
  40. zend_module_entry pdo_odbc_module_entry = {
  41. STANDARD_MODULE_HEADER_EX, NULL,
  42. pdo_odbc_deps,
  43. "PDO_ODBC",
  44. pdo_odbc_functions,
  45. PHP_MINIT(pdo_odbc),
  46. PHP_MSHUTDOWN(pdo_odbc),
  47. NULL,
  48. NULL,
  49. PHP_MINFO(pdo_odbc),
  50. PHP_PDO_ODBC_VERSION,
  51. STANDARD_MODULE_PROPERTIES
  52. };
  53. /* }}} */
  54. #ifdef COMPILE_DL_PDO_ODBC
  55. ZEND_GET_MODULE(pdo_odbc)
  56. #endif
  57. #ifdef SQL_ATTR_CONNECTION_POOLING
  58. zend_ulong pdo_odbc_pool_on = SQL_CP_OFF;
  59. zend_ulong pdo_odbc_pool_mode = SQL_CP_ONE_PER_HENV;
  60. #endif
  61. #if defined(DB2CLI_VER) && !defined(PHP_WIN32)
  62. PHP_INI_BEGIN()
  63. PHP_INI_ENTRY("pdo_odbc.db2_instance_name", NULL, PHP_INI_SYSTEM, NULL)
  64. PHP_INI_END()
  65. #endif
  66. /* {{{ PHP_MINIT_FUNCTION */
  67. PHP_MINIT_FUNCTION(pdo_odbc)
  68. {
  69. #ifdef SQL_ATTR_CONNECTION_POOLING
  70. char *pooling_val = NULL;
  71. #endif
  72. if (FAILURE == php_pdo_register_driver(&pdo_odbc_driver)) {
  73. return FAILURE;
  74. }
  75. #if defined(DB2CLI_VER) && !defined(PHP_WIN32)
  76. REGISTER_INI_ENTRIES();
  77. {
  78. char *instance = INI_STR("pdo_odbc.db2_instance_name");
  79. if (instance) {
  80. char *env = malloc(sizeof("DB2INSTANCE=") + strlen(instance));
  81. php_error_docref(NULL, E_DEPRECATED, "The pdo_odbc.db2_instance_name ini directive is deprecated and will be removed in the future");
  82. if (!env) {
  83. return FAILURE;
  84. }
  85. strcpy(env, "DB2INSTANCE=");
  86. strcat(env, instance);
  87. putenv(env);
  88. /* after this point, we can't free env without breaking the environment */
  89. }
  90. }
  91. #endif
  92. #ifdef SQL_ATTR_CONNECTION_POOLING
  93. /* ugh, we don't really like .ini stuff in PDO, but since ODBC connection
  94. * pooling is process wide, we can't set it from within the scope of a
  95. * request without affecting others, which goes against our isolated request
  96. * policy. So, we use cfg_get_string here to check it this once.
  97. * */
  98. if (FAILURE == cfg_get_string("pdo_odbc.connection_pooling", &pooling_val) || pooling_val == NULL) {
  99. pooling_val = "strict";
  100. }
  101. if (strcasecmp(pooling_val, "strict") == 0 || strcmp(pooling_val, "1") == 0) {
  102. pdo_odbc_pool_on = SQL_CP_ONE_PER_HENV;
  103. pdo_odbc_pool_mode = SQL_CP_STRICT_MATCH;
  104. } else if (strcasecmp(pooling_val, "relaxed") == 0) {
  105. pdo_odbc_pool_on = SQL_CP_ONE_PER_HENV;
  106. pdo_odbc_pool_mode = SQL_CP_RELAXED_MATCH;
  107. } else if (*pooling_val == '\0' || strcasecmp(pooling_val, "off") == 0) {
  108. pdo_odbc_pool_on = SQL_CP_OFF;
  109. } else {
  110. php_error_docref(NULL, E_CORE_ERROR, "Error in pdo_odbc.connection_pooling configuration. Value MUST be one of 'strict', 'relaxed' or 'off'");
  111. return FAILURE;
  112. }
  113. if (pdo_odbc_pool_on != SQL_CP_OFF) {
  114. SQLSetEnvAttr(SQL_NULL_HANDLE, SQL_ATTR_CONNECTION_POOLING, (void*)pdo_odbc_pool_on, 0);
  115. }
  116. #endif
  117. REGISTER_PDO_CLASS_CONST_LONG("ODBC_ATTR_USE_CURSOR_LIBRARY", PDO_ODBC_ATTR_USE_CURSOR_LIBRARY);
  118. REGISTER_PDO_CLASS_CONST_LONG("ODBC_ATTR_ASSUME_UTF8", PDO_ODBC_ATTR_ASSUME_UTF8);
  119. REGISTER_PDO_CLASS_CONST_LONG("ODBC_SQL_USE_IF_NEEDED", SQL_CUR_USE_IF_NEEDED);
  120. REGISTER_PDO_CLASS_CONST_LONG("ODBC_SQL_USE_DRIVER", SQL_CUR_USE_DRIVER);
  121. REGISTER_PDO_CLASS_CONST_LONG("ODBC_SQL_USE_ODBC", SQL_CUR_USE_ODBC);
  122. return SUCCESS;
  123. }
  124. /* }}} */
  125. /* {{{ PHP_MSHUTDOWN_FUNCTION
  126. */
  127. PHP_MSHUTDOWN_FUNCTION(pdo_odbc)
  128. {
  129. #if defined(DB2CLI_VER) && !defined(PHP_WIN32)
  130. UNREGISTER_INI_ENTRIES();
  131. #endif
  132. php_pdo_unregister_driver(&pdo_odbc_driver);
  133. return SUCCESS;
  134. }
  135. /* }}} */
  136. /* {{{ PHP_MINFO_FUNCTION
  137. */
  138. PHP_MINFO_FUNCTION(pdo_odbc)
  139. {
  140. php_info_print_table_start();
  141. php_info_print_table_header(2, "PDO Driver for ODBC (" PDO_ODBC_TYPE ")" , "enabled");
  142. #ifdef SQL_ATTR_CONNECTION_POOLING
  143. php_info_print_table_row(2, "ODBC Connection Pooling", pdo_odbc_pool_on == SQL_CP_OFF ?
  144. "Disabled" : (pdo_odbc_pool_mode == SQL_CP_STRICT_MATCH ? "Enabled, strict matching" : "Enabled, relaxed matching"));
  145. #else
  146. php_info_print_table_row(2, "ODBC Connection Pooling", "Not supported in this build");
  147. #endif
  148. php_info_print_table_end();
  149. #if defined(DB2CLI_VER) && !defined(PHP_WIN32)
  150. DISPLAY_INI_ENTRIES();
  151. #endif
  152. }
  153. /* }}} */
  154. /*
  155. * Local variables:
  156. * tab-width: 4
  157. * c-basic-offset: 4
  158. * End:
  159. * vim600: noet sw=4 ts=4 fdm=marker
  160. * vim<600: noet sw=4 ts=4
  161. */