xref: /PHP-8.4/ext/pdo_odbc/pdo_odbc.c (revision 11accb5c)
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 "ext/pdo/php_pdo.h"
25 #include "ext/pdo/php_pdo_driver.h"
26 #include "php_pdo_odbc.h"
27 #include "php_pdo_odbc_int.h"
28 #include "pdo_odbc_arginfo.h"
29 
30 static zend_class_entry *pdo_odbc_ce;
31 
32 /* {{{ pdo_odbc_deps[] */
33 static const zend_module_dep pdo_odbc_deps[] = {
34 	ZEND_MOD_REQUIRED("pdo")
35 	ZEND_MOD_END
36 };
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 	NULL,
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 
55 #ifdef COMPILE_DL_PDO_ODBC
56 ZEND_GET_MODULE(pdo_odbc)
57 #endif
58 
59 #ifdef SQL_ATTR_CONNECTION_POOLING
60 zend_ulong pdo_odbc_pool_on = SQL_CP_OFF;
61 zend_ulong pdo_odbc_pool_mode = SQL_CP_ONE_PER_HENV;
62 #endif
63 
64 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(pdo_odbc)65 PHP_MINIT_FUNCTION(pdo_odbc)
66 {
67 #ifdef SQL_ATTR_CONNECTION_POOLING
68 	char *pooling_val = NULL;
69 #endif
70 
71 	if (FAILURE == php_pdo_register_driver(&pdo_odbc_driver)) {
72 		return FAILURE;
73 	}
74 
75 #ifdef SQL_ATTR_CONNECTION_POOLING
76 	/* ugh, we don't really like .ini stuff in PDO, but since ODBC connection
77 	 * pooling is process wide, we can't set it from within the scope of a
78 	 * request without affecting others, which goes against our isolated request
79 	 * policy.  So, we use cfg_get_string here to check it this once.
80 	 * */
81 	if (FAILURE == cfg_get_string("pdo_odbc.connection_pooling", &pooling_val) || pooling_val == NULL) {
82 		pooling_val = "strict";
83 	}
84 	if (strcasecmp(pooling_val, "strict") == 0 || strcmp(pooling_val, "1") == 0) {
85 		pdo_odbc_pool_on = SQL_CP_ONE_PER_HENV;
86 		pdo_odbc_pool_mode = SQL_CP_STRICT_MATCH;
87 	} else if (strcasecmp(pooling_val, "relaxed") == 0) {
88 		pdo_odbc_pool_on = SQL_CP_ONE_PER_HENV;
89 		pdo_odbc_pool_mode = SQL_CP_RELAXED_MATCH;
90 	} else if (*pooling_val == '\0' || strcasecmp(pooling_val, "off") == 0) {
91 		pdo_odbc_pool_on = SQL_CP_OFF;
92 	} else {
93 		php_error_docref(NULL, E_CORE_ERROR, "Error in pdo_odbc.connection_pooling configuration. Value must be one of \"strict\", \"relaxed\", or \"off\"");
94 		return FAILURE;
95 	}
96 
97 	if (pdo_odbc_pool_on != SQL_CP_OFF) {
98 		SQLSetEnvAttr(SQL_NULL_HANDLE, SQL_ATTR_CONNECTION_POOLING, (void*)pdo_odbc_pool_on, 0);
99 	}
100 #endif
101 
102 	register_pdo_odbc_symbols(module_number);
103 
104 	REGISTER_PDO_CLASS_CONST_LONG("ODBC_ATTR_USE_CURSOR_LIBRARY", PDO_ODBC_ATTR_USE_CURSOR_LIBRARY);
105 	REGISTER_PDO_CLASS_CONST_LONG("ODBC_ATTR_ASSUME_UTF8", PDO_ODBC_ATTR_ASSUME_UTF8);
106 	REGISTER_PDO_CLASS_CONST_LONG("ODBC_SQL_USE_IF_NEEDED", SQL_CUR_USE_IF_NEEDED);
107 	REGISTER_PDO_CLASS_CONST_LONG("ODBC_SQL_USE_DRIVER", SQL_CUR_USE_DRIVER);
108 	REGISTER_PDO_CLASS_CONST_LONG("ODBC_SQL_USE_ODBC", SQL_CUR_USE_ODBC);
109 
110 	pdo_odbc_ce = register_class_Pdo_Odbc(pdo_dbh_ce);
111 	pdo_odbc_ce->create_object = pdo_dbh_new;
112 
113 	return php_pdo_register_driver_specific_ce(&pdo_odbc_driver, pdo_odbc_ce);
114 }
115 /* }}} */
116 
117 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(pdo_odbc)118 PHP_MSHUTDOWN_FUNCTION(pdo_odbc)
119 {
120 	php_pdo_unregister_driver(&pdo_odbc_driver);
121 	return SUCCESS;
122 }
123 /* }}} */
124 
125 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(pdo_odbc)126 PHP_MINFO_FUNCTION(pdo_odbc)
127 {
128 	php_info_print_table_start();
129 	php_info_print_table_row(2, "PDO Driver for ODBC (" PDO_ODBC_TYPE ")" , "enabled");
130 #ifdef SQL_ATTR_CONNECTION_POOLING
131 	php_info_print_table_row(2, "ODBC Connection Pooling",	pdo_odbc_pool_on == SQL_CP_OFF ?
132 			"Disabled" : (pdo_odbc_pool_mode == SQL_CP_STRICT_MATCH ? "Enabled, strict matching" : "Enabled, relaxed matching"));
133 #else
134 	php_info_print_table_row(2, "ODBC Connection Pooling", "Not supported in this build");
135 #endif
136 	php_info_print_table_end();
137 }
138 /* }}} */
139