Skip to content

Commit fa1cb28

Browse files
committed
增加了事务插入数组的方法 并对demo 进行了修改
1 parent 97c641d commit fa1cb28

File tree

6 files changed

+109
-38
lines changed

6 files changed

+109
-38
lines changed

LKDBHelper/Helper/LKDBHelper.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,25 @@
3939
@property(strong,nonatomic)NSString* encryptionKey;
4040

4141
/**
42-
* @brief execute database operations synchronously,not afraid of recursive deadlock 同步执行数据库操作 可递归调用
42+
* @brief execute database operations synchronously,not afraid of recursive deadlock
43+
44+
同步执行数据库操作 可递归调用
4345
*/
4446
-(void)executeDB:(void (^)(FMDatabase *db))block;
4547

4648
-(BOOL)executeSQL:(NSString *)sql arguments:(NSArray *)args;
4749
-(NSString *)executeScalarWithSQL:(NSString *)sql arguments:(NSArray *)args;
4850

51+
52+
/**
53+
* @brief execute database operations synchronously in a transaction
54+
block return the YES commit transaction returns the NO rollback transaction
55+
56+
同步执行数据库操作 在事务内部
57+
block 返回 YES commit 事务 返回 NO rollback 事务
58+
*/
59+
-(void)executeForTransaction:(BOOL (^)(LKDBHelper* helper))block;
60+
4961
@end
5062

5163
@interface LKDBHelper(DatabaseManager)

LKDBHelper/Helper/LKDBHelper.m

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,26 @@ -(NSString *)executeScalarWithSQL:(NSString *)sql arguments:(NSArray *)args
274274
}];
275275
return scalar;
276276
}
277-
277+
-(void)executeForTransaction:(BOOL (^)(LKDBHelper *))block
278+
{
279+
LKDBHelper* helper = self;
280+
[self executeDB:^(FMDatabase *db) {
281+
[db beginTransaction];
282+
BOOL isCommit = NO;
283+
if(block)
284+
{
285+
isCommit = block(helper);
286+
}
287+
if(isCommit)
288+
{
289+
[db commit];
290+
}
291+
else
292+
{
293+
[db rollback];
294+
}
295+
}];
296+
}
278297

279298
//splice 'where' 拼接where语句
280299
- (NSMutableArray *)extractQuery:(NSMutableString *)query where:(id)where

LKDBHelper/Helper/NSObject+LKDBHelper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,7 @@
7272
- (BOOL)deleteToDB;
7373
- (BOOL)isExistsFromDB;
7474

75+
///begin translate for insert models 开始事务插入数组
76+
+(void)insertToDBWithArray:(NSArray*)models filter:(void(^)(id model,BOOL inserted,BOOL*rollback))filter;
77+
7578
@end

LKDBHelper/Helper/NSObject+LKDBHelper.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,28 @@ -(BOOL)isExistsFromDB
167167
{
168168
return [self.class isExistsWithModel:self];
169169
}
170+
171+
172+
+(void)insertToDBWithArray:(NSArray *)models filter:(void (^)(id model, BOOL inseted, BOOL * rollback))filter
173+
{
174+
[[self getUsingLKDBHelper] executeForTransaction:^BOOL(LKDBHelper *helper) {
175+
176+
BOOL isRollback = NO;
177+
for (int i=0; i<models.count; i++)
178+
{
179+
id obj = [models objectAtIndex:i];
180+
BOOL inseted = [helper insertToDB:obj];
181+
if(filter)
182+
{
183+
filter(obj,inseted,&isRollback);
184+
}
185+
if(isRollback)
186+
{
187+
break;
188+
}
189+
}
190+
return (isRollback == NO);
191+
}];
192+
}
193+
170194
@end

