Permalink
Browse files

Fix for update

  • Loading branch information...
1 parent 44a3589 commit 5bcbf2ef901ed7538d4aa6baa4a27d9f8a5b2b70 @markrendle committed Nov 6, 2012
View
1 Simple.Data.BehaviourTest/Simple.Data.BehaviourTest.csproj
@@ -69,6 +69,7 @@
<Compile Include="DeleteTest.cs" />
<Compile Include="ExceptionsTesting.cs" />
<Compile Include="FindTest.cs" />
+ <Compile Include="GetCountTest.cs" />
<Compile Include="GetTest.cs" />
<Compile Include="InsertTest.cs" />
<Compile Include="NaturalNamingTest.cs" />
View
18 Simple.Data.SqlTest/UpdateTests.cs
@@ -138,5 +138,23 @@ public void TestUpdateWithTimestamp()
row = db.TimestampTest.Get(row.Id);
Assert.AreEqual("Updated", row.Description);
}
+
+ [Test]
+ public void TestUpdateByInputIsNotMutated()
+ {
+ var db = DatabaseHelper.Open();
+ var user = new Dictionary<string, object>() {
+ {"Id", 0},
+ {"Age", 1},
+ {"Name", "X"},
+ {"Password", "P"}
+ };
+
+ user["Id"] = db.Users.Insert(user).Id;
+
+ db.Users.UpdateById(user);
+
+ Assert.AreEqual(4, user.Keys.Count);
+ }
}
}
View
18 Simple.Data/Commands/GetCountCommand.cs
@@ -32,12 +32,24 @@ public object Execute(DataStrategy dataStrategy, DynamicTable table, InvokeMembe
{
var query = new SimpleQuery(dataStrategy, table.GetQualifiedName());
- if (args.Length == 1 && args[0] is SimpleExpression)
+ if (args.Length == 1)
{
- query = query.Where((SimpleExpression)args[0]);
+ var expression = args[0] as SimpleExpression;
+ if (expression != null)
+ {
+ return query.Where(expression).Count();
+ }
+ else
+ {
+ throw new BadExpressionException("GetCount expects an expression or no parameters.");
+ }
+ }
+ else if (args.Length == 0)
+ {
+ return query.Count();
}
- return query.Count();
+ throw new ArgumentException("GetCount expects an expression or no parameters.");
}
}
}
View
9 Simple.Data/DynamicTable.cs
@@ -137,6 +137,15 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
return true;
}
+ public override bool TrySetMember(SetMemberBinder binder, object value)
+ {
+ if (base.TrySetMember(binder, value))
+ {
+ return true;
+ }
+ throw new BadExpressionException("Cannot assign values to table columns.");
+ }
+
public ObjectReference As(string alias)
{
return new ObjectReference(_tableName, (_schema != null ? new ObjectReference(_schema.GetName()) : null), _dataStrategy, alias);
View
9 Simple.Data/ObjectReference.cs
@@ -175,6 +175,15 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
return true;
}
+ public override bool TrySetMember(SetMemberBinder binder, object value)
+ {
+ if (base.TrySetMember(binder, value))
+ {
+ return true;
+ }
+ throw new BadExpressionException("Cannot assign values to object references.");
+ }
+
public dynamic this[string name]
{
get { return new ObjectReference(name, this); }
View
16 Simple.Data/SimpleQuery.cs
@@ -698,22 +698,22 @@ public T ToScalarOrDefault<T>()
public dynamic First()
{
- return Run().First();
+ return Take(1).Run().First();
}
public dynamic FirstOrDefault()
{
- return Run().FirstOrDefault();
+ return Take(1).Run().FirstOrDefault();
}
public T First<T>()
{
- return Cast<T>().First();
+ return Take(1).Cast<T>().First();
}
public T FirstOrDefault<T>()
{
- return Cast<T>().FirstOrDefault();
+ return Take(1).Cast<T>().FirstOrDefault();
}
public T First<T>(Func<T, bool> predicate)
@@ -728,22 +728,22 @@ public T FirstOrDefault<T>(Func<T, bool> predicate)
public dynamic Single()
{
- return Run().First();
+ return Take(1).Run().First();
}
public dynamic SingleOrDefault()
{
- return Run().FirstOrDefault();
+ return Take(1).Run().FirstOrDefault();
}
public T Single<T>()
{
- return Cast<T>().Single();
+ return Take(1).Cast<T>().Single();
}
public T SingleOrDefault<T>()
{
- return Cast<T>().SingleOrDefault();
+ return Take(1).Cast<T>().SingleOrDefault();
}
public T Single<T>(Func<T, bool> predicate)

0 comments on commit 5bcbf2e

Please sign in to comment.