8
8
9
9
#import " LKDBHelper.h"
10
10
11
- #define checkClassIsInvalid ( modelClass )\
12
- if ([LKDBUtils checkStringIsEmpty: [modelClass getTableName ] ])\
11
+ #define LKDBCheck_tableNameIsInvalid ( tableName )\
12
+ if ([LKDBUtils checkStringIsEmpty: tableName ])\
13
13
{\
14
- LKErrorLog (@" model class name %@ table name is invalid! " , NSStringFromClass (modelClass) );\
14
+ LKErrorLog (@" \n Fail!Fail!Fail!Fail! \n with TableName is nil " );\
15
15
return NO ;\
16
16
}
17
17
18
- #define checkModelIsInvalid (model )\
18
+ #define LKDBCode_Async_Begin __LKDBWeak LKDBHelper* wself = self;\
19
+ [self asyncBlock: ^{__strong LKDBHelper* sself = wself;if (sself){
20
+
21
+ #define LKDBCode_Async_End }}];
22
+
23
+ #define LKDBCheck_modelIsInvalid (model )\
19
24
if (model == nil )\
20
25
{\
21
26
LKErrorLog (@" model is nil" );\
27
32
return NO ;\
28
33
}\
29
34
NSString * _model_tableName = model.db_tableName ?:[model.class getTableName ];\
30
- if (_model_tableName.length == 0 )\
35
+ if ([LKDBUtils checkStringIsEmpty: _model_tableName] )\
31
36
{\
32
37
LKErrorLog (@" model class name %@ table name is invalid!" ,NSStringFromClass (model.class ));\
33
38
return NO ;\
@@ -410,11 +415,15 @@ -(void)dropAllTable
410
415
411
416
[_createdTableNames removeAllObjects ];
412
417
}
418
+
413
419
-(BOOL )dropTableWithClass : (Class )modelClass
414
420
{
415
- checkClassIsInvalid (modelClass);
421
+ return [self dropTableWithTableName: [modelClass getTableName ]];
422
+ }
423
+ -(BOOL )dropTableWithTableName : (NSString *)tableName
424
+ {
425
+ LKDBCheck_tableNameIsInvalid (tableName);
416
426
417
- NSString * tableName = [modelClass getTableName ];
418
427
NSString * dropTable = [NSString stringWithFormat: @" drop table %@ " ,tableName];
419
428
420
429
BOOL isDrop = [self executeSQL: dropTable arguments: nil ];
@@ -481,7 +490,6 @@ -(void)fixSqlColumnsWithClass:(Class)clazz tableName:(NSString*)tableName
481
490
}
482
491
-(BOOL )_createTableWithModelClass : (Class )modelClass tableName : (NSString *)tableName
483
492
{
484
- checkClassIsInvalid (modelClass);
485
493
if ([self getTableCreatedWithTableName: tableName])
486
494
{
487
495
// 已创建表 就跳过
@@ -634,26 +642,23 @@ -(void)asyncBlock:(void(^)(void))block
634
642
#pragma mark - row count operation
635
643
-(NSInteger )rowCount : (Class )modelClass where : (id )where
636
644
{
637
- return [self rowCountBase: modelClass where: where];
645
+ return [self rowCountWithTableName: [ modelClass getTableName ] where: where];
638
646
}
639
647
-(void )rowCount : (Class )modelClass where : (id )where callback : (void (^)(NSInteger ))callback
640
648
{
641
649
if (callback)
642
650
{
643
- __LKDBWeak LKDBHelper* wself = self;
644
- [self asyncBlock: ^{
645
- __strong LKDBHelper* sself = wself;
646
- if (sself)
647
- {
648
- NSInteger result = [sself rowCountBase: modelClass where: where];
649
- callback (result);
650
- }
651
- }];
651
+ LKDBCode_Async_Begin
652
+ NSInteger result = [sself rowCountWithTableName: [modelClass getTableName ] where: where];
653
+ callback (result);
654
+ LKDBCode_Async_End
652
655
}
653
656
}
654
- -(NSInteger )rowCountBase : ( Class ) modelClass where : (id )where
657
+ -(NSInteger )rowCountWithTableName : ( NSString *) tableName where : (id )where
655
658
{
656
- NSMutableString * rowCountSql = [NSMutableString stringWithFormat: @" select count(rowid) from %@ " ,[modelClass getTableName ]];
659
+ LKDBCheck_tableNameIsInvalid (tableName);
660
+
661
+ NSMutableString * rowCountSql = [NSMutableString stringWithFormat: @" select count(rowid) from %@ " ,tableName];
657
662
658
663
NSMutableArray * valuesarray = [self extractQuery: rowCountSql where: where];
659
664
NSInteger result = [[self executeScalarWithSQL: rowCountSql arguments: valuesarray] integerValue ];
@@ -685,31 +690,26 @@ -(void)search:(Class)modelClass where:(id)where orderBy:(NSString *)orderBy offs
685
690
{
686
691
if (block)
687
692
{
688
- __LKDBWeak LKDBHelper* wself = self;
689
- [self asyncBlock: ^{
690
- __strong LKDBHelper* sself = wself;
691
- if (sself)
692
- {
693
- LKDBQueryParams* params = [[LKDBQueryParams alloc ]init];
694
- params.toClass = modelClass;
693
+ LKDBCode_Async_Begin
694
+ LKDBQueryParams* params = [[LKDBQueryParams alloc ]init];
695
+ params.toClass = modelClass;
695
696
696
- if ([where isKindOfClass: [NSDictionary class ]])
697
- {
698
- params.whereDic = where;
699
- }
700
- else if ([where isKindOfClass: [NSString class ]])
701
- {
702
- params.where = where;
703
- }
704
-
705
- params.orderBy = orderBy;
706
- params.offset = offset;
707
- params.count = count;
708
-
709
- NSMutableArray * array = [self searchBaseWithParams: params];
710
- block (array);
711
- }
712
- }];
697
+ if ([where isKindOfClass: [NSDictionary class ]])
698
+ {
699
+ params.whereDic = where;
700
+ }
701
+ else if ([where isKindOfClass: [NSString class ]])
702
+ {
703
+ params.where = where;
704
+ }
705
+
706
+ params.orderBy = orderBy;
707
+ params.offset = offset;
708
+ params.count = count;
709
+
710
+ NSMutableArray * array = [sself searchBaseWithParams: params];
711
+ block (array);
712
+ LKDBCode_Async_End
713
713
}
714
714
}
715
715
@@ -802,15 +802,10 @@ -(NSMutableArray *)searchWithParams:(LKDBQueryParams *)params
802
802
{
803
803
if (params.callback )
804
804
{
805
- __LKDBWeak LKDBHelper* wself = self;
806
- [self asyncBlock: ^{
807
- __strong LKDBHelper* sself = wself;
808
- if (sself)
809
- {
810
- NSMutableArray * array = [sself searchBaseWithParams: params];
811
- params.callback (array);
812
- }
813
- }];
805
+ LKDBCode_Async_Begin
806
+ NSMutableArray * array = [sself searchBaseWithParams: params];
807
+ params.callback (array);
808
+ LKDBCode_Async_End
814
809
return nil ;
815
810
}
816
811
else
@@ -965,19 +960,13 @@ -(BOOL)insertToDB:(NSObject *)model
965
960
}
966
961
-(void )insertToDB : (NSObject *)model callback : (void (^)(BOOL ))block
967
962
{
968
- __LKDBWeak LKDBHelper* wself = self;
969
- [self asyncBlock: ^{
970
- __strong LKDBHelper* sself = wself;
971
- BOOL result = NO ;
972
- if (sself)
973
- {
974
- result = [sself insertBase: model];
975
- }
976
- if (block)
977
- {
978
- block (result);
979
- }
980
- }];
963
+ LKDBCode_Async_Begin
964
+ BOOL result = [sself insertBase: model];
965
+ if (block)
966
+ {
967
+ block (result);
968
+ }
969
+ LKDBCode_Async_End
981
970
}
982
971
-(BOOL )insertWhenNotExists : (NSObject *)model
983
972
{
@@ -989,20 +978,17 @@ -(BOOL)insertWhenNotExists:(NSObject *)model
989
978
}
990
979
-(void )insertWhenNotExists : (NSObject *)model callback : (void (^)(BOOL ))block
991
980
{
992
- [self asyncBlock: ^{
993
- if (block != nil )
994
- {
995
- block ([self insertWhenNotExists: model]);
996
- }
997
- else
998
- {
999
- [self insertWhenNotExists: model];
1000
- }
1001
- }];
981
+ LKDBCode_Async_Begin
982
+ BOOL result = [sself insertWhenNotExists: model];
983
+ if (block)
984
+ {
985
+ block (result);
986
+ }
987
+ LKDBCode_Async_End
1002
988
}
1003
989
-(BOOL )insertBase : (NSObject *)model {
1004
990
1005
- checkModelIsInvalid (model);
991
+ LKDBCheck_modelIsInvalid (model);
1006
992
1007
993
Class modelClass = model.class ;
1008
994
@@ -1094,15 +1080,17 @@ -(BOOL)updateToDB:(NSObject *)model where:(id)where
1094
1080
}
1095
1081
-(void )updateToDB : (NSObject *)model where : (id )where callback : (void (^)(BOOL ))block
1096
1082
{
1097
- [self asyncBlock: ^{
1098
- BOOL result = [self updateToDBBase: model where: where];
1099
- if (block != nil )
1100
- block (result);
1101
- }];
1083
+ LKDBCode_Async_Begin
1084
+ BOOL result = [sself updateToDBBase: model where: where];
1085
+ if (block)
1086
+ {
1087
+ block (result);
1088
+ }
1089
+ LKDBCode_Async_End
1102
1090
}
1103
1091
-(BOOL )updateToDBBase : (NSObject *)model where : (id )where
1104
1092
{
1105
- checkModelIsInvalid (model);
1093
+ LKDBCheck_modelIsInvalid (model);
1106
1094
1107
1095
Class modelClass = model.class ;
1108
1096
@@ -1182,15 +1170,21 @@ -(BOOL)updateToDBBase:(NSObject *)model where:(id)where
1182
1170
}
1183
1171
-(BOOL )updateToDB : (Class )modelClass set : (NSString *)sets where : (id )where
1184
1172
{
1185
- checkClassIsInvalid (modelClass);
1173
+ return [self updateToDBWithTableName: [modelClass getTableName ] set: sets where: where];
1174
+ }
1175
+ -(BOOL )updateToDBWithTableName : (NSString *)tableName set : (NSString *)sets where : (id )where
1176
+ {
1177
+ LKDBCheck_tableNameIsInvalid (tableName);
1186
1178
1187
- NSMutableString * updateSQL = [NSMutableString stringWithFormat: @" update %@ set %@ " ,[modelClass getTableName ] ,sets];
1179
+ NSMutableString * updateSQL = [NSMutableString stringWithFormat: @" update %@ set %@ " ,tableName ,sets];
1188
1180
NSMutableArray * updateValues = [self extractQuery: updateSQL where: where];
1189
1181
1190
1182
BOOL execute = [self executeSQL: updateSQL arguments: updateValues];
1191
1183
1192
1184
if (execute == NO )
1193
- LKErrorLog (@" database update fail %@ ----->sql:%@ " ,NSStringFromClass (modelClass),updateSQL);
1185
+ {
1186
+ LKErrorLog (@" database update fail with TableName: %@ ----->sql:%@ " ,tableName,updateSQL);
1187
+ }
1194
1188
1195
1189
return execute;
1196
1190
}
@@ -1201,16 +1195,18 @@ -(BOOL)deleteToDB:(NSObject *)model
1201
1195
}
1202
1196
-(void )deleteToDB : (NSObject *)model callback : (void (^)(BOOL ))block
1203
1197
{
1204
- [self asyncBlock: ^{
1205
- BOOL isDeleted = [self deleteToDBBase: model];
1206
- if (block != nil )
1207
- block (isDeleted);
1208
- }];
1198
+ LKDBCode_Async_Begin
1199
+ BOOL isDeleted = [sself deleteToDBBase: model];
1200
+ if (block)
1201
+ {
1202
+ block (isDeleted);
1203
+ }
1204
+ LKDBCode_Async_End
1209
1205
}
1210
1206
1211
1207
-(BOOL )deleteToDBBase : (NSObject *)model
1212
1208
{
1213
- checkModelIsInvalid (model);
1209
+ LKDBCheck_modelIsInvalid (model);
1214
1210
1215
1211
Class modelClass = model.class ;
1216
1212
@@ -1254,22 +1250,23 @@ -(BOOL)deleteToDBBase:(NSObject *)model
1254
1250
1255
1251
-(BOOL )deleteWithClass : (Class )modelClass where : (id )where
1256
1252
{
1257
- return [self deleteWithClassBase: modelClass where: where];
1253
+ return [self deleteWithTableName: [ modelClass getTableName ] where: where];
1258
1254
}
1259
1255
-(void )deleteWithClass : (Class )modelClass where : (id )where callback : (void (^)(BOOL ))block
1260
1256
{
1261
- [self asyncBlock: ^{
1262
- BOOL isDeleted = [self deleteWithClassBase: modelClass where: where];
1263
- if (block != nil ) {
1264
- block (isDeleted);
1265
- }
1266
- }];
1257
+ LKDBCode_Async_Begin
1258
+ BOOL isDeleted = [sself deleteWithTableName: [modelClass getTableName ] where: where];
1259
+ if (block)
1260
+ {
1261
+ block (isDeleted);
1262
+ }
1263
+ LKDBCode_Async_End
1267
1264
}
1268
- -(BOOL )deleteWithClassBase : ( Class ) modelClass where : (id )where
1265
+ -(BOOL )deleteWithTableName : ( NSString *) tableName where : (id )where
1269
1266
{
1270
- checkClassIsInvalid (modelClass );
1267
+ LKDBCheck_tableNameIsInvalid (tableName );
1271
1268
1272
- NSMutableString * deleteSQL = [NSMutableString stringWithFormat: @" delete from %@ " ,[modelClass getTableName ] ];
1269
+ NSMutableString * deleteSQL = [NSMutableString stringWithFormat: @" delete from %@ " ,tableName ];
1273
1270
NSMutableArray * values = [self extractQuery: deleteSQL where: where];
1274
1271
1275
1272
BOOL result = [self executeSQL: deleteSQL arguments: values];
@@ -1279,7 +1276,7 @@ -(BOOL)deleteWithClassBase:(Class)modelClass where:(id)where
1279
1276
#pragma mark - other operation
1280
1277
-(BOOL )isExistsModel : (NSObject *)model
1281
1278
{
1282
- checkModelIsInvalid (model);
1279
+ LKDBCheck_modelIsInvalid (model);
1283
1280
NSString * pwhere = nil ;
1284
1281
if (model.rowid >0 )
1285
1282
{
@@ -1298,11 +1295,11 @@ -(BOOL)isExistsModel:(NSObject *)model
1298
1295
}
1299
1296
-(BOOL )isExistsClass : (Class )modelClass where : (id )where
1300
1297
{
1301
- return [self isExistsClassBase: modelClass where: where];
1298
+ return [self isExistsWithTableName: [ modelClass getTableName ] where: where];
1302
1299
}
1303
- -(BOOL )isExistsClassBase : ( Class ) modelClass where : (id )where
1300
+ -(BOOL )isExistsWithTableName : ( NSString *) tableName where : (id )where
1304
1301
{
1305
- return [self rowCount: modelClass where: where] > 0 ;
1302
+ return [self rowCountWithTableName: tableName where: where] > 0 ;
1306
1303
}
1307
1304
1308
1305
#pragma mark- clear operation
0 commit comments