Skip to content

Commit f2ad311

Browse files
committed
Fix setting nullable enums
1 parent 6a53501 commit f2ad311

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

src/SQLite.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,7 @@ protected virtual void Dispose(bool disposing)
16611661
}
16621662
}
16631663

1664-
var r = SQLite3.Close(Handle);
1664+
var r = SQLite3.Close2(Handle);
16651665
if (r != SQLite3.Result.OK)
16661666
{
16671667
string msg = SQLite3.GetErrmsg(Handle);
@@ -2071,11 +2071,7 @@ public Column(PropertyInfo prop, CreateFlags createFlags = CreateFlags.None)
20712071

20722072
public void SetValue(object obj, object val)
20732073
{
2074-
#if !USE_NEW_REFLECTION_API
2075-
if (ColumnType.IsEnum)
2076-
#else
2077-
if (ColumnType.GetTypeInfo().IsEnum)
2078-
#endif
2074+
if (val != null && ColumnType.GetTypeInfo().IsEnum)
20792075
{
20802076
_prop.SetValue(obj, Enum.ToObject(ColumnType, val));
20812077
}

tests/NullableTest.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,5 +243,73 @@ public void StringWhereNotNull()
243243
Assert.AreEqual(withEmpty, results[0]);
244244
Assert.AreEqual(withData, results[1]);
245245
}
246+
247+
public enum TestIntEnum
248+
{
249+
One = 1,
250+
Two = 2,
251+
}
252+
253+
[StoreAsText]
254+
public enum TestTextEnum
255+
{
256+
Alpha,
257+
Beta,
258+
}
259+
260+
public class NullableEnumClass
261+
{
262+
[PrimaryKey, AutoIncrement]
263+
public int ID { get; set; }
264+
265+
public TestIntEnum? NullableIntEnum { get; set; }
266+
public TestTextEnum? NullableTextEnum { get; set; }
267+
268+
public override bool Equals (object obj)
269+
{
270+
var other = (NullableEnumClass)obj;
271+
return this.ID == other.ID && this.NullableIntEnum == other.NullableIntEnum && this.NullableTextEnum == other.NullableTextEnum;
272+
}
273+
274+
public override int GetHashCode ()
275+
{
276+
return ID.GetHashCode () + NullableIntEnum.GetHashCode () + NullableTextEnum.GetHashCode ();
277+
}
278+
279+
public override string ToString ()
280+
{
281+
return string.Format ("[NullableEnumClass: ID={0}, NullableIntEnum={1}, NullableTextEnum={2}]", ID, NullableIntEnum, NullableTextEnum);
282+
}
283+
}
284+
285+
[Test]
286+
[Description ("Create a table with a nullable enum column then insert and select against it")]
287+
public void NullableEnum ()
288+
{
289+
SQLiteConnection db = new SQLiteConnection (TestPath.GetTempFileName ());
290+
db.CreateTable<NullableEnumClass> ();
291+
292+
var withNull = new NullableEnumClass { NullableIntEnum = null, NullableTextEnum = null };
293+
var with1 = new NullableEnumClass { NullableIntEnum = TestIntEnum.One, NullableTextEnum = null };
294+
var with2 = new NullableEnumClass { NullableIntEnum = TestIntEnum.Two, NullableTextEnum = null };
295+
var withNullA = new NullableEnumClass { NullableIntEnum = null, NullableTextEnum = TestTextEnum.Alpha };
296+
var with1B = new NullableEnumClass { NullableIntEnum = TestIntEnum.One, NullableTextEnum = TestTextEnum.Beta };
297+
298+
db.Insert (withNull);
299+
db.Insert (with1);
300+
db.Insert (with2);
301+
db.Insert (withNullA);
302+
db.Insert (with1B);
303+
304+
var results = db.Table<NullableEnumClass> ().OrderBy (x => x.ID).ToArray ();
305+
306+
Assert.AreEqual (5, results.Length);
307+
308+
Assert.AreEqual (withNull, results[0]);
309+
Assert.AreEqual (with1, results[1]);
310+
Assert.AreEqual (with2, results[2]);
311+
Assert.AreEqual (withNullA, results[3]);
312+
Assert.AreEqual (with1B, results[4]);
313+
}
246314
}
247315
}

0 commit comments

Comments
 (0)