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

Commit 2ceb9eb

Browse files
committed
Support looking up fieldDef by customized ParamName
1 parent bcf9c1b commit 2ceb9eb

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

src/ServiceStack.OrmLite/ModelDefinition.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,22 @@ public FieldDefinition GetFieldDefinition(string fieldName)
133133
return null;
134134
}
135135

136+
public FieldDefinition GetFieldDefinition(Func<string, bool> predicate)
137+
{
138+
foreach (var f in FieldDefinitionsWithAliases)
139+
{
140+
if (predicate(f.Alias))
141+
return f;
142+
}
143+
foreach (var f in FieldDefinitionsArray)
144+
{
145+
if (predicate(f.Name))
146+
return f;
147+
}
148+
149+
return null;
150+
}
151+
136152
public void AfterInit()
137153
{
138154
FieldDefinitionsArray = FieldDefinitions.ToArray();

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,16 @@ public virtual void SetParameterValues<T>(IDbCommand dbCmd, object obj)
873873
fieldMap.TryGetValue(fieldName, out fieldDef);
874874

875875
if (fieldDef == null)
876-
throw new ArgumentException("Field Definition '{0}' was not found".Fmt(fieldName));
876+
{
877+
if (OrmLiteConfig.ParamNameFilter != null)
878+
{
879+
fieldDef = modelDef.GetFieldDefinition(name =>
880+
string.Equals(OrmLiteConfig.ParamNameFilter(name), fieldName, StringComparison.OrdinalIgnoreCase));
881+
}
882+
883+
if (fieldDef == null)
884+
throw new ArgumentException("Field Definition '{0}' was not found".Fmt(fieldName));
885+
}
877886

878887
SetParameterValue<T>(fieldDef, p, obj);
879888
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using NUnit.Framework;
2+
using ServiceStack.DataAnnotations;
3+
using ServiceStack.Logging;
4+
5+
namespace ServiceStack.OrmLite.Tests.Issues
6+
{
7+
public class LegacyRow
8+
{
9+
[AutoIncrement]
10+
public int ID { get; set; }
11+
12+
public string Name { get; set; }
13+
14+
[Alias("Age-In-Years")]
15+
public int Age { get; set; }
16+
}
17+
18+
19+
[TestFixture]
20+
public class ParamNameIssues : OrmLiteTestBase
21+
{
22+
[Test]
23+
public void Does_use_ParamName_filter()
24+
{
25+
LogManager.LogFactory = new ConsoleLogFactory();
26+
27+
OrmLiteConfig.ParamNameFilter = name => name.Replace("-", "");
28+
29+
using (var db = OpenDbConnection())
30+
{
31+
db.DropAndCreateTable<LegacyRow>();
32+
33+
db.InsertAll(new []
34+
{
35+
new LegacyRow { Name = "Row1", Age = 1 },
36+
new LegacyRow { Name = "Row2", Age = 2 },
37+
});
38+
39+
var rows = db.Select<LegacyRow>();
40+
Assert.That(rows.Count, Is.EqualTo(2));
41+
}
42+
43+
OrmLiteConfig.ParamNameFilter = null;
44+
}
45+
}
46+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
<Compile Include="Issues\JsonUpdateOnlyIssue.cs" />
145145
<Compile Include="Issues\LoadReferencesNullReferenceIssue.cs" />
146146
<Compile Include="Issues\MergeParamsIssue.cs" />
147+
<Compile Include="Issues\ParamNameIssues.cs" />
147148
<Compile Include="MultipleConnectionIdTests.cs" />
148149
<Compile Include="UseCase\CustomerOrdersUseCaseAsync.cs" />
149150
<Compile Include="AutoQueryTests.cs" />

0 commit comments

Comments
 (0)