-
Notifications
You must be signed in to change notification settings - Fork 217
/
PgAllExpression.cs
137 lines (119 loc) · 4.69 KB
/
PgAllExpression.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
134
135
136
137
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions.Internal;
/// <summary>
/// Represents a PostgreSQL array ALL expression.
/// </summary>
/// <remarks>
/// See https://www.postgresql.org/docs/current/static/functions-comparisons.html
/// </remarks>
public class PgAllExpression : SqlExpression, IEquatable<PgAllExpression>
{
private static ConstructorInfo? _quotingConstructor;
/// <inheritdoc />
public override Type Type
=> typeof(bool);
/// <summary>
/// The value to test against the <see cref="Array" />.
/// </summary>
public virtual SqlExpression Item { get; }
/// <summary>
/// The array of values or patterns to test for the <see cref="Item" />.
/// </summary>
public virtual SqlExpression Array { get; }
/// <summary>
/// The operator.
/// </summary>
public virtual PgAllOperatorType OperatorType { get; }
/// <summary>
/// Constructs a <see cref="PgAllExpression" />.
/// </summary>
/// <param name="operatorType">The operator symbol to the array expression.</param>
/// <param name="item">The value to find.</param>
/// <param name="array">The array to search.</param>
/// <param name="typeMapping">The type mapping for the expression.</param>
public PgAllExpression(
SqlExpression item,
SqlExpression array,
PgAllOperatorType operatorType,
RelationalTypeMapping? typeMapping)
: base(typeof(bool), typeMapping)
{
if (array.Type.TryGetElementType(typeof(IEnumerable<>)) is null)
{
throw new ArgumentException("Array expression must be an IEnumerable", nameof(array));
}
Item = item;
Array = array;
OperatorType = operatorType;
}
/// <summary>
/// Creates a new expression that is like this one, but using the supplied children. If all of the children are the same, it will
/// return this expression.
/// </summary>
/// <param name="item">The <see cref="Item" /> property of the result.</param>
/// <param name="array">The <see cref="Array" /> property of the result.</param>
/// <returns>This expression if no children changed, or an expression with the updated children.</returns>
public virtual PgAllExpression Update(SqlExpression item, SqlExpression array)
=> item != Item || array != Array
? new PgAllExpression(item, array, OperatorType, TypeMapping)
: this;
/// <inheritdoc />
public override Expression Quote()
=> New(
_quotingConstructor ??= typeof(PgAllExpression).GetConstructor(
[typeof(SqlExpression), typeof(SqlExpression), typeof(PgAllOperatorType), typeof(RelationalTypeMapping)])!,
Item.Quote(),
Array.Quote(),
Constant(OperatorType),
RelationalExpressionQuotingUtilities.QuoteTypeMapping(TypeMapping));
/// <inheritdoc />
protected override Expression VisitChildren(ExpressionVisitor visitor)
=> Update((SqlExpression)visitor.Visit(Item), (SqlExpression)visitor.Visit(Array));
/// <inheritdoc />
public override bool Equals(object? obj)
=> obj is PgAllExpression e && Equals(e);
/// <inheritdoc />
public virtual bool Equals(PgAllExpression? other)
=> ReferenceEquals(this, other)
|| other is not null
&& base.Equals(other)
&& Item.Equals(other.Item)
&& Array.Equals(other.Array)
&& OperatorType.Equals(other.OperatorType);
/// <inheritdoc />
public override int GetHashCode()
=> HashCode.Combine(base.GetHashCode(), Item, Array, OperatorType);
/// <inheritdoc />
protected override void Print(ExpressionPrinter expressionPrinter)
{
expressionPrinter.Visit(Item);
expressionPrinter
.Append(" ")
.Append(
OperatorType switch
{
PgAllOperatorType.Like => "LIKE",
PgAllOperatorType.ILike => "ILIKE",
_ => throw new ArgumentOutOfRangeException($"Unhandled operator type: {OperatorType}")
})
.Append(" ALL(");
expressionPrinter.Visit(Array);
expressionPrinter.Append(")");
}
/// <inheritdoc />
public override string ToString()
=> $"{Item} {OperatorType} ALL({Array})";
}
/// <summary>
/// Determines the operator type for a <see cref="PgAllExpression" />.
/// </summary>
public enum PgAllOperatorType
{
/// <summary>
/// Represents a PostgreSQL LIKE ALL operator.
/// </summary>
Like,
/// <summary>
/// Represents a PostgreSQL ILIKE ALL operator.
/// </summary>
ILike,
}