forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionFormatterBase.cs
More file actions
133 lines (113 loc) · 6.98 KB
/
ExpressionFormatterBase.cs
File metadata and controls
133 lines (113 loc) · 6.98 KB
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 System.Collections.Generic;
namespace Simple.Data.Ado
{
using Schema;
abstract class ExpressionFormatterBase : IExpressionFormatter
{
private readonly Lazy<Operators> _operators;
private readonly Dictionary<SimpleExpressionType, Func<SimpleExpression, string>> _expressionFormatters;
protected ExpressionFormatterBase(Func<Operators> createOperators)
{
_operators = new Lazy<Operators>(createOperators);
_expressionFormatters = new Dictionary<SimpleExpressionType, Func<SimpleExpression, string>>
{
{SimpleExpressionType.And, LogicalExpressionToWhereClause},
{SimpleExpressionType.Or, LogicalExpressionToWhereClause},
{SimpleExpressionType.Equal, EqualExpressionToWhereClause},
{SimpleExpressionType.NotEqual, NotEqualExpressionToWhereClause},
{SimpleExpressionType.Function, FunctionExpressionToWhereClause},
{SimpleExpressionType.GreaterThan, expr => BinaryExpressionToWhereClause(expr, Operators.GreaterThan)},
{SimpleExpressionType.GreaterThanOrEqual, expr => BinaryExpressionToWhereClause(expr, Operators.GreaterThanOrEqual)},
{SimpleExpressionType.LessThan, expr => BinaryExpressionToWhereClause(expr, Operators.LessThan)},
{SimpleExpressionType.LessThanOrEqual, expr => BinaryExpressionToWhereClause(expr, Operators.LessThanOrEqual)},
{SimpleExpressionType.Empty, expr => string.Empty },
};
}
protected Operators Operators
{
get { return _operators.Value; }
}
public string Format(SimpleExpression expression)
{
Func<SimpleExpression, string> formatter;
if (_expressionFormatters.TryGetValue(expression.Type, out formatter))
{
return formatter(expression);
}
return string.Empty;
}
private string LogicalExpressionToWhereClause(SimpleExpression expression)
{
return string.Format("({0} {1} {2})",
Format((SimpleExpression)expression.LeftOperand),
expression.Type.ToString().ToUpperInvariant(),
Format((SimpleExpression)expression.RightOperand));
}
private string EqualExpressionToWhereClause(SimpleExpression expression)
{
if (expression.RightOperand == null) return string.Format("{0} {1}", FormatObject(expression.LeftOperand, null), Operators.IsNull);
if (CommonTypes.Contains(expression.RightOperand.GetType())) return FormatAsComparison(expression, Operators.Equal);
return FormatAsComparison(expression, Operators.Equal);
}
private string NotEqualExpressionToWhereClause(SimpleExpression expression)
{
if (expression.RightOperand == null) return string.Format("{0} {1}", FormatObject(expression.LeftOperand, null), Operators.IsNotNull);
if (CommonTypes.Contains(expression.RightOperand.GetType())) return FormatAsComparison(expression, Operators.NotEqual);
return FormatAsComparison(expression, Operators.NotEqual);
}
private string FormatAsComparison(SimpleExpression expression, string op)
{
return string.Format("{0} {1} {2}", FormatObject(expression.LeftOperand, expression.RightOperand), op,
FormatObject(expression.RightOperand, expression.LeftOperand));
}
private string FunctionExpressionToWhereClause(SimpleExpression expression)
{
var function = expression.RightOperand as SimpleFunction;
if (function == null) throw new InvalidOperationException("Expected SimpleFunction as the right operand.");
if (function.Name.Equals("like", StringComparison.InvariantCultureIgnoreCase))
{
return string.Format("{0} {1} {2}", FormatObject(expression.LeftOperand, expression.RightOperand), Operators.Like,
FormatObject(function.Args[0], expression.LeftOperand));
}
if (function.Name.Equals("notlike", StringComparison.InvariantCultureIgnoreCase))
{
return string.Format("{0} {1} {2}", FormatObject(expression.LeftOperand, expression.RightOperand), Operators.NotLike,
FormatObject(function.Args[0], expression.LeftOperand));
}
throw new NotSupportedException(string.Format("Unknown function '{0}'.", function.Name));
}
private string BinaryExpressionToWhereClause(SimpleExpression expression, string comparisonOperator)
{
return string.Format("{0} {1} {2}", FormatObject(expression.LeftOperand, expression.RightOperand),
comparisonOperator,
FormatObject(expression.RightOperand, expression.LeftOperand));
}
protected abstract string FormatObject(object value, object otherOperand);
protected abstract string FormatRange(IRange range, object otherOperand);
protected abstract string FormatList(IEnumerable list, object otherOperand);
}
public class Operators
{
public virtual string Equal { get { return "="; } }
public virtual string NotEqual { get { return "!="; } }
public virtual string GreaterThan { get { return ">"; } }
public virtual string GreaterThanOrEqual { get { return ">="; } }
public virtual string LessThan { get { return "<"; } }
public virtual string LessThanOrEqual { get { return "<="; } }
public virtual string IsNull { get { return "IS NULL"; } }
public virtual string IsNotNull { get { return "IS NOT NULL"; } }
public virtual string Like { get { return "LIKE"; } }
public virtual string NotLike { get { return "NOT LIKE"; } }
public virtual string In { get { return "IN"; } }
public virtual string NotIn { get { return "NOT IN"; } }
public virtual string Between { get { return "BETWEEN"; } }
public virtual string NotBetween { get { return "NOT BETWEEN"; } }
public virtual string Add { get { return "+"; } }
public virtual string Subtract { get { return "-"; } }
public virtual string Multiply { get { return "*"; } }
public virtual string Divide { get { return "/"; } }
public virtual string Modulo { get { return "%"; } }
}
}