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