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