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

Commit 758a964

Browse files
authored
To insert statement dbnull check (#652)
* Fix for when table class has reference type (treated as TEXT or JSON column) and is provided null. Previously generated `{}` for null values with if inserted and then retrieved results in different instance values. * Add additional test for ToInsertAndUpdateStatementTests.cs for ref with populated values. * Add additional test for ToInsertAndUpdateStatementTests.cs using `DateTime?` .
1 parent d5f3130 commit 758a964

File tree

3 files changed

+151
-2
lines changed

3 files changed

+151
-2
lines changed

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ protected virtual string FkOptionToString(OnFkOption option)
16141614

16151615
public virtual string GetQuotedValue(object value, Type fieldType)
16161616
{
1617-
if (value == null)
1617+
if (value == null || value == DBNull.Value)
16181618
return "NULL";
16191619

16201620
var converter = value.GetType().IsEnum

tests/ServiceStack.OrmLite.Tests/Shared/Person.cs

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ServiceStack.DataAnnotations;
1+
using System;
2+
using ServiceStack.DataAnnotations;
23

34
namespace ServiceStack.OrmLite.Tests.Shared
45
{
@@ -105,4 +106,101 @@ public enum Gender
105106
Male
106107
}
107108

109+
public class PersonWithReferenceType
110+
{
111+
public int Id { get; set; }
112+
public string FirstName { get; set; }
113+
public string LastName { get; set; }
114+
public Person BestFriend { get; set; }
115+
116+
public static PersonWithReferenceType[] TestValues = new[]
117+
{
118+
new PersonWithReferenceType
119+
{
120+
FirstName = "Test",
121+
LastName = "McTest",
122+
Id = 1
123+
},
124+
new PersonWithReferenceType
125+
{
126+
FirstName = "John",
127+
LastName = "Doe",
128+
Id = 2,
129+
BestFriend = new Person(1,"Jane","Doe",33)
130+
}
131+
};
132+
133+
protected bool Equals(PersonWithReferenceType other)
134+
{
135+
return Id == other.Id &&
136+
string.Equals(FirstName, other.FirstName) &&
137+
string.Equals(LastName, other.LastName) &&
138+
((BestFriend == null && other.BestFriend == null) || (BestFriend != null && BestFriend.Equals(other.BestFriend))) ;
139+
}
140+
141+
public override bool Equals(object obj)
142+
{
143+
if (ReferenceEquals(null, obj)) return false;
144+
if (ReferenceEquals(this, obj)) return true;
145+
if (obj.GetType() != this.GetType()) return false;
146+
return Equals((PersonWithReferenceType)obj);
147+
}
148+
149+
public override int GetHashCode()
150+
{
151+
unchecked
152+
{
153+
var hashCode = Id;
154+
hashCode = (hashCode * 397) ^ (FirstName != null ? FirstName.GetHashCode() : 0);
155+
hashCode = (hashCode * 397) ^ (LastName != null ? LastName.GetHashCode() : 0);
156+
hashCode = (hashCode * 397) ^ (BestFriend != null ? BestFriend.GetHashCode() : 0);
157+
return hashCode;
158+
}
159+
}
160+
}
161+
162+
public class TestProduct
163+
{
164+
public string Id { get; set; }
165+
166+
public string Name { get; set; }
167+
168+
public DateTime? Modified { get; set; }
169+
170+
public static TestProduct[] TestValues =
171+
{
172+
new TestProduct
173+
{
174+
Id = "1",
175+
Modified = null,
176+
Name = "Testing"
177+
}
178+
};
179+
180+
protected bool Equals(TestProduct other)
181+
{
182+
return Id == other.Id &&
183+
string.Equals(Name, other.Name) &&
184+
Modified.Equals(other.Modified);
185+
}
186+
187+
public override bool Equals(object obj)
188+
{
189+
if (ReferenceEquals(null, obj)) return false;
190+
if (ReferenceEquals(this, obj)) return true;
191+
if (obj.GetType() != this.GetType()) return false;
192+
return Equals((TestProduct)obj);
193+
}
194+
195+
public override int GetHashCode()
196+
{
197+
unchecked
198+
{
199+
var hashCode = (Id != null ? Id.GetHashCode() : 0);
200+
hashCode = (hashCode * 397) ^ (Name != null ? Name.GetHashCode() : 0);
201+
hashCode = (hashCode * 397) ^ (Modified != null ? Modified.GetHashCode() : 0);
202+
return hashCode;
203+
}
204+
}
205+
}
108206
}

tests/ServiceStack.OrmLite.Tests/ToInsertAndUpdateStatementTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,56 @@ public void Can_use_ToInsertStatement_to_generate_inline_SQL()
4545
Assert.That(insertedRow.Equals(row));
4646
}
4747
}
48+
49+
[Test]
50+
public void Correct_DbNull_in_ToInsertStatement()
51+
{
52+
using (var db = OpenDbConnection())
53+
{
54+
db.DropAndCreateTable<PersonWithReferenceType>();
55+
var row = PersonWithReferenceType.TestValues[0];
56+
57+
var sql = db.ToInsertStatement(row);
58+
sql.Print();
59+
db.ExecuteSql(sql);
60+
61+
var insertedRow = db.SingleById<PersonWithReferenceType>(row.Id);
62+
Assert.That(insertedRow.Equals(row));
63+
}
64+
}
65+
66+
[Test]
67+
public void Correct_Ref_in_ToInsertStatement()
68+
{
69+
using (var db = OpenDbConnection())
70+
{
71+
db.DropAndCreateTable<PersonWithReferenceType>();
72+
var row = PersonWithReferenceType.TestValues[1];
73+
74+
var sql = db.ToInsertStatement(row);
75+
sql.Print();
76+
db.ExecuteSql(sql);
77+
78+
var insertedRow = db.SingleById<PersonWithReferenceType>(row.Id);
79+
Assert.That(insertedRow.Equals(row));
80+
}
81+
}
82+
83+
[Test]
84+
public void Correct_nullable_in_ToInsertStatement()
85+
{
86+
using (var db = OpenDbConnection())
87+
{
88+
db.DropAndCreateTable<TestProduct>();
89+
var row = TestProduct.TestValues[0];
90+
91+
var sql = db.ToInsertStatement(row);
92+
sql.Print();
93+
db.ExecuteSql(sql);
94+
95+
var insertedRow = db.SingleById<TestProduct>(row.Id);
96+
Assert.AreEqual(insertedRow, row);
97+
}
98+
}
4899
}
49100
}

0 commit comments

Comments
 (0)