This repository was archived by the owner on Sep 7, 2021. It is now read-only.
forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoracle_cursor.cpp
More file actions
317 lines (270 loc) · 7.63 KB
/
Copy pathoracle_cursor.cpp
File metadata and controls
317 lines (270 loc) · 7.63 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
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
/* Copyright (C) 2003-2013 Runtime Revolution Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
/*DBCURSOR_ORACLE - CUSROR OBJECT FOR ORACLE DATABASES CHILD OF DBCURSOR
Attributes:
ORACLE_res - result set structure for ORACLE database
isBOF - True if at beginning of resultset (inherited)
isEOF - True if at end of resultset (inherited)
recordNum - Number of current record -1 (inherited)
recordCount - Number of records in resultset (inherited)
fieldCount - Number of columns (inherited)
fields - Array of fields (inherited)
connection - Pointer to dbconnection_ORACLE object that created cursor
Methods:
Open - opens cursor and retrieves resultset from connection
Close - closes cursor and frees any resources used by cursor
getFieldsInformation - get column names, types, and info
getRowData - Retrieve the data from the current row.
first - move to first row of resultset
next - move to next row of resultset
prev - move to previous row of resultset
last - move to last row of resultset
*/
#include "dboracle.h"
char DBCursor_ORACLE::errmsg[512];
/*Open - opens cursor and retrieves resultset from connection
Output: False on error*/
Bool DBCursor_ORACLE::open(DBConnection *newconnection, Cda_Def *oraclecursor, int p_rows)
{
if (!newconnection->getIsConnected())
return False;
connection = newconnection;
DBConnection_ORACLE *ORACLEconn = (DBConnection_ORACLE *)connection;
ORACLE_res = oraclecursor;
recordCount = -1;
maxrows = 1;
if (!maxrows)
cacherows = True;
rowsCached = 0;
if (!getFieldsInformation())
return False;
isBOF = True;
isEOF = False;
FetchedNum = 0;
recordNum = -1;
next();
//recordNum = 0;
return True;
}
//Close - close cursor and free resources used by cursor
void DBCursor_ORACLE::close()
{
//free column data
FreeFields();
//Make sure any stored query results are free.
if (ORACLE_res != NULL)
{
oclose(ORACLE_res);
delete ORACLE_res;
ORACLE_res = NULL;
}
isBOF = False;
isEOF = True;
recordNum = recordCount = fieldCount = 0;
}
/*first - move to first row of resultset
Output - False on error*/
Bool DBCursor_ORACLE::first()
{
return False;
}
/*last - move to last row of resultset
Output - False on error*/
Bool DBCursor_ORACLE::last()
{
return False;
}
/*next - move to next row of resultset
Output - False on error*/
Bool DBCursor_ORACLE::next()
{
if (fieldCount == 0 || isEOF == True)
return False;
isBOF = False;
FetchedNum++;
if (recordCount < 1 || FetchedNum == rowsCached)
{
ofen(ORACLE_res, maxrows);
if (ORACLE_res->rc == NULL_VALUE_RETURNED)
ORACLE_res->rc = 0;
if (ORACLE_res->rc == 0 || (ORACLE_res->rc == NO_DATA_FOUND && (ORACLE_res->rpc - recordCount) > 0))
{
rowsCached = ORACLE_res->rpc - recordCount;
if (ORACLE_res -> rc == NO_DATA_FOUND)
recordCount = ORACLE_res->rpc;
else
recordCount = -1;
FetchedNum = 0;
}
else
{
isEOF = True;
return False;
}
}
recordNum++;
if (recordNum == recordCount)
{
isEOF = true;
recordNum = recordCount - 1;
return False;
}
if (recordNum == 0)
isBOF = True;
else
isBOF = False;
getRowData();
return True;
}
/*prev - move to first row of resultset
Output - False on error*/
Bool DBCursor_ORACLE::prev()
{
return False;
}
/*getFieldsInformation - get column names, types, and info
Output: False on error*/
Bool DBCursor_ORACLE::getFieldsInformation()
{
sb4 dbsize;
int totalsize = 0;
fieldCount = 0;
while (True)
{
if (odescr(ORACLE_res, fieldCount + 1, &dbsize, 0, 0, 0, 0, 0, 0, 0))
if (ORACLE_res->rc == VAR_NOT_IN_LIST)
break;
fieldCount++;
}
fields = new DBField *[fieldCount];
unsigned int i=0;
for (i=0; i< fieldCount; i++)
{
DBField_ORACLE *ofield = new DBField_ORACLE();
fields[i] = (DBField *)ofield;
sb4 buflen = F_NAMESIZE;
sb2 scale, nullok, dbtype;
odescr(ORACLE_res, i+1, &dbsize, &dbtype, (sb1 *)ofield->fieldName, &buflen, 0, 0, &scale, &nullok);
ofield->fieldName[buflen] = '\0';
ofield->fieldNum = i + 1;
ofield->maxlength = dbsize+1;
ofield->externaltype = STRING_TYPE;
switch (dbtype)
{
case CHAR_TYPE:
case VARCHAR2_TYPE:
ofield->fieldType = FT_CHAR;
break;
case NUMBER_TYPE:
if (scale != 0)
ofield->fieldType = FT_FLOAT;
else
ofield->fieldType = FT_INTEGER;
break;
case LONG_TYPE:
ofield->fieldType = FT_STRING;
ofield->externaltype = LRAW_TYPE;
ofield->maxlength = LONG_CHUNK_SIZE;
break;
case ROWID_TYPE:
ofield->fieldType = FT_STRING;
break;
case DATE_TYPE :
ofield->fieldType = FT_DATE;
break;
case RAW_TYPE :
case LRAW_TYPE:
ofield->fieldType = FT_BLOB;
ofield->externaltype = LRAW_TYPE;
ofield->maxlength = LONG_CHUNK_SIZE;
break;
default:
ofield -> fieldType = FT_STRING;
}
ofield->isAutoIncrement = False;
ofield->isPrimaryKey = False;
ofield->isUnique = False;
ofield->isNotNull = nullok == 0;
//allocate for data and bind
ofield->maxlength = align(ofield->maxlength);
totalsize += ofield->maxlength;
ofield->freeBuffer = False;
}
if (cacherows)
{
maxrows = MAXCOLBUF / totalsize;
if (maxrows > MAXROWNUM)
maxrows = MAXROWNUM;
}
else
maxrows = 1;
for (i = 0; i < fieldCount; i++)
{
DBField_ORACLE *ofield = (DBField_ORACLE *)fields[i];
ofield -> databuffer = new char[maxrows * ofield -> maxlength];
ofield -> indp = new ub2[maxrows];
ofield -> dsize = new ub2[maxrows];
ofield -> errorcode = new ub2[maxrows];
if (odefin(ORACLE_res, i + 1, (ub1 *)ofield -> databuffer, ofield -> maxlength, ofield -> externaltype, -1, (sb2 *)ofield -> indp, 0, -1, -1, (ub2 *)ofield -> dsize, (ub2 *)ofield -> errorcode))
return False;
}
return True;
}
/*getRowData - Retrieve the data from the current row.
Output: False on error*/
Bool DBCursor_ORACLE::getRowData()
{
for (unsigned int i=0; i<fieldCount; i++)
{
DBField_ORACLE *ofield = (DBField_ORACLE *)fields[i];
ofield -> data = ofield -> databuffer + (ofield -> maxlength * FetchedNum);
ofield -> dataSize = ofield -> dsize[FetchedNum];
if (ofield -> extraData)
{
free(ofield -> extraData);
ofield -> extraData = NULL;
}
if (ofield -> errorcode[FetchedNum] != 1405)
ofield -> isNull = False;
if (ofield->errorcode[FetchedNum] == 1405)
ofield -> isNull = True;
else if (!cacherows && ofield->errorcode[FetchedNum] == 1406 && (ofield->externaltype == RAW_TYPE || ofield->externaltype == LRAW_TYPE))
{
int lflength = (size_t)ofield->indp[FetchedNum];
ofield->extraData = (char *)malloc(RAWSIZE*2);
memcpy(ofield->extraData,ofield->data,RAWSIZE);
sb4 offset = RAWSIZE;
sb2 result;
ub4 ret_len;
while (True)
{
result = oflng(ORACLE_res, i+1, (ub1 *)(ofield->extraData + offset), RAWSIZE, ofield->externaltype, &ret_len,offset);
if (result || ret_len <= 0)
break;
offset += ret_len;
ofield->extraData = (char *)realloc(ofield->extraData,offset + RAWSIZE);
}
ofield->data = (char *)(ofield->extraData);
ofield->dataSize = offset;
}
else if (ofield->errorcode[FetchedNum] != 0)
ofield -> isNull = True;
}
return True;
}
char *DBCursor_ORACLE::getErrorMessage()
{
if (ORACLE_res->rc != 0)
oerhms(ORACLE_res, ORACLE_res->rc, (text *)errmsg,
(sword) sizeof(errmsg));
return errmsg;
}