forked from FirebirdSQL/firebird-odbc-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OdbcObject.cpp
373 lines (298 loc) · 8.83 KB
/
OdbcObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
*
* The Original Code was created by James A. Starkey for IBPhoenix.
*
* Copyright (c) 1999, 2000, 2001 James A. Starkey
* All Rights Reserved.
*/
// OdbcObject.cpp: implementation of the OdbcObject class.
//
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>
#include "OdbcJdbc.h"
#include "OdbcEnv.h"
#include "OdbcConnection.h"
#include "OdbcStatement.h"
#include "OdbcObject.h"
#include "OdbcError.h"
#include "IscDbc/SQLException.h"
namespace OdbcJdbcLibrary {
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
OdbcObject::OdbcObject()
{
next = NULL; // NOMEY
errors = NULL;
infoPosted = false;
sqlDiagCursorRowCount = 0; // SQL_DIAG_CURSOR_ROW_COUNT
sqlDiagDynamicFunction = NULL; // SQL_DIAG_DYNAMIC_FUNCTION
sqlDiagDynamicFunctionCode = 0; // SQL_DIAG_DYNAMIC_FUNCTION_CODE
sqlDiagNumber = 0; // SQL_DIAG_NUMBER
sqlDiagReturnCode = SQL_SUCCESS; // SQL_DIAG_RETURNCODE
sqlDiagRowCount = 0; // SQL_DIAG_ROW_COUNT
}
OdbcObject::~OdbcObject()
{
clearErrors();
}
SQLRETURN OdbcObject::returnStringInfo(SQLPOINTER ptr, SQLSMALLINT maxLength, SQLSMALLINT* returnLength, const char * value)
{
int count = (int)strlen (value);
*returnLength = count;
if ( ptr && maxLength > 0 )
{
--maxLength;
if (count <= maxLength)
{
strcpy ((char*) ptr, value);
return sqlSuccess();
}
memcpy (ptr, value, maxLength);
((char*) ptr) [maxLength] = 0;
*returnLength = maxLength;
}
return sqlReturn (SQL_SUCCESS_WITH_INFO, "01004", "String data, right truncated");
}
SQLRETURN OdbcObject::returnStringInfo(SQLPOINTER ptr, SQLSMALLINT maxLength, SQLINTEGER *returnLength, const char *value)
{
int count = (int)strlen (value);
*returnLength = count;
if ( ptr && maxLength > 0 )
{
--maxLength;
if (count <= maxLength)
{
strcpy ((char*) ptr, value);
return sqlSuccess();
}
memcpy (ptr, value, maxLength);
((char*) ptr) [maxLength] = 0;
*returnLength = maxLength;
}
return sqlReturn (SQL_SUCCESS_WITH_INFO, "01004", "String data, right truncated");
}
int OdbcObject::sqlReturn(int code, const char * state, const char *text, int nativeCode)
{
postError (new OdbcError (nativeCode, state, text));
return code;
}
int OdbcObject::sqlSuccess()
{
if (infoPosted)
return SQL_SUCCESS_WITH_INFO;
return SQL_SUCCESS;
}
SQLRETURN OdbcObject::allocHandle(int handleType, SQLHANDLE * outputHandle)
{
*outputHandle = (SQLHANDLE)NULL;
return sqlReturn (SQL_ERROR, "HY092", "Invalid attribute/option identifier");
}
void OdbcObject::notYetImplemented(const char * msg)
{
}
int OdbcObject::stringLength(const SQLCHAR *string, int givenLength)
{
if (!string)
return 0;
if (givenLength == SQL_NTS)
return (int)strlen ((char*) string);
return givenLength;
}
// Return "true" on overflow
bool OdbcObject::setString(const SQLCHAR * string, int stringLength, SQLCHAR * target, int targetSize, SQLSMALLINT * targetLength)
{
--targetSize;
if (targetLength)
*targetLength = stringLength;
if (target)
{
if (stringLength <= targetSize)
{
memcpy (target, string, stringLength);
target [stringLength] = 0;
return false;
}
memcpy (target, string, targetSize);
target [targetSize] = 0;
postError (new OdbcError (0, "01004", "String data, right truncated"));
}
return true;
}
bool OdbcObject::setString(const char * string, SQLCHAR *target, int targetSize, SQLSMALLINT * targetLength)
{
return setString ((SQLCHAR*) string, (int)strlen (string), target, targetSize, targetLength);
}
bool OdbcObject::appendString(const char * string, int stringLength, SQLCHAR * target, int targetSize, SQLSMALLINT * targetLength)
{
--targetSize;
int length = *targetLength;
*targetLength += stringLength;
int l = targetSize - length;
if (stringLength <= l)
{
memcpy (target + length, string, stringLength);
target [length + stringLength] = 0;
return false;
}
if (l > 0)
memcpy (target, string, targetSize - length);
target [targetSize] = 0;
return true;
}
SQLRETURN OdbcObject::sqlError(UCHAR * stateBuffer, SQLINTEGER * nativeCode, UCHAR * msgBuffer, int msgBufferLength, SWORD * msgLength)
{
OdbcError *error = errors;
if (error)
{
errors = error->next;
SQLRETURN ret = error->sqlGetDiagRec (stateBuffer, nativeCode, msgBuffer, msgBufferLength, msgLength);
delete error;
return ret;
}
strcpy ((char*) stateBuffer, "00000");
msgBuffer [0] = 0;
*msgLength = 0;
return SQL_NO_DATA_FOUND;
}
OdbcError* OdbcObject::postError(OdbcError * error)
{
infoPosted = true;
OdbcError **ptr;
for (ptr = &errors; *ptr; ptr = &(*ptr)->next)
;
error->next = NULL;
*ptr = error;
error->connection = getConnection();
return error;
}
void OdbcObject::operator <<(OdbcObject * obj)
{
for (OdbcError *error = obj->errors; error; error = error->next)
postError(error);
infoPosted = obj->infoPosted;
sqlDiagCursorRowCount = obj->sqlDiagCursorRowCount;
sqlDiagDynamicFunction = obj->sqlDiagDynamicFunction;
sqlDiagDynamicFunctionCode = obj->sqlDiagDynamicFunctionCode;
sqlDiagNumber = obj->sqlDiagNumber;
sqlDiagReturnCode = obj->sqlDiagReturnCode;
sqlDiagRowCount = obj->sqlDiagRowCount;
obj->errors = NULL;
obj->clearErrors();
}
void OdbcObject::clearErrors()
{
for (OdbcError *error; (error = errors);)
{
errors = error->next;
delete error;
}
infoPosted = false;
sqlDiagDynamicFunction = NULL; // SQL_DIAG_DYNAMIC_FUNCTION
sqlDiagDynamicFunctionCode = 0; // SQL_DIAG_DYNAMIC_FUNCTION_CODE
sqlDiagNumber = 0; // SQL_DIAG_NUMBER
sqlDiagReturnCode = SQL_SUCCESS; // SQL_DIAG_RETURNCODE
sqlDiagRowCount = 0; // SQL_DIAG_ROW_COUNT
}
OdbcError* OdbcObject::postError(const char * sqlState, SQLException &exception)
{
return postError( new OdbcError( exception.getSqlcode(), exception.getFbcode(), sqlState, exception.getText() ) );
}
const char * OdbcObject::getString(char * * temp, const UCHAR * string, int length, const char *defaultValue)
{
if (!string)
return defaultValue;
if (length == SQL_NTS)
return (char*) string;
char *ret = *temp;
memcpy (ret, string, length);
ret [length] = 0;
*temp += length + 1;
return ret;
}
OdbcError* OdbcObject::postError(const char * state, JString msg)
{
return postError (new OdbcError (0, state, msg));
}
SQLRETURN OdbcObject::sqlGetDiagRec(int handleType, int recNumber, SQLCHAR * stateBuffer, SQLINTEGER * nativeCode, SQLCHAR * msgBuffer, int msgBufferLength, SQLSMALLINT * msgLength)
{
int n = 1;
for (OdbcError *error = errors; error; error = error->next, ++n)
if (n == recNumber)
return error->sqlGetDiagRec (stateBuffer, nativeCode, msgBuffer, msgBufferLength, msgLength);
strcpy ((char*) stateBuffer, "00000");
if (msgBuffer)
msgBuffer [0] = 0;
if ( msgLength )
*msgLength = 0;
return SQL_NO_DATA_FOUND;
}
SQLRETURN OdbcObject::sqlGetDiagField(int recNumber, int diagId, SQLPOINTER ptr, int bufferLength, SQLSMALLINT *stringLength)
{
int n = 1;
switch( diagId )
{
case SQL_DIAG_CURSOR_ROW_COUNT:
*(SQLINTEGER*)ptr = sqlDiagCursorRowCount;
return SQL_SUCCESS;
case SQL_DIAG_DYNAMIC_FUNCTION:
*(SQLCHAR *)ptr = 0; // sqlDiagDynamicFunction
return SQL_SUCCESS;
case SQL_DIAG_DYNAMIC_FUNCTION_CODE:
*(SQLINTEGER*)ptr = sqlDiagDynamicFunctionCode;
return SQL_SUCCESS;
case SQL_DIAG_NUMBER:
*(SQLINTEGER*)ptr = sqlDiagNumber;
if( ptr )
{
SQLSMALLINT &nCount = *stringLength;
n = 0;
for (OdbcError *error = errors; error; error = error->next, ++n);
*(SDWORD*)ptr = n;
}
return SQL_SUCCESS;
case SQL_DIAG_RETURNCODE:
*(SQLRETURN*)ptr = sqlDiagReturnCode;
return SQL_SUCCESS;
case SQL_DIAG_ROW_COUNT:
*(SQLINTEGER*)ptr = sqlDiagRowCount;
return SQL_SUCCESS;
}
if ( diagId == SQL_DIAG_NUMBER )
{
if( ptr )
{
SQLSMALLINT &nCount = *stringLength;
n = 0;
for (OdbcError *error = errors; error; error = error->next, ++n);
*(SDWORD*)ptr = n;
}
return SQL_SUCCESS;
}
if ( bufferLength && ptr )
{
*(char*)ptr = '\0';
for (OdbcError *error = errors; error; error = error->next, ++n)
if (n == recNumber)
return error->sqlGetDiagField (diagId, ptr, bufferLength, stringLength);
}
return SQL_NO_DATA_FOUND;
}
void OdbcObject::setCursorRowCount(int count)
{
sqlDiagCursorRowCount = count;
}
}; // end namespace OdbcJdbcLibrary