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