xref: /PHP-8.2/ext/pdo_odbc/pdo_odbc.c (revision 01b3fc03)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | https://www.php.net/license/3_01.txt                                 |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Author: Wez Furlong <wez@php.net>                                    |
14   +----------------------------------------------------------------------+
15 */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
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 
29 /* {{{ pdo_odbc_deps[] */
30 static const zend_module_dep pdo_odbc_deps[] = {
31 	ZEND_MOD_REQUIRED("pdo")
32 	ZEND_MOD_END
33 };
34 /* }}} */
35 
36 /* {{{ pdo_odbc_module_entry */
37 zend_module_entry pdo_odbc_module_entry = {
38 	STANDARD_MODULE_HEADER_EX, NULL,
39 	pdo_odbc_deps,
40 	"PDO_ODBC",
41 	NULL,
42 	PHP_MINIT(pdo_odbc),
43 	PHP_MSHUTDOWN(pdo_odbc),
44 	NULL,
45 	NULL,
46 	PHP_MINFO(pdo_odbc),
47 	PHP_PDO_ODBC_VERSION,
48 	STANDARD_MODULE_PROPERTIES
49 };
50 /* }}} */
51 
52 #ifdef COMPILE_DL_PDO_ODBC
53 ZEND_GET_MODULE(pdo_odbc)
54 #endif
55 
56 #ifdef SQL_ATTR_CONNECTION_POOLING
57 zend_ulong pdo_odbc_pool_on = SQL_CP_OFF;
58 zend_ulong pdo_odbc_pool_mode = SQL_CP_ONE_PER_HENV;
59 #endif
60 
61 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(pdo_odbc)62 PHP_MINIT_FUNCTION(pdo_odbc)
63 {
64 #ifdef SQL_ATTR_CONNECTION_POOLING
65 	char *pooling_val = NULL;
66 #endif
67 
68 	if (FAILURE == php_pdo_register_driver(&pdo_odbc_driver)) {
69 		return FAILURE;
70 	}
71 
72 #ifdef SQL_ATTR_CONNECTION_POOLING
73 	/* ugh, we don't really like .ini stuff in PDO, but since ODBC connection
74 	 * pooling is process wide, we can't set it from within the scope of a
75 	 * request without affecting others, which goes against our isolated request
76 	 * policy.  So, we use cfg_get_string here to check it this once.
77 	 * */
78 	if (FAILURE == cfg_get_string("pdo_odbc.connection_pooling", &pooling_val) || pooling_val == NULL) {
79 		pooling_val = "strict";
80 	}
81 	if (strcasecmp(pooling_val, "strict") == 0 || strcmp(pooling_val, "1") == 0) {
82 		pdo_odbc_pool_on = SQL_CP_ONE_PER_HENV;
83 		pdo_odbc_pool_mode = SQL_CP_STRICT_MATCH;
84 	} else if (strcasecmp(pooling_val, "relaxed") == 0) {
85 		pdo_odbc_pool_on = SQL_CP_ONE_PER_HENV;
86 		pdo_odbc_pool_mode = SQL_CP_RELAXED_MATCH;
87 	} else if (*pooling_val == '\0' || strcasecmp(pooling_val, "off") == 0) {
88 		pdo_odbc_pool_on = SQL_CP_OFF;
89 	} else {
90 		php_error_docref(NULL, E_CORE_ERROR, "Error in pdo_odbc.connection_pooling configuration. Value must be one of \"strict\", \"relaxed\", or \"off\"");
91 		return FAILURE;
92 	}
93 
94 	if (pdo_odbc_pool_on != SQL_CP_OFF) {
95 		SQLSetEnvAttr(SQL_NULL_HANDLE, SQL_ATTR_CONNECTION_POOLING, (void*)pdo_odbc_pool_on, 0);
96 	}
97 #endif
98 
99 	REGISTER_PDO_CLASS_CONST_LONG("ODBC_ATTR_USE_CURSOR_LIBRARY", PDO_ODBC_ATTR_USE_CURSOR_LIBRARY);
100 	REGISTER_PDO_CLASS_CONST_LONG("ODBC_ATTR_ASSUME_UTF8", PDO_ODBC_ATTR_ASSUME_UTF8);
101 	REGISTER_PDO_CLASS_CONST_LONG("ODBC_SQL_USE_IF_NEEDED", SQL_CUR_USE_IF_NEEDED);
102 	REGISTER_PDO_CLASS_CONST_LONG("ODBC_SQL_USE_DRIVER", SQL_CUR_USE_DRIVER);
103 	REGISTER_PDO_CLASS_CONST_LONG("ODBC_SQL_USE_ODBC", SQL_CUR_USE_ODBC);
104 
105 	return SUCCESS;
106 }
107 /* }}} */
108 
109 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(pdo_odbc)110 PHP_MSHUTDOWN_FUNCTION(pdo_odbc)
111 {
112 	php_pdo_unregister_driver(&pdo_odbc_driver);
113 	return SUCCESS;
114 }
115 /* }}} */
116 
117 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(pdo_odbc)118 PHP_MINFO_FUNCTION(pdo_odbc)
119 {
120 	php_info_print_table_start();
121 	php_info_print_table_header(2, "PDO Driver for ODBC (" PDO_ODBC_TYPE ")" , "enabled");
122 #ifdef SQL_ATTR_CONNECTION_POOLING
123 	php_info_print_table_row(2, "ODBC Connection Pooling",	pdo_odbc_pool_on == SQL_CP_OFF ?
124 			"Disabled" : (pdo_odbc_pool_mode == SQL_CP_STRICT_MATCH ? "Enabled, strict matching" : "Enabled, relaxed matching"));
125 #else
126 	php_info_print_table_row(2, "ODBC Connection Pooling", "Not supported in this build");
127 #endif
128 	php_info_print_table_end();
129 }
130 /* }}} */
131