Skip to content

Commit 54cf317

Browse files
committed
Introduce dedicated QueryParameterBinding for TypeId
1 parent dcf9a01 commit 54cf317

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

Orm/Xtensive.Orm/Orm/Providers/CommandProcessing/CommandFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ public virtual CommandPart CreateQueryPart(IQueryRequest request, string paramet
152152
}
153153
configuration.DynamicFilterValues.Add(binding, filterValues);
154154
continue;
155+
case QueryParameterBindingType.TypeIdentifier: {
156+
var originalTypeId = ((QueryTypeIdentifierParameterBinding) binding).OriginalTypeId;
157+
parameterValue = Session.StorageNode.TypeIdRegistry[Session.Domain.Model.Types[originalTypeId]];
158+
break;
159+
}
155160
default:
156161
throw new ArgumentOutOfRangeException("binding.BindingType");
157162
}

Orm/Xtensive.Orm/Orm/Providers/Requests/QueryParameterBindingType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,10 @@ public enum QueryParameterBindingType
4747
/// <see cref="ParameterBinding.TypeMapping"/> is ignored in this case.
4848
/// </summary>
4949
RowFilter,
50+
/// <summary>
51+
/// Indicated that parameter is type indentifier.
52+
/// <see cref="QueryParameterBinding.ValueAccessor"/> is ignored in this case.
53+
/// </summary>
54+
TypeIdentifier,
5055
}
5156
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2022 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
6+
using System;
7+
using Xtensive.Core;
8+
using Xtensive.Sql;
9+
10+
namespace Xtensive.Orm.Providers
11+
{
12+
/// <summary>
13+
/// A special version of <see cref="QueryParameterBinding"/> used for type identifire values within query.
14+
/// </summary>
15+
public class QueryTypeIdentifierParameterBinding : QueryParameterBinding
16+
{
17+
private static Func<ParameterContext, object> DummyFactory = (c) => null;
18+
19+
/// <summary>
20+
/// Gets type indentifier of type in Domain model - <see cref="Xtensive.Orm.Model.TypeInfo.TypeId"/>.
21+
/// </summary>
22+
public int OriginalTypeId { get; private set; }
23+
24+
/// <summary>
25+
/// Creates Instance of this class;
26+
/// </summary>
27+
/// <param name="originalTypeId">Type indentifier of type in Domain model - <see cref="Xtensive.Orm.Model.TypeInfo.TypeId"/></param>
28+
/// <param name="typeMapping">Type mapping for type of type idetifier.</param>
29+
public QueryTypeIdentifierParameterBinding(int originalTypeId, TypeMapping typeMapping)
30+
: base(typeMapping, DummyFactory, QueryParameterBindingType.TypeIdentifier)
31+
{
32+
OriginalTypeId = originalTypeId;
33+
}
34+
}
35+
}

Orm/Xtensive.Orm/Orm/Providers/SqlCompiler.Index.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ public QueryAndBindings(SqlSelect initialQuery, List<QueryParameterBinding> bind
4141
}
4242
}
4343

44-
private static readonly ConcurrentDictionary<int, Func<ParameterContext, object>> typeIdParameterAccessorCache =
45-
new ConcurrentDictionary<int, Func<ParameterContext, object>>();
46-
47-
private static readonly Func<int, Func<ParameterContext, object>> AccessorFactory = typeId => _ => typeId;
48-
4944
private TypeMapping int32TypeMapping;
5045

5146
protected override SqlProvider VisitFreeText(FreeTextProvider provider)
@@ -259,14 +254,14 @@ private QueryAndBindings BuildFilteredQuery(IndexInfo index)
259254
}
260255
else {
261256
if (filterByTypes.Count == 1) {
262-
var binding = CreateQueryParameterBinding(type);
257+
var binding = CreateTypeIdentifierBinding(type);
263258
bindings.Add(binding);
264259
filter = typeIdColumn == binding.ParameterReference;
265260
}
266261
else {
267262
var typeIdParameters = filterByTypes
268263
.Select(t => {
269-
var binding = CreateQueryParameterBinding(t);
264+
var binding = CreateTypeIdentifierBinding(t);
270265
bindings.Add(binding);
271266
return binding.ParameterReference;
272267
})
@@ -312,7 +307,7 @@ private QueryAndBindings BuildTypedQuery(IndexInfo index)
312307

313308
SqlUserColumn typeIdColumn;
314309
if (useParameterForTypeId) {
315-
var binding = CreateQueryParameterBinding(type);
310+
var binding = CreateTypeIdentifierBinding(type);
316311

317312
typeIdColumn = SqlDml.Column(binding.ParameterReference);
318313
bindings.Add(binding);
@@ -363,11 +358,7 @@ private object GetDiscriminatorValue(TypeDiscriminatorMap discriminatorMap, obje
363358
: fieldValue;
364359
}
365360

366-
private QueryParameterBinding CreateQueryParameterBinding(TypeInfo type) =>
367-
new QueryParameterBinding(
368-
int32TypeMapping ??= Driver.GetTypeMapping(WellKnownTypes.Int32),
369-
typeIdParameterAccessorCache.GetOrAdd(TypeIdRegistry[type], AccessorFactory),
370-
QueryParameterBindingType.Regular
371-
);
361+
private QueryParameterBinding CreateTypeIdentifierBinding(TypeInfo type) =>
362+
new QueryTypeIdentifierParameterBinding(type.TypeId, int32TypeMapping ??= Driver.GetTypeMapping(WellKnownTypes.Int32));
372363
}
373364
}

0 commit comments

Comments
 (0)