forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLikeExpression.cs
114 lines (96 loc) · 3.19 KB
/
LikeExpression.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
using System;
using System.Linq;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.Util;
namespace NHibernate.Criterion
{
/// <summary>
/// An <see cref="ICriterion"/> that represents an "like" constraint.
/// </summary>
/// <remarks>
/// The case sensitivity depends on the database settings for string
/// comparisons. Use <see cref="InsensitiveLikeExpression"/> if the
/// string comparison should not be case sensitive.
/// </remarks>
[Serializable]
public class LikeExpression : AbstractCriterion
{
private readonly string value;
private char? escapeChar;
private readonly bool ignoreCase;
private readonly IProjection projection;
private readonly TypedValue typedValue;
public LikeExpression(string propertyName, string value, char? escapeChar, bool ignoreCase)
{
this.projection = Projections.Property(propertyName);
this.value = value;
typedValue = new TypedValue(NHibernateUtil.String, this.value, false);
this.escapeChar = escapeChar;
this.ignoreCase = ignoreCase;
}
public LikeExpression(IProjection projection, string value, MatchMode matchMode)
{
this.projection = projection;
this.value = matchMode.ToMatchString(value);
typedValue = new TypedValue(NHibernateUtil.String, this.value, false);
}
public LikeExpression(string propertyName, string value)
: this(propertyName, value, null, false)
{
}
public LikeExpression(string propertyName, string value, MatchMode matchMode)
: this(propertyName, matchMode.ToMatchString(value))
{
}
public LikeExpression(string propertyName, string value, MatchMode matchMode, char? escapeChar, bool ignoreCase)
: this(propertyName, matchMode.ToMatchString(value), escapeChar, ignoreCase)
{
}
#region ICriterion Members
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var columns = CriterionUtil.GetColumnNamesAsSqlStringParts(projection, criteriaQuery, criteria);
if (columns.Length != 1)
throw new HibernateException("Like may only be used with single-column properties / projections.");
SqlStringBuilder lhs = new SqlStringBuilder(6);
if (ignoreCase)
{
Dialect.Dialect dialect = criteriaQuery.Factory.Dialect;
lhs.Add(dialect.LowercaseFunction)
.Add(StringHelper.OpenParen)
.AddObject(columns[0])
.Add(StringHelper.ClosedParen);
}
else
lhs.AddObject(columns[0]);
if (ignoreCase)
{
Dialect.Dialect dialect = criteriaQuery.Factory.Dialect;
lhs.Add(" like ")
.Add(dialect.LowercaseFunction)
.Add(StringHelper.OpenParen)
.Add(criteriaQuery.NewQueryParameter(typedValue).Single())
.Add(StringHelper.ClosedParen);
}
else
lhs.Add(" like ").Add(criteriaQuery.NewQueryParameter(typedValue).Single());
if (escapeChar.HasValue)
lhs.Add(" escape '" + escapeChar + "'");
return lhs.ToSqlString();
}
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
return new TypedValue[] { typedValue };
}
public override IProjection[] GetProjections()
{
return new IProjection[] { projection };
}
#endregion
public override string ToString()
{
return projection + " like " + value;
}
}
}