Skip to content

Commit 0b4f89e

Browse files
committed
Support union primary key 支持联合主键
overwrite -(NSArray*)getPrimaryKeyUnionArray
1 parent 071f5e5 commit 0b4f89e

File tree

8 files changed

+155
-61
lines changed

8 files changed

+155
-61
lines changed

LKDBHelper/Helper/LKDB+Mapping.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ extern inline NSString* LKSQLTypeFromObjcType(NSString *objcType);
7676

7777
@interface LKModelInfos : NSObject
7878

79-
-(id)initWithKeyMapping:(NSDictionary*)keyMapping propertyNames:(NSArray*)propertyNames propertyType:(NSArray*)propertyType;
79+
-(id)initWithKeyMapping:(NSDictionary*)keyMapping propertyNames:(NSArray*)propertyNames propertyType:(NSArray*)propertyType primaryKeys:(NSArray*)primaryKeys;
8080

8181
@property(readonly,nonatomic)int count;
82+
@property(readonly,nonatomic)NSArray* primaryKeys;
8283

8384
-(LKDBProperty*)objectWithIndex:(int)index;
8485
-(LKDBProperty*)objectWithPropertyName:(NSString*)propertyName;

LKDBHelper/Helper/LKDB+Mapping.m

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ @interface LKModelInfos()
4040
{
4141
__strong NSMutableDictionary* _proNameDic;
4242
__strong NSMutableDictionary* _sqlNameDic;
43+
__strong NSMutableArray* _primaryKeys;
4344
}
4445
-(void)removeWithColumeName:(NSString*)columeName;
4546
@end
@@ -100,11 +101,13 @@ +(void)removePropertyWithColumeName:(NSString *)columename
100101
#pragma mark- LKModelInfos
101102

102103
@implementation LKModelInfos
103-
- (id)initWithKeyMapping:(NSDictionary *)keyMapping propertyNames:(NSArray *)propertyNames propertyType:(NSArray *)propertyType
104+
- (id)initWithKeyMapping:(NSDictionary *)keyMapping propertyNames:(NSArray *)propertyNames propertyType:(NSArray *)propertyType primaryKeys:(NSArray *)primaryKeys
104105
{
105106
self = [super init];
106107
if (self) {
107108

109+
_primaryKeys = [primaryKeys copy];
110+
108111
_proNameDic = [[NSMutableDictionary alloc]init];
109112
_sqlNameDic = [[NSMutableDictionary alloc]init];
110113

@@ -187,6 +190,10 @@ -(void)addDBPropertyWithType:(NSString *)type cname:(NSString *)colume_name ctyp
187190
[_sqlNameDic setObject:db_property forKey:db_property.sqlColumeName];
188191
}
189192
}
193+
-(NSArray *)primaryKeys
194+
{
195+
return _primaryKeys;
196+
}
190197
-(int)count
191198
{
192199
return _sqlNameDic.count;

LKDBHelper/Helper/LKDBHelper.m

Lines changed: 82 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,49 @@ -(NSString*)dictionaryToSqlWhere:(NSDictionary*)dic andValues:(NSMutableArray*)v
182182
}
183183
return wherekey;
184184
}
185-
185+
//where sql statements about model primary keys
186+
-(NSMutableString*)primaryKeyWhereSQLWithModel:(NSObject*)model addPValues:(NSMutableArray*)addPValues
187+
{
188+
LKModelInfos* infos = [model.class getModelInfos];
189+
NSArray* primaryKeys = infos.primaryKeys;
190+
NSMutableString* pwhere = [NSMutableString string];
191+
if(primaryKeys.count>0)
192+
{
193+
for (int i=0; i<primaryKeys.count; i++) {
194+
NSString* pk = [primaryKeys objectAtIndex:i];
195+
if([LKDBUtils checkStringIsEmpty:pk] == NO)
196+
{
197+
LKDBProperty* property = [infos objectWithSqlColumeName:pk];
198+
id pvalue = nil;
199+
if(property && [property.type isEqualToString:LKSQLUserCalculate])
200+
{
201+
pvalue = [model userGetValueForModel:property];
202+
}
203+
else if(pk && property)
204+
{
205+
pvalue = [model modelGetValue:property];
206+
}
207+
208+
if(pvalue)
209+
{
210+
if(pwhere.length>0)
211+
[pwhere appendString:@"and"];
212+
213+
if(addPValues)
214+
{
215+
[pwhere appendFormat:@" %@=? ",pk];
216+
[addPValues addObject:pvalue];
217+
}
218+
else
219+
{
220+
[pwhere appendFormat:@" %@='%@' ",pk,pvalue];
221+
}
222+
}
223+
}
224+
}
225+
}
226+
return pwhere;
227+
}
186228
#pragma mark- dealloc
187229
-(void)dealloc
188230
{
@@ -265,7 +307,7 @@ -(BOOL)createTableWithModelClass:(Class)modelClass
265307
isTableCreated = YES;
266308
}
267309
[set close];
268-
}];
310+
}];
269311
if(isTableCreated)
270312
{
271313
//已创建表 就跳过
@@ -274,7 +316,8 @@ -(BOOL)createTableWithModelClass:(Class)modelClass
274316
}
275317

