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

Commit bb526d4

Browse files
committed
Include bools in UpdateNonDefaults
1 parent 846d056 commit bb526d4

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,10 @@ public virtual string ToUpdateStatement(T item, bool excludeDefaults = false)
744744
if (updateFields.Count > 0 && !updateFields.Contains(fieldDef.Name)) continue; // added
745745

746746
var value = fieldDef.GetValue(item);
747-
if (excludeDefaults && (value == null || value.Equals(value.GetType().GetDefaultValue()))) continue; //GetDefaultValue?
747+
if (excludeDefaults
748+
&& (value == null || value.Equals(value.GetType().GetDefaultValue()))
749+
&& !(value is bool))
750+
continue;
748751

749752
fieldDef.GetQuotedValue(item, DialectProvider);
750753

tests/ServiceStack.OrmLite.Tests/OrmLiteUpdateTests.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public void Can_Update_Only_Blobs()
166166
{
167167
db.DropAndCreateTable<SomeBlobs>();
168168

169-
db.Insert(new SomeBlobs {FirstName = "Bro", LastName = "Last"});
169+
db.Insert(new SomeBlobs { FirstName = "Bro", LastName = "Last" });
170170
db.Insert(new SomeBlobs { FirstName = "Sis", LastName = "Last" });
171171

172172
var existing = db.Select<SomeBlobs>(p => p.FirstName == "Bro").First();
@@ -183,7 +183,7 @@ public void Can_Update_Only_Blobs()
183183
existing.Blob2 = new byte[blob2Bytes];
184184
Buffer.BlockCopy(blob2Array, 0, existing.Blob2, 0, blob2Bytes);
185185

186-
db.UpdateOnly(existing, p => new {p.Blob1, p.Blob2, p.FirstName}, r => r.LastName == "Last" && r.FirstName == "Bro");
186+
db.UpdateOnly(existing, p => new { p.Blob1, p.Blob2, p.FirstName }, r => r.LastName == "Last" && r.FirstName == "Bro");
187187

188188
var verify = db.Select<SomeBlobs>(p => p.FirstName == "Bro").First();
189189

@@ -193,6 +193,46 @@ public void Can_Update_Only_Blobs()
193193
Assert.That(existing.Blob1, Is.EquivalentTo(verify.Blob1));
194194
Assert.That(existing.Blob2, Is.EquivalentTo(verify.Blob2));
195195
}
196+
197+
public class PocoWithBool
198+
{
199+
public int Id { get; set; }
200+
public bool Bool { get; set; }
201+
}
202+
203+
public class PocoWithNullableBool
204+
{
205+
public int Id { get; set; }
206+
public bool? Bool { get; set; }
207+
}
208+
209+
[Test]
210+
public void Can_UpdateOnly_bool_columns()
211+
{
212+
using (var db = OpenDbConnection())
213+
{
214+
db.DropAndCreateTable<PocoWithBool>();
215+
216+
db.Insert(new PocoWithBool { Id = 1, Bool = true });
217+
db.UpdateNonDefaults(new PocoWithBool { Bool = false }, x => x.Id == 1);
218+
var row = db.SingleById<PocoWithBool>(1);
219+
Assert.That(row.Bool, Is.False);
220+
}
221+
}
222+
223+
[Test]
224+
public void Can_UpdateOnly_nullable_bool_columns()
225+
{
226+
using (var db = OpenDbConnection())
227+
{
228+
db.DropAndCreateTable<PocoWithNullableBool>();
229+
230+
db.Insert(new PocoWithNullableBool { Id = 1, Bool = true });
231+
db.UpdateNonDefaults(new PocoWithNullableBool { Bool = false }, x => x.Id == 1);
232+
var row = db.SingleById<PocoWithNullableBool>(1);
233+
Assert.That(row.Bool, Is.False);
234+
}
235+
}
196236
}
197237

198238
[CompositeIndex("FirstName", "LastName")]
@@ -205,4 +245,5 @@ public class SomeBlobs
205245
public byte[] Blob1 { get; set; }
206246
public byte[] Blob2 { get; set; }
207247
}
248+
208249
}

0 commit comments

Comments
 (0)