Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

SQL Server doesn't try to write to TIMESTAMP columns

  • Loading branch information...
commit 58bc30901d7a8e80799ad92097d8b55641c0fd20 1 parent ec06753
@markrendle authored
View
2  Simple.Data.Ado/UpdateHelper.cs
@@ -93,7 +93,7 @@ private string CreateWhereExistsStatement(SimpleExpression criteria, Table table
private string GetUpdateClause(Table table, IEnumerable<KeyValuePair<string, object>> data)
{
var setClause = string.Join(", ",
- data.Where(kvp => table.HasColumn(kvp.Key))
+ data.Where(kvp => table.HasColumn(kvp.Key) && table.FindColumn(kvp.Key).IsWriteable)
.Select(kvp => CreateColumnUpdateClause(kvp.Key, kvp.Value, table)));
return string.Format("update {0} set {1}", table.QualifiedName, setClause);
}
View
46 Simple.Data.BehaviourTest/UpdateTest.cs
@@ -89,17 +89,18 @@ public void TestUpdateWithDynamicObjectAndOriginalValues()
{
dynamic newRecord = new SimpleRecord();
newRecord.Id = 1;
- newRecord.Name = "Steve";
+ newRecord.Name = "Steve-o";
newRecord.Age = 50;
dynamic originalRecord = new SimpleRecord();
- originalRecord.Id = 2;
+ originalRecord.Id = 1;
originalRecord.Name = "Steve";
originalRecord.Age = 50;
_db.Users.Update(newRecord, originalRecord);
- GeneratedSqlIs("update [dbo].[Users] set [Id] = @p1 where [dbo].[Users].[Id] = @p2");
- Parameter(0).Is(1);
- Parameter(1).Is(2);
+ GeneratedSqlIs("update [dbo].[Users] set [Name] = @p1 where ([dbo].[Users].[Id] = @p2 and [dbo].[Users].[Name] = @p3)");
+ Parameter(0).Is("Steve-o");
+ Parameter(1).Is(1);
+ Parameter(2).Is("Steve");
}
[Test]
@@ -107,17 +108,18 @@ public void TestUpdateWithDynamicObjectsAndOriginalValues()
{
dynamic newRecord = new SimpleRecord();
newRecord.Id = 1;
- newRecord.Name = "Steve";
+ newRecord.Name = "Steve-o";
newRecord.Age = 50;
dynamic originalRecord = new SimpleRecord();
- originalRecord.Id = 2;
+ originalRecord.Id = 1;
originalRecord.Name = "Steve";
originalRecord.Age = 50;
_db.Users.Update(new[] {newRecord}, new[] {originalRecord});
- GeneratedSqlIs("update [dbo].[Users] set [Id] = @p1 where [dbo].[Users].[Id] = @p2");
- Parameter(0).Is(1);
- Parameter(1).Is(2);
+ GeneratedSqlIs("update [dbo].[Users] set [Name] = @p1 where ([dbo].[Users].[Id] = @p2 and [dbo].[Users].[Name] = @p3)");
+ Parameter(0).Is("Steve-o");
+ Parameter(1).Is(1);
+ Parameter(2).Is("Steve");
}
[Test]
@@ -194,15 +196,15 @@ public void TestUpdateWithStaticObjectAndOriginalObject()
var newUser = new User
{
Id = 1,
- Name = "Steve",
+ Name = "Steve-o",
Age = 50
};
- var originalUser = new User {Id = 2, Name = "Steve", Age = 50};
+ var originalUser = new User {Id = 1, Name = "Steve", Age = 50};
_db.Users.Update(newUser, originalUser);
- GeneratedSqlIs(
- "update [dbo].[Users] set [Id] = @p1 where [dbo].[Users].[Id] = @p2");
- Parameter(0).Is(1);
- Parameter(1).Is(2);
+ GeneratedSqlIs("update [dbo].[Users] set [Name] = @p1 where ([dbo].[Users].[Id] = @p2 and [dbo].[Users].[Name] = @p3)");
+ Parameter(0).Is("Steve-o");
+ Parameter(1).Is(1);
+ Parameter(2).Is("Steve");
}
[Test]
@@ -211,15 +213,15 @@ public void TestUpdateWithStaticObjectsAndOriginalObject()
var newUser = new User
{
Id = 1,
- Name = "Steve",
+ Name = "Steve-o",
Age = 50
};
- var originalUser = new User { Id = 2, Name = "Steve", Age = 50 };
+ var originalUser = new User { Id = 1, Name = "Steve", Age = 50 };
_db.Users.Update(new[] {newUser}, new[] {originalUser});
- GeneratedSqlIs(
- "update [dbo].[Users] set [Id] = @p1 where [dbo].[Users].[Id] = @p2");
- Parameter(0).Is(1);
- Parameter(1).Is(2);
+ GeneratedSqlIs("update [dbo].[Users] set [Name] = @p1 where ([dbo].[Users].[Id] = @p2 and [dbo].[Users].[Name] = @p3)");
+ Parameter(0).Is("Steve-o");
+ Parameter(1).Is(1);
+ Parameter(2).Is("Steve");
}
[Test]
View
1  Simple.Data.InMemoryTest/Simple.Data.InMemoryTest.csproj
@@ -58,7 +58,6 @@
<ItemGroup>
<Compile Include="CanAssignArray.cs" />
<Compile Include="InMemoryTests.cs" />
- <Compile Include="JoinTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
View
2  Simple.Data.SqlServer/DbTypeLookup.cs
@@ -37,7 +37,7 @@ internal static class DbTypeLookup
{"varchar", SqlDbType.VarChar},
{"binary", SqlDbType.Binary},
{"char", SqlDbType.Char},
- {"timestamp", SqlDbType.Binary},
+ {"timestamp", SqlDbType.Timestamp},
{"nvarchar", SqlDbType.NVarChar},
{"nchar", SqlDbType.NChar},
{"xml", SqlDbType.Xml},
View
11 Simple.Data.SqlTest/UpdateTests.cs
@@ -118,5 +118,16 @@ public void ToListShouldExecuteQuery()
Assert.DoesNotThrow(() =>
db.Customers.Update(customers));
}
+
+ [Test]
+ public void TestUpdateWithTimestamp()
+ {
+ var db = DatabaseHelper.Open();
+ var row = db.TimestampTest.Insert(Description: "Inserted");
+ row.Description = "Updated";
+ db.TimestampTest.Update(row);
+ row = db.TimestampTest.Get(row.Id);
+ Assert.AreEqual("Updated", row.Description);
+ }
}
}
View
5 Simple.Data/DatabaseRunner.cs
@@ -10,6 +10,11 @@ public DatabaseRunner(Adapter adapter)
_adapter = adapter;
}
+ protected override Adapter Adapter
+ {
+ get { return _adapter; }
+ }
+
internal override IDictionary<string, object> FindOne(string tableName, SimpleExpression criteria)
{
return _adapter.FindOne(tableName, criteria);
View
16 Simple.Data/RunStrategy.cs
@@ -5,6 +5,8 @@ namespace Simple.Data
internal abstract class RunStrategy
{
+ protected abstract Adapter Adapter { get; }
+
/// <summary>
/// Finds data from the specified "table".
/// </summary>
@@ -65,16 +67,24 @@ internal abstract class RunStrategy
return changedValuesDict;
}
- protected static SimpleExpression CreateCriteriaFromOriginalValues(string tableName,
+ protected SimpleExpression CreateCriteriaFromOriginalValues(string tableName,
IDictionary<string, object> newValuesDict,
IDictionary<string, object>
originalValuesDict)
{
- var criteriaValues = originalValuesDict
+ var criteriaValues = Adapter.GetKey(tableName, originalValuesDict);
+
+ foreach (var kvp in originalValuesDict
.Where(
originalKvp =>
newValuesDict.ContainsKey(originalKvp.Key) &&
- !(Equals(newValuesDict[originalKvp.Key], originalKvp.Value)));
+ !(Equals(newValuesDict[originalKvp.Key], originalKvp.Value))))
+ {
+ if (!criteriaValues.ContainsKey(kvp.Key))
+ {
+ criteriaValues.Add(kvp);
+ }
+ };
return ExpressionHelper.CriteriaDictionaryToExpression(tableName, criteriaValues);
}
View
5 Simple.Data/TransactionRunner.cs
@@ -14,6 +14,11 @@ public TransactionRunner(IAdapterWithTransactions adapter, IAdapterTransaction a
_adapterTransaction = adapterTransaction;
}
+ protected override Adapter Adapter
+ {
+ get { return (Adapter) _adapter; }
+ }
+
internal override IDictionary<string, object> FindOne(string tableName, SimpleExpression criteria)
{
return Find(tableName, criteria).FirstOrDefault();
Please sign in to comment.
Something went wrong with that request. Please try again.