DCL 3.7.4
Loading...
Searching...
No Matches
SQLException.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2
3#ifdef __WINNT__
4#include <windows.h>
5#endif
6
7#include <dcl/Object.h>
8
9#if __DCL_HAVE_ALLOC_DEBUG
10#undef __DCL_ALLOC_LEVEL
11#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
12#endif
13
14#include <dcl/Charset.h>
15#include <dcl/Thread.h>
16#include <dcl/SQL.h>
17
18#if __DCL_HAVE_THIS_FILE__
19#undef __THIS_FILE__
20static const char_t __THIS_FILE__[] = __T("dcl/SQLException.cpp");
21#endif
22
23__DCL_BEGIN_NAMESPACE
24
26
28{
29 StringBuilder r;
30#ifdef __DCL_DEBUG
31 if (_connHandle->errorFileName()) {
32 r = _connHandle->errorFileName();
33 r += __T(":");
34 r += Int32::toString(_connHandle->errorLine(), 10) + __T(": ");
35 }
36#endif
37
38 if (_connHandle->errorCode() == SQL::eServerError) {
39 r += _connHandle->serverTitle();
40 r += __T(": ");
41
42 ByteString s;
43 size_t n = 1024;
44 ByteBuffer* buf = ByteBuffer::create(n);
45 bool b = _connHandle->getErrorMessage(buf->data(), &n);
46 if (b) {
47 buf->__dataLength = n;
48 s = buf;
49 }
50 buf->release();
51
52 if (s.length() > 0) {
53 try {
54 r += UTF8Decoder::decode(s);
55 }
56 catch (CharsetConvertException* _e) {
57 __DCL_TRACE1(L"Warning!! getServerMessage [%ls]\n", _e->toString().data());
58 _e->destroy();
59 }
60 }
61 else {
62 r += __T("The Server Error Message is unavailable!!");
63 }
64 }
65 else {
66 r += __T("DCL: ");
67 size_t n = 256;
68 CharBuffer* buf = CharBuffer::create_e(n);
69 if (!SQL::getErrorMessage(buf->data(), &n, _connHandle->errorCode())) {
70 if (buf->__allocLength < n) {
71 CharBuffer::extend(buf, n);
72 }
73 SQL::getErrorMessage(buf->data(), &n, _connHandle->errorCode());
74 }
75 r.append(buf->data(), n);
76 buf->release();
77 }
78 return r;
79}
80
83{
84 __DCL_ASSERT(_conn && _conn->handle());
85 __message = _cause ? _cause->toString() :
86 getServerMessage(_conn->handle());
87}
88
89SQLException::SQLException(SQLConnection* _conn, const String& _message)
91{
92 __DCL_ASSERT(_conn && _conn->handle());
93 __message = getServerMessage(_conn->handle()) + __T(": ");
94 __message = __message + _message;
95}
96
98 : Exception(_cause)
99{
100 __DCL_ASSERT(_query && _query->handle()->connection());
101 __message = _cause ? _cause->toString()
102 : getServerMessage(_query->connection()->handle());
103
104}
105
106SQLException::SQLException(SQLQuery* _query, const String& _message)
107 : Exception(NULL)
108{
109 __DCL_ASSERT(_query && _query->handle()->connection());
110 __message = getServerMessage(_query->connection()->handle()) + __T(": ");
111 __message = __message + _message;
112}
113
115 : Exception(_cause)
116{
117 __DCL_ASSERT(_field && _field->handle()->connection());
118 StringBuilder sb;
119 sb += _cause ? _cause->toString()
120 : getServerMessage(_field->handle()->connection());
121 sb += __T(": ");
122 sb += _field->name();
123 sb += __T("(");
124 sb += _field->dataTypeName();
125 sb += __T(",");
126 sb += _field->handle()->serverDataTypeName();
127 sb += __T(")");
128 __message = sb.toString();
129}
130
132 SQLField* _field,
133 const wchar_t* _cast,
134 Exception* _cause
135) : Exception(_cause)
136{
137 StringBuilder sb = __T("SQLData access type error: ");
138 sb += _field->name();
139 sb += __T("(");
140 sb += _field->dataTypeName();
141 sb += __T("), access: ");
142 sb += _cast;
143 __message = sb.toString();
144}
145
147 SQLField* _field,
148 const wchar_t* _cast,
149 AsErrorCode _errorCode
150) : Exception(NULL)
151{
152 StringBuilder sb = __T("SQLData access type error: ");
153 sb += _field->name();
154 sb += __T("(");
155 sb += _field->dataTypeName();
156 sb += __T("), access: ");
157 sb += _cast;
158 switch(_errorCode) {
159 case eInvalidCast: {
160 sb += __T(": Data convert impossible");
161 break;
162 }
163 case eOutOfRange: {
164 sb += __T(": Numeric convert out of range");
165 break;
166 }
167 default: {
168 __DCL_ASSERT(false);
169 }
170 }
171 __message = sb.toString();
172}
173
175{
176 return __message;
177}
178
179__DCL_END_NAMESPACE
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:312
wchar_t char_t
Definition Config.h:247
IOException *size_t r
Definition MediaInfo.cpp:82
#define __DCL_TRACE1(fmt, arg1)
Definition Object.h:399
#define __DCL_ASSERT(expr)
Definition Object.h:394
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
Definition Object.h:245
#define __T(str)
Definition Object.h:60
virtual String toString() const
Definition Exception.cpp:40
virtual void destroy()
Definition Exception.cpp:74
Exception(Exception *_cause=NULL)
String toString(unsigned _base=10) const
Definition Numeric.inl:93
const wchar_t * serverTitle() const
Definition SQLCore.inl:94
bool getErrorMessage(char *_buf, size_t *_buflen)
Definition SQLCore.cpp:796
Error errorCode() const
Definition SQLCore.inl:72
SQL::Connection * handle() const
Definition SQL.inl:122
String getServerMessage(SQL::Connection *_connHandle)
virtual String toString() const
SQLException(SQLConnection *_conn, Exception *_cause)
Definition SQL.h:48
@ eServerError
Definition SQLCore.h:21
static bool getErrorMessage(wchar_t *_buf, size_t *_buflen, Error _errorCode)
Definition SQLCore.cpp:38
SQLConnection * connection() const
Definition SQL.inl:116
SQL::Query * handle() const
Definition SQL.inl:111