forked from li6185377/LKDBHelper-SQLite-ORM
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: LKDBSearchHelper code refine
1. apply chaining to SQL commands : Select, Delete 2. code clean
- Loading branch information
Showing
32 changed files
with
498 additions
and
724 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
Demo-iOS/iOS-Demo/LKDBSearchHelper/Commands/LKDBTransaction.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@class LKDBHelper; | ||
|
||
/* DB Transcation wrapper */ | ||
|
||
typedef NS_ENUM(NSUInteger, LKDBSQLAction) { | ||
LKDBSQLActionInsert = 0, | ||
LKDBSQLActionUpdate = 1, | ||
LKDBSQLActionDelete = 2, | ||
}; | ||
|
||
@protocol LKDBTransactionData <NSObject> | ||
@property (nonatomic,assign) LKDBSQLAction action; | ||
@end | ||
|
||
@interface LKDBTransaction : NSObject | ||
+ (instancetype)newTransaction; | ||
|
||
- (void)batchUpdate:(NSArray <LKDBTransactionData> *)dataArr; | ||
- (void)batchInsert:(NSArray <LKDBTransactionData> *)dataArr; | ||
- (void)batchDelete:(NSArray <LKDBTransactionData> *)dataArr; | ||
- (void)update:(id <LKDBTransactionData>)object; | ||
- (void)insert:(id <LKDBTransactionData>)object; | ||
- (void)delete:(id <LKDBTransactionData>)object; | ||
|
||
- (void)executeToDB:(LKDBHelper *)dbHelper; | ||
|
||
@end |
82 changes: 82 additions & 0 deletions
82
Demo-iOS/iOS-Demo/LKDBSearchHelper/Commands/LKDBTransaction.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
|
||
|
||
#import "LKDBTransaction.h" | ||
#import <LKDBHelper.h> | ||
|
||
|
||
@interface LKDBTransaction() { | ||
NSMutableArray *_actionDataArr; | ||
} | ||
@end | ||
|
||
@implementation LKDBTransaction | ||
+ (instancetype)newTransaction { | ||
return [self new]; | ||
} | ||
|
||
- (instancetype)init | ||
{ | ||
if (self = [super init]) { | ||
_actionDataArr = [NSMutableArray array]; | ||
} | ||
return self; | ||
} | ||
|
||
// MARK:- DB ops | ||
- (void)batchUpdate:(NSArray <LKDBTransactionData> *)dataArr{ | ||
[dataArr enumerateObjectsUsingBlock:^(id <LKDBTransactionData> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { | ||
obj.action = LKDBSQLActionUpdate; | ||
[_actionDataArr addObject:obj]; | ||
}]; | ||
} | ||
|
||
- (void)batchInsert:(NSArray <LKDBTransactionData> *)dataArr{ | ||
[dataArr enumerateObjectsUsingBlock:^(id <LKDBTransactionData> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { | ||
obj.action = LKDBSQLActionInsert; | ||
[_actionDataArr addObject:obj]; | ||
}]; | ||
} | ||
|
||
- (void)batchDelete:(NSArray <LKDBTransactionData> *)dataArr{ | ||
[dataArr enumerateObjectsUsingBlock:^(id <LKDBTransactionData> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { | ||
obj.action = LKDBSQLActionDelete; | ||
[_actionDataArr addObject:obj]; | ||
}];} | ||
|
||
- (void)update:(id <LKDBTransactionData>)object{ | ||
object.action = LKDBSQLActionUpdate; | ||
[_actionDataArr addObject:object]; | ||
} | ||
|
||
- (void)insert:(id <LKDBTransactionData>)object{ | ||
object.action = LKDBSQLActionInsert; | ||
[_actionDataArr addObject:object]; | ||
} | ||
|
||
- (void)delete:(id <LKDBTransactionData>)object{ | ||
object.action = LKDBSQLActionDelete; | ||
[_actionDataArr addObject:object]; | ||
} | ||
|
||
// MARK:- excute | ||
- (void)executeToDB:(LKDBHelper *)dbHelper{ | ||
[dbHelper executeForTransaction:^BOOL(LKDBHelper *helper) { | ||
@try { | ||
for (NSObject <LKDBTransactionData> * object in _actionDataArr) { | ||
if(object.action == LKDBSQLActionInsert) | ||
[object saveToDB]; | ||
|
||
if(object.action == LKDBSQLActionUpdate) | ||
[object updateToDB]; | ||
|
||
if(object.action == LKDBSQLActionDelete) | ||
[object deleteToDB]; | ||
} | ||
return YES; | ||
} @catch (NSException *exception) { | ||
return NO; | ||
} | ||
}]; | ||
} | ||
|
||
@end |
41 changes: 41 additions & 0 deletions
41
Demo-iOS/iOS-Demo/LKDBSearchHelper/Commands/LKSQLCommand.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// LKSQLCommand.h | ||
// iOS-Demo | ||
// | ||
// Copyright © 2019年 Mars. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
@class LKSQLCondition, LKDBHelper; | ||
|
||
/* CRUD Base Class */ | ||
|
||
@interface LKSQLCommand : NSObject | ||
|
||
+ (instancetype)clause; | ||
- (LKSQLCommand *(^)(Class tblClass))from; | ||
- (LKSQLCommand *(^)(LKSQLCondition *sqlCondition))where; | ||
- (LKSQLCommand *(^)(NSArray *orderByList))orderBy; | ||
- (LKSQLCommand *(^)(NSArray *groupByList))groupBy; | ||
- (LKSQLCommand *(^)(NSInteger limit))limit; | ||
- (LKSQLCommand *(^)(NSInteger offset))offset; | ||
|
||
/* SQL translate */ | ||
- ( NSString *)toString; // @abstract | ||
|
||
/* LKDB wrapper */ | ||
- (id (^)())exec; // @abstract | ||
- (id (^)(LKDBHelper *))execIn; // @abstract | ||
|
||
|
||
|
||
/* accessor for Subclasses */ | ||
@property (nonatomic, readonly, assign) NSInteger mLimit; | ||
@property (nonatomic, readonly, assign) NSInteger mOffset; | ||
@property (nonatomic, readonly, assign) BOOL countOnly; | ||
@property (nonatomic, readonly, strong) Class tblClass; | ||
@property (nonatomic, readonly, strong) LKSQLCondition* conditionGroup; | ||
@property (nonatomic, readonly, copy) NSArray* orderByList; | ||
@property (nonatomic, readonly, copy) NSArray* groupByList; | ||
|
||
@end |
75 changes: 75 additions & 0 deletions
75
Demo-iOS/iOS-Demo/LKDBSearchHelper/Commands/LKSQLCommand.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// LKSQLCommand.m | ||
// iOS-Demo | ||
// | ||
// Copyright © 2019年 Mars. All rights reserved. | ||
// | ||
|
||
#import "LKSQLCommand.h" | ||
@interface LKSQLCommand () | ||
@property (nonatomic, assign) NSInteger mLimit; | ||
@property (nonatomic, assign) NSInteger mOffset; | ||
@property (nonatomic, assign) BOOL countOnly; | ||
@property (nonatomic, strong) Class tblClass; | ||
@property (nonatomic, strong) LKSQLCondition* conditionGroup; | ||
@property (nonatomic, copy) NSArray* orderByList; | ||
@property (nonatomic, copy) NSArray* groupByList; | ||
@end | ||
|
||
@implementation LKSQLCommand | ||
// MARK:- DB cretieria | ||
+ (instancetype)clause { return [self new]; } | ||
|
||
- (LKSQLCommand *(^)(Class tblClass))from | ||
{ | ||
return ^id(Class tblCls) { | ||
self.tblClass = tblCls; | ||
return self; | ||
}; | ||
} | ||
|
||
- (LKSQLCommand *(^)(LKSQLCondition *sqlCondition))where | ||
{ | ||
return ^id(LKSQLCondition* sqlCond) { | ||
self.conditionGroup = sqlCond; | ||
return self; | ||
}; | ||
} | ||
|
||
- (LKSQLCommand *(^)(NSArray *orderByList))orderBy | ||
{ | ||
return ^id(NSArray *orderByList) { | ||
self.orderByList = orderByList; | ||
return self; | ||
}; | ||
} | ||
|
||
- (LKSQLCommand *(^)(NSArray *groupByList))groupBy | ||
{ | ||
return ^id(NSArray *groupByList) { | ||
self.groupByList = groupByList; | ||
return self; | ||
}; | ||
} | ||
|
||
- (LKSQLCommand *(^)(NSInteger offset))offset | ||
{ | ||
return ^id(NSInteger offset) { | ||
self.mOffset = offset; | ||
return self; | ||
}; | ||
} | ||
|
||
- (LKSQLCommand *(^)(NSInteger limit))limit | ||
{ | ||
return ^id(NSInteger limit) { | ||
self.mLimit = limit; | ||
return self; | ||
}; | ||
} | ||
|
||
- ( NSString *)toString { /* @abstract */ return nil;} | ||
- (id (^)())exec { /* @abstract */ return nil;} | ||
- (id (^)(LKDBHelper *))execIn { /* @abstract */ return nil;} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
|
||
#import "LKSQLCommand.h" | ||
|
||
#define _SQLDelete LKSQLDelete.clause | ||
|
||
/* `Delete` Statement */ | ||
|
||
@interface LKSQLDelete : LKSQLCommand | ||
|
||
@end |
Oops, something went wrong.