24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
+
+
+
|
#import "MTXSQLite3Storage.h"
@implementation MTXSQLite3Storage
{
SL3Connection *_conn;
SL3PreparedStatement *_nextBatchSetStatement, *_nextBatchGetStatement;
SL3PreparedStatement *_joinedRoomsAddStatement;
SL3PreparedStatement *_joinedRoomsRemoveStatement;
SL3PreparedStatement *_joinedRoomsGetStatement;
}
+ (instancetype)storageWithPath: (OFString *)path
{
return [[[self alloc] initWithPath: path] autorelease];
}
|
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
|
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
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
|
@" device_id, next_batch\n"
@") VALUES (\n"
@" $device_id, $next_batch\n"
@")"] retain];
_nextBatchGetStatement = [[_conn prepareStatement:
@"SELECT next_batch FROM next_batch\n"
@"WHERE device_id=$device_id"] retain];
_joinedRoomsAddStatement = [[_conn prepareStatement:
@"INSERT INTO joined_rooms (\n"
@" user_id, room_id\n"
@") VALUES (\n"
@" $user_id, $room_id\n"
@")"] retain];
_joinedRoomsRemoveStatement = [[_conn prepareStatement:
@"DELETE FROM joined_rooms\n"
@"WHERE user_id=$user_id AND room_id=$room_id"] retain];
_joinedRoomsGetStatement = [[_conn prepareStatement:
@"SELECT room_id FROM joined_rooms\n"
@"WHERE user_id=$user_id"] retain];
objc_autoreleasePoolPop(pool);
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[_nextBatchSetStatement release];
[_nextBatchGetStatement release];
[_joinedRoomsAddStatement release];
[_joinedRoomsRemoveStatement release];
[_joinedRoomsGetStatement release];
[_conn release];
[super dealloc];
}
- (void)createTables
{
[_conn executeStatement: @"CREATE TABLE IF NOT EXISTS next_batch (\n"
@" device_id TEXT PRIMARY KEY,\n"
@" next_batch TEXT\n"
@")"];
[_conn executeStatement:
@"CREATE TABLE IF NOT EXISTS next_batch (\n"
@" device_id TEXT PRIMARY KEY,\n"
@" next_batch TEXT\n"
@");\n"
@"CREATE TABLE IF NOT EXISTS joined_rooms (\n"
@" user_id TEXT,\n"
@" room_id TEXT,\n"
@" PRIMARY KEY (user_id, room_id)\n"
@");"];
}
- (void)transactionWithBlock: (mtx_storage_transaction_block_t)block
{
[_conn transactionWithBlock: block];
}
|
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
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
|
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
@"$device_id": deviceID
}];
if (![_nextBatchGetStatement step])
return nil;
OFString *nextBatch =
[[_nextBatchGetStatement rowDictionary][@"next_batch"] retain];
[_nextBatchGetStatement.rowDictionary[@"next_batch"] retain];
objc_autoreleasePoolPop(pool);
return [nextBatch autorelease];
}
- (void)addJoinedRoom: (OFString *)roomID
forUser: (OFString *)userID
{
void *pool = objc_autoreleasePoolPush();
[_joinedRoomsAddStatement reset];
[_joinedRoomsAddStatement bindWithDictionary: @{
@"$room_id": roomID,
@"$user_id": userID
}];
[_joinedRoomsAddStatement step];
objc_autoreleasePoolPop(pool);
}
- (void)removeJoinedRoom: (OFString *)roomID
forUser: (OFString *)userID
{
void *pool = objc_autoreleasePoolPush();
[_joinedRoomsRemoveStatement reset];
[_joinedRoomsRemoveStatement bindWithDictionary: @{
@"$room_id": roomID,
@"$user_id": userID
}];
[_joinedRoomsRemoveStatement step];
objc_autoreleasePoolPop(pool);
}
- (OFArray<OFString *> *)joinedRoomsForUser: (OFString *)userID
{
OFMutableArray *joinedRooms = [OFMutableArray array];
void *pool = objc_autoreleasePoolPush();
[_joinedRoomsGetStatement reset];
[_joinedRoomsGetStatement bindWithDictionary: @{
@"$user_id": userID
}];
while ([_joinedRoomsGetStatement step])
[joinedRooms addObject:
_joinedRoomsGetStatement.rowDictionary[@"room_id"]];
objc_autoreleasePoolPop(pool);
return [joinedRooms autorelease];
}
@end
|