@@ -182,7 +182,49 @@ -(NSString*)dictionaryToSqlWhere:(NSDictionary*)dic andValues:(NSMutableArray*)v
182
182
}
183
183
return wherekey;
184
184
}
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
+ }
186
228
#pragma mark- dealloc
187
229
-(void )dealloc
188
230
{
@@ -265,7 +307,7 @@ -(BOOL)createTableWithModelClass:(Class)modelClass
265
307
isTableCreated = YES ;
266
308
}
267
309
[set close ];
268
- }];
310
+ }];
269
311
if (isTableCreated)
270
312
{
271
313
// 已创建表 就跳过
@@ -274,7 +316,8 @@ -(BOOL)createTableWithModelClass:(Class)modelClass
274
316
}
275
317
276
318
LKModelInfos* infos = [modelClass getModelInfos ];
277
- NSString * primaryKey = [modelClass getPrimaryKey ];
319
+ NSArray * primaryKeys = infos.primaryKeys ;
320
+
278
321
NSMutableString * table_pars = [NSMutableString string ];
279
322
for (int i=0 ; i<infos.count ; i++) {
280
323
@@ -309,13 +352,21 @@ -(BOOL)createTableWithModelClass:(Class)modelClass
309
352
{
310
353
[table_pars appendFormat: @" %@ %@ " ,LKSQLDefault,property.defaultValue];
311
354
}
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];
315
366
}
367
+ [pksb appendString: @" )" ];
316
368
}
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];
319
370
320
371
BOOL isCreated = [self executeSQL: createTableSQL arguments: nil ];
321
372
@@ -424,9 +475,8 @@ -(void)sqlString:(NSMutableString*)sql AddOder:(NSString*)orderby offset:(int)of
424
475
{
425
476
[sql appendFormat: @" order by %@ " ,orderby];
426
477
}
427
-
428
- // Add limit & offset only when count > 0
429
- if (count > 0 ) {
478
+ if (count>0 )
479
+ {
430
480
[sql appendFormat: @" limit %d offset %d " ,count,offset];
431
481
}
432
482
}
@@ -530,7 +580,7 @@ -(BOOL)insertBase:(NSObject*)model{
530
580
531
581
// 拼接insertSQL 语句 采用 replace 插入
532
582
NSString * insertSQL = [NSString stringWithFormat: @" replace into %@ (%@ ) values(%@ )" ,[modelClass getTableName ],insertKey,insertValuesString];
533
-
583
+
534
584
__block BOOL execute = NO ;
535
585
__block int lastInsertRowId = 0 ;
536
586
@@ -586,7 +636,7 @@ -(BOOL)updateToDBBase:(NSObject *)model where:(id)where
586
636
[updateValues addObject: value];
587
637
}
588
638
589
- NSMutableString * updateSQL = [NSMutableString stringWithFormat: @" update %@ set %@ where" ,[modelClass getTableName ],updateKey];
639
+ NSMutableString * updateSQL = [NSMutableString stringWithFormat: @" update %@ set %@ where " ,[modelClass getTableName ],updateKey];
590
640
591
641
// 添加where 语句
592
642
if ([where isKindOfClass: [NSString class ]] && [LKDBUtils checkStringIsEmpty: where]== NO )
@@ -608,19 +658,13 @@ -(BOOL)updateToDBBase:(NSObject *)model where:(id)where
608
658
else
609
659
{
610
660
// 如果不通过 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 )
613
663
{
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 ;
623
666
}
667
+ [updateSQL appendString: pwhere];
624
668
}
625
669
626
670
BOOL execute = [self executeSQL: updateSQL arguments: updateValues];
@@ -671,31 +715,26 @@ -(BOOL)deleteToDBBase:(NSObject *)model
671
715
[modelClass dbWillDelete: model];
672
716
673
717
NSMutableString * deleteSQL =[NSMutableString stringWithFormat: @" delete from %@ where " ,[modelClass getTableName ]];
674
- id primaryValue = nil ;
718
+ NSMutableArray * parsArray = [ NSMutableArray array ] ;
675
719
if (model.rowid > 0 )
676
720
{
677
- [deleteSQL appendFormat: @" rowid = %d " ,model.rowid];
721
+ [deleteSQL appendFormat: @" rowid = %d " ,model.rowid];
678
722
}
679
723
else
680
724
{
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 )
688
727
{
689
728
LKLog (@" delete fail : %@ primary value is nil" ,NSStringFromClass (modelClass));
690
729
return NO ;
691
730
}
731
+ [deleteSQL appendString: pwhere];
692
732
}
693
733
694
- NSArray * array = nil ;
695
- if (primaryValue)
696
- array = [NSArray arrayWithObject: primaryValue];
734
+ if (parsArray.count ==0 )
735
+ parsArray = nil ;
697
736
698
- BOOL execute = [self executeSQL: deleteSQL arguments: array ];
737
+ BOOL execute = [self executeSQL: deleteSQL arguments: parsArray ];
699
738
700
739
// callback
701
740
[modelClass dbDidIDeleted: model result: execute];
@@ -726,6 +765,7 @@ -(BOOL)deleteWithClassBase:(Class)modelClass where:(id)where
726
765
BOOL result = [self executeSQL: deleteSQL arguments: values];
727
766
return result;
728
767
}
768
+
729
769
#pragma mark - other operation
730
770
-(BOOL )isExistsModel : (NSObject *)model
731
771
{
@@ -734,19 +774,13 @@ -(BOOL)isExistsModel:(NSObject *)model
734
774
return YES ;
735
775
else
736
776
{
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 )
743
779
{
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 ;
746
782
}
747
-
748
- LKLog (@" exists model fail: primary key is nil or invalid" );
749
- return NO ;
783
+ return [self isExistsClass: model.class where: pwhere];
750
784
}
751
785
}
752
786
-(BOOL )isExistsClass : (Class )modelClass where : (id )where
0 commit comments