Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
ccworld1000 committed Dec 14, 2017
1 parent 4980414 commit 69400b6
Show file tree
Hide file tree
Showing 7 changed files with 1,315 additions and 1,437 deletions.
117 changes: 59 additions & 58 deletions CCSQLite/CCKeyValue.m
Expand Up @@ -11,53 +11,54 @@

@interface CCKeyValue ()

@property (atomic, copy) NSString *path;
@property (atomic, copy) NSString * path;

@property (atomic, copy) CCSQLiteSerializer objectSerializer;
@property (atomic, copy) CCSQLiteDeserializer objectDeserializer;

- (void) setObject: (id) object key : (NSString *) key;
- (id) objectForKey : (NSString *) key;
- (void) setObject:(id)object key:(NSString *)key;
- (id) objectForKey:(NSString *)key;

@end

@implementation CCKeyValue

+ (CCKeyValue *) defaultKeyValueWithPath : (NSString *)path {
static CCKeyValue *kv = nil;
+ (CCKeyValue *) defaultKeyValueWithPath:(NSString *)path {
static CCKeyValue * kv = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
kv = [CCKeyValue new];
kv.valueType = CCKeyValueTypeDefault;
});

kv.path = path;

return kv;
}

- (void) setObject: (id) object key : (NSString *) key inCollection: (NSString *) collection metadata: (NSData *)metadata {
- (void) setObject:(id)object key:(NSString *)key inCollection:(NSString *)collection metadata:(NSData *)metadata {
if (!_path || ![_path isKindOfClass:[NSString class]]) {
NSLog(@"The actual path of the database must be obtained!");
return;
}
NSString *c = nil;

NSString * c = nil;

if (!collection || ![collection isKindOfClass:[NSString class]]) {
c = CCSQLiteCollection;
} else {
c = collection;
}

if (!key || ![key isKindOfClass:[NSString class]]) {
NSLog(@"key is illegal");
return;
}

self.objectSerializer = [CCSQLite defaultSerializer];

switch (_valueType) {
case CCKeyValueTypePropertyList:
self.objectSerializer = [CCSQLite propertyListSerializer];
Expand All @@ -71,45 +72,45 @@ - (void) setObject: (id) object key : (NSString *) key inCollection: (NSString *
default:
break;
}
NSData *data = self.objectSerializer(c, key, object);
CCSQLiteQueue *innerQueue = [CCSQLiteQueue databaseQueueWithPath: _path];

NSData * data = self.objectSerializer(c, key, object);

CCSQLiteQueue * innerQueue = [CCSQLiteQueue databaseQueueWithPath:_path];

__block BOOL isOK = NO;
[innerQueue inTransaction:^(CCSQLite *db, BOOL *rollback) {
isOK = [db executeUpdate:@"insert into 'CCSQLite.Database2' (collection, key, data, metadata) values (?, ?, ?, ?);", c, key, data, metadata];

if (!isOK) {
isOK = [db executeUpdate:@"update 'CCSQLite.Database2' set data = ? , metadata = ? where collection = ? and key = ?;", data, metadata, c, key];
}

if (!isOK) {
NSLog(@"execute excpetion!");
}

*rollback = !isOK;
}];
}
[innerQueue inTransaction:^(CCSQLite * db, BOOL * rollback) {
isOK = [db executeUpdate:@"insert into 'CCSQLite.Database2' (collection, key, data, metadata) values (?, ?, ?, ?);", c, key, data, metadata];

if (!isOK) {
isOK = [db executeUpdate:@"update 'CCSQLite.Database2' set data = ? , metadata = ? where collection = ? and key = ?;", data, metadata, c, key];
}

if (!isOK) {
NSLog(@"execute excpetion!");
}

- (id) objectForKey : (NSString *) key inCollection: (NSString *) collection {
NSString *c = nil;
*rollback = !isOK;
}];
} /* setObject */


- (id) objectForKey:(NSString *)key inCollection:(NSString *)collection {
NSString * c = nil;
__block id object = nil;

if (!collection || ![collection isKindOfClass:[NSString class]]) {
c = CCSQLiteCollection;
} else {
c = collection;
}

if (!key || ![key isKindOfClass:[NSString class]]) {
NSLog(@"key is illegal");
return object;
}

self.objectDeserializer = [CCSQLite defaultDeserializer];

switch (_valueType) {
case CCKeyValueTypePropertyList:
self.objectDeserializer = [CCSQLite propertyListDeserializer];
Expand All @@ -124,30 +125,30 @@ - (id) objectForKey : (NSString *) key inCollection: (NSString *) collection {
break;
}

CCSQLiteQueue *innerQueue = [CCSQLiteQueue databaseQueueWithPath: _path];
NSString *sql = [NSString stringWithFormat:@"select * from '%@' where key = '%@' and collection = '%@';", CCSQLiteDatabase2, key, c];
__block NSData *data = nil;
[innerQueue inDatabase:^(CCSQLite *db) {
CCResultSet *r = [db executeQuery:sql];
while ([r next]) {
data = [r dataForColumn:CCKeyValueDataKey];
object = self.objectDeserializer(c, key, data);
break;
}
[r close];
}];
CCSQLiteQueue * innerQueue = [CCSQLiteQueue databaseQueueWithPath:_path];

NSString * sql = [NSString stringWithFormat:@"select * from '%@' where key = '%@' and collection = '%@';", CCSQLiteDatabase2, key, c];

__block NSData * data = nil;
[innerQueue inDatabase:^(CCSQLite * db) {
CCResultSet * r = [db executeQuery:sql];
while ([r next]) {
data = [r dataForColumn:CCKeyValueDataKey];
object = self.objectDeserializer(c, key, data);
break;
}

[r close];
}];

return object;
}
} /* objectForKey */

- (void) setObject: (id) object key : (NSString *) key {
- (void) setObject:(id)object key:(NSString *)key {
[self setObject:object key:key inCollection:nil metadata:nil];
}

- (id) objectForKey : (NSString *) key {
- (id) objectForKey:(NSString *)key {
return [self objectForKey:key inCollection:nil];
}

Expand Down
14 changes: 6 additions & 8 deletions CCSQLite/CCOptions.m
Expand Up @@ -37,10 +37,8 @@ @implementation CCOptions
@synthesize aggressiveWALTruncationSize = aggressiveWALTruncationSize;
@synthesize enableMultiProcessSupport = enableMultiProcessSupport;

- (id)init
{
if ((self = [super init]))
{
- (id) init {
if ((self = [super init])) {
corruptAction = CCOptionsCorruptAction_Rename;
pragmaSynchronous = CCOptionsPragmaSynchronous_Full;
pragmaJournalSizeLimit = 0;
Expand All @@ -52,9 +50,9 @@ - (id)init
return self;
}

- (id)copyWithZone:(NSZone __unused *)zone
{
CCOptions *copy = [[[self class] alloc] init];
- (id) copyWithZone:(NSZone __unused *)zone {
CCOptions * copy = [[[self class] alloc] init];

copy->corruptAction = corruptAction;
copy->pragmaSynchronous = pragmaSynchronous;
copy->pragmaJournalSizeLimit = pragmaJournalSizeLimit;
Expand All @@ -68,7 +66,7 @@ - (id)copyWithZone:(NSZone __unused *)zone
#endif
copy->aggressiveWALTruncationSize = aggressiveWALTruncationSize;
copy->enableMultiProcessSupport = enableMultiProcessSupport;

return copy;
}

Expand Down

0 comments on commit 69400b6

Please sign in to comment.