@@ -472,10 +472,7 @@ -(id)db_jsonObjectFromModel:(NSObject*)model
472
472
NSDictionary * jsonObject = nil ;
473
473
if (model.rowid > 0 )
474
474
{
475
- jsonObject = @{LKDB_TypeKey:LKDB_TypeKey_Model,
476
- LKDB_TableNameKey:model.db_tableName ,
477
- LKDB_ClassKey:NSStringFromClass (clazz),
478
- LKDB_RowIdKey:@(model.rowid )};
475
+ jsonObject = [self db_readInfoWithModel: model class: clazz];
479
476
}
480
477
else
481
478
{
@@ -487,15 +484,28 @@ -(id)db_jsonObjectFromModel:(NSObject*)model
487
484
BOOL success = [model saveToDB ];
488
485
if (success)
489
486
{
490
- jsonObject = @{LKDB_TypeKey:LKDB_TypeKey_Model,
491
- LKDB_TableNameKey:model.db_tableName ,
492
- LKDB_ClassKey:NSStringFromClass (clazz),
493
- LKDB_RowIdKey:@(model.rowid )};
487
+ jsonObject = [self db_readInfoWithModel: model class: clazz];
494
488
}
495
489
}
496
490
}
497
491
return jsonObject;
498
492
}
493
+ -(NSDictionary *)db_readInfoWithModel : (NSObject *)model class : (Class )clazz
494
+ {
495
+ NSMutableDictionary * jsonObject = [NSMutableDictionary dictionary ];
496
+ [jsonObject setObject: LKDB_TypeKey_Model forKey: LKDB_TypeKey];
497
+ [jsonObject setObject: model.db_tableName forKey: LKDB_TableNameKey];
498
+ [jsonObject setObject: NSStringFromClass (clazz) forKey: LKDB_ClassKey];
499
+ [jsonObject setObject: @(model.rowid) forKey: LKDB_RowIdKey];
500
+
501
+ NSDictionary * dic = [model db_getPrimaryKeysValues ];
502
+ if (dic.count > 0 && [NSJSONSerialization isValidJSONObject: dic])
503
+ {
504
+ [jsonObject setObject: dic forKey: LKDB_PValueKey];
505
+ }
506
+ return jsonObject;
507
+ }
508
+
499
509
-(NSString *)db_jsonStringFromObject : (NSObject *)jsonObject
500
510
{
501
511
if (jsonObject && [NSJSONSerialization isValidJSONObject: jsonObject])
@@ -572,12 +582,50 @@ -(id)db_objectWithDictionary:(NSDictionary*)dic
572
582
int rowid = [[dic objectForKey: LKDB_RowIdKey] intValue ];
573
583
NSString * tableName = [dic objectForKey: LKDB_TableNameKey];
574
584
575
- NSArray * array = [[clazz getUsingLKDBHelper ] searchWithSQL: [NSString stringWithFormat: @" select rowid,* from %@ where rowid=%d limit 1" ,tableName,rowid] toClass: clazz];
576
- if (array.count > 0 )
585
+ NSString * where = nil ;
586
+
587
+ NSString * rowCountWhere = [NSString stringWithFormat: @" select count(rowid) from %@ where rowid=%d limit 1" ,tableName,rowid];
588
+ int result = [[[clazz getUsingLKDBHelper ] executeScalarWithSQL: rowCountWhere arguments: nil ] intValue ];
589
+ if (result > 0 )
590
+ {
591
+ where = [NSString stringWithFormat: @" select rowid,* from %@ where rowid=%d limit 1" ,tableName,rowid];
592
+ }
593
+ else
594
+ {
595
+ NSDictionary * pv = [dic objectForKey: LKDB_PValueKey];
596
+ if (pv.count > 0 )
597
+ {
598
+ BOOL isNeedAddDot = NO ;
599
+ NSMutableString * sb = [NSMutableString stringWithFormat: @" select rowid,* from %@ where" ,tableName];
600
+
601
+ NSArray * allKeys = pv.allKeys ;
602
+ for (NSString * key in allKeys)
603
+ {
604
+ id obj = [pv objectForKey: key];
605
+ if (isNeedAddDot)
606
+ {
607
+ [sb appendString: @" and" ];
608
+ }
609
+ [sb appendFormat: @" %@ = '%@ '" ,key,obj];
610
+
611
+ isNeedAddDot = YES ;
612
+ }
613
+
614
+ [sb appendString: @" limit 1" ];
615
+
616
+ where = [NSString stringWithString: sb];
617
+ }
618
+ }
619
+
620
+ if (where)
577
621
{
578
- NSObject * result = [array objectAtIndex: 0 ];
579
- result.db_tableName = tableName;
580
- return result;
622
+ NSArray * array = [[clazz getUsingLKDBHelper ] searchWithSQL: where toClass: clazz];
623
+ if (array.count > 0 )
624
+ {
625
+ NSObject * result = [array objectAtIndex: 0 ];
626
+ result.db_tableName = tableName;
627
+ return result;
628
+ }
581
629
}
582
630
}
583
631
else if ([type isEqualToString: LKDB_TypeKey_JSON])
@@ -649,7 +697,30 @@ -(id)userGetValueForModel:(LKDBProperty *)property
649
697
return nil ;
650
698
}
651
699
652
-
700
+ -(NSDictionary *)db_getPrimaryKeysValues
701
+ {
702
+ LKModelInfos* infos = [self .class getModelInfos ];
703
+ NSArray * array = infos.primaryKeys ;
704
+ NSMutableDictionary * dic = [NSMutableDictionary dictionary ];
705
+ for (NSString * pname in array)
706
+ {
707
+ LKDBProperty* property = [infos objectWithSqlColumnName: pname];
708
+ id value = nil ;
709
+ if ([property.type isEqualToString: LKSQL_Mapping_UserCalculate])
710
+ {
711
+ value = [self userGetValueForModel: property];
712
+ }
713
+ else
714
+ {
715
+ value = [self modelGetValue: property];
716
+ }
717
+ if (value)
718
+ {
719
+ [dic setObject: value forKey: property.sqlColumnName];
720
+ }
721
+ }
722
+ return dic;
723
+ }
653
724
// 主键值 是否为空
654
725
-(BOOL )singlePrimaryKeyValueIsEmpty
655
726
{
0 commit comments