Skip to content
Browse files

Added UpdateBy dynamic method.

  • Loading branch information...
1 parent 61198a9 commit a9bb77dff5f27cb712d44465fa90810744a69cfb @markrendle committed Aug 6, 2010
Showing with 77 additions and 18 deletions.
  1. +19 −8 Simple.Data.IntegrationTest/DatabaseTest.cs
  2. +1 −1 Simple.Data.vsmdi
  3. +57 −9 Simple.Data/DynamicTable.cs
View
27 Simple.Data.IntegrationTest/DatabaseTest.cs
@@ -17,7 +17,7 @@ public void TestFindByDynamicSingleColumn()
{
dynamic database = new Database(new DbConnectionStub());
database.Users.FindByName("Foo");
- Assert.AreEqual("select * from Users where name = @p0", DatabaseStub.Sql);
+ Assert.AreEqual("select * from Users where name = @p0", DatabaseStub.Sql, true);
Assert.AreEqual("Foo", DatabaseStub.Parameters[0]);
}
@@ -26,7 +26,7 @@ public void TestFindByDynamicTwoColumns()
{
dynamic database = new Database(new DbConnectionStub());
database.Users.FindByNameAndPassword("Foo", "secret");
- Assert.AreEqual("select * from Users where name = @p0 and password = @p1", DatabaseStub.Sql);
+ Assert.AreEqual("select * from Users where name = @p0 and password = @p1", DatabaseStub.Sql, true);
Assert.AreEqual("Foo", DatabaseStub.Parameters[0]);
Assert.AreEqual("secret", DatabaseStub.Parameters[1]);
}
@@ -36,7 +36,7 @@ public void TestFindAllByDynamic()
{
dynamic database = new Database(new DbConnectionStub());
database.Users.FindAllByName("Foo");
- Assert.AreEqual("select * from Users where name = @p0", DatabaseStub.Sql);
+ Assert.AreEqual("select * from Users where name = @p0", DatabaseStub.Sql, true);
Assert.AreEqual("Foo", DatabaseStub.Parameters[0]);
}
@@ -45,7 +45,7 @@ public void TestQuery()
{
dynamic database = new Database(new DbConnectionStub());
database.Query("select * from Users where name = ? and age > ?", "Bob", 35);
- Assert.AreEqual("select * from Users where name = @p0 and age > @p1", DatabaseStub.Sql);
+ Assert.AreEqual("select * from Users where name = @p0 and age > @p1", DatabaseStub.Sql, true);
Assert.AreEqual("Bob", DatabaseStub.Parameters[0]);
Assert.AreEqual(35, DatabaseStub.Parameters[1]);
}
@@ -65,31 +65,42 @@ public void TestExecuteWithUpdate()
{
dynamic database = new Database(new DbConnectionStub());
database.Execute("update Users set name = ?, age = ? where id = ?", "Bob", 35, 1);
- Assert.AreEqual("update Users set name = @p0, age = @p1 where id = @p2", DatabaseStub.Sql);
+ Assert.AreEqual("update Users set name = @p0, age = @p1 where id = @p2", DatabaseStub.Sql, true);
Assert.AreEqual("Bob", DatabaseStub.Parameters[0]);
Assert.AreEqual(35, DatabaseStub.Parameters[1]);
Assert.AreEqual(1, DatabaseStub.Parameters[2]);
}
[TestMethod]
- public void TestNamedArgumentInsertOnTable()
+ public void TestInsertWithNamedArguments()
{
dynamic database = new Database(new DbConnectionStub());
database.Users.Insert(Name: "Steve", Age: 50);
- Assert.AreEqual("insert into Users (Name,Age) values (@p0,@p1)", DatabaseStub.Sql);
+ Assert.AreEqual("insert into Users (Name,Age) values (@p0,@p1)", DatabaseStub.Sql, true);
Assert.AreEqual("Steve", DatabaseStub.Parameters[0]);
Assert.AreEqual(50, DatabaseStub.Parameters[1]);
}
[TestMethod]
+ public void TestUpdateWithNamedArguments()
+ {
+ dynamic database = new Database(new DbConnectionStub());
+ database.Users.UpdateById(Id: 1, Name: "Steve", Age: 50);
+ Assert.AreEqual("update Users set Name = @p0, Age = @p1 where Id = @p2", DatabaseStub.Sql, true);
+ Assert.AreEqual("Steve", DatabaseStub.Parameters[0]);
+ Assert.AreEqual(50, DatabaseStub.Parameters[1]);
+ Assert.AreEqual(1, DatabaseStub.Parameters[2]);
+ }
+
+ [TestMethod]
public void TestInsertOnTable()
{
dynamic person = new ExpandoObject();
person.Name = "Phil";
person.Age = 42;
dynamic database = new Database(new DbConnectionStub());
database.Users.Insert(person);
- Assert.AreEqual("insert into Users (Name,Age) values (@p0,@p1)", DatabaseStub.Sql);
+ Assert.AreEqual("insert into Users (Name,Age) values (@p0,@p1)", DatabaseStub.Sql, true);
Assert.AreEqual("Phil", DatabaseStub.Parameters[0]);
Assert.AreEqual(42, DatabaseStub.Parameters[1]);
}
View
2 Simple.Data.vsmdi
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<TestLists xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<TestList name="Lists of Tests" id="8c43106b-9dc1-4907-a29f-aa66a61bf5b6">
- <RunConfiguration id="9521515a-ca67-4474-a3a5-0f6218187ced" name="Local" storage="local.testsettings" type="Microsoft.VisualStudio.TestTools.Common.TestRunConfiguration, Microsoft.VisualStudio.QualityTools.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
+ <RunConfiguration id="6276c44f-9ca4-4b7f-9d72-b6aabf66cabb" name="Local" storage="local.testsettings" type="Microsoft.VisualStudio.TestTools.Common.TestRunConfiguration, Microsoft.VisualStudio.QualityTools.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</TestList>
</TestLists>
View
66 Simple.Data/DynamicTable.cs
@@ -38,6 +38,11 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
result = Insert(binder, args);
success = true;
}
+ else if (binder.Name.StartsWith("UpdateBy"))
+ {
+ result = Update(binder, args);
+ success = true;
+ }
else
{
success = base.TryInvokeMember(binder, args, out result);
@@ -46,6 +51,31 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
return success;
}
+ private object Update(InvokeMemberBinder binder, object[] args)
+ {
+ var byColumns = GetUpdateByColumns(args, binder.Name.Substring(8));
+ if (byColumns == null) return 0;
+
+ var dict = NamedArgumentsToDictionary(binder, args);
+
+ List<object> values = new List<object>();
+ List<string> sets = new List<string>();
+
+ foreach (var pair in dict.Where(p => !byColumns.Contains(p.Key)))
+ {
+ sets.Add(pair.Key + " = ?");
+ values.Add(pair.Value);
+ }
+
+ var builder = new StringBuilder("update " + _tableName + " set " + string.Join(", ", sets));
+ builder.Append(" where " + string.Join(" and ", byColumns.Select(col => col + " = ?")));
+
+ values.AddRange(byColumns.Select(byColumn => dict[byColumn]));
+
+ _database.Execute(builder.ToString(), values.ToArray());
+ return null;
+ }
+
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (binder.Name == "All")
@@ -67,14 +97,7 @@ public void Insert(dynamic entity)
private object Insert(InvokeMemberBinder binder, IList<object> args)
{
- var insert = new Dictionary<string, object>();
-
- var index = 0;
- foreach (var argumentName in binder.CallInfo.ArgumentNames)
- {
- insert[argumentName] = args[index];
- index++;
- }
+ var insert = NamedArgumentsToDictionary(binder, args);
_database.Insert(_tableName, insert);
@@ -125,11 +148,36 @@ internal static string[] GetFindByColumns(IList<object> args, string methodName)
if (args == null) throw new ArgumentNullException("args");
if (args.Count == 0) throw new ArgumentException("No parameters specified.");
- var columns = methodName.ToSnakeCase().Split(new[] {"_and_"}, StringSplitOptions.RemoveEmptyEntries);
+ var columns = methodName.Split(new[] {"And"}, StringSplitOptions.RemoveEmptyEntries);
if (columns.Length == 0) throw new ArgumentException("No columns specified.");
if (columns.Length != args.Count) throw new ArgumentException("Parameter count mismatch.");
return columns;
}
+
+ internal static string[] GetUpdateByColumns(IList<object> args, string methodName)
+ {
+ if (args == null) throw new ArgumentNullException("args");
+ if (args.Count == 0) throw new ArgumentException("No parameters specified.");
+
+ var columns = methodName.Split(new[] { "And" }, StringSplitOptions.RemoveEmptyEntries);
+
+ if (columns.Length == 0) throw new ArgumentException("No columns specified.");
+ if (args.Count < columns.Length) throw new ArgumentException("Not enough update columns specified.");
+ if (args.Count == columns.Length) return null; // No values to actually update. Fail silently.
+ return columns;
+ }
+
+ internal static Dictionary<string, object> NamedArgumentsToDictionary(InvokeMemberBinder binder, IList<object> args)
+ {
+ var dict = new Dictionary<string, object>();
+
+ for (int i = 0; i < args.Count; i++)
+ {
+ dict.Add(binder.CallInfo.ArgumentNames[i], args[i]);
+ }
+
+ return dict;
+ }
}
}

0 comments on commit a9bb77d

Please sign in to comment.
Something went wrong with that request. Please try again.