-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPGSQLRecordset.m
More file actions
255 lines (215 loc) · 5.2 KB
/
PGSQLRecordset.m
File metadata and controls
255 lines (215 loc) · 5.2 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
//
// PGSQLRecordset.m
// PGSQLKit
//
// Created by Andy Satori on 5/29/07.
// Copyright 2007 Druware Software Designs. All rights reserved.
//
#import "PGSQLRecordset.h"
#import "libpq-fe.h"
@implementation PGSQLRecordset
-(id)initWithResult:(void *)result
{
self = [super init];
if (self != nil)
{
isOpen = YES;
isEOF = YES;
// this will default to NSUTF8StringEncoding with PG9
// defaultEncoding = NSMacOSRomanStringEncoding;
columns = [[[[NSMutableArray alloc] init] retain] autorelease];
pgResult = result;
rowCount = -1;
rowCount = PQntuples(pgResult);
// cache the colum list for faster data access via lookups by name
// Loop through and get the fields into Field Item Classes
PGSQLColumn *column;
int iCols = 0;
iCols = PQnfields(pgResult);
int i;
for ( i = 0; i < iCols; i++)
{
column = [[[PGSQLColumn alloc] initWithResult:pgResult
atIndex:i] autorelease];
[columns addObject:column];
}
if (rowCount == 0)
{
isEOF = YES;
return self;
}
isEOF = NO;
// move to the first record (and check EOF / BOF state)
[self moveFirst];
}
return self;
}
-(PGSQLField *)fieldByName:(NSString *)fieldName
{
return [currentRecord fieldByName:fieldName];
}
-(PGSQLField *)fieldByIndex:(long)fieldIndex
{
return [currentRecord fieldByIndex:fieldIndex];
}
- (NSArray *)columns
{
return columns;
}
- (long)recordCount
{
return rowCount;
}
- (void)setCurrentRecordWithRowIndex:(long)rowIndex
{
[currentRecord release];
currentRecord = [[PGSQLRecord alloc] initWithResult:pgResult
atRow:rowIndex
columns:columns];
[currentRecord setDefaultEncoding:defaultEncoding];
}
- (PGSQLRecord *)moveNext
{
if (rowCount == 0) {
return nil;
}
long currentRowIndex = -1;
if (currentRecord != nil)
{
currentRowIndex = [currentRecord rowNumber];
}
currentRowIndex++;
if (currentRowIndex >= rowCount) {
isEOF = true;
[currentRecord release];
currentRecord = nil;
return nil;
}
[self setCurrentRecordWithRowIndex:currentRowIndex];
return [[currentRecord retain] autorelease];
}
- (PGSQLRecord *)moveFirst
{
if (rowCount == 0) {
return nil;
}
long currentRowIndex = 0;
isEOF = false;
[self setCurrentRecordWithRowIndex:currentRowIndex];
return [[currentRecord retain] autorelease];
}
- (PGSQLRecord *)movePrevious
{
if (rowCount == 0) {
return nil;
}
long currentRowIndex = -1;
if (currentRecord != nil)
{
currentRowIndex = [currentRecord rowNumber];
}
currentRowIndex--;
if (currentRowIndex < 0) {
isEOF = true;
currentRecord = nil;
return nil;
}
[self setCurrentRecordWithRowIndex:currentRowIndex];
return [[currentRecord retain] autorelease];
}
- (PGSQLRecord *)moveLast
{
if (rowCount == 0) {
return nil;
}
long currentRowIndex = rowCount;
isEOF = false;
[self setCurrentRecordWithRowIndex:currentRowIndex];
return [[currentRecord retain] autorelease];
}
-(void)close
{
if (isOpen) {
[columns release];
columns = nil;
PQclear(pgResult);
pgResult = nil;
}
[currentRecord release];
currentRecord = nil;
isOpen = NO;
}
-(void)dealloc
{
[self close];
[super dealloc];
}
-(BOOL)isEOF
{
return isEOF;
}
-(NSDictionary *)dictionaryFromRecord
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
long i;
for (i = 0; i < [columns count]; i++)
{
PGSQLColumn *column = [columns objectAtIndex:i];
// for each column, add the value for the key.
// select oid, typname from pg_type
switch ([column type])
{
/*
case SQL_UNKNOWN_TYPE:
[dict setValue:[[self fieldByName:[column name]] asData] forKey:[[column name] lowercaseString]];
break;
case SQL_CHAR:
case SQL_VARCHAR:
[dict setValue:[[self fieldByName:[column name]] asString] forKey:[[column name] lowercaseString]];
break;
case SQL_NUMERIC:
case SQL_DECIMAL:
case SQL_INTEGER:
case SQL_SMALLINT:
case SQL_FLOAT:
case SQL_REAL:
case SQL_DOUBLE:
[dict setValue:[[self fieldByName:[column name]] asNumber] forKey:[[column name] lowercaseString]];
break;
case SQL_DATETIME:
[dict setValue:[[self fieldByName:[column name]] asDate] forKey:[[column name] lowercaseString]];
NSLog(@"Date Being Set: %@ for: %@", [[self fieldByName:[column name]] asDate], [[column name] lowercaseString]);
break;
case 11: // Undefined, MSSQL SHORTDATETIME
[dict setValue:[[self fieldByName:[column name]] asDate] forKey:[[column name] lowercaseString]];
NSLog(@"Date Being Set: %@ for: %@", [[self fieldByName:[column name]] asDate], [[column name] lowercaseString]);
break;
*/
case 16: // BOOL
if ([[self fieldByName:[column name]] asBoolean])
{
[dict setValue:@"true" forKey:[column name]];
} else {
[dict setValue:@"false" forKey:[column name]];
}
break;
default:
[dict setValue:[[self fieldByName:[column name]] asString:defaultEncoding] forKey:[column name]];
break;
}
}
NSDictionary *result = [[[NSDictionary alloc] initWithDictionary:dict] autorelease];
[dict release];
return result;
}
-(NSStringEncoding)defaultEncoding
{
return defaultEncoding;
}
-(void)setDefaultEncoding:(NSStringEncoding)value
{
if (defaultEncoding != value) {
defaultEncoding = value;
}
}
@end