]> git.stg.codes - stg.git/blob - stglibs/ibpp.lib/exception.cpp
Merge branch 'stg-2.409-radius'
[stg.git] / stglibs / ibpp.lib / exception.cpp
1 ///////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //      File    : $Id: exception.cpp,v 1.1 2007/05/05 17:00:42 faust Exp $\r
4 //      Subject : IBPP, Initialization of the library\r
5 //\r
6 ///////////////////////////////////////////////////////////////////////////////\r
7 //\r
8 //      (C) Copyright 2000-2006 T.I.P. Group S.A. and the IBPP Team (www.ibpp.org)\r
9 //\r
10 //      The contents of this file are subject to the IBPP License (the "License");\r
11 //      you may not use this file except in compliance with the License.  You may\r
12 //      obtain a copy of the License at http://www.ibpp.org or in the 'license.txt'\r
13 //      file which must have been distributed along with this file.\r
14 //\r
15 //      This software, distributed under the License, is distributed on an "AS IS"\r
16 //      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the\r
17 //      License for the specific language governing rights and limitations\r
18 //      under the License.\r
19 //\r
20 ///////////////////////////////////////////////////////////////////////////////\r
21 //\r
22 //      COMMENTS\r
23 //      * Tabulations should be set every four characters when editing this file.\r
24 //\r
25 ///////////////////////////////////////////////////////////////////////////////\r
26 \r
27 #ifdef _MSC_VER\r
28 #pragma warning(disable: 4786 4996)\r
29 #ifndef _DEBUG\r
30 #pragma warning(disable: 4702)\r
31 #endif\r
32 #endif\r
33 \r
34 #include "_ibpp.h"\r
35 \r
36 #ifdef HAS_HDRSTOP\r
37 #pragma hdrstop\r
38 #endif\r
39 \r
40 #include <stdarg.h>\r
41 #include <stdio.h>\r
42 \r
43 using namespace ibpp_internals;\r
44 \r
45 // None of the exception classes methods are implemented inline, because they\r
46 // are all declared throw() and Borland compilers at least, but possibly some\r
47 // others emit a warning like "W8026 - functions with exception specification\r
48 // are not expanded inline". Nothing we have to worry about, but we don't want\r
49 // people concerned by such warnings.\r
50 \r
51 IBPP::Exception::~Exception() throw()\r
52 {\r
53 }\r
54 \r
55 IBPP::LogicException::~LogicException() throw()\r
56 {\r
57 }\r
58 \r
59 IBPP::SQLException::~SQLException() throw()\r
60 {\r
61 }\r
62 \r
63 IBPP::WrongType::~WrongType() throw()\r
64 {\r
65 }\r
66 \r
67 //\r
68 //      (((((((( ExceptionBase Implementation ))))))))\r
69 //\r
70 \r
71 void ExceptionBase::buildErrorMessage(const char* message)\r
72 {\r
73         if (! mContext.empty())\r
74                 mWhat.append(_("Context: ")).append(mContext).append("\n");\r
75 \r
76         if (message != 0 && *message != 0 )\r
77                 mWhat.append(_("Message: ")).append(message).append("\n");\r
78         \r
79         mWhat.append("\n");\r
80 }\r
81 \r
82 void ExceptionBase::raise(const std::string& context, const char* message, va_list argptr)\r
83 {\r
84         mContext.assign(context);\r
85 \r
86         if (message != 0)\r
87         {\r
88                 char buffer[1024];\r
89 #if defined(_MSC_VER) || defined(__DMC__)\r
90                 _vsnprintf(buffer, sizeof(buffer)-1, message, argptr);\r
91 #else\r
92                 vsnprintf(buffer, sizeof(buffer)-1, message, argptr);\r
93 #endif\r
94                 buffer[sizeof(buffer)-1] = 0;\r
95         \r
96                 buildErrorMessage(buffer);\r
97         }\r
98         else\r
99                 buildErrorMessage(0);\r
100 }\r
101 \r
102 ExceptionBase::ExceptionBase() throw()\r
103 {\r
104 }\r
105 \r
106 ExceptionBase::ExceptionBase(const ExceptionBase& copied) throw()\r
107 {\r
108         mContext = copied.mContext;\r
109         mWhat = copied.mWhat;\r
110 }\r
111 \r
112 ExceptionBase& ExceptionBase::operator=(const ExceptionBase& copied) throw()\r
113 {\r
114         mContext = copied.mContext;\r
115         mWhat = copied.mWhat;\r
116         return *this;\r
117 }\r
118 \r
119 ExceptionBase::ExceptionBase(const std::string& context,\r
120                                                                 const char* message, ...) throw()\r
121 {\r
122         va_list argptr;\r
123         va_start(argptr, message);\r
124         mWhat.assign("*** IBPP::Exception ***\n");\r
125         raise(context, message, argptr);\r
126         va_end(argptr);\r
127 }\r
128 \r
129 ExceptionBase::~ExceptionBase() throw()\r
130 {\r
131 }\r
132 \r
133 const char* ExceptionBase::Origin() const throw()\r
134 {\r
135         return mContext.c_str();\r
136 }\r
137 \r
138 const char* ExceptionBase::ErrorMessage() const throw()\r
139 {\r
140         return mWhat.c_str();\r
141 }\r
142 \r
143 const char* ExceptionBase::what() const throw()\r
144 {\r
145         return mWhat.c_str();\r
146 }\r
147 \r
148 //      (((((((( LogicExceptionImpl Implementation ))))))))\r
149 \r
150 // The following constructors are small and could be inlined, but for object\r
151 // code compacity of the library it is much better to have them non-inlined.\r
152 // The amount of code generated by compilers for a throw is well-enough.\r
153 \r
154 LogicExceptionImpl::LogicExceptionImpl() throw()\r
155         : ExceptionBase()\r
156 {\r
157 }\r
158 \r
159 LogicExceptionImpl::LogicExceptionImpl(const LogicExceptionImpl& copied) throw()\r
160         : IBPP::LogicException(), ExceptionBase(copied)\r
161 {\r
162 }\r
163 \r
164 LogicExceptionImpl& LogicExceptionImpl::operator=(const LogicExceptionImpl& copied) throw()\r
165 {\r
166         ExceptionBase::operator=(copied);\r
167         return *this;\r
168 }\r
169 \r
170 LogicExceptionImpl::LogicExceptionImpl(const std::string& context,\r
171                                                                                 const char* message, ...) throw()\r
172 {\r
173         va_list argptr;\r
174         va_start(argptr, message);\r
175         mWhat.assign("*** IBPP::LogicException ***\n");\r
176         raise(context, message, argptr);\r
177         va_end(argptr);\r
178 }\r
179 \r
180 LogicExceptionImpl::~LogicExceptionImpl() throw ()\r
181 {\r
182 }\r
183 \r
184 const char* LogicExceptionImpl::Origin() const throw()\r
185 {\r
186         return ExceptionBase::Origin();\r
187 }\r
188 \r
189 const char* LogicExceptionImpl::ErrorMessage() const throw()\r
190 {\r
191         return ExceptionBase::what();\r
192 }\r
193 \r
194 const char* LogicExceptionImpl::what() const throw()\r
195 {\r
196         return ExceptionBase::what();\r
197 }\r
198 \r
199 //      (((((((( SQLExceptionImpl Implementation ))))))))\r
200 \r
201 SQLExceptionImpl::SQLExceptionImpl() throw()\r
202         : ExceptionBase(), mSqlCode(0), mEngineCode(0)\r
203 {\r
204 }\r
205 \r
206 SQLExceptionImpl::SQLExceptionImpl(const SQLExceptionImpl& copied) throw()\r
207         : IBPP::SQLException(), ExceptionBase(copied), mSqlCode(copied.mSqlCode),\r
208                 mEngineCode(copied.mEngineCode)\r
209 {\r
210 }\r
211 \r
212 SQLExceptionImpl& SQLExceptionImpl::operator=(const SQLExceptionImpl& copied) throw()\r
213 {\r
214         ExceptionBase::operator=(copied);\r
215         mSqlCode = copied.mSqlCode;\r
216         mEngineCode = copied.mEngineCode;\r
217         return *this;\r
218 }\r
219 \r
220 SQLExceptionImpl::SQLExceptionImpl(const IBS& status, const std::string& context,\r
221                                                                         const char* message, ...) throw()\r
222 {\r
223         va_list argptr;\r
224         va_start(argptr, message);\r
225         mWhat.assign("*** IBPP::SQLException ***\n");\r
226         raise(context, message, argptr);\r
227         va_end(argptr);\r
228         mSqlCode = status.SqlCode();\r
229         mEngineCode = status.EngineCode();\r
230         mWhat.append(status.ErrorMessage());\r
231 }\r
232 \r
233 SQLExceptionImpl::~SQLExceptionImpl() throw ()\r
234 {\r
235 }\r
236 \r
237 const char* SQLExceptionImpl::Origin() const throw()\r
238 {\r
239         return ExceptionBase::Origin();\r
240 }\r
241 \r
242 const char* SQLExceptionImpl::ErrorMessage() const throw()\r
243 {\r
244         return ExceptionBase::what();\r
245 }\r
246 \r
247 const char* SQLExceptionImpl::what() const throw()\r
248 {\r
249         return ExceptionBase::what();\r
250 }\r
251 \r
252 int SQLExceptionImpl::SqlCode() const throw()\r
253 {\r
254         return mSqlCode;\r
255 }\r
256 \r
257 int SQLExceptionImpl::EngineCode() const throw()\r
258 {\r
259         return mEngineCode;\r
260 }\r
261 \r
262 //      (((((((( WrongTypeImpl Implementation ))))))))\r
263 \r
264 // The following constructors are small and could be inlined, but for object\r
265 // code compacity of the library it is much better to have them non-inlined.\r
266 // The amount of code generated by compilers for a throw is well-enough.\r
267 \r
268 WrongTypeImpl::WrongTypeImpl() throw()\r
269         : IBPP::WrongType(), ExceptionBase()\r
270 {\r
271 }\r
272 \r
273 WrongTypeImpl::WrongTypeImpl(const WrongTypeImpl& copied) throw()\r
274         : IBPP::WrongType(), ExceptionBase(copied)\r
275 {\r
276 }\r
277 \r
278 WrongTypeImpl& WrongTypeImpl::operator=(const WrongTypeImpl& copied) throw()\r
279 {\r
280         ExceptionBase::operator=(copied);\r
281         return *this;\r
282 }\r
283 \r
284 WrongTypeImpl::WrongTypeImpl(const std::string& context, int sqlType, IITYPE varType,\r
285                                 const char* message, ...) throw()\r
286 {\r
287         va_list argptr;\r
288         va_start(argptr, message);\r
289         mWhat.assign("*** IBPP::WrongType ***\n");\r
290         raise(context, message, argptr);\r
291         va_end(argptr);\r
292 \r
293         std::string info;\r
294         switch (sqlType & ~1)\r
295         {\r
296                 case SQL_TEXT :                 info.append("CHAR"); break;\r
297                 case SQL_VARYING :              info.append("VARCHAR"); break;\r
298                 case SQL_SHORT :                info.append("SMALLINT"); break;\r
299                 case SQL_LONG :                 info.append("INTEGER"); break;\r
300                 case SQL_INT64 :                info.append("BIGINT"); break;\r
301                 case SQL_FLOAT :                info.append("FLOAT"); break;\r
302                 case SQL_DOUBLE :               info.append("DOUBLE"); break;\r
303                 case SQL_TIMESTAMP :    info.append("TIMESTAMP"); break;\r
304                 case SQL_TYPE_DATE :    info.append("DATE"); break;\r
305                 case SQL_TYPE_TIME :    info.append("TIME"); break;\r
306                 case SQL_BLOB :                 info.append("BLOB"); break;\r
307                 case SQL_ARRAY :                info.append("ARRAY"); break;\r
308         }\r
309         info.append(" ").append(_(" and ")).append(" ");\r
310         switch (varType)\r
311         {\r
312                 case ivArray :          info.append("Array"); break;\r
313                 case ivBlob :           info.append("Blob"); break;\r
314                 case ivDate :           info.append("Date"); break;\r
315                 case ivTime :           info.append("Time"); break;\r
316                 case ivTimestamp :      info.append("Timestamp"); break;\r
317                 case ivString :         info.append("std::string"); break;\r
318                 case ivInt16 :          info.append("int16_t"); break;\r
319                 case ivInt32 :          info.append("int32_t"); break;\r
320                 case ivInt64 :          info.append("int64_t"); break;\r
321                 case ivFloat :          info.append("float"); break;\r
322                 case ivDouble :         info.append("double"); break;\r
323                 case ivBool :           info.append("bool"); break;\r
324                 case ivDBKey :          info.append("DBKey"); break;\r
325                 case ivByte :           info.append("int8_t"); break;\r
326         }\r
327         mWhat.append(info).append("\n");\r
328 }\r
329 \r
330 WrongTypeImpl::~WrongTypeImpl() throw ()\r
331 {\r
332 }\r
333 \r
334 const char* WrongTypeImpl::Origin() const throw()\r
335 {\r
336         return ExceptionBase::Origin();\r
337 }\r
338 \r
339 const char* WrongTypeImpl::ErrorMessage() const throw()\r
340 {\r
341         return ExceptionBase::what();\r
342 }\r
343 \r
344 const char* WrongTypeImpl::what() const throw()\r
345 {\r
346         return ExceptionBase::what();\r
347 }\r
348 \r
349 //\r
350 //      EOF\r
351 //\r