Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit e857320

Browse files
committed
Add support for OrmLiteConfig.OnDbNullFilter
1 parent 3e2f296 commit e857320

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

src/ServiceStack.OrmLite/FieldDefinition.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public string FieldName
2727

2828
public Type FieldType { get; set; }
2929

30+
public object FieldTypeDefaultValue { get; set; }
31+
3032
public Type TreatAsType { get; set; }
3133

3234
public Type ColumnType

src/ServiceStack.OrmLite/OrmLiteConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ public static IOrmLiteExecFilter ExecFilter
184184

185185
public static Func<string, string> StringFilter { get; set; }
186186

187+
public static Func<FieldDefinition, object> OnDbNullFilter { get; set; }
188+
187189
public static Func<string, string> SanitizeFieldNameForParamNameFn = fieldName =>
188190
(fieldName ?? "").Replace(" ", "");
189191
}

src/ServiceStack.OrmLite/OrmLiteConfigExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ internal static ModelDefinition GetModelDefinition(this Type modelType)
144144
Name = propertyInfo.Name,
145145
Alias = aliasAttr != null ? aliasAttr.Name : null,
146146
FieldType = propertyType,
147+
FieldTypeDefaultValue = propertyType.GetDefaultValue(),
147148
TreatAsType = treatAsType,
148149
PropertyInfo = propertyInfo,
149150
IsNullable = isNullable,

src/ServiceStack.OrmLite/OrmLiteUtils.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,15 @@ public static bool HandledDbNullValue(FieldDefinition fieldDef, IDataReader data
374374
if (fieldDef == null || fieldDef.SetValueFn == null || colIndex == NotFound) return true;
375375
if (dataReader.IsDBNull(colIndex))
376376
{
377-
if (fieldDef.IsNullable)
377+
var value = fieldDef.IsNullable ? null : fieldDef.FieldTypeDefaultValue;
378+
if (OrmLiteConfig.OnDbNullFilter != null)
378379
{
379-
fieldDef.SetValueFn(instance, null);
380-
}
381-
else
382-
{
383-
fieldDef.SetValueFn(instance, fieldDef.FieldType.GetDefaultValue());
380+
var useValue = OrmLiteConfig.OnDbNullFilter(fieldDef);
381+
if (useValue != null)
382+
value = useValue;
384383
}
384+
385+
fieldDef.SetValueFn(instance, value);
385386
return true;
386387
}
387388
return false;

tests/ServiceStack.OrmLite.Tests/OrmLiteExecFilterTests.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void Can_mock_store_procedure()
8484
using (var db = OpenDbConnection())
8585
{
8686
var person = db.SqlScalar<Person>("exec sp_name @firstName, @age",
87-
new {firstName = "aName", age = 1});
87+
new { firstName = "aName", age = 1 });
8888

8989
Assert.That(person.FirstName, Is.EqualTo("Mocked"));
9090
}
@@ -109,5 +109,26 @@ public void Does_use_StringFilter()
109109

110110
OrmLiteConfig.StringFilter = null;
111111
}
112+
113+
[Test]
114+
public void Does_use_StringFilter_on_null_strings()
115+
{
116+
OrmLiteConfig.OnDbNullFilter = fieldDef =>
117+
fieldDef.FieldType == typeof(string)
118+
? "NULL"
119+
: null;
120+
121+
using (var db = OpenDbConnection())
122+
{
123+
db.DropAndCreateTable<Poco>();
124+
125+
db.Insert(new Poco { Name = null });
126+
var row = db.Select<Poco>().First();
127+
128+
Assert.That(row.Name, Is.EqualTo("NULL"));
129+
}
130+
131+
OrmLiteConfig.OnDbNullFilter = null;
132+
}
112133
}
113134
}

0 commit comments

Comments
 (0)