-
Notifications
You must be signed in to change notification settings - Fork 7
/
SQLite3Connection.m
225 lines (173 loc) · 6.74 KB
/
SQLite3Connection.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
//
// SQLite3Connection.m
// SQLite3
//
// Created by Mirek Rusin on 25/06/2011.
// Copyright 2011 Inteliv Ltd. All rights reserved.
//
#import "SQLite3Connection.h"
@implementation SQLite3Connection
@synthesize connection;
@synthesize delegate;
- (id) initWithConnection: (SQLite3ConnectionRef) anConnection {
if ((self = [super init])) {
connection = SQLite3ConnectionRetain(anConnection);
delegate = nil;
}
return self;
}
- (id) initWithPath: (NSString *) path flags: (SQLite3OpenOptions) flags zVfs: (const char *) zVfs {
if ((self = [super init])) {
connection = SQLite3ConnectionCreate(kCFAllocatorDefault, (CFStringRef)path, flags, zVfs);
delegate = nil;
}
return self;
}
- (id) initWithPath: (NSString *) path flags: (SQLite3OpenOptions) flags {
if ((self = [self initWithPath: path flags: flags zVfs: NULL])) {
delegate = nil;
}
return self;
}
//- (id) initWithPath: (NSString *) path {
// if ((self = [self initWithPath: path flags:]
//}
//+ (id) connection: (NSString *) name;
- (void) dealloc {
SQLite3ConnectionRelease(connection);
[super dealloc];
}
- (void) setDelegate: (id<SQLite3ConnectionDelegate>) value {
if (delegate != value) {
delegate = value;
}
}
//- (void) replaceBusyHandlerWithDefaultBusyTimeout: (int) bt {
////- (void) setBusyTimeout {
//// SQLite3ConnectionGetBu
//// SQLite3ConnectionSetBusyTimeout(<#SQLite3ConnectionRef connection#>, <#CFTimeInterval ti#>)
//}
- (SQLite3Status) status {
return SQLite3ConnectionGetStatus(connection);
}
- (SQLite3Statement *) createStatementWithQuery: (NSString *) sql {
return [[SQLite3Statement alloc] initWithConnection: self query: sql];
}
- (SQLite3Statement *) statementWithQuery: (NSString *) sql {
return [[self createStatementWithQuery: sql] autorelease];
}
- (SQLite3Status) enumerateWithQuery: (NSString *) sql usingBlock: (void (^)(NSDictionary *row, BOOL *terminate)) block {
SQLite3Status status = kSQLite3StatusOK;
SQLite3Statement *statement = [self createStatementWithQuery: sql];
BOOL terminate = NO;
for (NSDictionary *row in statement) {
block(row, &terminate);
if (terminate) {
break;
}
}
// status = [statement status];
[statement release];
return status;
}
- (SQLite3Status) execute: (NSString *) sql {
return SQLite3ConnectionExecute(connection, (CFStringRef) sql);
}
- (SQLite3Status) execute: (NSString *) sql withDictionaryBindings: (NSDictionary *) dictionary {
return SQLite3ConnectionExecuteWithDictionaryBindings(connection, (CFStringRef)sql, (CFDictionaryRef)dictionary);
}
- (SQLite3Status) execute: (NSString *) sql withArrayBindings: (NSArray *) array {
return SQLite3ConnectionExecuteWithArrayBindings(connection, (CFStringRef)sql, (CFArrayRef)array);
}
#pragma mark Value results
- (boolean_t) boolWithQuery: (NSString *) sql {
return SQLite3ConnectionGetBOOLWithQuery(connection, (CFStringRef)sql);
}
- (int32_t) intWithQuery: (NSString *) sql {
return SQLite3ConnectionGetInt32WithQuery(connection, (CFStringRef)sql);
}
- (int64_t) int64WithQuery: (NSString *) sql {
return SQLite3ConnectionGetInt64WithQuery(connection, (CFStringRef)sql);
}
- (double_t) doubleWithQuery: (NSString *) sql {
return SQLite3ConnectionGetDoubleWithQuery(connection, (CFStringRef)sql);
}
#pragma mark Reference counted object results
- (id) createObjectWithQuery: (NSString *) sql {
return (id)SQLite3ConnectionCreateCFTypeWithQuery(connection, (CFStringRef)sql);
}
- (NSString *) createStringWithQuery: (NSString *) sql {
return (NSString *)SQLite3ConnectionCreateStringWithQuery(connection, (CFStringRef)sql);
}
- (NSNumber *) createNumberWithQuery: (NSString *) sql {
return (NSNumber *)SQLite3ConnectionCreateNumberWithQuery(connection, (CFStringRef)sql);
}
- (NSDate *) createDateWithQuery: (NSString *) sql {
return (NSDate *)SQLite3ConnectionCreateDateWithQuery(connection, (CFStringRef)sql);
}
- (NSData *) createDataWithQuery: (NSString *) sql {
return (NSData *)SQLite3ConnectionCreateDataWithQuery(connection, (CFStringRef)sql);
}
#pragma mark Autoreleased object results
- (id) objectWithQuery: (NSString *) sql {
return [[self createObjectWithQuery: sql] autorelease];
}
- (NSString *) stringWithQuery: (NSString *) sql {
return [[self createStringWithQuery: sql] autorelease];
}
- (NSNumber *) numberWithQuery: (NSString *) sql {
return [[self createNumberWithQuery: sql] autorelease];
}
- (NSDate *) dateWithQuery: (NSString *) sql {
return [[self createDateWithQuery: sql] autorelease];
}
- (NSData *) dataWithQuery: (NSString *) sql {
return [[self createDataWithQuery: sql] autorelease];
}
#pragma mark Utility functions
- (BOOL) doesTableExist: (NSString *) name {
return SQLite3ConnectionDoesTableExist(connection, (CFStringRef)name);
}
- (BOOL) doesViewExist: (NSString *) name {
return SQLite3ConnectionDoesViewExist(connection, (CFStringRef)name);
}
- (BOOL) doesTableOrViewExist: (NSString *) name {
return SQLite3ConnectionDoesTableOrViewExist(connection, (CFStringRef)name);
}
- (BOOL) doesIndexExist: (NSString *) name {
return SQLite3ConnectionDoesIndexExist(connection, (CFStringRef)name);
}
- (SQLite3Status) dropTable: (NSString *) name {
return SQLite3ConnectionDropTable(connection, (CFStringRef)name);
}
- (SQLite3Status) dropView: (NSString *) name {
return SQLite3ConnectionDropView(connection, (CFStringRef)name);
}
- (SQLite3Status) dropIndex: (NSString *) name {
return SQLite3ConnectionDropIndex(connection, (CFStringRef)name);
}
- (SQLite3Status) dropTableIfExists: (NSString *) name {
return SQLite3ConnectionDropTableIfExists(connection, (CFStringRef)name);
}
- (SQLite3Status) dropViewIfExists: (NSString *) name {
return SQLite3ConnectionDropViewIfExists(connection, (CFStringRef)name);
}
- (SQLite3Status) dropIndexIfExists: (NSString *) name {
return SQLite3ConnectionDropIndexIfExists(connection, (CFStringRef)name);
}
- (BOOL) doesTableOrView: (NSString *) name haveRowWithID: (int64_t) rowID {
return SQLite3ConnectionDoesTableOrViewHaveRowWithID(connection, (CFStringRef)name, rowID);
}
- (NSArray *) createArrayWithTableOrView: (NSString *) name forRowID: (int64_t) rowID {
return (NSArray *)SQLite3ConnectionCreateArrayWithTableOrViewForRowID(connection, (CFStringRef)name, rowID);
}
- (NSDictionary *) createDictionaryWithTableOrView: (NSString *) name forRowID: (int64_t) rowID {
return (NSDictionary *)SQLite3ConnectionCreateDictionaryWithTableOrViewForRowID(connection, (CFStringRef)name, rowID);
}
- (NSDictionary *) dictionaryWithTableOrView: (NSString *) name forRowID: (int64_t) rowID {
return [[self createDictionaryWithTableOrView: name forRowID: rowID] autorelease];
}
- (NSArray *) arrayWithTableOrView: (NSString *) name forRowID: (int64_t) rowID {
return [[self createArrayWithTableOrView: name forRowID: rowID] autorelease];
}
@end