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

Commit ba7d19a

Browse files
committed
Replace complete db params [@1] and not partial tokens [@1]0
1 parent 7a189f4 commit ba7d19a

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Collections.ObjectModel;
99
using System.Linq.Expressions;
1010
using System.Runtime.CompilerServices;
11+
using System.Text.RegularExpressions;
1112
using ServiceStack.Text;
1213

1314
namespace ServiceStack.OrmLite
@@ -2112,7 +2113,9 @@ protected string ConvertInExpressionToSql(MethodCallExpression m, object quotedC
21122113

21132114
for (var i = renameParams.Count - 1; i >= 0; i--)
21142115
{
2115-
subSelect = subSelect.Replace(renameParams[i].Item1, renameParams[i].Item2);
2116+
//Replace complete db params [@1] and not partial tokens [@1]0
2117+
var paramsRegex = new Regex(renameParams[i].Item1 + "([^\\d])");
2118+
subSelect = paramsRegex.Replace(subSelect, renameParams[i].Item2 + "$1");
21162119
}
21172120

21182121
return $"{quotedColName} IN ({subSelect})";
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using NUnit.Framework;
7+
using ServiceStack.OrmLite.Tests.Shared;
8+
using ServiceStack.Text;
9+
10+
namespace ServiceStack.OrmLite.Tests.Issues
11+
{
12+
public class Record
13+
{
14+
public int Id { get; set; }
15+
public int Value { get; set; }
16+
}
17+
18+
[TestFixture]
19+
public class MergeParamsIssue : OrmLiteTestBase
20+
{
21+
[Test]
22+
public void Does_merge_params_correctly()
23+
{
24+
using (var db = OpenDbConnection())
25+
{
26+
db.DropAndCreateTable<Record>();
27+
28+
db.InsertAll(new[] {
29+
new Record { Id = 1, Value = 1, },
30+
});
31+
32+
var x1 = db.SingleById<Record>(1);
33+
var q = db.From<Record>();
34+
q.Where(x => x.Value == 1);
35+
q.Where(x => x.Value == 1);
36+
q.Where(x => x.Value == 1);
37+
q.Where(x => x.Value == 1);
38+
q.Where(x => x.Value == 1);
39+
q.Where(x => x.Value == 1);
40+
q.Where(x => x.Value == 1);
41+
q.Where(x => x.Value == 1);
42+
43+
var qIn = db.From<Record>();
44+
qIn.Where(x => x.Value == 1);
45+
qIn.Where(x => x.Value == 1 && x.Value == 1);
46+
q.Where(x => Sql.In(x.Value, qIn));
47+
var expression = q.ToSelectStatement();
48+
expression.Print();
49+
50+
for (var i = 0; i < 11; i++)
51+
{
52+
Assert.That(expression, Does.Contain(db.GetDialectProvider().ParamString + i));
53+
}
54+
}
55+
}
56+
}
57+
}

tests/ServiceStack.OrmLite.Tests/ServiceStack.OrmLite.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
<Compile Include="Expression\SqlExpressionJoinTests.cs" />
146146
<Compile Include="Issues\JoinAliasWithSchemaIssue.cs" />
147147
<Compile Include="Issues\LoadReferencesNullReferenceIssue.cs" />
148+
<Compile Include="Issues\MergeParamsIssue.cs" />
148149
<Compile Include="UseCase\CustomerOrdersUseCaseAsync.cs" />
149150
<Compile Include="AutoQueryTests.cs" />
150151
<Compile Include="CaptureSqlCommandFilterTests.cs" />

0 commit comments

Comments
 (0)