12
12
#import < sqlite3.h>
13
13
14
14
#import " PFSQLiteStatement.h"
15
+ #import " PFThreadsafety.h"
15
16
16
17
@interface PFSQLiteDatabaseResult ()
17
18
18
19
@property (nonatomic , copy , readonly ) NSDictionary *columnNameToIndexMap;
19
20
@property (nonatomic , strong , readonly ) PFSQLiteStatement *statement;
21
+ @property (nonatomic , strong , readonly ) dispatch_queue_t databaseQueue;
20
22
21
23
@end
22
24
23
25
@implementation PFSQLiteDatabaseResult
24
26
25
27
@synthesize columnNameToIndexMap = _columnNameToIndexMap;
26
28
27
- - (instancetype )initWithStatement : (PFSQLiteStatement *)stmt {
29
+ - (instancetype )initWithStatement : (PFSQLiteStatement *)stmt queue : ( dispatch_queue_t ) queue {
28
30
if ((self = [super init ])) {
29
31
_statement = stmt;
32
+ _databaseQueue = queue;
30
33
}
31
34
return self;
32
35
}
@@ -36,7 +39,9 @@ - (BOOL)next {
36
39
}
37
40
38
41
- (int )step {
39
- return sqlite3_step ([self .statement sqliteStatement ]);
42
+ return PFThreadSafetyPerform (_databaseQueue, ^{
43
+ return sqlite3_step ([self .statement sqliteStatement ]);
44
+ });
40
45
}
41
46
42
47
- (BOOL )close {
@@ -48,47 +53,57 @@ - (int)intForColumn:(NSString *)columnName {
48
53
}
49
54
50
55
- (int )intForColumnIndex : (int )columnIndex {
51
- return sqlite3_column_int ([self .statement sqliteStatement ], columnIndex);
56
+ return PFThreadSafetyPerform (_databaseQueue, ^{
57
+ return sqlite3_column_int ([self .statement sqliteStatement ], columnIndex);
58
+ });
52
59
}
53
60
54
61
- (long )longForColumn : (NSString *)columnName {
55
62
return [self longForColumnIndex: [self columnIndexForName: columnName]];
56
63
}
57
64
58
65
- (long )longForColumnIndex : (int )columnIndex {
59
- return (long )sqlite3_column_int64 ([self .statement sqliteStatement ], columnIndex);
66
+ return PFThreadSafetyPerform (_databaseQueue, ^{
67
+ return (long )sqlite3_column_int64 ([self .statement sqliteStatement ], columnIndex);
68
+ });
60
69
}
61
70
62
71
- (BOOL )boolForColumn : (NSString *)columnName {
63
72
return [self boolForColumnIndex: [self columnIndexForName: columnName]];
64
73
}
65
74
66
75
- (BOOL )boolForColumnIndex : (int )columnIndex {
67
- return ([self intForColumnIndex: columnIndex] != 0 );
76
+ return PFThreadSafetyPerform (_databaseQueue, ^{
77
+ return ([self intForColumnIndex: columnIndex] != 0 );
78
+ });
68
79
}
69
80
70
81
- (double )doubleForColumn : (NSString *)columnName {
71
82
return [self doubleForColumnIndex: [self columnIndexForName: columnName]];
72
83
}
73
84
74
85
- (double )doubleForColumnIndex : (int )columnIndex {
75
- return sqlite3_column_double ([self .statement sqliteStatement ], columnIndex);
86
+ return PFThreadSafetyPerform (_databaseQueue, ^{
87
+ return sqlite3_column_double ([self .statement sqliteStatement ], columnIndex);
88
+ });
76
89
}
77
90
78
91
- (NSString *)stringForColumn : (NSString *)columnName {
79
92
return [self stringForColumnIndex: [self columnIndexForName: columnName]];
80
93
}
81
94
82
95
- (NSString *)stringForColumnIndex : (int )columnIndex {
83
- if ([self columnIndexIsNull: columnIndex]) {
84
- return nil ;
85
- }
96
+ return PFThreadSafetyPerform (_databaseQueue, ^NSString *{
97
+ if ([self columnIndexIsNull: columnIndex]) {
98
+ return nil ;
99
+ }
86
100
87
- const char *str = (const char *)sqlite3_column_text ([self .statement sqliteStatement ], columnIndex);
88
- if (!str) {
89
- return nil ;
90
- }
91
- return [NSString stringWithUTF8String: str];
101
+ const char *str = (const char *)sqlite3_column_text ([self .statement sqliteStatement ], columnIndex);
102
+ if (!str) {
103
+ return nil ;
104
+ }
105
+ return [NSString stringWithUTF8String: str];
106
+ });
92
107
}
93
108
94
109
- (NSDate *)dateForColumn : (NSString *)columnName {
@@ -105,42 +120,48 @@ - (NSData *)dataForColumn:(NSString *)columnName {
105
120
}
106
121
107
122
- (NSData *)dataForColumnIndex : (int )columnIndex {
108
- if ([self columnIndexIsNull: columnIndex]) {
109
- return nil ;
110
- }
123
+ return PFThreadSafetyPerform (_databaseQueue, ^NSData *{
124
+ if ([self columnIndexIsNull: columnIndex]) {
125
+ return nil ;
126
+ }
111
127
112
- int size = sqlite3_column_bytes ([self .statement sqliteStatement ], columnIndex);
113
- const char *buffer = sqlite3_column_blob ([self .statement sqliteStatement ], columnIndex);
114
- if (buffer == nil ) {
115
- return nil ;
116
- }
117
- return [NSData dataWithBytes: buffer length: size];
128
+ int size = sqlite3_column_bytes ([self .statement sqliteStatement ], columnIndex);
129
+ const char *buffer = sqlite3_column_blob ([self .statement sqliteStatement ], columnIndex);
130
+ if (buffer == nil ) {
131
+ return nil ;
132
+ }
133
+ return [NSData dataWithBytes: buffer length: size];
134
+ });
118
135
}
119
136
120
137
- (id )objectForColumn : (NSString *)columnName {
121
138
return [self objectForColumnIndex: [self columnIndexForName: columnName]];
122
139
}
123
140
124
141
- (id )objectForColumnIndex : (int )columnIndex {
125
- int columnType = sqlite3_column_type ([self .statement sqliteStatement ], columnIndex);
126
- switch (columnType) {
127
- case SQLITE_INTEGER:
128
- return @([self longForColumnIndex: columnIndex]);
129
- case SQLITE_FLOAT:
130
- return @([self doubleForColumnIndex: columnIndex]);
131
- case SQLITE_BLOB:
132
- return [self dataForColumnIndex: columnIndex];
133
- default :
134
- return [self stringForColumnIndex: columnIndex];
135
- }
142
+ return PFThreadSafetyPerform (_databaseQueue, ^id {
143
+ int columnType = sqlite3_column_type ([self .statement sqliteStatement ], columnIndex);
144
+ switch (columnType) {
145
+ case SQLITE_INTEGER:
146
+ return @([self longForColumnIndex: columnIndex]);
147
+ case SQLITE_FLOAT:
148
+ return @([self doubleForColumnIndex: columnIndex]);
149
+ case SQLITE_BLOB:
150
+ return [self dataForColumnIndex: columnIndex];
151
+ default :
152
+ return [self stringForColumnIndex: columnIndex];
153
+ }
154
+ });
136
155
}
137
156
138
157
- (BOOL )columnIsNull : (NSString *)columnName {
139
158
return [self columnIndexIsNull: [self columnIndexForName: columnName]];
140
159
}
141
160
142
161
- (BOOL )columnIndexIsNull : (int )columnIndex {
143
- return (sqlite3_column_type ([self .statement sqliteStatement ], columnIndex) == SQLITE_NULL);
162
+ return PFThreadSafetyPerform (_databaseQueue, ^{
163
+ return (sqlite3_column_type ([self .statement sqliteStatement ], columnIndex) == SQLITE_NULL);
164
+ });
144
165
}
145
166
146
167
- (int )columnIndexForName : (NSString *)columnName {
@@ -154,13 +175,15 @@ - (int)columnIndexForName:(NSString *)columnName {
154
175
155
176
- (NSDictionary *)columnNameToIndexMap {
156
177
if (!_columnNameToIndexMap) {
157
- int columnCount = sqlite3_column_count ([self .statement sqliteStatement ]);
158
- NSMutableDictionary *mutableColumnNameToIndexMap = [[NSMutableDictionary alloc ] initWithCapacity: columnCount];
159
- for (int i = 0 ; i < columnCount; ++i) {
160
- NSString *key = [NSString stringWithUTF8String: sqlite3_column_name ([self .statement sqliteStatement ], i)];
161
- mutableColumnNameToIndexMap[[key lowercaseString ]] = @(i);
162
- }
163
- _columnNameToIndexMap = mutableColumnNameToIndexMap;
178
+ PFThreadsafetySafeDispatchSync (_databaseQueue, ^{
179
+ int columnCount = sqlite3_column_count ([self .statement sqliteStatement ]);
180
+ NSMutableDictionary *mutableColumnNameToIndexMap = [[NSMutableDictionary alloc ] initWithCapacity: columnCount];
181
+ for (int i = 0 ; i < columnCount; ++i) {
182
+ NSString *key = [NSString stringWithUTF8String: sqlite3_column_name ([self .statement sqliteStatement ], i)];
183
+ mutableColumnNameToIndexMap[[key lowercaseString ]] = @(i);
184
+ }
185
+ _columnNameToIndexMap = mutableColumnNameToIndexMap;
186
+ });
164
187
}
165
188
return _columnNameToIndexMap;
166
189
}
0 commit comments