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