Skip to content

Commit

Permalink
Refactor: LKDBSearchHelper code refine
Browse files Browse the repository at this point in the history
1. apply chaining to SQL commands : Select, Delete
2. code clean
  • Loading branch information
wving5 committed May 24, 2019
1 parent 36cfd7f commit 7e44761
Show file tree
Hide file tree
Showing 32 changed files with 498 additions and 724 deletions.
66 changes: 34 additions & 32 deletions Demo-iOS/iOS-Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
366B23461CFD8CEB006743C0 /* Snip20130620_6.png in Resources */ = {isa = PBXBuildFile; fileRef = 366B23451CFD8CEB006743C0 /* Snip20130620_6.png */; };
6BCE93B2428F3E6E81B59859 /* libPods-iOS-Demo.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A05ED3193E536326FFEB6E6B /* libPods-iOS-Demo.a */; };
B61E1DF32296A3BF00D34103 /* LKDBSQLBuilderTest.m in Sources */ = {isa = PBXBuildFile; fileRef = B61E1DF22296A3BF00D34103 /* LKDBSQLBuilderTest.m */; };
B61E1DF62297DBFD00D34103 /* LKSQLCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = B61E1DF52297DBFD00D34103 /* LKSQLCommand.m */; };
B6617501229035B900285B6E /* FMDatabase+Debug.m in Sources */ = {isa = PBXBuildFile; fileRef = B66174FF229035B900285B6E /* FMDatabase+Debug.m */; };
B661751A22903FA000285B6E /* LKDBTransaction.m in Sources */ = {isa = PBXBuildFile; fileRef = B661750A22903FA000285B6E /* LKDBTransaction.m */; };
B661751B22903FA000285B6E /* LKDBSQLite.m in Sources */ = {isa = PBXBuildFile; fileRef = B661750B22903FA000285B6E /* LKDBSQLite.m */; };
B661751C22903FA000285B6E /* LKDBPersistenceObject.m in Sources */ = {isa = PBXBuildFile; fileRef = B661750E22903FA000285B6E /* LKDBPersistenceObject.m */; };
B661751D22903FA000285B6E /* LKDBSelect.m in Sources */ = {isa = PBXBuildFile; fileRef = B661750F22903FA000285B6E /* LKDBSelect.m */; };
B661751D22903FA000285B6E /* LKSQLSelect.m in Sources */ = {isa = PBXBuildFile; fileRef = B661750F22903FA000285B6E /* LKSQLSelect.m */; };
B661751E22903FA000285B6E /* LKDBStringBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = B661751322903FA000285B6E /* LKDBStringBuilder.m */; };
B661751F22903FA000285B6E /* LKDBDelete.m in Sources */ = {isa = PBXBuildFile; fileRef = B661751522903FA000285B6E /* LKDBDelete.m */; };
B661751F22903FA000285B6E /* LKSQLDelete.m in Sources */ = {isa = PBXBuildFile; fileRef = B661751522903FA000285B6E /* LKSQLDelete.m */; };
B661752022903FA000285B6E /* LKSQLCompositeCondition.m in Sources */ = {isa = PBXBuildFile; fileRef = B661751622903FA000285B6E /* LKSQLCompositeCondition.m */; };
B661752122903FA000285B6E /* LKSQLCondition.m in Sources */ = {isa = PBXBuildFile; fileRef = B661751922903FA000285B6E /* LKSQLCondition.m */; };
/* End PBXBuildFile section */
Expand All @@ -42,24 +41,22 @@
AC46E909280B6824E099951C /* Pods-iOS-Demo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-iOS-Demo.release.xcconfig"; path = "Pods/Target Support Files/Pods-iOS-Demo/Pods-iOS-Demo.release.xcconfig"; sourceTree = "<group>"; };
B61E1DF12296A3BF00D34103 /* LKDBSQLBuilderTest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LKDBSQLBuilderTest.h; sourceTree = "<group>"; };
B61E1DF22296A3BF00D34103 /* LKDBSQLBuilderTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LKDBSQLBuilderTest.m; sourceTree = "<group>"; };
B61E1DF42297DBFD00D34103 /* LKSQLCommand.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LKSQLCommand.h; sourceTree = "<group>"; };
B61E1DF52297DBFD00D34103 /* LKSQLCommand.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LKSQLCommand.m; sourceTree = "<group>"; };
B66174FF229035B900285B6E /* FMDatabase+Debug.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "FMDatabase+Debug.m"; sourceTree = "<group>"; };
B6617500229035B900285B6E /* FMDatabase+Debug.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "FMDatabase+Debug.h"; sourceTree = "<group>"; };
B661750922903FA000285B6E /* LKDBStringBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LKDBStringBuilder.h; sourceTree = "<group>"; };
B661750A22903FA000285B6E /* LKDBTransaction.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LKDBTransaction.m; sourceTree = "<group>"; };
B661750B22903FA000285B6E /* LKDBSQLite.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LKDBSQLite.m; sourceTree = "<group>"; };
B661750C22903FA000285B6E /* LKDBDelete.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LKDBDelete.h; sourceTree = "<group>"; };
B661750C22903FA000285B6E /* LKSQLDelete.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LKSQLDelete.h; sourceTree = "<group>"; };
B661750D22903FA000285B6E /* LKSQLCondition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LKSQLCondition.h; sourceTree = "<group>"; };
B661750E22903FA000285B6E /* LKDBPersistenceObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LKDBPersistenceObject.m; sourceTree = "<group>"; };
B661750F22903FA000285B6E /* LKDBSelect.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LKDBSelect.m; sourceTree = "<group>"; };
B661750F22903FA000285B6E /* LKSQLSelect.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LKSQLSelect.m; sourceTree = "<group>"; };
B661751022903FA000285B6E /* LKSQLCompositeCondition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LKSQLCompositeCondition.h; sourceTree = "<group>"; };
B661751122903FA000285B6E /* LKDBSearchHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LKDBSearchHelper.h; sourceTree = "<group>"; };
B661751122903FA000285B6E /* LKSQLBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LKSQLBuilder.h; sourceTree = "<group>"; };
B661751222903FA000285B6E /* LKDBTransaction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LKDBTransaction.h; sourceTree = "<group>"; };
B661751322903FA000285B6E /* LKDBStringBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LKDBStringBuilder.m; sourceTree = "<group>"; };
B661751422903FA000285B6E /* LKDBSQLite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LKDBSQLite.h; sourceTree = "<group>"; };
B661751522903FA000285B6E /* LKDBDelete.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LKDBDelete.m; sourceTree = "<group>"; };
B661751522903FA000285B6E /* LKSQLDelete.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LKSQLDelete.m; sourceTree = "<group>"; };
B661751622903FA000285B6E /* LKSQLCompositeCondition.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LKSQLCompositeCondition.m; sourceTree = "<group>"; };
B661751722903FA000285B6E /* LKDBSelect.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LKDBSelect.h; sourceTree = "<group>"; };
B661751822903FA000285B6E /* LKDBPersistenceObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LKDBPersistenceObject.h; sourceTree = "<group>"; };
B661751722903FA000285B6E /* LKSQLSelect.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LKSQLSelect.h; sourceTree = "<group>"; };
B661751922903FA000285B6E /* LKSQLCondition.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LKSQLCondition.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -137,37 +134,43 @@
name = Frameworks;
sourceTree = "<group>";
};
B61E1DED22962B5500D34103 /* ignored */ = {
B61E1DED22962B5500D34103 /* Commands */ = {
isa = PBXGroup;
children = (
B661751722903FA000285B6E /* LKDBSelect.h */,
B661750F22903FA000285B6E /* LKDBSelect.m */,
B661750C22903FA000285B6E /* LKDBDelete.h */,
B661751522903FA000285B6E /* LKDBDelete.m */,
B61E1DF42297DBFD00D34103 /* LKSQLCommand.h */,
B61E1DF52297DBFD00D34103 /* LKSQLCommand.m */,
B661751722903FA000285B6E /* LKSQLSelect.h */,
B661750F22903FA000285B6E /* LKSQLSelect.m */,
B661750C22903FA000285B6E /* LKSQLDelete.h */,
B661751522903FA000285B6E /* LKSQLDelete.m */,
B661751222903FA000285B6E /* LKDBTransaction.h */,
B661750A22903FA000285B6E /* LKDBTransaction.m */,
B661751122903FA000285B6E /* LKDBSearchHelper.h */,
B661751822903FA000285B6E /* LKDBPersistenceObject.h */,
B661750E22903FA000285B6E /* LKDBPersistenceObject.m */,
B661751422903FA000285B6E /* LKDBSQLite.h */,
B661750B22903FA000285B6E /* LKDBSQLite.m */,
);
path = ignored;
path = Commands;
sourceTree = "<group>";
};
B661750822903FA000285B6E /* LKDBSearchHelper */ = {
B61E1DF72297EF1A00D34103 /* ConditionBuilder */ = {
isa = PBXGroup;
children = (
B61E1DED22962B5500D34103 /* ignored */,
B61E1DF12296A3BF00D34103 /* LKDBSQLBuilderTest.h */,
B61E1DF22296A3BF00D34103 /* LKDBSQLBuilderTest.m */,
B661750922903FA000285B6E /* LKDBStringBuilder.h */,
B661751322903FA000285B6E /* LKDBStringBuilder.m */,
B661750D22903FA000285B6E /* LKSQLCondition.h */,
B661751922903FA000285B6E /* LKSQLCondition.m */,
B661751022903FA000285B6E /* LKSQLCompositeCondition.h */,
B661751622903FA000285B6E /* LKSQLCompositeCondition.m */,
);
path = ConditionBuilder;
sourceTree = "<group>";
};
B661750822903FA000285B6E /* LKDBSearchHelper */ = {
isa = PBXGroup;
children = (
B661751122903FA000285B6E /* LKSQLBuilder.h */,
B61E1DF12296A3BF00D34103 /* LKDBSQLBuilderTest.h */,
B61E1DF22296A3BF00D34103 /* LKDBSQLBuilderTest.m */,
B61E1DED22962B5500D34103 /* Commands */,
B61E1DF72297EF1A00D34103 /* ConditionBuilder */,
);
path = LKDBSearchHelper;
sourceTree = "<group>";
};
Expand Down Expand Up @@ -295,17 +298,16 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B661751C22903FA000285B6E /* LKDBPersistenceObject.m in Sources */,
B661751E22903FA000285B6E /* LKDBStringBuilder.m in Sources */,
366B23441CFD8A63006743C0 /* LKTestModels.m in Sources */,
B661751A22903FA000285B6E /* LKDBTransaction.m in Sources */,
366B23301CFD87F5006743C0 /* AppDelegate.m in Sources */,
B661752122903FA000285B6E /* LKSQLCondition.m in Sources */,
B661751D22903FA000285B6E /* LKDBSelect.m in Sources */,
B661751D22903FA000285B6E /* LKSQLSelect.m in Sources */,
366B232D1CFD87F5006743C0 /* main.m in Sources */,
B661752022903FA000285B6E /* LKSQLCompositeCondition.m in Sources */,
B661751F22903FA000285B6E /* LKDBDelete.m in Sources */,
B661751B22903FA000285B6E /* LKDBSQLite.m in Sources */,
B61E1DF62297DBFD00D34103 /* LKSQLCommand.m in Sources */,
B661751F22903FA000285B6E /* LKSQLDelete.m in Sources */,
B61E1DF32296A3BF00D34103 /* LKDBSQLBuilderTest.m in Sources */,
B6617501229035B900285B6E /* FMDatabase+Debug.m in Sources */,
);
Expand Down
3 changes: 3 additions & 0 deletions Demo-iOS/iOS-Demo/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#import <UIKit/UIKit.h>

// TEST auto-running on `UIApplicationDidFinishLaunchingNotification`
#define ENALBE_LKDBTest 0

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
Expand Down
2 changes: 2 additions & 0 deletions Demo-iOS/iOS-Demo/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.tv = textview;
[self.window makeKeyAndVisible];

#if ENALBE_LKDBTest == 1
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self test];
});
#endif
return YES;
}

Expand Down
31 changes: 31 additions & 0 deletions Demo-iOS/iOS-Demo/LKDBSearchHelper/Commands/LKDBTransaction.h
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 Demo-iOS/iOS-Demo/LKDBSearchHelper/Commands/LKDBTransaction.m
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 Demo-iOS/iOS-Demo/LKDBSearchHelper/Commands/LKSQLCommand.h
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 Demo-iOS/iOS-Demo/LKDBSearchHelper/Commands/LKSQLCommand.m
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
11 changes: 11 additions & 0 deletions Demo-iOS/iOS-Demo/LKDBSearchHelper/Commands/LKSQLDelete.h
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
Loading

0 comments on commit 7e44761

Please sign in to comment.