Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.
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
2 changes: 1 addition & 1 deletion src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,7 @@ protected virtual string FkOptionToString(OnFkOption option)

public virtual string GetQuotedValue(object value, Type fieldType)
{
if (value == null)
if (value == null || value == DBNull.Value)
return "NULL";

var converter = value.GetType().IsEnum
Expand Down
100 changes: 99 additions & 1 deletion tests/ServiceStack.OrmLite.Tests/Shared/Person.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ServiceStack.DataAnnotations;
using System;
using ServiceStack.DataAnnotations;

namespace ServiceStack.OrmLite.Tests.Shared
{
Expand Down Expand Up @@ -105,4 +106,101 @@ public enum Gender
Male
}

public class PersonWithReferenceType
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Person BestFriend { get; set; }

public static PersonWithReferenceType[] TestValues = new[]
{
new PersonWithReferenceType
{
FirstName = "Test",
LastName = "McTest",
Id = 1
},
new PersonWithReferenceType
{
FirstName = "John",
LastName = "Doe",
Id = 2,
BestFriend = new Person(1,"Jane","Doe",33)
}
};

protected bool Equals(PersonWithReferenceType other)
{
return Id == other.Id &&
string.Equals(FirstName, other.FirstName) &&
string.Equals(LastName, other.LastName) &&
((BestFriend == null && other.BestFriend == null) || (BestFriend != null && BestFriend.Equals(other.BestFriend))) ;
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((PersonWithReferenceType)obj);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = Id;
hashCode = (hashCode * 397) ^ (FirstName != null ? FirstName.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (LastName != null ? LastName.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (BestFriend != null ? BestFriend.GetHashCode() : 0);
return hashCode;
}
}
}

public class TestProduct
{
public string Id { get; set; }

public string Name { get; set; }

public DateTime? Modified { get; set; }

public static TestProduct[] TestValues =
{
new TestProduct
{
Id = "1",
Modified = null,
Name = "Testing"
}
};

protected bool Equals(TestProduct other)
{
return Id == other.Id &&
string.Equals(Name, other.Name) &&
Modified.Equals(other.Modified);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((TestProduct)obj);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = (Id != null ? Id.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Name != null ? Name.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Modified != null ? Modified.GetHashCode() : 0);
return hashCode;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,56 @@ public void Can_use_ToInsertStatement_to_generate_inline_SQL()
Assert.That(insertedRow.Equals(row));
}
}

[Test]
public void Correct_DbNull_in_ToInsertStatement()
{
using (var db = OpenDbConnection())
{
db.DropAndCreateTable<PersonWithReferenceType>();
var row = PersonWithReferenceType.TestValues[0];

var sql = db.ToInsertStatement(row);
sql.Print();
db.ExecuteSql(sql);

var insertedRow = db.SingleById<PersonWithReferenceType>(row.Id);
Assert.That(insertedRow.Equals(row));
}
}

[Test]
public void Correct_Ref_in_ToInsertStatement()
{
using (var db = OpenDbConnection())
{
db.DropAndCreateTable<PersonWithReferenceType>();
var row = PersonWithReferenceType.TestValues[1];

var sql = db.ToInsertStatement(row);
sql.Print();
db.ExecuteSql(sql);

var insertedRow = db.SingleById<PersonWithReferenceType>(row.Id);
Assert.That(insertedRow.Equals(row));
}
}

[Test]
public void Correct_nullable_in_ToInsertStatement()
{
using (var db = OpenDbConnection())
{
db.DropAndCreateTable<TestProduct>();
var row = TestProduct.TestValues[0];

var sql = db.ToInsertStatement(row);
sql.Print();
db.ExecuteSql(sql);

var insertedRow = db.SingleById<TestProduct>(row.Id);
Assert.AreEqual(insertedRow, row);
}
}
}
}