-
-
Notifications
You must be signed in to change notification settings - Fork 524
Expand file tree
/
Copy pathPostgresCompiler.cs
More file actions
121 lines (95 loc) · 3.1 KB
/
PostgresCompiler.cs
File metadata and controls
121 lines (95 loc) · 3.1 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
using System;
using System.Linq;
namespace SqlKata.Compilers
{
public class PostgresCompiler : Compiler
{
public PostgresCompiler()
{
LastId = "SELECT lastval() AS id";
}
public override string EngineCode { get; } = EngineCodes.PostgreSql;
public override bool SupportsFilterClause { get; set; } = true;
protected override string CompileBasicStringCondition(SqlResult ctx, BasicStringCondition x)
{
var column = Wrap(x.Column);
var value = Resolve(ctx, x.Value) as string;
if (value == null)
{
throw new ArgumentException("Expecting a non nullable string");
}
var method = x.Operator;
if (new[] { "starts", "ends", "contains", "like", "ilike" }.Contains(x.Operator))
{
method = x.CaseSensitive ? "LIKE" : "ILIKE";
switch (x.Operator)
{
case "starts":
value = $"{value}%";
break;
case "ends":
value = $"%{value}";
break;
case "contains":
value = $"%{value}%";
break;
}
}
string sql;
if (x.Value is UnsafeLiteral)
{
sql = $"{column} {checkOperator(method)} {value}";
}
else
{
sql = $"{column} {checkOperator(method)} {Parameter(ctx, value)}";
}
if (!string.IsNullOrEmpty(x.EscapeCharacter))
{
sql = $"{sql} ESCAPE '{x.EscapeCharacter}'";
}
return x.IsNot ? $"NOT ({sql})" : sql;
}
protected override string CompileBasicDateSelect(SqlResult ctx, DateQueryColumn x)
{
var column = Wrap(x.Column);
string left;
if (x.Part == "time")
{
left = $"{column}::time";
}
else if (x.Part == "date")
{
left = $"{column}::date";
}
else
{
left = $"DATE_PART('{x.Part.ToUpperInvariant()}', {column})";
}
return left;
}
protected override string CompileBasicDateCondition(SqlResult ctx, BasicDateCondition condition)
{
var column = Wrap(condition.Column);
string left;
if (condition.Part == "time")
{
left = $"{column}::time";
}
else if (condition.Part == "date")
{
left = $"{column}::date";
}
else
{
left = $"DATE_PART('{condition.Part.ToUpperInvariant()}', {column})";
}
var sql = $"{left} {condition.Operator} {Parameter(ctx, condition.Value)}";
if (condition.IsNot)
{
return $"NOT ({sql})";
}
return sql;
}
}
}