-
Notifications
You must be signed in to change notification settings - Fork 1
/
SLStmt.m
503 lines (371 loc) · 9.62 KB
/
SLStmt.m
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//
// SLStmt.m
//
// Copyright 2008 Steven Fisher.
//
// This file is covered by the MIT/X11 License.
// See LICENSE.TXT for more information.
//
#import "SLStmt.h"
#import "SLDatabase.h"
@implementation SLStmt
@synthesize stmt, currentSQL;
- (BOOL)setResult: (int)err
error: (NSError **)outError;
{
errorCode = (err & 0xFF);
NSError *theError = nil;
if ( ( errorCode != SQLITE_OK ) && ( errorCode < 100 ) ) {
const char *msg = sqlite3_errmsg([database dtbs]);
theError = [NSError errorWithDomain: @"sqlite"
code: errorCode
userInfo: [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithLongLong: err], @"Code",
[NSString stringWithUTF8String: msg], @"Message", nil]];
}
if (outError) {
*outError = theError;
}
return (theError == nil);
}
+ (id)stmtWithDatabase: (SLDatabase *)inDatabase;
{
return [[[self alloc] initWithDatabase: inDatabase] autorelease];
}
- (id)initWithDatabase: (SLDatabase *)inDatabase;
{
self = [super init];
if (!self) return self;
database = [inDatabase retain];
return self;
}
- (void)dealloc;
{
if (stmt) {
NSError *error;
[self closeWithError: &error];
}
[fullSQL release];
[currentSQL release];
currentSQL = nil;
[database release];
[super dealloc];
}
- (void)updateCurrentSQL;
{
intptr_t length = (intptr_t)nextSQL - (intptr_t)thisSQL;
NSMutableData *data = [NSMutableData dataWithCapacity: length+1];
[data appendBytes: thisSQL length: length];
const int zero = 0;
[data appendBytes: &zero length: sizeof(zero)];
[currentSQL release];
currentSQL = [[NSString stringWithUTF8String: [data bytes]] retain];
}
- (BOOL)prepareSQL: (NSString *)inSQL
error: (NSError **)outError;
{
[inSQL retain];
NSError *error;
[self closeWithError: &error];
[fullSQL release];
fullSQL = inSQL;
thisSQL = [fullSQL UTF8String];
[self setResult: sqlite3_prepare_v2([database dtbs], thisSQL, -1, &stmt, &nextSQL)
error: outError];
[self updateCurrentSQL];
bind = 0;
return (errorCode == SQLITE_OK) && (stmt != NULL);
}
- (BOOL)prepareNextWithError: (NSError **)outError;
{
// finalize previous statement, if any
if ( stmt ) {
if ( ![self setResult: sqlite3_finalize( stmt )
error: outError] ) {
return FALSE;
}
stmt = NULL;
}
// return immediately if there's no more SQL to process
if ( ( nextSQL == NULL ) || ( *nextSQL == 0 ) )
return NO;
// otherwise, prepare the next chunk of SQL and update the currentSQL property
thisSQL = nextSQL;
[self setResult: sqlite3_prepare_v2([database dtbs], nextSQL, -1, &stmt, &nextSQL)
error: outError];
[self updateCurrentSQL];
// success, if no error
return (errorCode == SQLITE_OK) && (stmt != NULL);
}
- (BOOL)resetWithError: (NSError **)outError;
{
if ( !stmt ) {
return NO;
}
bind = 0;
column = 0;
[self setResult: sqlite3_reset( stmt )
error: outError];
return (errorCode == SQLITE_OK);
}
- (BOOL)closeWithError: (NSError **)outError;
{
BOOL ok = YES;
if ( stmt ) {
[self setResult: sqlite3_finalize( stmt )
error: outError];
stmt = NULL;
[currentSQL release];
currentSQL = nil;
}
return ok;
}
- (sqlite3_stmt *)stmt;
{
return stmt;
}
- (BOOL)stepWithError: (NSError **)outError;
{
column = 0;
[self setResult: sqlite3_step( stmt )
error: outError];
return ( ( errorCode == SQLITE_ROW ) | (errorCode == SQLITE_DONE ) );
}
- (BOOL)stepHasRowWithError: (NSError **)outError;
{
column = 0;
[self setResult: sqlite3_step( stmt )
error: outError];
return ( errorCode == SQLITE_ROW );
}
- (BOOL)stepOverRowsWithError: (NSError **)outError;
{
column = 0;
do {
[self setResult: sqlite3_step( stmt )
error: outError];
} while ( errorCode == SQLITE_ROW);
return ( errorCode == SQLITE_DONE );
}
- (NSArray *)columnNames;
{
int columnCount = sqlite3_column_count( stmt );
id columns = [NSMutableArray arrayWithCapacity: columnCount];
for (int i = 0; i < columnCount; i++ ) {
const char *name = sqlite3_column_name( stmt, i);
[columns addObject: [NSString stringWithUTF8String: name]];
}
return [NSArray arrayWithArray: columns];
}
- (int)columnCount;
{
return sqlite3_column_count( stmt );
}
- (NSString *)columnName: (int)inColumn;
{
const char *text = sqlite3_column_name( stmt, inColumn );
if ( text == NULL )
return nil;
return [NSString stringWithUTF8String: text];
}
- (NSString *)columnName;
{
const char *text = sqlite3_column_name( stmt, column );
if ( text == NULL )
return nil;
return [NSString stringWithUTF8String: text];
}
- (int)intValue: (int)inColumn;
{
return sqlite3_column_int( stmt, inColumn );
}
- (int)intValue;
{
return [self intValue: column++];
}
- (long long)longLongValue: (int)inColumn;
{
return sqlite3_column_int64( stmt, inColumn );
}
- (long long)longLongValue;
{
return [self longLongValue: column++];
}
- (NSString *)stringValue: (int)inColumn;
{
const char *text = (char *)sqlite3_column_text( stmt, inColumn);
if ( text == NULL )
return nil;
return [NSString stringWithUTF8String: text];
}
- (NSString *)stringValue;
{
const char *text = (char *)sqlite3_column_text( stmt, column++);
if ( text == NULL )
return nil;
return [NSString stringWithUTF8String: text];
}
- (int)columnType: (int)inColumn;
{
return sqlite3_column_type( stmt, inColumn );
}
- (int)columnType;
{
return sqlite3_column_type( stmt, column );
}
- (id)value: (int)inColumn;
{
int type = sqlite3_column_type( stmt, inColumn );
switch (type) {
case SQLITE_INTEGER:
return [NSNumber numberWithLongLong: sqlite3_column_int64( stmt, inColumn )];
case SQLITE_FLOAT:
return [NSNumber numberWithDouble: sqlite3_column_double( stmt, inColumn )];
case SQLITE_BLOB: {
const void *bytes = sqlite3_column_blob( stmt, inColumn );
return [NSData dataWithBytes: bytes
length: sqlite3_column_bytes( stmt, inColumn )];
}
case SQLITE_NULL:
return nil;
case SQLITE_TEXT:
return [NSString stringWithUTF8String: (char *)sqlite3_column_text( stmt, inColumn ) ];
default:
return nil;
}
}
- (id)value;
{
return [self value: column++];
}
- (NSDictionary *)allValues;
{
NSMutableDictionary *temp_values = [[NSMutableDictionary alloc] init];
int n = [self columnCount];
for ( int i = 0; i < n; i++ ) {
id value = [self value: i];
if (!value)
continue;
NSString *name = [self columnName: i];
[temp_values setObject: value
forKey: name];
}
NSDictionary *values = [NSDictionary dictionaryWithDictionary: temp_values];
[temp_values release];
return values;
}
- (NSArray *)bindNames;
{
NSInteger theCount = sqlite3_bind_parameter_count( stmt );
NSMutableArray *theBinds = [NSMutableArray arrayWithCapacity: theCount];
for ( NSInteger i = 1; i<=theCount; i++ ) {
[theBinds addObject: [NSString stringWithUTF8String: sqlite3_bind_parameter_name( stmt, i )]];
}
return [NSArray arrayWithArray: theBinds];
}
- (BOOL)bindLongLong: (long long)value
forIndex: (int)inIndex
error: (NSError **)outError;
{
[self setResult: sqlite3_bind_int64( stmt, inIndex+1, value )
error: outError];
return ( errorCode == SQLITE_OK );
}
- (BOOL)bindLongLong: (long long)value
error: (NSError **)outError;
{
return [self bindLongLong: value
forIndex: bind++
error: outError];
}
- (BOOL)bindNullForIndex: (int)inIndex
error: (NSError **)outError;
{
[self setResult: sqlite3_bind_null( stmt, inIndex+1 )
error: outError];
return ( errorCode == SQLITE_OK );
}
- (BOOL)bindNullWithError: (NSError **)outError;
{
return [self bindNullForIndex: bind++
error: outError];
}
- (BOOL)bindData: (NSData *)value
forIndex: (int)inIndex
error: (NSError **)outError;
{
[self setResult: sqlite3_bind_blob( stmt, inIndex+1, [value bytes], [value length], SQLITE_TRANSIENT )
error: outError];
return ( errorCode == SQLITE_OK );
}
- (BOOL)bindData: (NSData *)value
error: (NSError **)outError;
{
return [self bindData: value
forIndex: bind++
error: outError];
}
- (BOOL)bindValue: (id)value
forIndex: (int)inIndex
error: (NSError **)outError;
{
BOOL ok = NO;
if ( [value isKindOfClass: [NSData class]] ) {
ok = [self bindData: value
forIndex: inIndex
error: outError];
} else if ( [value isKindOfClass: [NSNumber class]] ) {
id str = [value description];
[self setResult: sqlite3_bind_text( stmt, inIndex+1, [str UTF8String], -1, SQLITE_TRANSIENT )
error: outError];
ok = ( errorCode == SQLITE_OK );
} else if ( [value isKindOfClass: [NSString class]] ) {
[self setResult: sqlite3_bind_text( stmt, inIndex+1, [value UTF8String], -1, SQLITE_TRANSIENT )
error: outError];
ok = ( errorCode == SQLITE_OK );
} else if ( value == [NSNull null] ) {
ok = [self bindNullForIndex: inIndex
error: outError];
} else {
id theError = [NSError errorWithDomain: @"sqlite"
code: -1
userInfo: nil];
if (outError) {
*outError = theError;
}
}
return ok;
}
- (BOOL)bindValue: (id)value
error: (NSError **)outError;
{
return [self bindValue: value
forIndex: bind++
error: outError];
}
- (NSSet *)bindDictionary: (NSDictionary *)bindings;
{
if (bindings == nil)
return nil;
id accepted = [NSMutableSet setWithCapacity: bindings.count];
NSArray *keys = [bindings allKeys];
for (NSString *key in keys) {
NSInteger bindIndex = [self findBinding: key];
if ( bindIndex < 0 ) {
continue;
}
id value = [bindings valueForKey: key];
NSError *error = nil;
[self bindValue: value
forIndex: bindIndex
error: &error];
if (error == nil) {
[accepted addObject: key];
}
}
return [NSSet setWithSet: accepted];
}
- (int)findBinding: (NSString *)name;
{
return sqlite3_bind_parameter_index( stmt, [name UTF8String]) - 1;
}
@end