forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsensitiveLikeExpression.cs
126 lines (110 loc) · 3.71 KB
/
InsensitiveLikeExpression.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
using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Dialect;
using NHibernate.Engine;
using NHibernate.SqlCommand;
namespace NHibernate.Criterion
{
/// <summary>
/// An <see cref="ICriterion"/> that represents an "like" constraint
/// that is <b>not</b> case sensitive.
/// </summary>
//TODO:H2.0.3 renamed this to ILikeExpression
[Serializable]
public class InsensitiveLikeExpression : AbstractCriterion
{
private readonly string propertyName;
private readonly object value;
private readonly IProjection projection;
/// <summary>
/// Initializes a new instance of the <see cref="InsensitiveLikeExpression"/> class.
/// </summary>
/// <param name="projection">The projection.</param>
/// <param name="value">The value.</param>
/// <param name="matchMode">The match mode.</param>
public InsensitiveLikeExpression(IProjection projection, string value, MatchMode matchMode)
{
this.projection = projection;
this.value = matchMode.ToMatchString(value);
}
/// <summary>
/// Initializes a new instance of the <see cref="InsensitiveLikeExpression"/> class.
/// </summary>
/// <param name="projection">The projection.</param>
/// <param name="value">The value.</param>
public InsensitiveLikeExpression(IProjection projection, object value)
{
this.projection = projection;
this.value = value;
}
/// <summary>
/// Initialize a new instance of the <see cref="InsensitiveLikeExpression" />
/// class for a named Property and its value.
/// </summary>
/// <param name="propertyName">The name of the Property in the class.</param>
/// <param name="value">The value for the Property.</param>
public InsensitiveLikeExpression(string propertyName, object value)
{
this.propertyName = propertyName;
this.value = value;
}
public InsensitiveLikeExpression(string propertyName, string value, MatchMode matchMode)
: this(propertyName, matchMode.ToMatchString(value))
{
}
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
//TODO: add default capacity
SqlStringBuilder sqlBuilder = new SqlStringBuilder();
var columnNames =
CriterionUtil.GetColumnNamesAsSqlStringParts(propertyName, projection, criteriaQuery, criteria);
if (columnNames.Length != 1)
{
throw new HibernateException("insensitive like may only be used with single-column properties");
}
if (criteriaQuery.Factory.Dialect is PostgreSQLDialect)
{
sqlBuilder.AddObject(columnNames[0]);
sqlBuilder.Add(" ilike ");
}
else
{
sqlBuilder.Add(criteriaQuery.Factory.Dialect.LowercaseFunction)
.Add("(")
.AddObject(columnNames[0])
.Add(")")
.Add(" like ");
}
sqlBuilder.Add(criteriaQuery.NewQueryParameter(GetParameterTypedValue(criteria, criteriaQuery)).Single());
return sqlBuilder.ToSqlString();
}
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
List<TypedValue> typedValues = new List<TypedValue>();
if (projection != null)
{
typedValues.AddRange(projection.GetTypedValues(criteria, criteriaQuery));
}
typedValues.Add(GetParameterTypedValue(criteria, criteriaQuery));
return typedValues.ToArray();
}
public TypedValue GetParameterTypedValue(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var matchValue = value.ToString().ToLower();
return CriterionUtil.GetTypedValue(criteriaQuery, criteria, projection, propertyName, matchValue);
}
public override IProjection[] GetProjections()
{
if (projection != null)
{
return new IProjection[] { projection };
}
return null;
}
public override string ToString()
{
return (projection ?? (object)propertyName) + " ilike " + value;
}
}
}