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

Commit f7eb67c

Browse files
authored
Merge pull request #558 from BruceCowan-AI/FixToUpdateOrInsertStatement
Fix ToUpdateStatement and ToInsertStatement with subset named columns
2 parents 76f7d9a + f0d4de1 commit f7eb67c

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

src/ServiceStack.OrmLite.Oracle.Tests/ServiceStack.OrmLite.Oracle.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@
400400
<Compile Include="..\..\tests\ServiceStack.OrmLite.Tests\TestHelpers.cs">
401401
<Link>TestHelpers.cs</Link>
402402
</Compile>
403+
<Compile Include="..\..\tests\ServiceStack.OrmLite.Tests\ToInsertAndUpdateStatementTests.cs">
404+
<Link>ToInsertAndUpdateStatementTests.cs</Link>
405+
</Compile>
403406
<Compile Include="..\..\tests\ServiceStack.OrmLite.Tests\TypeDescriptorMetadataTests.cs">
404407
<Link>TypeDescriptorMetadataTests.cs</Link>
405408
</Compile>

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Linq;
1818
using System.Linq.Expressions;
1919
using System.Text;
20+
using System.Text.RegularExpressions;
2021
using System.Threading;
2122
using System.Threading.Tasks;
2223
using ServiceStack.DataAnnotations;
@@ -743,7 +744,7 @@ public virtual string MergeParamsIntoSql(string sql, IEnumerable<IDbDataParamete
743744
var quotedValue = dbParam.Value != null
744745
? GetQuotedValue(dbParam.Value, dbParam.Value.GetType())
745746
: "null";
746-
sql = sql.Replace(dbParam.ParameterName, quotedValue);
747+
sql = Regex.Replace(sql, dbParam.ParameterName + @"(,|\s|\)|$)", quotedValue + "$1");
747748
}
748749
return sql;
749750
}

tests/ServiceStack.OrmLite.Tests/DefaultValueTests.cs

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Data;
23
using NUnit.Framework;
34
using ServiceStack.DataAnnotations;
45
using ServiceStack.Text;
@@ -12,6 +13,8 @@ public class DefaultValues
1213
[Default(1)]
1314
public int DefaultInt { get; set; }
1415

16+
public int DefaultIntNoDefault { get; set; }
17+
1518
[Default(1)]
1619
public int? NDefaultInt { get; set; }
1720

@@ -39,28 +42,62 @@ public void Can_create_table_with_DefaultValues()
3942
{
4043
using (var db = OpenDbConnection())
4144
{
42-
db.DropAndCreateTable<DefaultValues>();
45+
var row = CreateAndInitialize(db);
46+
47+
var expectedDate = Dialect != Dialect.MySql && Dialect != Dialect.Firebird
48+
? DateTime.UtcNow.Date
49+
: DateTime.Now.Date; //MySql CURRENT_TIMESTAMP == LOCAL_TIME
50+
51+
Assert.That(row.CreatedDateUtc, Is.GreaterThan(expectedDate));
52+
Assert.That(row.NCreatedDateUtc, Is.GreaterThan(expectedDate));
53+
}
54+
}
55+
56+
private DefaultValues CreateAndInitialize(IDbConnection db)
57+
{
58+
db.DropAndCreateTable<DefaultValues>();
4359

44-
db.GetLastSql().Print();
60+
db.GetLastSql().Print();
4561

46-
db.Insert(new DefaultValues { Id = 1 });
62+
db.Insert(new DefaultValues { Id = 1 });
63+
64+
65+
var row = db.SingleById<DefaultValues>(1);
66+
67+
row.PrintDump();
68+
Assert.That(row.DefaultInt, Is.EqualTo(1));
69+
Assert.That(row.DefaultIntNoDefault, Is.EqualTo(0));
70+
Assert.That(row.NDefaultInt, Is.EqualTo(1));
71+
Assert.That(row.DefaultDouble, Is.EqualTo(1.1).Within(.1d));
72+
Assert.That(row.NDefaultDouble, Is.EqualTo(1.1).Within(.1d));
73+
Assert.That(row.DefaultString, Is.EqualTo("String"));
74+
75+
return row;
76+
}
77+
78+
[Test]
79+
public void Can_use_ToUpdateStatement_to_generate_inline_SQL()
80+
{
81+
using (var db = OpenDbConnection())
82+
{
83+
CreateAndInitialize(db);
4784

4885
var row = db.SingleById<DefaultValues>(1);
86+
row.DefaultIntNoDefault = 42;
87+
88+
var sql = db.ToUpdateStatement(row);
89+
sql.Print();
90+
db.ExecuteSql(sql);
91+
92+
row = db.SingleById<DefaultValues>(1);
4993

50-
row.PrintDump();
5194
Assert.That(row.DefaultInt, Is.EqualTo(1));
95+
Assert.That(row.DefaultIntNoDefault, Is.EqualTo(42));
5296
Assert.That(row.NDefaultInt, Is.EqualTo(1));
5397
Assert.That(row.DefaultDouble, Is.EqualTo(1.1).Within(.1d));
5498
Assert.That(row.NDefaultDouble, Is.EqualTo(1.1).Within(.1d));
5599
Assert.That(row.DefaultString, Is.EqualTo("String"));
56-
57-
var expectedDate = Dialect != Dialect.MySql && Dialect != Dialect.Firebird
58-
? DateTime.UtcNow.Date
59-
: DateTime.Now.Date; //MySql CURRENT_TIMESTAMP == LOCAL_TIME
60-
61-
Assert.That(row.CreatedDateUtc, Is.GreaterThan(expectedDate));
62-
Assert.That(row.NCreatedDateUtc, Is.GreaterThan(expectedDate));
63100
}
64101
}
65102
}
66-
}
103+
}

0 commit comments

Comments
 (0)