Skip to content

Commit 9e4257c

Browse files
committed
Preparing port of QueryPlan.
SVN: trunk@3046
1 parent 976fad9 commit 9e4257c

8 files changed

+332
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using NHibernate.Type;
3+
4+
namespace NHibernate.Engine.Query
5+
{
6+
/// <summary> Descriptor regarding a named parameter. </summary>
7+
[Serializable]
8+
public class NamedParameterDescriptor
9+
{
10+
private readonly string name;
11+
private readonly IType expectedType;
12+
private readonly int[] sourceLocations;
13+
private readonly bool jpaStyle;
14+
15+
public NamedParameterDescriptor(string name, IType expectedType, int[] sourceLocations, bool jpaStyle)
16+
{
17+
this.name = name;
18+
this.expectedType = expectedType;
19+
this.sourceLocations = sourceLocations;
20+
this.jpaStyle = jpaStyle;
21+
}
22+
23+
public string Name
24+
{
25+
get { return name; }
26+
}
27+
28+
public IType ExpectedType
29+
{
30+
get { return expectedType; }
31+
}
32+
33+
public int[] SourceLocations
34+
{
35+
get { return sourceLocations; }
36+
}
37+
38+
/// <summary>
39+
/// Not supported yet (AST parse needed)
40+
/// </summary>
41+
public bool JpaStyle
42+
{
43+
get { return jpaStyle; }
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using NHibernate.Type;
3+
4+
namespace NHibernate.Engine.Query
5+
{
6+
[Serializable]
7+
public class OrdinalParameterDescriptor
8+
{
9+
private readonly int ordinalPosition;
10+
private readonly IType expectedType;
11+
private readonly int sourceLocation;
12+
13+
public OrdinalParameterDescriptor(int ordinalPosition, IType expectedType, int sourceLocation)
14+
{
15+
this.ordinalPosition = ordinalPosition;
16+
this.expectedType = expectedType;
17+
this.sourceLocation = sourceLocation;
18+
}
19+
20+
public int OrdinalPosition
21+
{
22+
get { return ordinalPosition; }
23+
}
24+
25+
public IType ExpectedType
26+
{
27+
get { return expectedType; }
28+
}
29+
30+
public int SourceLocation
31+
{
32+
get { return sourceLocation; }
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Engine.Query
4+
{
5+
/// <summary>
6+
/// Implements a parameter parser recognizer specifically for the purpose
7+
/// of journaling parameter locations.
8+
/// </summary>
9+
public class ParamLocationRecognizer : ParameterParser.IRecognizer
10+
{
11+
private readonly Dictionary<string, NamedParameterDescription> namedParameterDescriptions =
12+
new Dictionary<string, NamedParameterDescription>();
13+
14+
private readonly List<int> ordinalParameterLocationList = new List<int>();
15+
16+
/// <summary>
17+
/// Convenience method for creating a param location recognizer and
18+
/// initiating the parse.
19+
/// </summary>
20+
/// <param name="query">The query to be parsed for parameter locations. </param>
21+
/// <returns> The generated recognizer, with journaled location info. </returns>
22+
public static ParamLocationRecognizer parseLocations(string query)
23+
{
24+
ParamLocationRecognizer recognizer = new ParamLocationRecognizer();
25+
ParameterParser.Parse(query, recognizer);
26+
return recognizer;
27+
}
28+
29+
/// <summary>
30+
/// The dictionary of named parameter locations.
31+
/// The dictionary is keyed by parameter name.
32+
/// </summary>
33+
public IDictionary<string, NamedParameterDescription> NamedParameterDescriptionMap
34+
{
35+
get { return namedParameterDescriptions; }
36+
}
37+
38+
/// <summary>
39+
/// The list of ordinal parameter locations.
40+
/// </summary>
41+
/// <remarks>
42+
/// The list elements are integers, representing the location for that given ordinal.
43+
/// Thus OrdinalParameterLocationList[n] represents the location for the nth parameter.
44+
/// </remarks>
45+
public List<int> OrdinalParameterLocationList
46+
{
47+
get { return ordinalParameterLocationList; }
48+
}
49+
50+
#region IRecognizer Members
51+
52+
public void OutParameter(int position)
53+
{
54+
// don't care...
55+
}
56+
57+
public void OrdinalParameter(int position)
58+
{
59+
ordinalParameterLocationList.Add(position);
60+
}
61+
62+
public void NamedParameter(string name, int position)
63+
{
64+
GetOrBuildNamedParameterDescription(name, false).Add(position);
65+
}
66+
67+
public void Ejb3PositionalParameter(string name, int position)
68+
{
69+
GetOrBuildNamedParameterDescription(name, true).Add(position);
70+
}
71+
72+
public void Other(char character)
73+
{
74+
// don't care...
75+
}
76+
77+
private NamedParameterDescription GetOrBuildNamedParameterDescription(string name, bool jpa)
78+
{
79+
NamedParameterDescription desc = namedParameterDescriptions[name];
80+
if (desc == null)
81+
{
82+
desc = new NamedParameterDescription(jpa);
83+
namedParameterDescriptions[name] = desc;
84+
}
85+
return desc;
86+
}
87+
#endregion
88+
89+
public class NamedParameterDescription
90+
{
91+
private readonly bool jpaStyle;
92+
private readonly List<int> positions = new List<int>();
93+
94+
public NamedParameterDescription(bool jpaStyle)
95+
{
96+
this.jpaStyle = jpaStyle;
97+
}
98+
99+
internal void Add(int position)
100+
{
101+
positions.Add(position);
102+
}
103+
104+
public int[] BuildPositionsArray()
105+
{
106+
return positions.ToArray();
107+
}
108+
109+
public bool JpaStyle
110+
{
111+
get { return jpaStyle; }
112+
}
113+
}
114+
115+
}
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Iesi.Collections.Generic;
4+
using NHibernate.Type;
5+
6+
namespace NHibernate.Engine.Query
7+
{
8+
/// <summary> Encapsulates metadata about parameters encountered within a query. </summary>
9+
[Serializable]
10+
public class ParameterMetadata
11+
{
12+
private static readonly OrdinalParameterDescriptor[] EmptyOrdinals = new OrdinalParameterDescriptor[0];
13+
private readonly OrdinalParameterDescriptor[] ordinalDescriptors;
14+
private readonly Dictionary<string, NamedParameterDescriptor> namedDescriptorMap;
15+
16+
public ParameterMetadata(OrdinalParameterDescriptor[] ordinalDescriptors,
17+
IDictionary<string, NamedParameterDescriptor> namedDescriptorMap)
18+
{
19+
if (ordinalDescriptors == null)
20+
{
21+
this.ordinalDescriptors = EmptyOrdinals;
22+
}
23+
else
24+
{
25+
OrdinalParameterDescriptor[] copy = new OrdinalParameterDescriptor[ordinalDescriptors.Length];
26+
Array.Copy(ordinalDescriptors, 0, copy, 0, ordinalDescriptors.Length);
27+
this.ordinalDescriptors = copy;
28+
}
29+
30+
if (namedDescriptorMap == null)
31+
this.namedDescriptorMap = new Dictionary<string, NamedParameterDescriptor>();
32+
else
33+
this.namedDescriptorMap = new Dictionary<string, NamedParameterDescriptor>(namedDescriptorMap);
34+
}
35+
36+
public int OrdinalParameterCount
37+
{
38+
get { return ordinalDescriptors.Length; }
39+
}
40+
41+
public ISet<string> NamedParameterNames
42+
{
43+
get { return new HashedSet<string>(namedDescriptorMap.Keys); }
44+
}
45+
46+
public OrdinalParameterDescriptor GetOrdinalParameterDescriptor(int position)
47+
{
48+
if (position < 1 || position > ordinalDescriptors.Length)
49+
{
50+
throw new IndexOutOfRangeException("Remember that ordinal parameters are 1-based!");
51+
}
52+
return ordinalDescriptors[position - 1];
53+
}
54+
55+
public IType GetOrdinalParameterExpectedType(int position)
56+
{
57+
return GetOrdinalParameterDescriptor(position).ExpectedType;
58+
}
59+
60+
public int GetOrdinalParameterSourceLocation(int position)
61+
{
62+
return GetOrdinalParameterDescriptor(position).SourceLocation;
63+
}
64+
65+
public NamedParameterDescriptor GetNamedParameterDescriptor(string name)
66+
{
67+
NamedParameterDescriptor meta;
68+
namedDescriptorMap.TryGetValue(name, out meta);
69+
if (meta == null)
70+
throw new QueryParameterException("could not locate named parameter [" + name + "]");
71+
72+
return meta;
73+
}
74+
75+
public IType GetNamedParameterExpectedType(string name)
76+
{
77+
return GetNamedParameterDescriptor(name).ExpectedType;
78+
}
79+
80+
public int[] GetNamedParameterSourceLocations(string name)
81+
{
82+
return GetNamedParameterDescriptor(name).SourceLocations;
83+
}
84+
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using NHibernate.Type;
3+
4+
namespace NHibernate.Engine.Query
5+
{
6+
[Serializable]
7+
public class ReturnMetadata
8+
{
9+
private readonly string[] returnAliases;
10+
private readonly IType[] returnTypes;
11+
12+
public ReturnMetadata(string[] returnAliases, IType[] returnTypes)
13+
{
14+
this.returnAliases = returnAliases;
15+
this.returnTypes = returnTypes;
16+
}
17+
18+
public string[] ReturnAliases
19+
{
20+
get { return returnAliases; }
21+
}
22+
23+
public IType[] ReturnTypes
24+
{
25+
get { return returnTypes; }
26+
}
27+
}
28+
}

src/NHibernate/Engine/Query/Sql/NativeSQLQueryNonScalarReturn.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected internal NativeSQLQueryNonScalarReturn(string alias, IDictionary prope
3030

3131
if (propertyResults != null)
3232
{
33-
CollectionHelper.PutAll(this.propertyResults, propertyResults);
33+
ArrayHelper.AddAll(this.propertyResults, propertyResults);
3434
}
3535
}
3636

src/NHibernate/NHibernate-2.0.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,11 @@
597597
<Compile Include="Engine\Loading\LoadContexts.cs" />
598598
<Compile Include="Engine\Loading\LoadingCollectionEntry.cs" />
599599
<Compile Include="Engine\Nullability.cs" />
600+
<Compile Include="Engine\Query\NamedParameterDescriptor.cs" />
601+
<Compile Include="Engine\Query\OrdinalParameterDescriptor.cs" />
602+
<Compile Include="Engine\Query\ParameterMetadata.cs" />
603+
<Compile Include="Engine\Query\ParamLocationRecognizer.cs" />
604+
<Compile Include="Engine\Query\ReturnMetadata.cs" />
600605
<Compile Include="Engine\Query\Sql\INativeSQLQueryReturn.cs" />
601606
<Compile Include="Engine\Query\Sql\NativeSQLQueryCollectionReturn.cs" />
602607
<Compile Include="Engine\Query\Sql\NativeSQLQueryJoinReturn.cs" />
@@ -705,6 +710,7 @@
705710
<Compile Include="IStatelessSession.cs" />
706711
<Compile Include="Mapping\PropertyGeneration.cs" />
707712
<Compile Include="Proxy\IEntityNotFoundDelegate.cs" />
713+
<Compile Include="QueryParameterException.cs" />
708714
<Compile Include="SessionException.cs" />
709715
<Compile Include="SqlCommand\SubselectClauseExtractor.cs" />
710716
<Compile Include="Engine\SubselectFetch.cs" />
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
4+
namespace NHibernate
5+
{
6+
[Serializable]
7+
public class QueryParameterException : QueryException
8+
{
9+
// TODO : whitout default constructor can't be serialized
10+
public QueryParameterException(string message) : base(message) { }
11+
public QueryParameterException(string message, Exception inner) : base(message, inner) { }
12+
protected QueryParameterException(SerializationInfo info,StreamingContext context): base(info, context) { }
13+
}
14+
}

0 commit comments

Comments
 (0)