Skip to content

Commit 1087b41

Browse files
author
ljh
committed
外部支持设置 是否把 null 转为 @""
1 parent 7995b26 commit 1087b41

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

LKDBHelper/Helper/LKDBHelper.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020
@interface LKDBHelper : NSObject
2121

2222
/**
23-
* @brief 是否打印数据库出错日志 默认 NO
23+
* @brief Log error message, Default: NO
2424
*/
2525
+ (void)setLogError:(BOOL)logError;
2626

27+
/**
28+
* @brief null is '' , Default: NO
29+
*/
30+
+ (void)setNullToEmpty:(BOOL)empty;
31+
2732
/**
2833
* @brief filepath the use of : "documents/db/" + fileName + ".db"
2934
* refer: FMDatabase.h + (instancetype)databaseWithPath:(NSString*)inPath;

LKDBHelper/Helper/LKDBHelper.m

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ + (void)setLogError:(BOOL)logError
7777
}
7878
#endif
7979
}
80+
static BOOL LKDBNullIsEmptyString = NO;
81+
+ (void)setNullToEmpty:(BOOL)empty
82+
{
83+
LKDBNullIsEmptyString = empty;
84+
}
85+
+ (BOOL)nullIsEmpty
86+
{
87+
return LKDBNullIsEmptyString;
88+
}
8089

8190
+ (NSMutableArray*)dbHelperSingleArray
8291
{
@@ -595,12 +604,18 @@ - (void)fixSqlColumnsWithClass:(Class)clazz tableName:(NSString*)tableName
595604
if (property.defaultValue) {
596605
[addColumePars appendFormat:@" %@ %@", LKSQL_Attribute_Default, property.defaultValue];
597606
}
598-
599-
NSString* alertSQL = [NSString stringWithFormat:@"alter table %@ add column %@", tableName, addColumePars];
600-
NSString* initColumnValue = [NSString stringWithFormat:@"update %@ set %@=%@", tableName, property.sqlColumnName, [property.sqlColumnType isEqualToString:LKSQL_Type_Text] ? @"null" : @"0"];
601-
607+
NSString *alertSQL = [NSString stringWithFormat:@"alter table %@ add column %@", tableName, addColumePars];
608+
NSString *defaultValue = @"0";
609+
if ([property.sqlColumnType isEqualToString:LKSQL_Type_Text]) {
610+
if (LKDBNullIsEmptyString) {
611+
defaultValue = @"";
612+
}
613+
else {
614+
defaultValue = @"null";
615+
}
616+
}
617+
NSString* initColumnValue = [NSString stringWithFormat:@"update %@ set %@=%@", tableName, property.sqlColumnName, defaultValue];
602618
BOOL success = [db executeUpdate:alertSQL];
603-
604619
if (success) {
605620
[db executeUpdate:initColumnValue];
606621
[alterAddColumns addObject:property];
@@ -760,7 +775,12 @@ - (id)modelValueWithProperty:(LKDBProperty*)property model:(NSObject*)model
760775
}
761776

762777
if (value == nil) {
763-
value = [NSNull null];
778+
if (LKDBNullIsEmptyString) {
779+
value = @"";
780+
}
781+
else {
782+
value = [NSNull null];
783+
}
764784
}
765785

766786
return value;

LKDBHelper/Helper/NSObject+LKModel.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
static char LKModelBase_Key_TableName;
2424
static char LKModelBase_Key_Inserting;
2525

26+
@interface LKDBHelper (LKDBHelper_LKModel)
27+
+ (BOOL)nullIsEmpty;
28+
@end
29+
2630
@implementation NSObject (LKModel)
2731

2832
+ (LKDBHelper*)getUsingLKDBHelper
@@ -322,7 +326,9 @@ - (void)modelSetValue:(LKDBProperty *)property value:(NSString *)value
322326
//不继续遍历
323327
}
324328
else if ([columnClass isSubclassOfClass:[NSString class]]) {
325-
modelValue = [value copy];
329+
if (![LKDBHelper nullIsEmpty] || value.length > 0) {
330+
modelValue = [value copy];
331+
}
326332
}
327333
else if ([columnClass isSubclassOfClass:[NSNumber class]]) {
328334
modelValue = [[LKDBUtils numberFormatter] numberFromString:value];

0 commit comments

Comments
 (0)