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

Commit 1366560

Browse files
committed
Remove magic behavior for bools in db.UpdateNonDefault(), don't compare default values in nullable types
1 parent bb526d4 commit 1366560

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -744,9 +744,8 @@ 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
748-
&& (value == null || value.Equals(value.GetType().GetDefaultValue()))
749-
&& !(value is bool))
747+
if (excludeDefaults
748+
&& (value == null || (!fieldDef.IsNullable && value.Equals(value.GetType().GetDefaultValue()))))
750749
continue;
751750

752751
fieldDef.GetQuotedValue(item, DialectProvider);
@@ -760,6 +759,9 @@ public virtual string ToUpdateStatement(T item, bool excludeDefaults = false)
760759
.Append(DialectProvider.GetQuotedValue(value, fieldDef.FieldType));
761760
}
762761

762+
if (setFields.Length == 0)
763+
throw new ArgumentException("No non-null or non-default values were provided for type: " + typeof(T).Name);
764+
763765
return string.Format("UPDATE {0} SET {1} {2}",
764766
DialectProvider.GetQuotedTableName(modelDef), setFields, WhereExpression);
765767
}

tests/ServiceStack.OrmLite.Tests/OrmLiteUpdateTests.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,16 @@ public void Can_UpdateOnly_bool_columns()
213213
{
214214
db.DropAndCreateTable<PocoWithBool>();
215215

216-
db.Insert(new PocoWithBool { Id = 1, Bool = true });
217-
db.UpdateNonDefaults(new PocoWithBool { Bool = false }, x => x.Id == 1);
216+
db.Insert(new PocoWithBool { Id = 1, Bool = false });
218217
var row = db.SingleById<PocoWithBool>(1);
219218
Assert.That(row.Bool, Is.False);
219+
220+
db.UpdateNonDefaults(new PocoWithBool { Bool = true }, x => x.Id == 1);
221+
row = db.SingleById<PocoWithBool>(1);
222+
Assert.That(row.Bool, Is.True);
223+
224+
Assert.Throws<ArgumentException>(() =>
225+
db.UpdateNonDefaults(new PocoWithBool { Bool = false }, x => x.Id == 1));
220226
}
221227
}
222228

@@ -228,11 +234,37 @@ public void Can_UpdateOnly_nullable_bool_columns()
228234
db.DropAndCreateTable<PocoWithNullableBool>();
229235

230236
db.Insert(new PocoWithNullableBool { Id = 1, Bool = true });
231-
db.UpdateNonDefaults(new PocoWithNullableBool { Bool = false }, x => x.Id == 1);
232237
var row = db.SingleById<PocoWithNullableBool>(1);
238+
Assert.That(row.Bool, Is.True);
239+
240+
db.UpdateNonDefaults(new PocoWithNullableBool { Bool = false }, x => x.Id == 1);
241+
row = db.SingleById<PocoWithNullableBool>(1);
233242
Assert.That(row.Bool, Is.False);
234243
}
235244
}
245+
246+
public class PocoWithNullableInt
247+
{
248+
public int Id { get; set; }
249+
public int? Int { get; set; }
250+
}
251+
252+
[Test]
253+
public void Can_UpdateOnly_nullable_int_columns()
254+
{
255+
using (var db = OpenDbConnection())
256+
{
257+
db.DropAndCreateTable<PocoWithNullableInt>();
258+
259+
db.Insert(new PocoWithNullableInt { Id = 1, Int = 1 });
260+
var row = db.SingleById<PocoWithNullableInt>(1);
261+
Assert.That(row.Int, Is.EqualTo(1));
262+
263+
db.UpdateNonDefaults(new PocoWithNullableInt { Int = 0 }, x => x.Id == 1);
264+
row = db.SingleById<PocoWithNullableInt>(1);
265+
Assert.That(row.Int, Is.EqualTo(0));
266+
}
267+
}
236268
}
237269

238270
[CompositeIndex("FirstName", "LastName")]

0 commit comments

Comments
 (0)