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
119 lines (100 loc) · 6.06 KB
/
ExpressionFormatterBase.cs
File metadata and controls
119 lines (100 loc) · 6.06 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
using System;
using System.Collections;
using System.Collections.Generic;
namespace Simple.Data.Ado
{
abstract class ExpressionFormatterBase : IExpressionFormatter
{
private readonly Dictionary<SimpleExpressionType, Func<SimpleExpression, string>> _expressionFormatters;
protected ExpressionFormatterBase()
{
_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, ">")},
{SimpleExpressionType.GreaterThanOrEqual, expr => BinaryExpressionToWhereClause(expr, ">=")},
{SimpleExpressionType.LessThan, expr => BinaryExpressionToWhereClause(expr, "<")},
{SimpleExpressionType.LessThanOrEqual, expr => BinaryExpressionToWhereClause(expr, "<=")},
{SimpleExpressionType.Empty, expr => string.Empty },
};
}
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} IS NULL", FormatObject(expression.LeftOperand, null));
if (CommonTypes.Contains(expression.RightOperand.GetType())) return FormatAsComparison(expression, "=");
return FormatAsComparison(expression, "=");
}
private string NotEqualExpressionToWhereClause(SimpleExpression expression)
{
if (expression.RightOperand == null) return string.Format("{0} IS NOT NULL", FormatObject(expression.LeftOperand, null));
if (CommonTypes.Contains(expression.RightOperand.GetType())) return FormatAsComparison(expression, "!=");
return FormatAsComparison(expression, "!=");
}
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 TryFormatAsInList(SimpleExpression expression, IEnumerable list, string op)
{
return (list != null)
?
string.Format("{0} {1} {2}", FormatObject(expression.LeftOperand, expression.RightOperand), op, FormatList(list, expression.LeftOperand))
:
null;
}
private string TryFormatAsRange(SimpleExpression expression, IRange range, string op)
{
return (range != null)
?
string.Format("{0} {1} {2}", FormatObject(expression.LeftOperand, expression.RightOperand), op, FormatRange(range, expression.LeftOperand))
:
null;
}
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} LIKE {1}", FormatObject(expression.LeftOperand, expression.RightOperand),
FormatObject(function.Args[0], expression.LeftOperand));
}
if (function.Name.Equals("notlike", StringComparison.InvariantCultureIgnoreCase))
{
return string.Format("{0} NOT LIKE {1}", FormatObject(expression.LeftOperand, expression.RightOperand),
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);
}
}