xref: /PHP-5.3/ext/pdo_dblib/pdo_dblib.c (revision a2045ff3)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2013 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 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_01.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   |         Frank M. Kromann <frank@kromann.info>                        |
17   +----------------------------------------------------------------------+
18 */
19 
20 /* $Id$ */
21 
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25 
26 #include "php.h"
27 #include "php_ini.h"
28 #include "ext/standard/info.h"
29 #include "pdo/php_pdo.h"
30 #include "pdo/php_pdo_driver.h"
31 #include "php_pdo_dblib.h"
32 #include "php_pdo_dblib_int.h"
33 #include "zend_exceptions.h"
34 
35 ZEND_DECLARE_MODULE_GLOBALS(dblib)
36 static PHP_GINIT_FUNCTION(dblib);
37 
38 const zend_function_entry pdo_dblib_functions[] = {
39 	PHP_FE_END
40 };
41 
42 #if ZEND_MODULE_API_NO >= 20050922
43 static const zend_module_dep pdo_dblib_deps[] = {
44 	ZEND_MOD_REQUIRED("pdo")
45 	ZEND_MOD_END
46 };
47 #endif
48 
49 #if PDO_DBLIB_IS_MSSQL
50 zend_module_entry pdo_mssql_module_entry = {
51 #else
52 zend_module_entry pdo_dblib_module_entry = {
53 #endif
54 #if ZEND_MODULE_API_NO >= 20050922
55 	STANDARD_MODULE_HEADER_EX, NULL,
56 	pdo_dblib_deps,
57 #else
58 	STANDARD_MODULE_HEADER,
59 #endif
60 #if PDO_DBLIB_IS_MSSQL
61 	"pdo_mssql",
62 #elif defined(PHP_WIN32)
63 	"pdo_sybase",
64 #else
65 	"pdo_dblib",
66 #endif
67 	pdo_dblib_functions,
68 	PHP_MINIT(pdo_dblib),
69 	PHP_MSHUTDOWN(pdo_dblib),
70 	NULL,
71 	PHP_RSHUTDOWN(pdo_dblib),
72 	PHP_MINFO(pdo_dblib),
73 	"1.0.1",
74 	PHP_MODULE_GLOBALS(dblib),
75 	PHP_GINIT(dblib),
76 	NULL,
77 	NULL,
78 	STANDARD_MODULE_PROPERTIES_EX
79 };
80 
81 #if defined(COMPILE_DL_PDO_DBLIB) || defined(COMPILE_DL_PDO_MSSQL)
82 #if PDO_DBLIB_IS_MSSQL
ZEND_GET_MODULE(pdo_mssql)83 ZEND_GET_MODULE(pdo_mssql)
84 #else
85 ZEND_GET_MODULE(pdo_dblib)
86 #endif
87 #endif
88 
89 int error_handler(DBPROCESS *dbproc, int severity, int dberr,
90 	int oserr, char *dberrstr, char *oserrstr)
91 {
92 	pdo_dblib_err *einfo;
93 	char *state = "HY000";
94 	TSRMLS_FETCH();
95 
96 	einfo = (pdo_dblib_err*)dbgetuserdata(dbproc);
97 	if (!einfo) einfo = &DBLIB_G(err);
98 
99 	einfo->severity = severity;
100 	einfo->oserr = oserr;
101 	einfo->dberr = dberr;
102 	if (einfo->oserrstr) {
103 		efree(einfo->oserrstr);
104 	}
105 	if (einfo->dberrstr) {
106 		efree(einfo->dberrstr);
107 	}
108 	if (oserrstr) {
109 		einfo->oserrstr = estrdup(oserrstr);
110 	} else {
111 		einfo->oserrstr = NULL;
112 	}
113 	if (dberrstr) {
114 		einfo->dberrstr = estrdup(dberrstr);
115 	} else {
116 		einfo->dberrstr = NULL;
117 	}
118 
119 	switch (dberr) {
120 		case SYBESEOF:
121 		case SYBEFCON:	state = "01002"; break;
122 		case SYBEMEM:	state = "HY001"; break;
123 		case SYBEPWD:	state = "28000"; break;
124 	}
125 	strcpy(einfo->sqlstate, state);
126 
127 #if 0
128 	php_error_docref(NULL TSRMLS_CC, E_WARNING,
129 		"dblib error: %d %s (severity %d)",
130 		dberr, dberrstr, severity);
131 #endif
132 
133 	return INT_CANCEL;
134 }
135 
msg_handler(DBPROCESS * dbproc,DBINT msgno,int msgstate,int severity,char * msgtext,char * srvname,char * procname,DBUSMALLINT line)136 int msg_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate,
137 	int severity, char *msgtext, char *srvname, char *procname, DBUSMALLINT line)
138 {
139 	pdo_dblib_err *einfo;
140 	TSRMLS_FETCH();
141 
142 	if (severity) {
143 		einfo = (pdo_dblib_err*)dbgetuserdata(dbproc);
144 		if (!einfo) {
145 			einfo = &DBLIB_G(err);
146 		}
147 
148 		if (einfo->lastmsg) {
149 			efree(einfo->lastmsg);
150 		}
151 
152 		einfo->lastmsg = estrdup(msgtext);
153 	}
154 
155 #if 0
156 	php_error_docref(NULL TSRMLS_CC, E_WARNING, "dblib message: %s (severity %d)", msgtext, severity);
157 #endif
158 
159 	return 0;
160 }
161 
PHP_GINIT_FUNCTION(dblib)162 static PHP_GINIT_FUNCTION(dblib)
163 {
164 	memset(dblib_globals, 0, sizeof(*dblib_globals));
165 	dblib_globals->err.sqlstate = dblib_globals->sqlstate;
166 }
167 
PHP_RSHUTDOWN_FUNCTION(pdo_dblib)168 PHP_RSHUTDOWN_FUNCTION(pdo_dblib)
169 {
170 	if (DBLIB_G(err).oserrstr) {
171 		efree(DBLIB_G(err).oserrstr);
172 		DBLIB_G(err).oserrstr = NULL;
173 	}
174 	if (DBLIB_G(err).dberrstr) {
175 		efree(DBLIB_G(err).dberrstr);
176 		DBLIB_G(err).dberrstr = NULL;
177 	}
178 	if (DBLIB_G(err).lastmsg) {
179 		efree(DBLIB_G(err).lastmsg);
180 		DBLIB_G(err).lastmsg = NULL;
181 	}
182 	return SUCCESS;
183 }
184 
PHP_MINIT_FUNCTION(pdo_dblib)185 PHP_MINIT_FUNCTION(pdo_dblib)
186 {
187 	if (FAIL == dbinit()) {
188 		return FAILURE;
189 	}
190 
191 	if (FAILURE == php_pdo_register_driver(&pdo_dblib_driver)) {
192 		return FAILURE;
193 	}
194 
195 	/* TODO:
196 
197 	dbsetifile()
198 	dbsetmaxprocs()
199 	dbsetlogintime()
200 	dbsettime()
201 
202 	 */
203 
204 #if !PHP_DBLIB_IS_MSSQL
205 	dberrhandle(error_handler);
206 	dbmsghandle(msg_handler);
207 #endif
208 
209 	return SUCCESS;
210 }
211 
PHP_MSHUTDOWN_FUNCTION(pdo_dblib)212 PHP_MSHUTDOWN_FUNCTION(pdo_dblib)
213 {
214 	php_pdo_unregister_driver(&pdo_dblib_driver);
215 	dbexit();
216 	return SUCCESS;
217 }
218 
PHP_MINFO_FUNCTION(pdo_dblib)219 PHP_MINFO_FUNCTION(pdo_dblib)
220 {
221 	php_info_print_table_start();
222 	php_info_print_table_header(2, "PDO Driver for "
223 #if PDO_DBLIB_IS_MSSQL
224 		"MSSQL"
225 #elif defined(PHP_WIN32)
226 		"FreeTDS/Sybase/MSSQL"
227 #else
228 		"FreeTDS/Sybase"
229 #endif
230 		" DB-lib", "enabled");
231 	php_info_print_table_row(2, "Flavour", PDO_DBLIB_FLAVOUR);
232 	php_info_print_table_end();
233 }
234 
235