forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicBatchingHelper.cs
76 lines (65 loc) · 1.88 KB
/
DynamicBatchingHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using NHibernate.Criterion;
using NHibernate.Engine;
using NHibernate.Param;
using NHibernate.SqlCommand;
using NHibernate.Type;
namespace NHibernate.Loader
{
internal class DynamicBatchingHelper
{
private static string BatchIdPlaceholder = "$$BATCH_ID_PLACEHOLDER$$";
public static SqlStringBuilder BuildBatchFetchRestrictionFragment()
{
return new SqlStringBuilder(1).Add(BatchIdPlaceholder);
}
public static SqlString ExpandBatchIdPlaceholder(
SqlString sqlString,
ISet<IParameterSpecification> specifications,
string[] columns,
IType[] types,
ISessionFactoryImplementor factory)
{
var parameters = GeneratePositionalParameters(specifications, types, factory);
var wherePart = InExpression.GetSqlString(columns, types.Length, parameters, factory.Dialect);
return sqlString.ReplaceLast(BatchIdPlaceholder, wherePart);
}
public static List<Parameter> GeneratePositionalParameters(
ISet<IParameterSpecification> specifications,
IType[] types,
ISessionFactoryImplementor factory)
{
var parameters = new List<Parameter>();
for (var i = 0; i < types.Length; i++)
{
var specification = new PositionalParameterSpecification(1, 0, i) { ExpectedType = types[i] };
foreach (var id in specification.GetIdsForBackTrack(factory))
{
var p = Parameter.Placeholder;
p.BackTrack = id;
parameters.Add(p);
}
specifications.Add(specification);
}
return parameters;
}
public static int GetIdsToLoad(object[] batch, out object[] idsToLoad)
{
int numberOfIds = Array.IndexOf(batch, null);
if (numberOfIds < 0)
{
idsToLoad = batch;
return batch.Length;
}
if (numberOfIds == 1)
{
idsToLoad = null;
return numberOfIds;
}
idsToLoad = new object[numberOfIds];
Array.Copy(batch, 0, idsToLoad, 0, numberOfIds);
return numberOfIds;
}
}
}