Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD YTDB #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 22 additions & 4 deletions YTKKeyValueStore/YTKKeyValueStore.h 100644 → 100755
Expand Up @@ -25,8 +25,6 @@

- (void)createTableWithName:(NSString *)tableName;

- (BOOL)isTableExists:(NSString *)tableName;

- (void)clearTable:(NSString *)tableName;

- (void)close;
Expand All @@ -48,8 +46,7 @@
- (NSNumber *)getNumberById:(NSString *)numberId fromTable:(NSString *)tableName;

- (NSArray *)getAllItemsFromTable:(NSString *)tableName;

- (NSUInteger)getCountFromTable:(NSString *)tableName;
- (NSArray *)getAllIdFromTable:(NSString *)tableName;

- (void)deleteObjectById:(NSString *)objectId fromTable:(NSString *)tableName;

Expand All @@ -59,3 +56,24 @@


@end
/**
* 简单粗暴的封装一层
*/
@interface YTDB : YTKKeyValueStore
/**
* 直接传入对象保存,object传入nil时删除数据,不传入table名称时都传入default表
*/
+(void)putObject:(id)object fromId:(NSString *)objectId;
+(void)putObject:(id)object fromId:(NSString *)objectId formTable:(NSString *)table;
/**
* 获取对象
*/
+(id)getObjectById:(NSString *)objectId;
+(id)getObjectById:(NSString *)objectId fromTable:(NSString *)table;

/**
* 删除表
*/
+(void)deleteDefaultTable;
+(void)deleteTable:(NSString *)table;
@end
120 changes: 93 additions & 27 deletions YTKKeyValueStore/YTKKeyValueStore.m 100644 → 100755
Expand Up @@ -8,13 +8,12 @@

#import "YTKKeyValueStore.h"
#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
#import "FMDatabaseQueue.h"

#ifdef DEBUG
#define debugLog(...) NSLog(__VA_ARGS__)
#define debugMethod() NSLog(@"%s", __func__)
#define debugError() NSLog(@"Error at %s Line:%d", __func__, __LINE__)
#define debugError() NSLog(@"Error at %s Line:%zd", __func__, __LINE__)
#else
#define debugLog(...)
#define debugMethod()
Expand All @@ -39,6 +38,7 @@ @interface YTKKeyValueStore()

@implementation YTKKeyValueStore

static NSString *const DEFAULT_TABLE_NAME = @"default_table";
static NSString *const DEFAULT_DB_NAME = @"database.sqlite";

static NSString *const CREATE_TABLE_SQL =
Expand All @@ -55,7 +55,7 @@ PRIMARY KEY(id)) \

static NSString *const SELECT_ALL_SQL = @"SELECT * from %@";

static NSString *const COUNT_ALL_SQL = @"SELECT count(*) as num from %@";
static NSString *const SELECT_ALL_ID_SQL = @"SELECT id from %@";

static NSString *const CLEAR_ALL_SQL = @"DELETE from %@";

