-
-
Notifications
You must be signed in to change notification settings - Fork 524
Expand file tree
/
Copy pathSqliteCompiler.cs
More file actions
89 lines (74 loc) · 2.68 KB
/
SqliteCompiler.cs
File metadata and controls
89 lines (74 loc) · 2.68 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
using System.Collections.Generic;
namespace SqlKata.Compilers
{
public class SqliteCompiler : Compiler
{
public override string EngineCode { get; } = EngineCodes.Sqlite;
protected override string OpeningIdentifier { get; set; } = "\"";
protected override string ClosingIdentifier { get; set; } = "\"";
protected override string LastId { get; set; } = "select last_insert_rowid() as id";
public override bool SupportsFilterClause { get; set; } = true;
public override string CompileTrue()
{
return "1";
}
public override string CompileFalse()
{
return "0";
}
protected override string CompileBasicDateSelect(SqlResult ctx, DateQueryColumn x)
{
var column = Wrap(x.Column);
var formatMap = new Dictionary<string, string> {
{ "date", "%Y-%m-%d" },
{ "time", "%H:%M:%S" },
{ "year", "%Y" },
{ "month", "%m" },
{ "day", "%d" },
{ "hour", "%H" },
{ "minute", "%M" },
};
if (!formatMap.ContainsKey(x.Part))
{
return column;
}
var sql = $"strftime('{formatMap[x.Part]}', {column})";
return sql;
}
public override string CompileLimit(SqlResult ctx)
{
var limit = ctx.Query.GetLimit(EngineCode);
var offset = ctx.Query.GetOffset(EngineCode);
if (limit == 0 && offset > 0)
{
ctx.Bindings.Add(offset);
return $"LIMIT -1 OFFSET {parameterPlaceholder}";
}
return base.CompileLimit(ctx);
}
protected override string CompileBasicDateCondition(SqlResult ctx, BasicDateCondition condition)
{
var column = Wrap(condition.Column);
var value = Parameter(ctx, condition.Value);
var formatMap = new Dictionary<string, string> {
{ "date", "%Y-%m-%d" },
{ "time", "%H:%M:%S" },
{ "year", "%Y" },
{ "month", "%m" },
{ "day", "%d" },
{ "hour", "%H" },
{ "minute", "%M" },
};
if (!formatMap.ContainsKey(condition.Part))
{
return $"{column} {condition.Operator} {value}";
}
var sql = $"strftime('{formatMap[condition.Part]}', {column}) {condition.Operator} cast({value} as text)";
if (condition.IsNot)
{
return $"NOT ({sql})";
}
return sql;
}
}
}