LKDBHelper/LKAppDelegate.m

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -52,34 +52,38 @@ -(void)test
5252
{
5353
addText(@"示例 开始 example start \n\n");
5454

55-
//清空数据库
55+
///获取 LKTest 类使用的 LKDBHelper
5656
LKDBHelper* globalHelper = [LKTest getUsingLKDBHelper];
5757

58+
///删除所有表 delete all table
5859
[globalHelper dropAllTable];
5960

6061
addText(@"LKTest create table sql :\n%@\n",[LKTest getCreateTableSQL]);
6162
addText(@"LKTestForeign create table sql :\n%@\n",[LKTestForeign getCreateTableSQL]);
6263

63-
//清空表数据 clear table data
64+
//清空表数据 clear table data
6465
[LKDBHelper clearTableData:[LKTest class]];
6566

67+
//初始化数据模型 init object
68+
LKTest* test = [[LKTest alloc]init];
69+
test.name = @"zhan san";
70+
test.age = 16;
6671

72+
//外键 foreign key
6773
LKTestForeign* foreign = [[LKTestForeign alloc]init];
6874
foreign.address = @":asdasdasdsadasdsdas";
6975
foreign.postcode = 123341;
7076
foreign.addid = 213214;
7177

72-
//插入数据 insert table row
73-
LKTest* test = [[LKTest alloc]init];
74-
test.name = @"zhan san";
75-
test.age = 16;
76-
77-
//外键 foreign key
7878
test.address = foreign;
79+
80+
81+
///复杂对象 complex object
7982
test.blah = @[@"1",@"2",@"3"];
8083
test.blah = @[@"0",@[@1],@{@"2":@2},foreign];
8184
test.hoho = @{@"array":test.blah,@"foreign":foreign,@"normal":@123456,@"date":[NSDate date]};
8285

86+
///other
8387
test.isGirl = YES;
8488
test.like = 'I';
8589
test.img = [UIImage imageNamed:@"41.png"];
@@ -92,80 +96,85 @@ -(void)test
9296
test.data = [@"hahaha" dataUsingEncoding:NSUTF8StringEncoding];
9397

9498
addText(@"%f",test.score);
95-
//异步 插入第一条 数据 Insert the first
99+
//同步 插入第一条 数据 synchronous insert the first
96100
[test saveToDB];
97101
//or
98102
//[globalHelper insertToDB:test];
99103

100-
//多主键 的插入成功
104+
//更改主键继续插入 Insert the change after the primary key
101105
test.age = 17;
102106
[globalHelper insertToDB:test];
103107

104108
//事物 transaction
105-
[globalHelper executeDB:^(FMDatabase *db) {
106-
107-
[db beginTransaction];
109+
[globalHelper executeForTransaction:^BOOL(LKDBHelper *helper) {
108110

109111
test.name = @"1";
110-
[globalHelper insertToDB:test];
112+
BOOL success = [helper insertToDB:test];
111113

112114
test.name = @"2";
113-
[globalHelper insertToDB:test];
115+
success = [helper insertToDB:test];
114116

115117
//重复主键 duplicate primary key
116118
test.name = @"1";
117119
test.rowid = 0; //no new object,should set rowid:0
118-
BOOL insertSucceed = [globalHelper insertWhenNotExists:test];
119-
120+
BOOL insertSucceed = [helper insertWhenNotExists:test];
121+
120122
//insert fail
121123
if(insertSucceed == NO)
122-
[db rollback];
124+
{
125+
///rollback
126+
return NO;
127+
}
123128
else
124-
[db commit];
125-
129+
{
130+
///commit
131+
return YES;
132+
}
126133
}];
127134

128135

129136
addText(@"同步插入 完成! Insert completed synchronization");
130137

131138
sleep(1);
132139

133-
134-
//改个 主键 插入第2条数据 update primary column value Insert the second
135140
test.name = @"li si";
136141
[globalHelper insertToDB:test callback:^(BOOL isInsert) {
137142
addText(@"asynchronization insert complete: %@",isInsert>0?@"YES":@"NO");
138143
}];
139144

140145
//查询 search
141-
addText(@"同步搜索 sync search");
142-
143-
NSMutableArray* arraySync = nil;
144-
arraySync = [globalHelper searchWithSQL:@"select * from @t" toClass:[LKTest class]];
145-
for (id obj in arraySync) {
146+
NSMutableArray* searchResultArray = nil;
147+
148+
addText(@"\n search one: \n");
149+
///同步搜索 执行sql语句 把结果变为LKTest对象
150+
///Synchronous search executes the SQL statement put the results into a LKTest object
151+
searchResultArray = [globalHelper searchWithSQL:@"select * from @t" toClass:[LKTest class]];
152+
for (id obj in searchResultArray) {
146153
addText(@"%@",[obj printAllPropertys]);
147154
}
148155

149-
//查询 search
150-
addText(@"同步搜索 sync search 2");
151-
arraySync = [LKTest searchWithWhere:nil orderBy:nil offset:0 count:100];
152-
for (id obj in arraySync) {
156+
addText(@"\n search two: \n");
157+
///搜索所有值 search all
158+
searchResultArray = [LKTest searchWithWhere:nil orderBy:nil offset:0 count:100];
159+
for (id obj in searchResultArray) {
153160
addText(@"%@",[obj printAllPropertys]);
154161
}
155162

156-
//查询 单个 列 search single column
157-
addText(@"只获取name那列的值 search with column 'name' results");
163+
addText(@"查询 单个 列 search single column");
164+
///只获取name那列的值 search with column 'name' results
158165
NSArray* nameArray = [LKTest searchColumn:@"name" where:nil orderBy:nil offset:0 count:0];
159166
addText(@"%@",[nameArray componentsJoinedByString:@","]);
160167

168+
169+
///
161170
addText(@"休息2秒 开始 为了说明 是异步插入的\n"
162171
"rest for 2 seconds to start is asynchronous inserted to illustrate");
163172

164173
sleep(2);
165174

166175
addText(@"休息2秒 结束 \n rest for 2 seconds at the end");
167176

168-
//异步
177+
//异步 asynchronous
169178
[globalHelper search:[LKTest class] where:nil orderBy:nil offset:0 count:100 callback:^(NSMutableArray *array) {
170179

171180
addText(@"异步搜索 结束, async search end");
@@ -175,21 +184,23 @@ -(void)test
175184

176185
sleep(1);
177186

178-
//修改 update
187+
///修改 update object
179188
LKTest* test2 = [array objectAtIndex:0];
180189
test2.name = @"wang wu";
181190

182191
[globalHelper updateToDB:test2 where:nil];
183192

184193
addText(@"修改完成 , update completed ");
185194

195+
///all
186196
array = [globalHelper search:[LKTest class] where:nil orderBy:nil offset:0 count:100];
187197
for (NSObject* obj in array) {
188198
addText(@"%@",[obj printAllPropertys]);
189199
}
190200

191-
test2.rowid = 0;
192201

202+
///delete
203+
test2.rowid = 0;
193204
BOOL ishas = [globalHelper isExistsModel:test2];
194205
if(ishas)
195206
{
@@ -200,6 +211,7 @@ -(void)test
200211
addText(@"删除完成, delete completed");
201212
sleep(1);
202213

214+
///all
203215
array = [globalHelper search:[LKTest class] where:nil orderBy:nil offset:0 count:100];
204216
for (NSObject* obj in array) {
205217
addText(@"%@",[obj printAllPropertys]);

LKDBHelper/LKTestModels.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ +(NSString *)getPrimaryKey
140140
{
141141
return @"name";
142142
}
143+
///复合主键 这个优先级最高
143144
+(NSArray *)getPrimaryKeyUnionArray
144145
{
145146
return @[@"name",@"MyAge"];

0 commit comments

Comments
 (0)