-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathAFSQLManager.m
139 lines (103 loc) · 4.58 KB
/
AFSQLManager.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
//
// AFSQLManager.m
// AFSQLManager
//
// Created by Alvaro Franco on 4/17/14.
// Copyright (c) 2014 AlvaroFranco. All rights reserved.
//
#import "AFSQLManager.h"
@interface AFSQLManager ()
@property (nonatomic, strong) NSDictionary *currentDbInfo;
@end
@implementation AFSQLManager
+(instancetype)sharedManager {
static AFSQLManager *sharedManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedManager = [[self alloc]init];
});
return sharedManager;
}
-(void)createDatabaseWithName:(NSString *)name openImmediately:(BOOL)openImmediately withStatusBlock:(statusBlock)status {
NSError *error = nil;
[[NSData data]writeToFile:[[NSBundle mainBundle]pathForResource:[[name lastPathComponent]stringByDeletingPathExtension] ofType:[name pathExtension]] options:NSDataWritingAtomic error:&error];
if (!error) {
if (openImmediately) {
[self openLocalDatabaseWithName:name andStatusBlock:^(BOOL success, NSError *error) {
if (success) {
_currentDbInfo = @{@"name": name};
}
status(success, error);
}];
} else {
status(YES, nil);
}
} else {
status(NO, error);
}
}
-(void)openLocalDatabaseWithName:(NSString *)name andStatusBlock:(statusBlock)status {
NSString *path = [[NSBundle mainBundle]pathForResource:[[name lastPathComponent]stringByDeletingPathExtension] ofType:[name pathExtension]];
if (sqlite3_open([path UTF8String], &_database) != SQLITE_OK) {
NSLog(@"Failed to open database!");
status(NO, nil);
} else {
NSLog(@"Database opened properly");
status(YES, nil);
}
}
-(void)closeLocalDatabaseWithName:(NSString *)name andStatusBlock:(statusBlock)status {
if (sqlite3_close(_database) == SQLITE_OK) {
status(YES, nil);
} else {
}
}
-(void)renameDatabaseWithName:(NSString *)originalName toName:(NSString *)newName andStatus:(statusBlock)status {
if ([[_currentDbInfo objectForKey:@"name"]isEqualToString:originalName]) {
sqlite3_close(_database);
}
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager moveItemAtPath:[[NSBundle mainBundle]pathForResource:[[originalName lastPathComponent]stringByDeletingPathExtension] ofType:[originalName pathExtension]] toPath:[[NSBundle mainBundle]pathForResource:[[newName lastPathComponent]stringByDeletingPathExtension] ofType:[newName pathExtension]] error:&error];
if ([[_currentDbInfo objectForKey:@"name"]isEqualToString:originalName] && !error) {
[self openLocalDatabaseWithName:newName andStatusBlock:nil];
_currentDbInfo = @{@"name": newName};
status(YES, nil);
} else if ([[_currentDbInfo objectForKey:@"name"]isEqualToString:originalName] && error) {
[self openLocalDatabaseWithName:originalName andStatusBlock:nil];
status(YES, error);
}
}
-(void)deleteDatabaseWithName:(NSString *)name andStatus:(statusBlock)status {
if ([[_currentDbInfo objectForKey:@"name"]isEqualToString:name]) {
sqlite3_close(_database);
}
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:[[NSBundle mainBundle]pathForResource:[[name lastPathComponent]stringByDeletingPathExtension] ofType:[name pathExtension]] error:&error];
if (!error) {
status(YES, nil);
_currentDbInfo = @{@"name": [NSNull null]};
} else {
status(NO, error);
[self openLocalDatabaseWithName:name andStatusBlock:nil];
}
}
-(void)performQuery:(NSString *)query withBlock:(completionBlock)completion {
NSString *fixedQuery = [query stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [fixedQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
NSMutableArray *row = [NSMutableArray array];
for (int i = 0; i < sqlite3_column_count(statement); i++) {
[row addObject:((char *)sqlite3_column_text(statement, i)) ? [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, i)] : [NSNull null]];
}
if (completion) {
completion(row, nil, NO);
}
}
sqlite3_finalize(statement);
completion(nil, nil, YES);
}
}
@end