Skip to content

Commit 5bcbf2e

Browse files
committed
Fix for update
1 parent 44a3589 commit 5bcbf2e

6 files changed

Lines changed: 60 additions & 11 deletions

File tree

Simple.Data.BehaviourTest/Simple.Data.BehaviourTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<Compile Include="DeleteTest.cs" />
7070
<Compile Include="ExceptionsTesting.cs" />
7171
<Compile Include="FindTest.cs" />
72+
<Compile Include="GetCountTest.cs" />
7273
<Compile Include="GetTest.cs" />
7374
<Compile Include="InsertTest.cs" />
7475
<Compile Include="NaturalNamingTest.cs" />

Simple.Data.SqlTest/UpdateTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,23 @@ public void TestUpdateWithTimestamp()
138138
row = db.TimestampTest.Get(row.Id);
139139
Assert.AreEqual("Updated", row.Description);
140140
}
141+
142+
[Test]
143+
public void TestUpdateByInputIsNotMutated()
144+
{
145+
var db = DatabaseHelper.Open();
146+
var user = new Dictionary<string, object>() {
147+
{"Id", 0},
148+
{"Age", 1},
149+
{"Name", "X"},
150+
{"Password", "P"}
151+
};
152+
153+
user["Id"] = db.Users.Insert(user).Id;
154+
155+
db.Users.UpdateById(user);
156+
157+
Assert.AreEqual(4, user.Keys.Count);
158+
}
141159
}
142160
}

Simple.Data/Commands/GetCountCommand.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,24 @@ public object Execute(DataStrategy dataStrategy, DynamicTable table, InvokeMembe
3232
{
3333
var query = new SimpleQuery(dataStrategy, table.GetQualifiedName());
3434

35-
if (args.Length == 1 && args[0] is SimpleExpression)
35+
if (args.Length == 1)
3636
{
37-
query = query.Where((SimpleExpression)args[0]);
37+
var expression = args[0] as SimpleExpression;
38+
if (expression != null)
39+
{
40+
return query.Where(expression).Count();
41+
}
42+
else
43+
{
44+
throw new BadExpressionException("GetCount expects an expression or no parameters.");
45+
}
46+
}
47+
else if (args.Length == 0)
48+
{
49+
return query.Count();
3850
}
3951

40-
return query.Count();
52+
throw new ArgumentException("GetCount expects an expression or no parameters.");
4153
}
4254
}
4355
}

Simple.Data/DynamicTable.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
137137
return true;
138138
}
139139

140+
public override bool TrySetMember(SetMemberBinder binder, object value)
141+
{
142+
if (base.TrySetMember(binder, value))
143+
{
144+
return true;
145+
}
146+
throw new BadExpressionException("Cannot assign values to table columns.");
147+
}
148+
140149
public ObjectReference As(string alias)
141150
{
142151
return new ObjectReference(_tableName, (_schema != null ? new ObjectReference(_schema.GetName()) : null), _dataStrategy, alias);

Simple.Data/ObjectReference.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
175175
return true;
176176
}
177177

178+
public override bool TrySetMember(SetMemberBinder binder, object value)
179+
{
180+
if (base.TrySetMember(binder, value))
181+
{
182+
return true;
183+
}
184+
throw new BadExpressionException("Cannot assign values to object references.");
185+
}
186+
178187
public dynamic this[string name]
179188
{
180189
get { return new ObjectReference(name, this); }

Simple.Data/SimpleQuery.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -698,22 +698,22 @@ public T ToScalarOrDefault<T>()
698698

699699
public dynamic First()
700700
{
701-
return Run().First();
701+
return Take(1).Run().First();
702702
}
703703

704704
public dynamic FirstOrDefault()
705705
{
706-
return Run().FirstOrDefault();
706+
return Take(1).Run().FirstOrDefault();
707707
}
708708

709709
public T First<T>()
710710
{
711-
return Cast<T>().First();
711+
return Take(1).Cast<T>().First();
712712
}
713713

714714
public T FirstOrDefault<T>()
715715
{
716-
return Cast<T>().FirstOrDefault();
716+
return Take(1).Cast<T>().FirstOrDefault();
717717
}
718718

719719
public T First<T>(Func<T, bool> predicate)
@@ -728,22 +728,22 @@ public T FirstOrDefault<T>(Func<T, bool> predicate)
728728

729729
public dynamic Single()
730730
{
731-
return Run().First();
731+
return Take(1).Run().First();
732732
}
733733

734734
public dynamic SingleOrDefault()
735735
{
736-
return Run().FirstOrDefault();
736+
return Take(1).Run().FirstOrDefault();
737737
}
738738

739739
public T Single<T>()
740740
{
741-
return Cast<T>().Single();
741+
return Take(1).Cast<T>().Single();
742742
}
743743

744744
public T SingleOrDefault<T>()
745745
{
746-
return Cast<T>().SingleOrDefault();
746+
return Take(1).Cast<T>().SingleOrDefault();
747747
}
748748

749749
public T Single<T>(Func<T, bool> predicate)

0 commit comments

Comments
 (0)