-
Notifications
You must be signed in to change notification settings - Fork 38
/
AppGrouping.m
275 lines (237 loc) · 8.76 KB
/
AppGrouping.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
//
// AppGrouping.m
// ASiST
//
// Created by Oliver on 21.10.09.
// Copyright 2009 Drobnik.com. All rights reserved.
//
#import "AppGrouping.h"
#import "App.h"
static sqlite3_stmt *init_statement = nil;
static sqlite3_stmt *init_apps_statement = nil;
static sqlite3_stmt *insert_statement = nil;
static sqlite3_stmt *insert_apps_statement = nil;
static sqlite3_stmt *update_statement = nil;
static sqlite3_stmt *delete_statement = nil;
@implementation AppGrouping
@synthesize myDescription, apps, primaryKey;
- (id) initWithAppSet:(NSSet *)appSet
{
if (self = [super init])
{
apps = [[NSMutableSet alloc] initWithSet:appSet];
}
return self;
}
- (void) dealloc
{
[apps release];
[myDescription release];
[super dealloc];
}
- (BOOL)containsAppsOfSet:(NSSet *)appSet
{
return [appSet intersectsSet:apps];
}
- (void)appendAppsOfSet:(NSSet *)appSet
{
int previousCount = [apps count];
[apps unionSet:appSet];
if (previousCount < [apps count])
{
// something added
assignmentsChanged = YES;
}
}
#pragma mark Database
- (id)initWithPrimaryKey:(NSInteger)pk database:(sqlite3 *)db {
if (self = [super init])
{
primaryKey = pk;
database = db;
if (init_statement == nil)
{
const char *sql = "SELECT description FROM AppGrouping WHERE id=?";
if (sqlite3_prepare_v2(database, sql, -1, &init_statement, NULL) != SQLITE_OK)
{
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
sqlite3_bind_int(init_statement, 1, primaryKey);
if (sqlite3_step(init_statement) == SQLITE_ROW)
{
self.myDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement, 0)];
} else {
}
// Reset the statement for future reuse.
sqlite3_reset(init_statement);
// load app assigments for this grouping
apps = [[NSMutableSet alloc] init];
if (init_apps_statement == nil)
{
const char *sql = "SELECT app_id FROM AppAppGrouping WHERE appgrouping_id=?";
if (sqlite3_prepare_v2(database, sql, -1, &init_apps_statement, NULL) != SQLITE_OK)
{
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
sqlite3_bind_int(init_apps_statement, 1, primaryKey);
while (sqlite3_step(init_apps_statement) == SQLITE_ROW)
{
NSUInteger appID = sqlite3_column_int(init_apps_statement, 0);
App *assignedApp = [DB appForID:appID];
[apps addObject:assignedApp];
}
// Reset the statement for future reuse.
sqlite3_reset(init_apps_statement);
}
return self;
}
- (void)insertAppGroupingsIntoDatabase
{
if (insert_apps_statement == nil) {
static char *sql = "INSERT INTO AppAppGrouping(appgrouping_id, app_id) VALUES(?, ?)";
if (sqlite3_prepare_v2(database, sql, -1, &insert_apps_statement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
for (App *oneApp in apps)
{
sqlite3_bind_int(insert_apps_statement, 1, primaryKey);
sqlite3_bind_int(insert_apps_statement, 2, oneApp.apple_identifier);
int success = sqlite3_step(insert_apps_statement);
// Because we want to reuse the statement, we "reset" it instead of "finalizing" it.
sqlite3_reset(insert_apps_statement);
if (success == SQLITE_ERROR) {
NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
}
}
}
- (void)deleteAppGroupingsFromDatabase
{
sqlite3_stmt *tmp_statement = NULL;
const char *sql = "DELETE FROM AppAppGrouping WHERE appgrouping_id=?";
if (sqlite3_prepare_v2(database, sql, -1, &tmp_statement, NULL) != SQLITE_OK)
{
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
else
{
// Bind the primary key variable.
sqlite3_bind_int(tmp_statement, 1, primaryKey);
// Execute the query.
int success = sqlite3_step(tmp_statement);
sqlite3_finalize(tmp_statement);
// Handle errors.
if (success != SQLITE_DONE)
{
NSAssert1(0, @"Error: failed to delete from database with message '%s'.", sqlite3_errmsg(database));
}
}
}
- (void)insertIntoDatabase:(sqlite3 *)db {
database = db;
if (insert_statement == nil) {
static char *sql = "INSERT INTO AppGrouping(description) VALUES(?)";
if (sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
sqlite3_bind_text(insert_statement, 1, [@"No Description" UTF8String], -1, SQLITE_TRANSIENT);
int success = sqlite3_step(insert_statement);
// Because we want to reuse the statement, we "reset" it instead of "finalizing" it.
sqlite3_reset(insert_statement);
if (success == SQLITE_ERROR) {
NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
} else {
// SQLite provides a method which retrieves the value of the most recently auto-generated primary key sequence
// in the database. To access this functionality, the table should have a column declared of type
// "INTEGER PRIMARY KEY"
primaryKey = sqlite3_last_insert_rowid(database);
[self insertAppGroupingsIntoDatabase];
}
}
- (void)updateInDatabase {
// Compile the delete statement if needed.
if (update_statement == nil) {
const char *sql = "UPDATE AppGrouping set description = ? WHERE id=?";
if (sqlite3_prepare_v2(database, sql, -1, &update_statement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
// Bind the primary key variable.
sqlite3_bind_text(update_statement, 1, [myDescription UTF8String], -1, SQLITE_TRANSIENT);
// Execute the query.
int success = sqlite3_step(update_statement);
// Reset the statement for future use.
sqlite3_reset(update_statement);
// Handle errors.
if (success != SQLITE_DONE) {
NSAssert1(0, @"Error: failed to update in database with message '%s'.", sqlite3_errmsg(database));
}
if (assignmentsChanged)
{
// remove existing assignments
[self deleteAppGroupingsFromDatabase];
// add all new assignments
[self insertAppGroupingsIntoDatabase];
}
}
- (void)deleteFromDatabase {
// Compile the delete statement if needed.
if (delete_statement == nil) {
const char *sql = "DELETE FROM AppGrouping WHERE id=?";
if (sqlite3_prepare_v2(database, sql, -1, &delete_statement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
// Bind the primary key variable.
sqlite3_bind_int(delete_statement, 1, primaryKey);
// Execute the query.
int success = sqlite3_step(delete_statement);
// Reset the statement for future use.
sqlite3_reset(delete_statement);
// Handle errors.
if (success != SQLITE_DONE) {
NSAssert1(0, @"Error: failed to delete from database with message '%s'.", sqlite3_errmsg(database));
}
}
- (void)snatchReportsFromAppGrouping:(AppGrouping *)otherGrouping
{
sqlite3_stmt *statement;
const char *sql = "UPDATE Report SET AppGrouping_id = ? WHERE AppGrouping_id=?";
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) != SQLITE_OK)
{
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
// Bind the primary key variable.
sqlite3_bind_int(statement, 1, primaryKey);
sqlite3_bind_int(statement, 2, otherGrouping.primaryKey);
// Execute the query.
int success = sqlite3_step(statement);
// Reset the statement for future use.
// Handle errors.
if (success != SQLITE_DONE)
{
NSAssert1(0, @"Error: failed to update from database with message '%s'.", sqlite3_errmsg(database));
}
sqlite3_finalize(statement);
const char *sql2 = "UPDATE AppAppGrouping SET AppGrouping_id = ? WHERE AppGrouping_id=?";
if (sqlite3_prepare_v2(database, sql2, -1, &statement, NULL) != SQLITE_OK)
{
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
// Bind the primary key variable.
sqlite3_bind_int(statement, 1, primaryKey);
sqlite3_bind_int(statement, 2, otherGrouping.primaryKey);
// Execute the query.
success = sqlite3_step(statement);
// Reset the statement for future use.
// Handle errors.
if (success != SQLITE_DONE)
{
NSAssert1(0, @"Error: failed to update from database with message '%s'.", sqlite3_errmsg(database));
}
sqlite3_finalize(statement);
}
@end