Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/SQLite.Net/SQLiteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ public T ExecuteScalar<T>()
if (r == Result.Row)
{
ColType colType = _sqlitePlatform.SQLiteApi.ColumnType(stmt, 0);
val = (T)ReadCol(stmt, 0, colType, typeof(T));
var clrType = Nullable.GetUnderlyingType(typeof (T)) ?? typeof (T);
val = (T)ReadCol(stmt, 0, colType, clrType);
}
else if (r == Result.Done)
{
Expand Down
39 changes: 39 additions & 0 deletions tests/NullableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,45 @@ public override int GetHashCode()
}
}

[Test]
public void NullableScalarInt()
{
var db = new SQLiteConnection(new SQLitePlatformTest(), TestPath.GetTempFileName());
db.CreateTable<NullableIntClass>();

var withNull = new NullableIntClass
{
NullableInt = null
};
var with0 = new NullableIntClass
{
NullableInt = 0
};
var with1 = new NullableIntClass
{
NullableInt = 1
};
var withMinus1 = new NullableIntClass
{
NullableInt = -1
};

db.Insert(withNull);
db.Insert(with0);
db.Insert(with1);
db.Insert(withMinus1);

var actualShouldBeNull = db.ExecuteScalar<int?>("select NullableInt from NullableIntClass order by ID limit 1");
var actualShouldBe0 = db.ExecuteScalar<int?>("select NullableInt from NullableIntClass order by ID limit 1 offset 1");
var actualShouldBe1 = db.ExecuteScalar<int?>("select NullableInt from NullableIntClass order by ID limit 1 offset 2");
var actualShouldBeMinus1 = db.ExecuteScalar<int?>("select NullableInt from NullableIntClass order by ID limit 1 offset 3");

Assert.AreEqual(null, actualShouldBeNull);
Assert.AreEqual(0, actualShouldBe0);
Assert.AreEqual(1, actualShouldBe1);
Assert.AreEqual(-1, actualShouldBeMinus1);
}

[Test]
[Description("Create a table with a nullable int column then insert and select against it")]
public void NullableFloat()
Expand Down