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 pathpostgresql_cursor.cpp
More file actions
366 lines (328 loc) · 8.97 KB
/
postgresql_cursor.cpp
File metadata and controls
366 lines (328 loc) · 8.97 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
/* 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_POSTGRESQL - CUSROR OBJECT FOR POSTGRESQL DATABASES CHILD OF DBCURSOR
Attributes:
POSTGRESQL_res - result set structure for POSTGRESQL 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_POSTGRESQL 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 "dbpostgresql.h"
/*Open - opens cursor and retrieves resultset from connection
Output: False on error*/
Bool DBCursor_POSTGRESQL::open(DBConnection *newconnection, PGresult *querycursor)
{
if (!newconnection->getIsConnected())
return False;
//store result set
connection = newconnection;
POSTGRESQL_res = querycursor;
//if no result set (could be no records) and there is an error then exit
if (POSTGRESQL_res == NULL)
return False;
//get number of columns and rows
recordCount = (unsigned int)PQntuples(POSTGRESQL_res);
fieldCount = PQnfields(POSTGRESQL_res);
//get column info
if (!getFieldsInformation())
return False;
//fetch first row in result set
if (recordCount != 0)
first();
return True;
}
//Close - close cursor and free resources used by cursor
void DBCursor_POSTGRESQL::close()
{
FreeFields();
if (POSTGRESQL_res != NULL)
{
PQclear(POSTGRESQL_res);
POSTGRESQL_res = NULL;
}
isBOF = False;
isEOF = True;
recordNum = recordCount = fieldCount = 0;
}
/// Move to start of query, setting isBOF to true and recordNum to 0
Bool DBCursor_POSTGRESQL::first()
{
//exit if no more records
if (recordCount == 0)
return False;
recordNum = 0;
if (!getRowData())
return False;
isBOF = True;
isEOF = False;
return True;
}
/// Move to end of query, setting isEOF to true and recordNum to recordCount - 1 (the last record)
Bool DBCursor_POSTGRESQL::last()
{
if (recordCount == 0)
return False;
recordNum = recordCount - 1;
if (!getRowData())
return False;
isBOF = False;
isEOF = True;
return True;
}
/// Move to next record if one exists. If moving to the new record would take us beyond
/// the last record then set isEOF to true, and remain on the last record.
Bool DBCursor_POSTGRESQL::next()
{
if (recordCount == 0 || isEOF == True)
return False;
isBOF = False;
recordNum++;
if (recordNum == recordCount)
{
isEOF = true;
recordNum = recordCount - 1;
return False;
}
getRowData();
return True;
}
Bool DBCursor_POSTGRESQL::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;
// Not sure how these should behavior here, for now we just do the minimum
// and make them false if we know they should be.
isBOF = False;
isEOF = False;
recordNum = p_record_index;
getRowData();
return True;
}
/// Move to previous record if one exists. If already at start of query, do nothing.
/// If the new record would take us before the first record then set isBOF to true
/// and set recordNum to 0 (the first record).
Bool DBCursor_POSTGRESQL::prev()
{
if (recordCount == 0)
return False;
if (isBOF)
return False;
isEOF = false;
recordNum--;
if (recordNum == -1)
{
isBOF =True;
recordNum = 0;
return False;
}
getRowData();
return True;
}
/*getFieldsInformation - get column names, types, and info
Output: False on error*/
Bool DBCursor_POSTGRESQL::getFieldsInformation()
{
fields = new (nothrow) DBField *[fieldCount];
for (unsigned int i=0; i< fieldCount; i++)
{
DBField *tfield = new (nothrow) DBField();
fields[i] = tfield;
char* tmpStr;
tmpStr = PQfname(POSTGRESQL_res, i);
//get field name..truncate if necessary
if (strlen(tmpStr) > F_NAMESIZE -6)
strncpy(tfield->fieldName, tmpStr, F_NAMESIZE-6);
else
strcpy(tfield->fieldName, tmpStr);
//get field number
tfield->fieldNum = i+1;
//get max length
tfield->maxlength = PQftype(POSTGRESQL_res,i);
//get field type..convert database specific type to generic
switch (PQftype(POSTGRESQL_res, i))
{
case PG_TYPE_BOOL:
tfield->fieldType = FT_BOOLEAN;
break;
case PG_TYPE_CHAR:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_NAME:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_CHAR16:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_INT2:
tfield->fieldType = FT_INTEGER;
break;
case PG_TYPE_INT28:
tfield->fieldType = FT_INTEGER;
break;
case PG_TYPE_INT4:
tfield->fieldType = FT_INTEGER;
break;
case PG_TYPE_TEXT:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_CHAR2:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_CHAR4:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_CHAR8:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_PATH:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_FILENAME:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_FLOAT4:
tfield->fieldType = FT_FLOAT;
break;
case PG_TYPE_FLOAT8:
tfield->fieldType = FT_DOUBLE;
break;
case PG_TYPE_NUMERIC:
tfield->fieldType = FT_DOUBLE;
break;
case PG_TYPE_MONEY:
tfield->fieldType = FT_DOUBLE;
break;
case PG_TYPE_BPCHAR:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_VARCHAR:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_DATE:
tfield->fieldType = FT_DATE;
break;
case PG_TYPE_TIME:
tfield->fieldType = FT_TIME;
break;
case PG_TYPE_DATETIME:
tfield->fieldType = FT_DATETIME;
break;
case PG_TYPE_TIMESTAMP:
tfield->fieldType = FT_TIMESTAMP;
break;
case PG_TYPE_TIMESPAN:
tfield->fieldType = FT_TIME;
break;
case PG_TYPE_RELTIME:
tfield->fieldType = FT_TIME;
break;
case PG_TYPE_ABSTIME:
tfield->fieldType = FT_DATETIME;
break;
//Array Types (Changes the type category to array)
case PG_TYPE_ARR_BOOL:
tfield->fieldType = FT_BOOLEAN;
break;
case PG_TYPE_ARR_CHAR:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_ARR_NAME:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_ARR_INT2:
tfield->fieldType = FT_INTEGER;
break;
case PG_TYPE_ARR_INT4:
tfield->fieldType = FT_INTEGER;
break;
case PG_TYPE_ARR_TEXT:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_ARR_VARCHAR:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_ARR_INT8:
tfield->fieldType = FT_INTEGER;
break;
case PG_TYPE_ARR_PATH:
tfield->fieldType = FT_STRING;
break;
case PG_TYPE_ARR_FLOAT4:
tfield->fieldType = FT_FLOAT;
break;
case PG_TYPE_ARR_FLOAT8:
tfield->fieldType = FT_DOUBLE;
break;
case PG_TYPE_ARR_RELTIME:
tfield->fieldType = FT_TIME;
break;
case PG_TYPE_ARR_ABSTIME:
tfield->fieldType = FT_DATETIME;
break;
case PG_TYPE_ARR_NUMERIC:
tfield->fieldType = FT_DOUBLE;
break;
// OK-2007-06-19
case PG_TYPE_ARR_BYTEA:
tfield->fieldType = FT_BLOB;
break;
case PG_TYPE_BYTEA:
tfield->fieldType = FT_BLOB;
break;
default:
tfield->fieldType = FT_STRING;
}
//get column characteristics
tfield->isAutoIncrement = False;
tfield->isPrimaryKey = False;
tfield->isUnique = False;
tfield->isNotNull = False;
}
return True;
}
/*getRowData - Retrieve the data from the current row.
Output: False on error*/
Bool DBCursor_POSTGRESQL::getRowData()
{
for (unsigned int i=0; i<fieldCount; i++)
{
fields[i] -> dataSize = PQgetlength(POSTGRESQL_res, recordNum, i);
fields[i] -> isNull = PQgetisnull(POSTGRESQL_res, recordNum, i);
if (fields[i] -> isNull)
fields[i] -> dataSize = 0;
fields[i] -> freeBuffer = False;
if (fields[i] -> dataSize == 0)
continue;
if (fields[i] -> fieldType == FT_BLOB)
fields[i] -> data = (char *)PQunescapeBytea((unsigned char *)PQgetvalue(POSTGRESQL_res, recordNum, i), (size_t *)&fields[i] -> dataSize);
else
fields[i] -> data = PQgetvalue(POSTGRESQL_res, recordNum, i);
}
return True;
}