forked from overcyn/Enqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PRDb.m
425 lines (373 loc) · 15.5 KB
/
PRDb.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#import "PRCore.h"
#import "PRAlbumArtController.h"
#import "PRDb.h"
#import "PRDefaults.h"
#import "PRHistory.h"
#import "PRLibrary.h"
#import "PRPlaybackOrder.h"
#import "PRPlaylists.h"
#import "PRQueue.h"
#import "PRStatement.h"
#import "PRUpdate060Operation.h"
#import "sqlite_str.h"
#include <ctype.h>
#include <string.h>
#include <sys/file.h>
#import "PRLog.h"
#import "PRException.h"
NSString * const PRFilePboardType = @"PRFilePboardType";
NSString * const PRIndexesPboardType = @"PRIndexesPboardType";
NSString * const PRColFloat = @"PRColFloat";
NSString * const PRColInteger = @"PRColInteger";
NSString * const PRColString = @"PRColString";
NSString * const PRColData = @"PRColData";
@implementation PRDb {
sqlite3 *sqlDb;
PRHistory *history;
PRLibrary *library;
PRPlaylists *playlists;
PRQueue *queue;
PRPlaybackOrder *playbackOrder;
PRAlbumArtController *albumArtController;
int _transaction;
NSMutableDictionary *_cachedStatements;
__weak PRCore *_core;
}
#pragma mark - Initialization
- (id)initWithCore:(PRCore *)core {
if (!(self = [super init])) {return nil;}
_core = core;
history = [[PRHistory alloc] initWithDb:self];
library = [[PRLibrary alloc] initWithDb:self];
playlists = [[PRPlaylists alloc] initWithDb:self];
queue = [[PRQueue alloc] initWithDb:self];
playbackOrder = [[PRPlaybackOrder alloc] initWithDb:self];
albumArtController = [[PRAlbumArtController alloc] initWithDb:self];
_cachedStatements = [[NSMutableDictionary alloc] init];
_transaction = 0;
BOOL e = ([[[NSFileManager alloc] init] fileExistsAtPath:[[PRDefaults sharedDefaults] libraryPath] isDirectory:nil] &&
[self open] && [self update] && [self initialize]);
if (!e) {
NSLog(@"create");
NSError *err;
int e = [self move:&err];
if (err && e) {
[[PRLog sharedLog] presentError:err];
} else if (err && !e) {
[[PRLog sharedLog] presentFatalError:err];
}
if (![self open]) {
[[PRLog sharedLog] presentFatalError:[self databaseCouldNotBeInitializedError]];
}
[self create];
if (![self initialize]) {
[[PRLog sharedLog] presentFatalError:[self databaseCouldNotBeInitializedError]];
}
}
return self;
}
#pragma mark - Initialization Priv
- (BOOL)open {
if (sqlite3_initialize() != SQLITE_OK) {
return NO;
}
sqlite3_close(sqlDb);
return (SQLITE_OK == sqlite3_open_v2([[[PRDefaults sharedDefaults] libraryPath] fileSystemRepresentation], &sqlDb,
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL) &&
SQLITE_OK == sqlite3_extended_result_codes(sqlDb, YES) &&
SQLITE_OK == sqlite3_exec(sqlDb, "PRAGMA foreign_keys = ON", NULL, NULL, NULL) &&
SQLITE_OK == sqlite3_create_collation(sqlDb, "NOCASE2", SQLITE_UTF16, NULL, no_case) &&
SQLITE_OK == sqlite3_create_function_v2(sqlDb, "hfs_begins", 2, SQLITE_UTF16, NULL, hfs_begins, NULL, NULL, NULL) &&
SQLITE_OK == sqlite3_create_collation(sqlDb, "hfs_compare", SQLITE_UTF16, NULL, hfs_compare));
}
- (BOOL)initialize {
return (
[history initialize] &&
[library initialize] &&
[playlists initialize] &&
[queue initialize]);
}
- (BOOL)update {
NSArray *result = [self attempt:@"SELECT version FROM schema_version" bindings:nil columns:@[PRColInteger]];
if (!result || [result count] != 1) {
return NO;
}
int version = [[[result objectAtIndex:0] objectAtIndex:0] intValue];
if (version == 1) {
[self begin];
BOOL e = ([self attempt:@"DROP TABLE IF EXISTS now_playing_view_source"] &&
[self attempt:@"DROP TABLE IF EXISTS playback_order"] &&
[self attempt:@"ALTER TABLE library ADD COLUMN lastModified TEXT NOT NULL DEFAULT '' "] &&
[self attempt:@"CREATE INDEX IF NOT EXISTS index_path ON library (path COLLATE NOCASE)"] &&
[self attempt:@"DELETE FROM library WHERE file_id NOT IN (SELECT min(file_id) FROM library GROUP BY path COLLATE NOCASE)"] &&
[self attempt:@"DROP TABLE IF EXISTS history"] &&
[self attempt:@"CREATE TABLE IF NOT EXISTS history ("
"file_id INTEGER NOT NULL, "
"date TEXT NOT NULL, "
"FOREIGN KEY(file_id) REFERENCES library(file_id) ON UPDATE CASCADE ON DELETE CASCADE)"] &&
[self attempt:@"CREATE TABLE IF NOT EXISTS queue ("
"queue_index INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
"playlist_item_id INTEGER NOT NULL UNIQUE, "
"FOREIGN KEY(playlist_item_id) REFERENCES playlist_items(playlist_item_id) ON UPDATE CASCADE ON DELETE CASCADE)"] &&
[self attempt:@"UPDATE schema_version SET version = 2"]);
[self commit];
if (!e) {
return NO;
}
version = 2;
}
if (version == 2) {
[self begin];
BOOL e = ([self attempt:@"CREATE TABLE playback_order ("
"index_ INTEGER PRIMARY KEY, "
"playlist_item_id INTEGER NOT NULL, "
"CHECK (index_ > 0), "
"FOREIGN KEY(playlist_item_id) REFERENCES playlist_items(playlist_item_id) ON UPDATE RESTRICT ON DELETE CASCADE)"] &&
[self attempt:@"UPDATE schema_version SET version = 3"]);
[self commit];
if (!e) {
return NO;
}
version = 3;
}
if (version == 3) {
[self begin];
BOOL e = ([self attempt:@"UPDATE playlists SET browserInfo = x'', listViewSortColumn = -2, "
"albumListViewSortColumn = -2 WHERE type = 1 OR type = 2 OR type = 3"] &&
[self attempt:@"UPDATE schema_version SET version = 4"]);
[self commit];
if (!e) {
return NO;
}
version = 4;
}
if (version == 4) {
[self begin];
BOOL e = ([self attempt:@"DROP INDEX IF EXISTS index_path"] &&
[self attempt:@"CREATE INDEX index_path ON library (path COLLATE hfs_compare)"] &&
[self attempt:@"UPDATE schema_version SET version = 5"]);
[self commit];
if (!e) {
return NO;
}
version = 5;
}
if (version == 5) {
[self begin];
BOOL e = ([self attempt:@"ALTER TABLE library ADD COLUMN lyrics TEXT NOT NULL DEFAULT '' "] &&
[self attempt:@"ALTER TABLE library ADD COLUMN compilation INT NOT NULL DEFAULT 0 "] &&
[self attempt:@"CREATE INDEX index_compilation ON library (compilation)"] &&
[self attempt:@"UPDATE schema_version SET version = 6"]);
[[_core opQueue] addOperation:[PRUpdate060Operation operationWithCore:_core]];
[self commit];
if (!e) {
return NO;
}
// version = 6;
}
//
// if (version == 6) {
// [self begin];
// e = [self attempt:@"REINDEX NOCASE2"];
// if (!e) {return NO;}
//
// e = [self attempt:@"ALTER TABLE library ADD COLUMN resolvedYear INT NOT NULL DEFAULT 0 "];
// if (!e) {return NO;}
// [self commit];
// version = 7;
// }
return YES;
}
- (void)create {
[self execute:@"CREATE TABLE schema_version (version INTEGER NOT NULL)"];
[self execute:@"INSERT INTO schema_version (version) VALUES (6)"];
[history create];
[library create];
[playlists create];
[queue create];
[playbackOrder create];
}
- (BOOL)move:(NSError **)err {
NSFileManager *fileManager = [[NSFileManager alloc] init];
BOOL libraryExists = [fileManager fileExistsAtPath:[[PRDefaults sharedDefaults] libraryPath]];
BOOL artExists = [fileManager fileExistsAtPath:[[PRDefaults sharedDefaults] cachedAlbumArtPath]];
if (!libraryExists && !artExists && err) {
*err = nil;
return YES;
}
NSString *folder;
int i = 1;
while (YES) {
NSString *folderName;
NSString *date = [[NSDate date] descriptionWithCalendarFormat:@"%Y-%m-%d" timeZone:nil locale:nil];
if (i == 1) {
folderName = [NSString stringWithFormat:@"Backup %@", date];
} else {
folderName = [NSString stringWithFormat:@"Backup %@ %d", date, i];
}
folder = [[[PRDefaults sharedDefaults] backupPath] stringByAppendingPathComponent:folderName];
if (![fileManager fileExistsAtPath:folder isDirectory:nil]) {
break;
}
i++;
if (i >= 50) {
*err = [self databaseCouldNotBeMovedError];
return NO;
}
}
int e = [fileManager createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:nil error:nil];
if (!e && err) {
*err = [self databaseCouldNotBeMovedError];
return NO;
}
NSString *library_ = [[PRDefaults sharedDefaults] libraryPath];
NSString *newLibrary = [folder stringByAppendingPathComponent:@"Enqueue.db"];
e = [fileManager moveItemAtPath:library_ toPath:newLibrary error:nil];
if (!e && err) {
*err = [self databaseCouldNotBeMovedError];
return NO;
}
NSString *art = [[PRDefaults sharedDefaults] cachedAlbumArtPath];
NSString *newArt = [folder stringByAppendingPathComponent:@"Cached Album Art"];
e = [fileManager moveItemAtPath:art toPath:newArt error:nil];
if (!e && err) {
*err = [self databaseCouldNotBeMovedError];
return NO;
}
if (err) {
*err = [self databaseWasMovedError:folder];
}
return YES;
}
#pragma mark - Accessors
@synthesize sqlDb, history, library, playlists, queue, albumArtController, playbackOrder;
- (long)lastInsertRowid {
return sqlite3_last_insert_rowid(sqlDb);
}
#pragma mark - Action
- (void)begin {
if (_transaction == 0) {
[self executeCached:@"BEGIN EXCLUSIVE"];
}
_transaction += 1;
}
- (void)rollback {
[self executeCached:@"ROLLBACK"];
}
- (void)commit {
if (_transaction < 1) {
[PRException raise:NSInternalInconsistencyException format:@"Commit index > 1"];
} else if (_transaction == 1) {
[self executeCached:@"COMMIT"];
_transaction = 0;
} else {
_transaction -= 1;
}
}
- (NSArray *)execute:(NSString *)string {
return [self execute:string bindings:nil columns:nil];
}
- (NSArray *)execute:(NSString *)string bindings:(NSDictionary *)bindings columns:(NSArray *)columns {
PRStatement *stmt = [[PRStatement alloc] initWithString:string bindings:bindings columns:columns db:self];
id rlt = nil;
[stmt zExecute:&rlt];
return rlt;
}
- (NSArray *)executeCached:(NSString *)string {
return [self executeCached:string bindings:nil columns:nil];
}
- (NSArray *)executeCached:(NSString *)string bindings:(NSDictionary *)bindings columns:(NSArray *)columns {
PRStatement *statement = [_cachedStatements objectForKey:string];
if (!statement || ![[statement columns] isEqual:columns]) {
statement = [[PRStatement alloc] initWithString:string bindings:bindings columns:columns db:self];
[_cachedStatements setObject:statement forKey:string];
}
NSArray *rlt = nil;
[statement zExecute:&rlt];
return rlt;
}
- (NSArray *)attempt:(NSString *)string {
return [self attempt:string bindings:nil columns:nil];
}
- (NSArray *)attempt:(NSString *)string bindings:(NSDictionary *)bindings columns:(NSArray *)columns {
PRStatement *stmt = [[PRStatement alloc] initWithString:string bindings:bindings columns:columns db:self];
id rlt = nil;
[stmt zExecute:&rlt];
return rlt;
}
- (NSArray *)explain:(NSString *)string {
return [self explain:string bindings:nil columns:nil];
}
- (NSArray *)explain:(NSString *)string bindings:(NSDictionary *)bindings columns:(NSArray *)columns {
NSArray *explain = [self execute:[NSString stringWithFormat:@"EXPLAIN %@",string]
bindings:bindings
columns:@[PRColInteger,PRColInteger,PRColInteger,PRColInteger,PRColInteger,PRColInteger,PRColInteger,PRColInteger]];
NSDate *date = [NSDate date];
NSArray *rlt = [self execute:string bindings:bindings columns:columns];
NSLog(@"time:%f string:%@ explain:%@",[date timeIntervalSinceNow],string, explain);
return rlt;
}
- (BOOL)zTransaction:(BOOL(^)(void))block {
if (_transaction == 0) {
[self executeCached:@"BEGIN EXCLUSIVE"];
}
_transaction += 1;
BOOL success = block();
_transaction -= 1;
if (_transaction == 0) {
if (success) {
[self executeCached:@"COMMIT"];
} else {
[self executeCached:@"ROLLBACK"];
}
}
return success;
}
- (BOOL)zExecute:(NSString *)string {
return [self zExecute:string bindings:nil columns:nil out:nil];
}
- (BOOL)zExecute:(NSString *)string bindings:(NSDictionary *)bindings columns:(NSArray *)columns out:(NSArray **)outValue {
PRStatement *stmt = [[PRStatement alloc] initWithString:string bindings:bindings columns:columns db:self];
return [stmt zExecute:outValue];
}
- (BOOL)zExecuteCached:(NSString *)string {
return [self zExecuteCached:string bindings:nil columns:nil out:nil];
}
- (BOOL)zExecuteCached:(NSString *)string bindings:(NSDictionary *)bindings columns:(NSArray *)columns out:(NSArray **)outValue {
PRStatement *statement = [_cachedStatements objectForKey:string];
if (!statement || ![[statement columns] isEqual:columns]) {
statement = [[PRStatement alloc] initWithString:string bindings:bindings columns:columns db:self];
[_cachedStatements setObject:statement forKey:string];
}
return [statement zExecute:outValue];
}
#pragma mark - Error
- (NSError *)databaseWasMovedError:(NSString *)newPath {
NSString *description = @"The Enqueue library file does not appear to be valid.";
NSString *recovery = [NSString stringWithFormat:@"A new library has been created and the previous library has been moved to:%@", newPath];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
description, NSLocalizedDescriptionKey,
recovery, NSLocalizedRecoverySuggestionErrorKey,
nil];
return [NSError errorWithDomain:PREnqueueErrorDomain code:0 userInfo:userInfo];
}
- (NSError *)databaseCouldNotBeMovedError {
NSString *description = @"The Enqueue database does not appear to be valid.";
NSString *recovery = @"Enqueue could not move the existing database and must close. "
"If this problem persists please contact support.";
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
description, NSLocalizedDescriptionKey,
recovery, NSLocalizedRecoverySuggestionErrorKey,
nil];
return [NSError errorWithDomain:PREnqueueErrorDomain code:0 userInfo:userInfo];
}
- (NSError *)databaseCouldNotBeInitializedError {
NSString *description = @"Enqueue could not initialize the database and must close.";
NSString *recovery = @"If this problem persists please contact support.";
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
description, NSLocalizedDescriptionKey,
recovery, NSLocalizedRecoverySuggestionErrorKey,
nil];
return [NSError errorWithDomain:PREnqueueErrorDomain code:0 userInfo:userInfo];
}
@end