276318
LKModelInfos* infos = [modelClass getModelInfos];
277-
NSString* primaryKey = [modelClass getPrimaryKey];
319+
NSArray* primaryKeys = infos.primaryKeys;
320+
278321
NSMutableString* table_pars = [NSMutableString string];
279322
for (int i=0; i<infos.count; i++) {
280323

@@ -309,13 +352,21 @@ -(BOOL)createTableWithModelClass:(Class)modelClass
309352
{
310353
[table_pars appendFormat:@" %@ %@",LKSQLDefault,property.defaultValue];
311354
}
312-
if(primaryKey && [property.sqlColumeName isEqualToString:primaryKey])
313-
{
314-
[table_pars appendFormat:@" %@",LKSQLPrimaryKey];
355+
}
356+
NSMutableString* pksb = [NSMutableString string];
357+
if(primaryKeys.count>0)
358+
{
359+
pksb = [NSMutableString stringWithString:@",primary key("];
360+
for (int i=0; i<primaryKeys.count; i++) {
361+
NSString* pk = [primaryKeys objectAtIndex:i];
362+
if(i>0)
363+
[pksb appendString:@","];
364+
365+
[pksb appendString:pk];
315366
}
367+
[pksb appendString:@")"];
316368
}
317-
NSString* createTableSQL = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(%@)",tableName,table_pars];
318-
369+
NSString* createTableSQL = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(%@%@)",tableName,table_pars,pksb];
319370

320371
BOOL isCreated = [self executeSQL:createTableSQL arguments:nil];
321372

@@ -424,9 +475,8 @@ -(void)sqlString:(NSMutableString*)sql AddOder:(NSString*)orderby offset:(int)of
424475
{
425476
[sql appendFormat:@" order by %@",orderby];
426477
}
427-
428-
// Add limit & offset only when count > 0
429-
if (count > 0) {
478+
if(count>0)
479+
{
430480
[sql appendFormat:@" limit %d offset %d",count,offset];
431481
}
432482
}
@@ -530,7 +580,7 @@ -(BOOL)insertBase:(NSObject*)model{
530580

531581
//拼接insertSQL 语句 采用 replace 插入
532582
NSString* insertSQL = [NSString stringWithFormat:@"replace into %@(%@) values(%@)",[modelClass getTableName],insertKey,insertValuesString];
533-
583+
534584
__block BOOL execute = NO;
535585
__block int lastInsertRowId = 0;
536586

@@ -586,7 +636,7 @@ -(BOOL)updateToDBBase:(NSObject *)model where:(id)where
586636
[updateValues addObject:value];
587637
}
588638

589-
NSMutableString* updateSQL = [NSMutableString stringWithFormat:@"update %@ set %@ where",[modelClass getTableName],updateKey];
639+
NSMutableString* updateSQL = [NSMutableString stringWithFormat:@"update %@ set %@ where ",[modelClass getTableName],updateKey];
590640

591641
//添加where 语句
592642
if([where isKindOfClass:[NSString class]] && [LKDBUtils checkStringIsEmpty:where]== NO)
@@ -608,19 +658,13 @@ -(BOOL)updateToDBBase:(NSObject *)model where:(id)where
608658
else
609659
{
610660
//如果不通过 rowid 来 更新数据 那 primarykey 一定要有值
611-
NSString* primaryKey = [modelClass getPrimaryKey];
612-
if([LKDBUtils checkStringIsEmpty:primaryKey] == NO)
661+
NSString* pwhere = [self primaryKeyWhereSQLWithModel:model addPValues:updateValues];
662+
if(pwhere.length ==0)
613663
{
614-
LKDBProperty* property = [infos objectWithSqlColumeName:primaryKey];
615-
if(property)
616-
{
617-
[updateSQL appendFormat:@" %@=?",property.sqlColumeName];
618-
619-
id value = [self modelValueWithProperty:property model:model];
620-
621-
[updateValues addObject:value];
622-
}
664+
LKLog(@"database update fail : %@ no find primary key!",NSStringFromClass(modelClass));
665+
return NO;
623666
}
667+
[updateSQL appendString:pwhere];
624668
}
625669

626670
BOOL execute = [self executeSQL:updateSQL arguments:updateValues];
@@ -671,31 +715,26 @@ -(BOOL)deleteToDBBase:(NSObject *)model
671715
[modelClass dbWillDelete:model];
672716

673717
NSMutableString* deleteSQL =[NSMutableString stringWithFormat:@"delete from %@ where ",[modelClass getTableName]];
674-
id primaryValue = nil;
718+
NSMutableArray* parsArray = [NSMutableArray array];
675719
if(model.rowid > 0)
676720
{
677-
[deleteSQL appendFormat:@" rowid = %d",model.rowid];
721+
[deleteSQL appendFormat:@"rowid = %d",model.rowid];
678722
}
679723
else
680724
{
681-
primaryValue = [model getPrimaryValue];
682-
if(primaryValue)
683-
{
684-
NSString* primarykey = [modelClass getPrimaryKey];
685-
[deleteSQL appendFormat:@" %@=? ",primarykey];
686-
}
687-
else
725+
NSString* pwhere = [self primaryKeyWhereSQLWithModel:model addPValues:parsArray];
726+
if(pwhere.length==0)
688727
{
689728
LKLog(@"delete fail : %@ primary value is nil",NSStringFromClass(modelClass));
690729
return NO;
691730
}
731+
[deleteSQL appendString:pwhere];
692732
}
693733

694-
NSArray* array = nil;
695-
if(primaryValue)
696-
array = [NSArray arrayWithObject:primaryValue];
734+
if(parsArray.count==0)
735+
parsArray = nil;
697736

698-
BOOL execute = [self executeSQL:deleteSQL arguments:array];
737+
BOOL execute = [self executeSQL:deleteSQL arguments:parsArray];
699738

700739
//callback
701740
[modelClass dbDidIDeleted:model result:execute];
@@ -726,6 +765,7 @@ -(BOOL)deleteWithClassBase:(Class)modelClass where:(id)where
726765
BOOL result = [self executeSQL:deleteSQL arguments:values];
727766
return result;
728767
}
768+
729769
#pragma mark - other operation
730770
-(BOOL)isExistsModel:(NSObject *)model
731771
{
@@ -734,19 +774,13 @@ -(BOOL)isExistsModel:(NSObject *)model
734774
return YES;
735775
else
736776
{
737-
Class modelClass = model.class;
738-
739-
NSString* primarykey = [modelClass getPrimaryKey];
740-
id primaryValue = [model getPrimaryValue];
741-
742-
if(primarykey&&primaryValue)
777+
NSMutableString* pwhere = [self primaryKeyWhereSQLWithModel:model addPValues:nil];
778+
if(pwhere.length == 0)
743779
{
744-
NSString* where = [NSString stringWithFormat:@"%@ = '%@'",primarykey,primaryValue];
745-
return [self isExistsClass:modelClass where:where];
780+
LKLog(@"exists model fail: primary key is nil or invalid");
781+
return NO;
746782
}
747-
748-
LKLog(@"exists model fail: primary key is nil or invalid");
749-
return NO;
783+
return [self isExistsClass:model.class where:pwhere];
750784
}
751785
}
752786
-(BOOL)isExistsClass:(Class)modelClass where:(id)where

