1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Brian Bruns
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Library General Public
6 : * License as published by the Free Software Foundation; either
7 : * version 2 of the License, or (at your option) any later version.
8 : *
9 : * This library is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : * Library General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Library General Public
15 : * License along with this library; if not, write to the
16 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 : * Boston, MA 02111-1307, USA.
18 : */
19 :
20 : #if HAVE_CONFIG_H
21 : #include <config.h>
22 : #endif /* HAVE_CONFIG_H */
23 :
24 : #include <stdio.h>
25 :
26 : #if HAVE_STDLIB_H
27 : #include <stdlib.h>
28 : #endif /* HAVE_STDLIB_H */
29 :
30 : #include "tds.h"
31 : #include "sybdb.h"
32 : #include "syberror.h"
33 : #include "dblib.h"
34 : /* #include "fortify.h" */
35 :
36 : #ifdef DMALLOC
37 : #include <dmalloc.h>
38 : #endif
39 :
40 : TDS_RCSID(var, "$Id: dbutil.c,v 1.33 2005/07/17 21:58:19 jklowden Exp $");
41 :
42 : /*
43 : * test include consistency
44 : * I don't think all compiler are able to compile this code... if not comment it
45 : */
46 : #if ENABLE_EXTRA_CHECKS
47 :
48 : #if defined(__GNUC__) && __GNUC__ >= 2
49 : #define COMPILE_CHECK(name,check) \
50 : extern int name[(check)?1:-1] __attribute__ ((unused))
51 : #else
52 : #define COMPILE_CHECK(name,check) \
53 : extern int name[(check)?1:-1]
54 : #endif
55 :
56 : /* TODO test SYBxxx consistency */
57 :
58 : #define TEST_ATTRIBUTE(t,sa,fa,sb,fb) \
59 : COMPILE_CHECK(t,sizeof(((sa*)0)->fa) == sizeof(((sb*)0)->fb) && (int)(&((sa*)0)->fa) == (int)(&((sb*)0)->fb))
60 :
61 : TEST_ATTRIBUTE(t21,TDS_MONEY4,mny4,DBMONEY4,mny4);
62 : TEST_ATTRIBUTE(t22,TDS_OLD_MONEY,mnyhigh,DBMONEY,mnyhigh);
63 : TEST_ATTRIBUTE(t23,TDS_OLD_MONEY,mnylow,DBMONEY,mnylow);
64 : TEST_ATTRIBUTE(t24,TDS_DATETIME,dtdays,DBDATETIME,dtdays);
65 : TEST_ATTRIBUTE(t25,TDS_DATETIME,dttime,DBDATETIME,dttime);
66 : TEST_ATTRIBUTE(t26,TDS_DATETIME4,days,DBDATETIME4,days);
67 : TEST_ATTRIBUTE(t27,TDS_DATETIME4,minutes,DBDATETIME4,minutes);
68 : TEST_ATTRIBUTE(t28,TDS_NUMERIC,precision,DBNUMERIC,precision);
69 : TEST_ATTRIBUTE(t29,TDS_NUMERIC,scale,DBNUMERIC,scale);
70 : TEST_ATTRIBUTE(t30,TDS_NUMERIC,array,DBNUMERIC,array);
71 : #endif
72 :
73 : /*
74 : * The next 2 functions receive the info and error messages that come from the TDS layer.
75 : * The address of this function is passed to the TDS layer in dbinit().
76 : * It takes a pointer to a DBPROCESS, it's just that the TDS layer didn't
77 : * know what it really was.
78 : */
79 : int
80 : _dblib_handle_info_message(const TDSCONTEXT * tds_ctx, TDSSOCKET * tds, TDSMESSAGE * msg)
81 228 : {
82 228 : DBPROCESS *dbproc = (tds && tds->parent)? (DBPROCESS *) tds->parent : NULL;
83 :
84 : /*
85 : * Check to see if the user supplied a function, else ignore the message.
86 : */
87 228 : if (_dblib_msg_handler) {
88 221 : _dblib_msg_handler(dbproc,
89 : msg->msgno,
90 : msg->state,
91 : msg->severity, msg->message, msg->server, msg->proc_name, msg->line_number);
92 : }
93 :
94 228 : if (msg->severity > 10) {
95 : /*
96 : * Sybase docs say SYBESMSG is generated only in specific
97 : * cases (severity greater than 16, or deadlock occurred, or
98 : * a syntax error occurred.) However, actual observed
99 : * behavior is that SYBESMSG is always generated for
100 : * server messages with severity greater than 10.
101 : */
102 41 : tds_client_msg(tds_ctx, tds, SYBESMSG, EXSERVER, -1, -1,
103 : "General SQL Server error: Check messages from the SQL Server.");
104 : }
105 228 : return SUCCEED;
106 : }
107 :
108 : /** \internal
109 : * \dblib_internal
110 : * \brief handle errors generated by libtds
111 : * \param tds_ctx actually a dbproc: contains all information needed by db-lib to manage communications with the server.
112 : * \param tds contains all information needed by libtds to manage communications with the server.
113 : * \param msg the message to send
114 : * \returns
115 : * \remarks This function is called by libtds. It exists to interpret error message numbers coming from libtds,
116 : * and to convert the db-lib error handler's return code into one that the libtds function will know
117 : * (one hopes) how to handle. libtds cannot issue db-lib message numbers because ct-lib and db-lib
118 : * define different error message numbers. (N.B. ODBC actually uses db-lib msgno numbers for its
119 : * driver-specific "server errors". The ct-lib -> db-lib error number conversion has to be public to let
120 : * both libraries perform the conversion.
121 : */
122 : int
123 : _dblib_handle_err_message(const TDSCONTEXT * tds_ctx, TDSSOCKET * tds, TDSMESSAGE * msg)
124 41 : {
125 41 : DBPROCESS *dbproc = NULL;
126 41 : int rc = INT_CANCEL;
127 :
128 41 : if (tds && tds->parent) {
129 41 : dbproc = (DBPROCESS *) tds->parent;
130 : }
131 41 : if (msg->msgno > 0) {
132 : /*
133 : * now check to see if the user supplied a function,
134 : * if not, ignore the problem
135 : */
136 41 : if (_dblib_err_handler) {
137 39 : rc = _dblib_err_handler(dbproc, msg->severity, msg->msgno, msg->state, msg->message, msg->server);
138 : }
139 : }
140 :
141 : /*
142 : * Preprocess the return code to handle INT_TIMEOUT/INT_CONTINUE
143 : * for non-SYBETIME errors in the strange and different ways as
144 : * specified by Sybase and Microsoft.
145 : */
146 41 : if (msg->msgno != SYBETIME) {
147 41 : switch (rc) {
148 : case INT_TIMEOUT:
149 0 : rc = INT_EXIT;
150 0 : break;
151 : case INT_CONTINUE:
152 0 : if (!dbproc || !dbproc->msdblib)
153 : /* Sybase behavior */
154 0 : rc = INT_EXIT;
155 : else
156 : /* Microsoft behavior */
157 0 : rc = INT_CANCEL;
158 : break;
159 : default:
160 : break;
161 : }
162 : }
163 :
164 41 : switch (rc) {
165 : case INT_EXIT:
166 0 : exit(EXIT_FAILURE);
167 : break;
168 : case INT_CANCEL:
169 41 : return SUCCEED;
170 : break;
171 : case INT_TIMEOUT:
172 : /* XXX do something clever */
173 0 : return SUCCEED;
174 : break;
175 : case INT_CONTINUE:
176 : /* XXX do something clever */
177 0 : return SUCCEED;
178 : break;
179 : default:
180 : /* unknown return code from error handler */
181 0 : return FAIL;
182 : break;
183 : }
184 :
185 : /* notreached */
186 : return FAIL;
187 : }
188 :
189 : void
190 : _dblib_setTDS_version(TDSLOGIN * tds_login, DBINT version)
191 0 : {
192 0 : switch (version) {
193 : case DBVERSION_42:
194 0 : tds_set_version(tds_login, 4, 2);
195 0 : break;
196 : case DBVERSION_46:
197 0 : tds_set_version(tds_login, 4, 6);
198 0 : break;
199 : case DBVERSION_100:
200 0 : tds_set_version(tds_login, 5, 0);
201 : break;
202 : }
203 0 : }
|