-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiSQLiteOpenHelper.m
More file actions
195 lines (145 loc) · 5.01 KB
/
Copy pathiSQLiteOpenHelper.m
File metadata and controls
195 lines (145 loc) · 5.01 KB
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
//
// iSQLiteOpenHelper.m
// iSQLiteOpenHelper
//
// Created by Xuz on 10/22/17.
// Copyright © 2017 since2006. All rights reserved.
//
#import "iSQLiteOpenHelper.h"
@implementation iSQLiteOpenHelper
@synthesize databaseName = _databaseName;
@synthesize databaseVersion = _databaseVersion;
- (id)initWithDbFile:(NSString *)path version:(int)version {
self = [super init];
if (self) {
self.databaseName = [path lastPathComponent];
self.databaseVersion = version;
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex: 0];
NSString *dbPath = [documentDirectory stringByAppendingPathComponent: self.databaseName];
BOOL exists =[fm fileExistsAtPath: dbPath];
NSLog(@"dbPath: %@", dbPath);
if (!exists) {
NSError *error;
BOOL success = [fm copyItemAtPath:path toPath:dbPath error:&error];
if (!success) {
NSLog(@"%@", [error description]);
}
self.database = [FMDatabase databaseWithPath:dbPath];
if (![self.database open]) {
NSLog(@"Failed to open database.");
[self checkError];
} else {
[self.database setShouldCacheStatements: YES];
}
[self onCreate:self.database];
[self updateDatabase];
} else {
self.database = [FMDatabase databaseWithPath:dbPath];
if (![self.database open]) {
NSLog(@"Failed to open database.");
[self checkError];
} else {
[self.database setShouldCacheStatements: YES];
}
[self updateDatabase];
}
}
return self;
}
- (id)initWithDbName:(NSString *)name version:(int)version {
self = [super init];
if (self) {
self.databaseName = name;
self.databaseVersion = version;
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex: 0];
NSString *dbPath = [documentDirectory stringByAppendingPathComponent: self.databaseName];
BOOL exists = [fm fileExistsAtPath: dbPath];
self.database = [FMDatabase databaseWithPath:dbPath];
if (![self.database open]) {
NSLog(@"Failed to open database.");
[self checkError];
} else {
[self.database setShouldCacheStatements: YES];
}
if (!exists) {
[self onCreate:self.database];
[self _setDbVersion:self.databaseVersion];
} else {
[self updateDatabase];
}
}
return self;
}
- (void)onCreate:(FMDatabase *)db {
}
- (void)onUpgrade:(FMDatabase *)db oldVersion:(int)oldVersion newVersion:(int)newVersion {
}
-(void)checkError {
if ([self.database hadError]) {
NSLog(@"Err %d: %@", [self.database lastErrorCode], [self.database lastErrorMessage]);
}
}
- (FMDatabase *)getWritableDatabase {
return self.database;
}
- (void)close {
[self.database close];
}
- (int)getLastInsertRowId {
int rowId = 0;
FMResultSet *rs = [self.database executeQuery:@"select last_insert_rowid() as rowId"];
if (rs == nil) {
[self checkError];
rowId = 0;
}
if ([rs next]) {
rowId = [rs intForColumn:@"rowId"];
}
[rs close];
return rowId;
}
- (int)getAffectedRows {
int rows = 0;
FMResultSet *rs = [self.database executeQuery:@"select changes() as rows"];
if (rs == nil) {
[self checkError];
rows = 0;
}
if ([rs next]) {
rows = [rs intForColumn:@"rows"];
}
[rs close];
return rows;
}
// -----------------------------------------------------------------------------
- (void)updateDatabase {
int currentVersion = [self _getDbVersion];
NSLog(@"database oldVersion: %d newVersion: %d", currentVersion, self.databaseVersion);
if (currentVersion < self.databaseVersion) {
[self onUpgrade:self.database oldVersion:currentVersion newVersion:self.databaseVersion];
[self _setDbVersion:self.databaseVersion];
}
}
- (int)_getDbVersion {
FMResultSet *rs = [self.database executeQuery:@"pragma user_version"];
if (rs == nil) {
[self checkError];
}
int version = 0;
if ([rs next]) {
version = [rs intForColumn:@"user_version"];
}
return version;
}
- (void)_setDbVersion:(int)version {
NSString *sql = [NSString stringWithFormat:@"pragma user_version = %d", version];
BOOL result = [self.database executeUpdate:sql];
if (!result) {
[self checkError];
}
}
@end