Expand All @@ -81,7 +81,7 @@ - (id)initDBWithName:(NSString *)dbName {
self = [super init];
if (self) {
NSString * dbPath = [PATH_OF_DOCUMENT stringByAppendingPathComponent:dbName];
debugLog(@"dbPath = %@", dbPath);
// debugLog(@"dbPath = %@", dbPath);
if (_dbQueue) {
[self close];
}
Expand All @@ -93,7 +93,7 @@ - (id)initDBWithName:(NSString *)dbName {
- (id)initWithDBWithPath:(NSString *)dbPath {
self = [super init];
if (self) {
debugLog(@"dbPath = %@", dbPath);
// debugLog(@"dbPath = %@", dbPath);
if (_dbQueue) {
[self close];
}
Expand All @@ -116,20 +116,6 @@ - (void)createTableWithName:(NSString *)tableName {
}
}

- (BOOL)isTableExists:(NSString *)tableName{
if ([YTKKeyValueStore checkTableName:tableName] == NO) {
return NO;
}
__block BOOL result;
[_dbQueue inDatabase:^(FMDatabase *db) {
result = [db tableExists:tableName];
}];
if (!result) {
debugLog(@"ERROR, table: %@ not exists in current DB", tableName);
}
return result;
}

- (void)clearTable:(NSString *)tableName {
if ([YTKKeyValueStore checkTableName:tableName] == NO) {
return;
Expand All @@ -148,6 +134,14 @@ - (void)putObject:(id)object withId:(NSString *)objectId intoTable:(NSString *)t
if ([YTKKeyValueStore checkTableName:tableName] == NO) {
return;
}
if(!object)
{
object = @[@""];
}
if([object isKindOfClass:NSString.class])
{
object = @[object];
}
NSError * error;
NSData * data = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error];
if (error) {
Expand Down Expand Up @@ -271,24 +265,25 @@ - (NSArray *)getAllItemsFromTable:(NSString *)tableName {
}
return result;
}

- (NSUInteger)getCountFromTable:(NSString *)tableName
- (NSArray *)getAllIdFromTable:(NSString *)tableName
{
if ([YTKKeyValueStore checkTableName:tableName] == NO) {
return 0;
return nil;
}
NSString * sql = [NSString stringWithFormat:COUNT_ALL_SQL, tableName];
__block NSInteger num = 0;
NSString * sql = [NSString stringWithFormat:SELECT_ALL_ID_SQL, tableName];
__block NSMutableArray * result = [NSMutableArray array];
[_dbQueue inDatabase:^(FMDatabase *db) {
FMResultSet * rs = [db executeQuery:sql];
if ([rs next]) {
num = [rs unsignedLongLongIntForColumn:@"num"];
while ([rs next])
{
[result addObject:[rs stringForColumn:@"id"]];
}
[rs close];
}];
return num;
return result;
}


- (void)deleteObjectById:(NSString *)objectId fromTable:(NSString *)tableName {
if ([YTKKeyValueStore checkTableName:tableName] == NO) {
return;
Expand Down Expand Up @@ -347,4 +342,75 @@ - (void)close {
_dbQueue = nil;
}

@end

@implementation YTDB
+(instancetype)share:(NSString *)dbName
{
static YTDB *db = nil;
static dispatch_once_t once;
dispatch_once(&once, ^{
db = [[YTDB alloc]initDBWithName:dbName];
});
return db;
}
/**
* 直接传入对象保存,object传入nil时删除数据
*/
+(void)putObject:(id)object fromId:(NSString *)objectId
{
[self putObject:object fromId:objectId formTable:nil];
}
+(void)putObject:(id)object fromId:(NSString *)objectId formTable:(NSString *)table
{
table = table ? table : DEFAULT_TABLE_NAME;
objectId = objectId ? objectId : @"";
YTDB *db = [YTDB share:DEFAULT_DB_NAME];
@synchronized(db)
{
[db createTableWithName:table];
if(object)
{
[db putObject:object withId:objectId intoTable:table];
}else{
[db deleteObjectById:objectId fromTable:table];
}
}
}
/**
* 获取对象
*/
+(id)getObjectById:(NSString *)objectId
{
return [self getObjectById:objectId fromTable:nil];
}
+(id)getObjectById:(NSString *)objectId fromTable:(NSString *)table
{
table = table ? table : DEFAULT_TABLE_NAME;
objectId = objectId ? objectId : @"";
YTDB *db = [YTDB share:DEFAULT_DB_NAME];
@synchronized(db)
{
return [db getObjectById:objectId fromTable:table];
}
}

/**
* 删除表
*/
+(void)deleteDefaultTable
{
[self deleteTable:nil];
}
+(void)deleteTable:(NSString *)table
{
table = table ? table : DEFAULT_TABLE_NAME;
YTDB *db = [YTDB share:DEFAULT_DB_NAME];
@synchronized(db)
{
[db clearTable:table];
}
}


@end