forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInExpression.cs
133 lines (111 loc) · 3.57 KB
/
InExpression.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.SqlTypes;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Expression
{
/// <summary>
/// An <see cref="ICriterion"/> that constrains the property
/// to a specified list of values.
/// </summary>
/// <remarks>
/// InExpression - should only be used with a Single Value column - no multicolumn properties...
/// </remarks>
public class InExpression : AbstractCriterion
{
private readonly string _propertyName;
private readonly object[ ] _values;
/// <summary>
///
/// </summary>
/// <param name="propertyName"></param>
/// <param name="values"></param>
public InExpression( string propertyName, object[ ] values )
{
_propertyName = propertyName;
_values = values;
}
private static Parameter[ ] GenerateValueParameters( string prefix, SqlType sqlType, int count )
{
Parameter[ ] parameters = new Parameter[count];
for( int i = 0; i < count; i++ )
{
string parameterName = StringHelper.Suffix( prefix, StringHelper.Underscore + i.ToString() );
parameters[ i ] = new Parameter( parameterName, sqlType );
}
return parameters;
}
public override SqlString ToSqlString( ISessionFactoryImplementor factory, System.Type persistentClass, string alias, IDictionary aliasClasses )
{
//TODO: add default capacity
SqlStringBuilder result = new SqlStringBuilder();
IType propertyType = AbstractCriterion.GetType( factory, persistentClass, _propertyName, aliasClasses );
SqlType[ ] columnSqlTypes = propertyType.SqlTypes( factory );
string[ ] columnNames = AbstractCriterion.GetColumns( factory, persistentClass, _propertyName, alias, aliasClasses );
// Generate SqlString of the form:
// columnName1 in (values) and columnName2 in (values) and ...
for( int columnIndex = 0; columnIndex < columnNames.Length; columnIndex++ )
{
string columnName = columnNames[ columnIndex ];
SqlType columnSqlType = columnSqlTypes[ columnIndex ];
if( columnIndex > 0 )
{
result.Add( " and " );
}
result
.Add( columnName )
.Add( " in (" );
if( _values.Length > 0 )
{
Parameter[ ] valueParameters = GenerateValueParameters( columnName, columnSqlType, _values.Length );
for( int i = 0; i < valueParameters.Length; i++ )
{
if( i > 0 )
{
result.Add( StringHelper.CommaSpace );
}
result.Add( valueParameters[ i ] );
}
}
result.Add( ")" );
}
return result.ToSqlString();
}
public override TypedValue[ ] GetTypedValues( ISessionFactoryImplementor sessionFactory, System.Type persistentClass, IDictionary aliasClasses )
{
ArrayList list = new ArrayList();
IType type = GetType( sessionFactory, persistentClass, _propertyName, aliasClasses );
if( type.IsComponentType )
{
IAbstractComponentType actype = ( IAbstractComponentType ) type;
IType[ ] types = actype.Subtypes;
for( int i = 0; i < types.Length; i++ )
{
for( int j = 0; j < _values.Length; j++ )
{
object subval = _values[ j ] == null ?
null :
actype.GetPropertyValues( _values[ j ] )[ i ];
list.Add( new TypedValue( types[ i ], subval ) );
}
}
}
else
{
for( int j = 0; j < _values.Length; j++ )
{
list.Add( new TypedValue( type, _values[ j ] ) );
}
}
return ( TypedValue[ ] ) list.ToArray( typeof( TypedValue ) );
}
/// <summary></summary>
public override string ToString()
{
return _propertyName + " in (" + StringHelper.ToString( _values ) + ')';
}
}
}