Skip to content

Commit

Permalink
增加调用sqliteb的原生函数计算sun,min,max,avg的API
Browse files Browse the repository at this point in the history
增加调用sqliteb的原生函数计算sun,min,max,avg的API.
  • Loading branch information
huangzhibiao committed Jul 14, 2017
1 parent 1c90eab commit 0002672
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 19 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,4 @@
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "BGFMDB/ViewController.m"
timestampString = "520328519.947833"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "197"
endingLineNumber = "197"
landmarkName = "-viewDidLoad"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
Binary file modified BGFMDB/.DS_Store
Binary file not shown.
8 changes: 7 additions & 1 deletion BGFMDB/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ - (void)viewDidLoad {
*/
[p save];

/**
使用原生函数求某个整数类型的属性的总和,最大值,最小值,平均值等.
*/
// NSInteger num = [People bg_sqliteMethodWithType:bg_sum key:@"age"];
// NSLog(@"sum(age) = %@",@(num));

/**
同步存储或更新.
当自定义“唯一约束”时可以使用此接口存储更方便,当"唯一约束"的数据存在时,此接口会更新旧数据,没有则存储新数据.
Expand Down Expand Up @@ -397,7 +403,7 @@ -(People*)people{
People* p = [People new];
p.name = @"斯巴达";
p.num = @(220.88);
p.age = 50;
p.age = 3;
p.sex = @"";
p.eye = @"末世眼皮";
p.Url = [NSURL URLWithString:@"http://www.gmjk.com"];
Expand Down
4 changes: 4 additions & 0 deletions BGFMDB/libs/BGFMDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@
keyPath查询数据条数.
*/
-(NSInteger)countForTable:(NSString* _Nonnull)name forKeyPathAndValues:(NSArray* _Nonnull)keyPathValues;
/**
直接调用sqliteb的原生函数计算sun,min,max,avg等.
*/
-(NSInteger)sqliteMethodForTable:(NSString* _Nonnull)name type:(bg_sqliteMethodType)methodType key:(NSString* _Nonnull)key;
/**
刷新数据库,即将旧数据库的数据复制到新建的数据库,这是为了去掉没用的字段.
@name 表名称.
Expand Down
49 changes: 49 additions & 0 deletions BGFMDB/libs/BGFMDB.m
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,55 @@ -(NSInteger)countForTable:(NSString* _Nonnull)name conditions:(NSString* _Nullab
dispatch_semaphore_signal(self.semaphore);
return count;
}
/**
直接调用sqliteb的原生函数计算sun,min,max,avg等.
*/
-(NSInteger)sqliteMethodQueueForTable:(NSString* _Nonnull)name type:(bg_sqliteMethodType)methodType key:(NSString*)key{
NSAssert(name,@"表名不能为空!");
NSAssert(key,@"属性名不能为空!");
__block NSUInteger num = 0;
NSString* method;
switch (methodType) {
case bg_min:
method = [NSString stringWithFormat:@"min(%@%@)",BG,key];
break;
case bg_max:
method = [NSString stringWithFormat:@"max(%@%@)",BG,key];
break;
case bg_sum:
method = [NSString stringWithFormat:@"sum(%@%@)",BG,key];
break;
case bg_avg:
method = [NSString stringWithFormat:@"avg(%@%@)",BG,key];
break;
default:
NSAssert(NO,@"请传入方法类型!");
break;
}
[self executeDB:^(FMDatabase * _Nonnull db){
NSString* SQL = [NSString stringWithFormat:@"select %@ from %@",method,name];
debug(SQL);
[db executeStatements:SQL withResultBlock:^int(NSDictionary *resultsDictionary) {
num = [[resultsDictionary.allValues lastObject] integerValue];
return 0;
}];
}];
return num;
}

/**
直接调用sqliteb的原生函数计算sun,min,max,avg等.
*/
-(NSInteger)sqliteMethodForTable:(NSString* _Nonnull)name type:(bg_sqliteMethodType)methodType key:(NSString*)key{
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
NSInteger num = 0;
@autoreleasepool {
num = [self sqliteMethodQueueForTable:name type:methodType key:key];
}
dispatch_semaphore_signal(self.semaphore);
return num;
}

/**
keyPath查询数据条数.
*/
Expand Down
8 changes: 8 additions & 0 deletions BGFMDB/libs/BGTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ typedef NS_ENUM(NSInteger,dealState){//处理状态
Incomplete = 0,//处理不完整
Complete = 1//处理完整
};

typedef NS_ENUM(NSInteger,bg_sqliteMethodType){//处理状态
bg_min,//求最小值
bg_max,//求最大值
bg_sum,//求总和值
bg_avg//求平均值
};

//keyPath查询用的关系,Equal:等于的关系;Contains:包含的关系.
typedef NSString* _Nonnull Relation;
extern Relation const Equal;
Expand Down
6 changes: 6 additions & 0 deletions BGFMDB/libs/NSObject+BGModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,12 @@ NSMutableData,UIImage,NSDate,NSURL,NSRange,CGRect,CGSize,CGPoint,自定义对象
即查询user.student.name=@"小芳" 和 user.student.content中包含@“书”这个字符串的对象的条数.
*/
+(NSInteger)countForKeyPathAndValues:(NSArray* _Nonnull)keyPathValues;
/**
直接调用sqliteb的原生函数计算sun,min,max,avg等.
用法:NSInteger num = [People bg_sqliteMethodWithType:bg_sum key:@"age"];
提示: 不支持keyPath
*/
+(NSInteger)bg_sqliteMethodWithType:(bg_sqliteMethodType)methodType key:(NSString* _Nonnull)key;
/**
获取本类数据表当前版本号.
*/
Expand Down
11 changes: 11 additions & 0 deletions BGFMDB/libs/NSObject+BGModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,17 @@ +(NSInteger)countForKeyPathAndValues:(NSArray* _Nonnull)keyPathValues{
[[BGFMDB shareManager] closeDB];
return count;
}
/**
直接调用sqliteb的原生函数计算sun,min,max,avg等.
用法:NSInteger num = [People bg_sqliteMethodWithType:bg_sum key:@"age"];
提示: 不支持keyPath
*/
+(NSInteger)bg_sqliteMethodWithType:(bg_sqliteMethodType)methodType key:(NSString* _Nonnull)key{
NSInteger num = [[BGFMDB shareManager] sqliteMethodForTable:NSStringFromClass([self class]) type:methodType key:key];
//关闭数据库
[[BGFMDB shareManager] closeDB];
return num;
}
/**
获取本类数据表当前版本号.
*/
Expand Down

0 comments on commit 0002672

Please sign in to comment.