Skip to content

Commit

Permalink
扩展执行自定义SQL语句接口
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuxuewei committed Dec 17, 2018
1 parent 174aeed commit c9e6ee1
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 12 deletions.
14 changes: 9 additions & 5 deletions Example/XWDatabase/Model/XWPerson.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ + (XWPerson *)testPerson:(int)index {
person.books = @{@"name":@"iOS 从入门到掉头发"}.mutableCopy;
person.number = [NSNumber numberWithBool:YES];
person.floatNumber = [NSNumber numberWithFloat:3.1415926];

/// 存储二进制文件
UIImage *image = [UIImage imageNamed:@"icon"];
person.icon = UIImageJPEGRepresentation(image, 0.5).mutableCopy;
person.image = image;
// person.icon = UIImageJPEGRepresentation(image, 0.5).mutableCopy;

person.pFloat = 1.1111;
person.pInt = 3;
person.pDouble = 2.2222;
Expand All @@ -49,10 +52,11 @@ + (XWPerson *)testPerson:(int)index {
person.pRange = NSMakeRange(0, 99);
person.indexPath = [NSIndexPath indexPathForItem:1 inSection:2];

XWBook *book = [XWBook new];
book.name = @"name";
book.author = @"JK";
person.favoriteBook = book;
/// 存储自定义对象
// XWBook *book = [XWBook new];
// book.name = @"name";
// book.author = @"JK";
// person.favoriteBook = book;

return person;
}
Expand Down
6 changes: 3 additions & 3 deletions Example/XWDatabase/XWViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ - (void)viewDidLoad
[super viewDidLoad];

/// 增
// [self saveOnePerson];
[self saveOnePerson];
// [self saveModels];

/// 删
Expand Down Expand Up @@ -55,7 +55,7 @@ - (void)saveOnePerson
- (void)saveModels
{
NSMutableArray *persons = [[NSMutableArray alloc] init];
for (int i = 0; i < 1000; i++) {
for (int i = 0; i < 100; i++) {
[persons addObject:[XWPerson testPerson:i]];
}
[XWDatabase saveModels:persons completion:^(BOOL isSuccess) {
Expand Down Expand Up @@ -112,7 +112,7 @@ - (void)updateModel
- (void)getOnePerson
{
XWPerson *person = [XWPerson new];
person.cardID = @"81";
person.cardID = @"2";
[XWDatabase getModel:person completion:^(XWPerson * obj) {
NSLog(@" <XWDatabase> getOnePerson (%@) name: %@",obj,obj.name);
dispatch_async(dispatch_get_main_queue(), ^{
Expand Down
2 changes: 1 addition & 1 deletion XWDatabase.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'XWDatabase'
s.version = '1.0.0'
s.version = '1.0.1'
s.summary = '数据库工具类,直接操作模型读写数据库,FMDB封装'

# This description is used to generate tags and improve search results.
Expand Down
35 changes: 32 additions & 3 deletions XWDatabase/Classes/XWDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

#import <Foundation/Foundation.h>
#import "XWDatabaseModelProtocol.h"
@class FMResultSet;

NS_ASSUME_NONNULL_BEGIN

typedef void(^XWDatabaseCompletion)(BOOL isSuccess); /// 操作回调
typedef void(^XWDatabaseReturnObject)(id _Nullable obj); /// 返回对象
typedef void(^XWDatabaseReturnObjects)(NSArray * _Nullable objs); /// 返回对象数组
typedef void(^XWDatabaseCompletion)(BOOL isSuccess); /// 操作回调
typedef void(^XWDatabaseReturnObject)(id _Nullable obj); /// 返回对象
typedef void(^XWDatabaseReturnObjects)(NSArray * _Nullable objs); /// 返回对象数组
typedef void(^XWDatabaseReturnResultSet)(FMResultSet * _Nullable resultSet);/// 返回 FMResultSet 原生查询结果-需自己解析

@interface XWDatabase : NSObject

Expand Down Expand Up @@ -125,6 +127,33 @@ typedef void(^XWDatabaseReturnObjects)(NSArray * _Nullable objs); /// 返
@param completion 结果
*/
+ (void)getModels:(Class<XWDatabaseModelProtocol>)cls sortColumn:(NSString * _Nullable)sortColumn isOrderDesc:(BOOL)isOrderDesc condition:(NSString * _Nullable)condition completion:(XWDatabaseReturnObjects)completion;

#pragma mark - 执行自定义SQL语句

/**
执行单条自定义 SQL 更新语句
@param sql 自定义 SQL 更新语句
@param completion 完成回调
*/
+ (void)executeUpdateSql:(NSString *)sql completion:(XWDatabaseCompletion)completion;

/**
执行多条自定义 SQL 更新语句
@param sqls 多条自定义 SQL 更新语句
@param completion 完成回调
*/
+ (void)executeUpdateSqls:(NSArray <NSString *> *)sqls completion:(XWDatabaseCompletion)completion;

/**
执行单条自定义 SQL 查询语句
@param sql 自定义 SQL 查询语句
@param completion 完成回调
*/
+ (void)executeQuerySql:(NSString *)sql completion:(XWDatabaseReturnResultSet)completion;

@end

NS_ASSUME_NONNULL_END
65 changes: 65 additions & 0 deletions XWDatabase/Classes/XWDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,71 @@ + (void)getModels:(Class<XWDatabaseModelProtocol>)cls sortColumn:(NSString *)sor
}];
}

#pragma mark - 执行自定义SQL语句

/**
执行单条自定义 SQL 更新语句
@param sql 自定义 SQL 更新语句
@param completion 完成回调
*/
+ (void)executeUpdateSql:(NSString *)sql completion:(XWDatabaseCompletion)completion {
if (!sql || sql.length == 0) {
completion ? completion(NO) : nil;
return;
}
[XWLivingThread executeTaskInMain:^{
[self p_executeUpdate:sql completion:completion];
}];
}

/**
执行多条自定义 SQL 更新语句
@param sqls 多条自定义 SQL 更新语句
@param completion 完成回调
*/
+ (void)executeUpdateSqls:(NSArray <NSString *> *)sqls completion:(XWDatabaseCompletion)completion {
if (!sqls || sqls.count == 0) {
completion ? completion(NO) : nil;
return;
}
[XWLivingThread executeTaskInMain:^{
[[XWDatabaseQueue shareInstance] inTransaction:^(FMDatabase * _Nonnull database, BOOL * _Nonnull rollback) {
[sqls enumerateObjectsUsingBlock:^(NSString * sql, NSUInteger idx, BOOL * _Nonnull stop) {
if (![database executeUpdate:sql]) {
completion ? completion(NO) : nil;
*rollback = YES;
return ;
}
if (idx == sqls.count - 1) {
completion ? completion(YES) : nil;
}
}];
}];
}];
}

/**
执行单条自定义 SQL 查询语句
@param sql 自定义 SQL 查询语句
@param completion 完成回调
*/
+ (void)executeQuerySql:(NSString *)sql completion:(XWDatabaseReturnResultSet)completion {
if (!sql || sql.length == 0) {
completion ? completion(nil) : nil;
return;
}
[XWLivingThread executeTaskInMain:^{
[[XWDatabaseQueue shareInstance] inDatabase:^(FMDatabase * _Nonnull database) {
FMResultSet *resultSet = [XWDatabaseQueue executeQuerySql:sql database:database];
completion ? completion(resultSet) : nil;
}];
}];
}


#pragma mark - private
#pragma mark
+ (void)p_saveModel:(NSObject <XWDatabaseModelProtocol>*)obj completion:(XWDatabaseCompletion)completion {
Expand Down

0 comments on commit c9e6ee1

Please sign in to comment.