Repository navigation
Expand file tree
/
Copy pathCResults.cpp
More file actions
238 lines (201 loc) · 6.26 KB
/
Copy pathCResults.cpp
File metadata and controls
238 lines (201 loc) · 6.26 KB
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
/*
+==========================================================+
| |
| ODBC extension for Win32 Perl |
| ----------------------------- |
| |
| by Dave Roth <rothd@roth.net> |
| |
| version v970208 |
| |
| Copyright (c) 1996-1997 Dave Roth. All rights reserved. |
| This program is free software; you can redistribute |
| it and/or modify it under the same terms as Perl itself. |
| |
+==========================================================+
based on original code by Dan DeMaggio (dmag@umich.edu)
Use under GNU General Public License or Larry Wall's "Artistic License"
*/
#define WIN32_LEAN_AND_MEAN
#include <stdlib.h>
#include <math.h>
#include <windows.h>
#include <stdio.h>
#include <sql.h>
#include <sqlext.h>
#include <odbcinst.h>
#if defined(__cplusplus)
extern "C" {
#endif
#include <EXTERN.h>
#include "perl.h"
#define NO_XSLOCKS
#include "XSub.h"
#if defined(__cplusplus)
}
#endif
#include "ODBCbuild.h"
#include "ODBC.h"
#include "CResults.hpp"
CResults::CResults(ODBC_TYPE *h){
char szBuff[100];
int iTemp;
BOOL bResult = FALSE;
dRowSetSize = 0;
sNumOfCols = 0;
dSize = 0;
dReturnSize = 0;
szColumn = 0;
iODBC = h->conn;
SQLGetStmtOption(h->hstmt, SQL_ROWSET_SIZE, &dRowSetSize);
if (SQLNumResultCols(h->hstmt, (SWORD *)&sNumOfCols) == SQL_SUCCESS){
if (szColumn = new char* [sNumOfCols + 1]){ // Alloc array of strings
memset(szColumn, 0x00, sizeof(char*) * (sNumOfCols + 1));
}
// TESTING: multiply alloced data by dRowSetSize
if (dSize = new SQLLEN [(sNumOfCols + 1) * dRowSetSize]){ // Alloc array of sizes
memset(dSize, 0x00, (sizeof(SQLLEN) * (sNumOfCols + 1) * dRowSetSize));
}
// TESTING: multiply alloced data by dRowSetSize
if (dReturnSize = new SQLLEN [(sNumOfCols + 1) * dRowSetSize]){ // Alloc array of return sizes
memset(dReturnSize, 0x00, (sizeof(SQLLEN) * (sNumOfCols + 1) * dRowSetSize));
}
if(szColumn && dSize && dReturnSize){
SWORD dColType;
for(iTemp = 1; iTemp <= sNumOfCols; iTemp++){
if (SQLColAttributes(h->hstmt, iTemp, SQL_COLUMN_DISPLAY_SIZE, NULL, 0, NULL, &dSize[iTemp]) == SQL_SUCCESS){
// If we cant resolve the size define it as the MAX Buffer
// size. Later we can change this if needed.
// This is just a patch. We need a mechanism to monitor
// if this needs more memory or not. Now a user can configure
// the memory size by hand. But this will be the size for ALL of this type.
// Sad. Very sad. We need to fix this hack!!!!!
if(dSize[iTemp] > MAX_DATA_ASSUME_SIZE){
dSize[iTemp] = h->iMaxBufSize;
}
// Here is dRowSetSize again.
if (!(szColumn[iTemp] = (char *) new UCHAR [(++dSize[iTemp]) * dRowSetSize])){
sprintf((char *)szBuff, "Could not allocate enough memory (%d bytes) for column %d.\n", (dSize[iTemp] * dRowSetSize), iTemp);
::ODBCError((char *)szBuff, h, "CResults()", "1");
break;
}
memset(szColumn[iTemp], 0x00, dSize[iTemp] * dRowSetSize);
}
}
// So far so good, now lets bind to the columns
if(! h->Error->ErrNum){
for(iTemp = 1; iTemp <= sNumOfCols; iTemp++){
SWORD sTemp;
SWORD sSQLType;
if (iTemp){
dColType = SQL_C_CHAR;
SQLDescribeCol(h->hstmt, iTemp, 0, 0, &sTemp, &sSQLType,(SQLULEN *) &szBuff, &sTemp, &sTemp);
if ((sSQLType == SQL_LONGVARBINARY) || (sSQLType == SQL_BINARY) || (sSQLType == SQL_VARBINARY)){
dColType = SQL_C_BINARY;
}
}else{
dColType = SQL_C_BOOKMARK;
}
if(SQLBindCol(h->hstmt, iTemp, dColType, szColumn[iTemp], dSize[iTemp], &dReturnSize[iTemp]) != SQL_SUCCESS){
sprintf((char *)szBuff, "Could not bind column %d.\n", iTemp);
::ODBCError((char *)szBuff, h, "CResults()", "2");
bResult = FALSE;
}
}
bResult = TRUE;
}
}
}
if(bResult == FALSE){
RemoveBuffers();
if(dSize){
delete [] dSize;
}
if(dReturnSize){
delete [] dReturnSize;
}
sNumOfCols = 0;
}
}
CResults::~CResults(){
RemoveBuffers();
if(dSize){
delete [] dSize;
}
if(dReturnSize){
delete [] dReturnSize;
}
sNumOfCols = 0;
}
SDWORD CResults::Size(int iElement){
SDWORD dReturn = 0;
if (iElement >= 0 && iElement <= sNumOfCols){
dReturn = dSize[iElement];
}
return dReturn;
}
SDWORD CResults::ReturnSize(int iElement){
SDWORD dReturn = 0;
if (iElement >= 0 && iElement <= sNumOfCols){
// We should be reporting the correct size regardless of SQL_NULL_DATA so
// remark out the if (dReturnSize... for now...until we know we don't need it.
// if (dReturnSize[iElement] == SQL_NULL_DATA){
switch(dReturnSize[iElement]){
case SQL_NULL_DATA:
dReturn = 0;
break;
case SQL_NO_TOTAL:
// This is bad. If we don't know how long the data is this assumes
// that the data is a null terminated string. BAD!
dReturn = strlen(szColumn[iElement]);
break;
default:
dReturn = dReturnSize[iElement];
break;
}
// }
}
return dReturn;
}
SWORD CResults::NumOfCols(){
return sNumOfCols;
}
char *CResults::operator[](int iElement){;
char *szReturn = 0;
if (iElement >= 0 && iElement <= sNumOfCols){
switch(dReturnSize[iElement]){
case SQL_NULL_DATA:
szReturn = NULL_VALUE;
break;
case SQL_NO_TOTAL:
default:
szReturn = szColumn[iElement];
break;
}
}
return szReturn;
}
void CResults::RemoveBuffers(){
int iTemp;
ODBC_Conn *h;
if (h = ::_NT_ODBC_Verify(iODBC)){
if (h->hstmt){
SQLFreeStmt(h->hstmt, SQL_UNBIND);
}
}
if(szColumn){
for(iTemp = 0; iTemp <= sNumOfCols; iTemp++){
if (szColumn[iTemp]) delete [] szColumn[iTemp];
}
delete [] szColumn;
}
}
void CResults::Clean(){
int iTemp;
for(iTemp = 1; iTemp <= sNumOfCols; iTemp++){
memset(szColumn[iTemp], 0x00, (dSize[iTemp] * dRowSetSize));
}
}
DWORD CResults::RowSetSize(){
return dRowSetSize;
}