LKDBHelper/Helper/LKDBUtils.m

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@
88

99
#import "LKDBUtils.h"
1010

11+
@interface LKDateFormatter : NSDateFormatter
12+
@property(strong,nonatomic)NSLock* lock;
13+
@end
14+
15+
@implementation LKDateFormatter
16+
//防止在IOS5下 多线程 格式化时间时 崩溃
17+
-(NSDate *)dateFromString:(NSString *)string
18+
{
19+
[_lock lock];
20+
NSDate* date = [super dateFromString:string];
21+
[_lock unlock];
22+
return date;
23+
}
24+
-(NSString *)stringFromDate:(NSDate *)date
25+
{
26+
[_lock lock];
27+
NSString* string = [super stringFromDate:date];
28+
[_lock unlock];
29+
return string;
30+
}
31+
@end
32+
1133
@implementation LKDBUtils
1234
+(NSString *)getDocumentPath
1335
{
@@ -40,7 +62,7 @@ +(BOOL)isFileExists:(NSString *)filepath
4062
}
4163
+(BOOL)deleteWithFilepath:(NSString *)filepath
4264
{
43-
return [[NSFileManager defaultManager] removeItemAtPath:filepath error:nil];
65+
return [[NSFileManager defaultManager] removeItemAtPath:filepath error:nil];
4466
}
4567
+(NSArray*)getFilenamesWithDir:(NSString*)dir
4668
{
@@ -49,7 +71,7 @@ +(NSArray*)getFilenamesWithDir:(NSString*)dir
4971
return fileList;
5072
}
5173
+(BOOL)checkStringIsEmpty:(NSString *)string
52-
{
74+
{
5375
if(string == nil)
5476
{
5577
return YES;
@@ -67,7 +89,7 @@ +(NSDateFormatter*)getDBDateFormat
6789
static NSDateFormatter* format;
6890
static dispatch_once_t onceToken;
6991
dispatch_once(&onceToken, ^{
70-
format = [[NSDateFormatter alloc]init];
92+
format = [[LKDateFormatter alloc]init];
7193
format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
7294
});
7395
return format;

LKDBHelper/Helper/NSObject+LKModel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
主键列名 如果rowid<0 则跟据此名称update 和delete
3030
*/
3131
+(NSString*)getPrimaryKey;
32+
//return multi primary key 返回联合主键
33+
+(NSArray*) getPrimaryKeyUnionArray;
3234

3335
@property int rowid;
3436

0 commit comments

Comments
 (0)