This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathsqlite_cursor.cpp
More file actions
401 lines (328 loc) · 8.89 KB
/
sqlite_cursor.cpp
File metadata and controls
401 lines (328 loc) · 8.89 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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* Copyright (C) 2003-2015 LiveCode 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_SQLITE - CUSROR OBJECT FOR MYSQL DATABASES CHILD OF DBCURSOR
Attributes:
mysql_res - result set structure for MYSQL 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_mysql 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 "dbsqlite.h"
#include <sqlitedecode.h>
#define ENTER
DBCursor_SQLITE::DBCursor_SQLITE(SqliteDatabase &db, bool enable_binary)
: mDB(db)
{
ENTER;
mDataset = mDB.CreateDataset();
m_enable_binary = enable_binary;
}
DBCursor_SQLITE::~DBCursor_SQLITE()
{
ENTER;
close();
delete mDataset;
}
/*Open - opens cursor and retrieves resultset from connection
Output: False on error*/
Bool DBCursor_SQLITE::open(DBConnection *newconnection)
{
ENTER;
if (!newconnection->getIsConnected())
return False;
connection = newconnection;
try
{
recordCount = mDataset -> num_rows();
fieldCount = mDataset -> field_count();
if(!getFieldsInformation())
return False;
if(recordCount != 0)
first();
} catch(DbErrors &e)
{
MDEBUG("***** Caught error [%s]\n", e.getMsg());
return False;
}
MDEBUG0("Cursor::open OK\n");
return True;
}
//Close - close cursor and free resources used by cursor
void DBCursor_SQLITE::close()
{
ENTER;
MDEBUG0("\n--\n");
FreeFields();
isBOF = False;
isEOF = True;
recordNum = recordCount = fieldCount = 0;
MDEBUG0("\nBye cursor\n");
}
/*first - move to first row of resultset
Output - False on error*/
Bool DBCursor_SQLITE::first()
{
ENTER;
try {
//exit if no more records
if (recordCount == 0)
return False;
recordNum = 0;
mDataset->first();
if(!getRowData())
return False;
isBOF = True;
isEOF = False;
} catch(DbErrors &e) {
MDEBUG("***** Caught error [%s]\n", e.getMsg());
return False;
}
return True;
}
/*last - move to last row of resultset
Output - False on error*/
Bool DBCursor_SQLITE::last()
{
ENTER;
try {
if (recordCount == 0)
return False;
recordNum = recordCount-1;
mDataset->last();
if (!getRowData())
return False;
isBOF = False;
isEOF = True;
} catch(DbErrors &e) {
MDEBUG("***** Caught error [%s]\n", e.getMsg());
return False;
}
return True;
}
/*next - move to next row of resultset
Output - False on error*/
Bool DBCursor_SQLITE::next()
{
ENTER;
try {
//exit if at last record or there are no records
if (recordCount == 0 || isEOF == True)
return False;
isBOF = False;
recordNum++;
if (recordNum == recordCount) {
isEOF = true;
recordNum = recordCount-1;
return False;
}
mDataset->next();
getRowData();
} catch(DbErrors &e) {
MDEBUG("***** Caught error [%s]\n", e.getMsg());
return False;
}
return True;
}
/*prev - move to first row of resultset
Output - False on error*/
Bool DBCursor_SQLITE::prev()
{
ENTER;
try {
if (recordCount == 0)
return False;
if (isBOF)
return False;
isEOF = false;
recordNum--;
if (recordNum == -1) {
isBOF =True;
recordNum = 0;
return False;
}
mDataset->prev();
getRowData();
} catch(DbErrors &e) {
MDEBUG("***** Caught error [%s]\n", e.getMsg());
return False;
}
return True;
}
Bool DBCursor_SQLITE::move(int p_record_index)
{
if (recordCount == 0)
return False;
if (p_record_index < 0)
return False;
else if (p_record_index > recordCount - 1)
return False;
isBOF = False;
isEOF = False;
if (!mDataset -> seek(p_record_index))
return False;
recordNum = p_record_index;
getRowData();
return True;
}
/*getFieldsInformation - get column names, types, and info
Output: False on error*/
Bool DBCursor_SQLITE::getFieldsInformation()
{
ENTER;
//create an array of columns
fields = new (nothrow) DBField *[fieldCount];
Fields *t_fields_object;
t_fields_object = mDataset -> get_fields_object();
for(int i = 0 ; i < fieldCount; i++) {
DBField *tfield = new (nothrow) DBField();
fields[i] = tfield;
const char *name = mDataset->fieldName(i);
if(name) {
MDEBUG("Field name=[%s]\n", name);
const field_value fv = mDataset->get_field_value_by_index(i);
// OK-2008-10-29 : [[Bug 6588]] - Get the field type from the props structure instead of from the field value,
// because we are returning the declared column type rather than the actual type of the data in the column.
field_prop t_props;
t_props = (*t_fields_object)[i].props;
fType type;
type = t_props . type;
///////////////////
// TYPE
if (strlen(name) > F_NAMESIZE -6)
strncpy(tfield->fieldName, name, F_NAMESIZE-6);
else
strcpy(tfield->fieldName, name);
switch(type) {
case ft_Short :
case ft_UShort :
MDEBUG0("Type: smallint\n");
tfield->fieldType = FT_SMALLINT;
break;
case ft_Long :
case ft_ULong :
MDEBUG0("Type: integer\n");
tfield->fieldType = FT_INTEGER;
break;
case ft_Float:
MDEBUG0("Type: float\n");
tfield->fieldType = FT_FLOAT;
break;
case ft_Double:
case ft_LongDouble:
MDEBUG0("Type: double\n");
tfield->fieldType = FT_DOUBLE;
break;
case ft_Object :
if (m_enable_binary)
tfield -> fieldType = FT_BLOB;
else
tfield -> fieldType = FT_STRING;
break;
case ft_String :
default:
MDEBUG0("Type: string\n");
tfield->fieldType = FT_STRING;
break;
}
//get field number
tfield->fieldNum = i+1;
//get max length
tfield->maxlength = MAX_BYTES_PER_ROW;
//TODO: get column characteristics
tfield->isAutoIncrement = 0;
tfield->isPrimaryKey = 0;
tfield->isUnique = 0;
tfield->isNotNull = 0;
// OK-2009-05-26: [[Bug 8065]] - Field data must start out as NULL so we can tell
// whether or not it needs to be freed.
tfield -> data = NULL;
}
}
return True;
}
/*getRowData - Retrieve the data from the current row.
Output: False on error*/
Bool DBCursor_SQLITE::getRowData()
{
ENTER;
Bool ret = true;
try {
for(int i = 0; i < fieldCount; i++) {
const field_value fv = mDataset->get_field_value_by_index(i);
// OK-2009-05-26: [[Bug 8065]] - Clear out any data from previous records, otherwise we no longer
// have a pointer to it and the memory is lost.
if (fields[i] -> data != NULL)
{
delete fields[i] -> data;
fields[i] -> data = NULL;
}
//distinguish between empty column and null column
fields[i]->isNull = fv.get_isNull();
if(!fields[i]->isNull) {
std::string tmp = fv.get_asString();
// MW-2014-01-29: [[ Sqlite382 ]] If the field type is BLOB and we aren't in binary mode,
// we must decode the data; otherwise we just use the contents directly.
if (fv.get_fType() == ft_Object && !m_enable_binary)
{
// This code has moved from libsqlite/sqlitedataset.cpp:callback().
int bufsize;
bufsize = tmp.size();
char *mybuff;
mybuff = (char *)malloc(bufsize);
memset(mybuff,0,bufsize);
bufsize = sqlite_decode_binary((const unsigned char *)tmp.c_str(), bufsize, ( unsigned char *)mybuff, bufsize);
if (bufsize == -1)
{
fields[i] -> data = NULL;
fields[i] -> dataSize = 0;
fields[i] -> isNull = True;
free(mybuff);
}
else
{
fields[i] -> data = mybuff;
fields[i] -> dataSize = bufsize;
}
}
else
{
int bufsize = tmp.size();
fields[i]->data = new (nothrow) char[tmp.size() + 1];
memset(fields[i]->data,0,bufsize+1);
memcpy(fields[i]->data, tmp.data(), tmp.size());
fields[i]->dataSize = tmp.size();
}
// OK-2009-05-26: [[Bug 8065]] - Because we copied the data in the line above,
// We have to set FreeBuffer otherwise it will not be freed when the cursor is closed,
// leading to memory leaks.
fields[i] -> freeBuffer = True;
}
MDEBUG("getRowData() -- fv=[%s]\n", fields[i]->data);
}
} catch(DbErrors &e) {
ret = False;
}
return ret;
}