-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSqliteQuery.m
278 lines (186 loc) · 5.22 KB
/
SqliteQuery.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
//
// SqliteQuery.m
//
// Sqlite Objective C wrapper by Stephen Anderson is licensed under
// a Creative Commons Attribution-ShareAlike 3.0 Unported License.
// Permissions beyond the scope of this license may be available at
// ruralcoder.com.
#import "SqliteQuery.h"
@interface SqliteQuery ()
{
sqlite3_stmt *compiledStatement;
sqlite3 *database;
}
@end
@implementation SqliteQuery
- (NSString*)errorMessage
{
return [NSString stringWithUTF8String:sqlite3_errmsg(database)];
}
- (id) initWithStatement:(NSString*)sqlStatement database:(sqlite3*)db
{
NSLog(@"Info - Query '%@'", sqlStatement);
self = [super init];
if (self)
{
int status = sqlite3_prepare_v2(db, [sqlStatement UTF8String], -1, &compiledStatement, NULL);
if (status != SQLITE_OK)
{
NSLog(@"Error %d: sqlite3_prepare_v2 '%@'", status, [self errorMessage]);
return nil;
}
}
return self;
}
- (NSString*) sqlQuery
{
return [NSString stringWithUTF8String:sqlite3_sql(compiledStatement)];
}
- (void) bindDouble:(NSInteger)columnIndex value:(double)value
{
sqlite3_bind_double(compiledStatement, columnIndex, value);
}
- (void) bindInt:(NSInteger)columnIndex value:(int)value;
{
sqlite3_bind_int(compiledStatement, columnIndex, value);
}
- (void) bindInt64:(NSInteger)columnIndex value:(sqlite3_int64)value;
{
sqlite3_bind_int64(compiledStatement, columnIndex, value);
}
- (void) bindNull:(NSInteger)columnIndex;
{
sqlite3_bind_null(compiledStatement, columnIndex);
}
- (void) bindText:(NSInteger)columnIndex value:(NSString*)value;
{
sqlite3_bind_text(compiledStatement, columnIndex, [value UTF8String], -1, SQLITE_TRANSIENT);
}
- (void) bindText16:(NSInteger)columnIndex value:(NSString*)value;
{
sqlite3_bind_text16(compiledStatement, columnIndex, value, -1, SQLITE_TRANSIENT);
}
- (void) bindValue:(NSInteger)columnIndex value:(const sqlite3_value*)value;
{
sqlite3_bind_value(compiledStatement, columnIndex, value);
}
- (void) bindZeroBlob:(NSInteger)columnIndex length:(int)length;
{
sqlite3_bind_zeroblob(compiledStatement, columnIndex, length);
}
- (void) bindBlob:(NSInteger)columnIndex data:(NSData*)data
{
sqlite3_bind_blob(compiledStatement, columnIndex, [data bytes], [data length], SQLITE_TRANSIENT);
}
- (NSInteger) parameterCount
{
return sqlite3_bind_parameter_count(compiledStatement);
}
- (NSString*) parameterName:(NSInteger)index
{
return [NSString stringWithUTF8String:sqlite3_bind_parameter_name(compiledStatement, index)];
}
- (NSInteger) findParameterByName:(NSString*)name
{
return 0;
}
- (void) clearBindings
{
sqlite3_clear_bindings(compiledStatement);
}
- (NSInteger) columnCount
{
return sqlite3_column_count(compiledStatement);
}
- (NSString*) columnName:(NSInteger)index
{
return [NSString stringWithUTF8String:sqlite3_column_name(compiledStatement, index)];
}
- (NSInteger) findColumnByName:(NSString*)name
{
return 0;
}
- (NSData*) columnBlob:(NSInteger)columnIndex
{
NSData* blob = nil;
NSInteger blobSize = sqlite3_column_bytes(compiledStatement, columnIndex);
blob = [NSData dataWithBytes:sqlite3_column_blob(compiledStatement, columnIndex)
length:blobSize];
return blob;
}
- (NSInteger) columnBytes:(NSInteger)columnIndex;
{
return sqlite3_column_bytes(compiledStatement, columnIndex);
}
- (NSInteger) columnBytes16:(NSInteger)columnIndex;
{
return sqlite3_column_bytes16(compiledStatement, columnIndex);
}
- (double) columnDouble:(NSInteger)columnIndex;
{
return sqlite3_column_double(compiledStatement, columnIndex);
}
- (NSInteger) columnInt:(NSInteger)columnIndex;
{
return sqlite3_column_int(compiledStatement, columnIndex);
}
- (sqlite3_int64) columnInt64:(NSInteger)columnIndex;
{
return sqlite3_column_int64(compiledStatement, columnIndex);
}
- (NSString*) columnText:(NSInteger)columnIndex;
{
return [NSString stringWithUTF8String:(const char *)sqlite3_column_text(compiledStatement, columnIndex)];
}
- (NSString*) columnText16:(NSInteger)columnIndex;
{
return nil;
}
- (NSInteger) columnType:(NSInteger)columnIndex;
{
return sqlite3_column_type(compiledStatement, columnIndex);
}
- (sqlite3_value*) columnValue:(NSInteger)columnIndex
{
return sqlite3_column_value(compiledStatement, columnIndex );
}
- (id) columnObject:(NSInteger)columnIndex
{
SqliteDataTypes type = sqlite3_column_type(compiledStatement, columnIndex);
switch (type) {
case SqliteDataTypeInteger:
return [NSNumber numberWithInteger:[self columnInt:columnIndex]];
case SqliteDataTypeBlob:
return [self columnBlob:columnIndex];
case SqliteDataTypeText:
return [self columnText:columnIndex];
case SqliteDataTypeFloat:
return [NSNumber numberWithDouble:[self columnDouble:columnIndex]];
case SqliteDataTypeNull:
default:
return nil;
}
}
- (SqliteCodes) step
{
SqliteCodes status = sqlite3_step(compiledStatement);
if (status != SQLITE_ROW && status != SQLITE_DONE)
NSLog(@"Error %d: SqliteQuery::Step '%@'", status, [self errorMessage]);
return status;
}
- (void) next
{
sqlite3_next_stmt(database, compiledStatement);
}
- (void) reset
{
sqlite3_reset(compiledStatement);
}
- (void) dealloc
{
sqlite3_finalize(compiledStatement);
compiledStatement = nil;
database = nil;
[super dealloc];
}
@end