Skip to content

Commit bfa39f5

Browse files
Merge branch 'master' of github.com:sqlkata/querybuilder
2 parents 056e613 + 1d049d3 commit bfa39f5

File tree

3 files changed

+312
-29
lines changed

3 files changed

+312
-29
lines changed
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
using SqlKata.Compilers;
2+
using SqlKata.Tests.Infrastructure;
3+
using Xunit;
4+
5+
namespace SqlKata.Tests.Oracle
6+
{
7+
public class OracleDateConditionTests : TestSupport
8+
{
9+
private const string TableName = "Table";
10+
private const string SqlPlaceholder = "GENERATED_SQL";
11+
12+
private OracleCompiler compiler;
13+
14+
public OracleDateConditionTests()
15+
{
16+
compiler = Compilers.Get<OracleCompiler>(EngineCodes.Oracle);
17+
}
18+
19+
[Fact]
20+
public void SimpleWhereDateTest()
21+
{
22+
// Arrange:
23+
var query = new Query(TableName)
24+
.Select()
25+
.WhereDate("STAMP", "=", "2018-04-01");
26+
27+
// Act:
28+
var ctx = compiler.Compile(query);
29+
30+
// Assert:
31+
Assert.Equal($"SELECT * FROM \"{TableName}\" WHERE TO_CHAR(\"STAMP\", 'YY-MM-DD') = TO_CHAR(TO_DATE(?, 'YY-MM-DD'), 'YY-MM-DD')", ctx.RawSql);
32+
Assert.Equal("2018-04-01", ctx.Bindings[0]);
33+
Assert.Single(ctx.Bindings);
34+
}
35+
36+
[Fact]
37+
public void SimpleWhereDatePartDateTest()
38+
{
39+
// Arrange:
40+
var query = new Query(TableName)
41+
.Select()
42+
.WhereDatePart("date", "STAMP", "=", "2018-04-01");
43+
44+
// Act:
45+
var ctx = compiler.Compile(query);
46+
47+
// Assert:
48+
Assert.Equal($"SELECT * FROM \"{TableName}\" WHERE TO_CHAR(\"STAMP\", 'YY-MM-DD') = TO_CHAR(TO_DATE(?, 'YY-MM-DD'), 'YY-MM-DD')", ctx.RawSql);
49+
Assert.Equal("2018-04-01", ctx.Bindings[0]);
50+
Assert.Single(ctx.Bindings);
51+
}
52+
53+
[Fact]
54+
public void SimpleWhereTimeWithSecondsTest()
55+
{
56+
// Arrange:
57+
var query = new Query(TableName)
58+
.Select()
59+
.WhereTime("STAMP", "=", "19:01:10");
60+
61+
// Act:
62+
var ctx = compiler.Compile(query);
63+
64+
// Assert:
65+
Assert.Equal($"SELECT * FROM \"{TableName}\" WHERE TO_CHAR(\"STAMP\", 'HH24:MI:SS') = TO_CHAR(TO_DATE(?, 'HH24:MI:SS'), 'HH24:MI:SS')", ctx.RawSql);
66+
Assert.Equal("19:01:10", ctx.Bindings[0]);
67+
Assert.Single(ctx.Bindings);
68+
}
69+
70+
[Fact]
71+
public void SimpleWhereDatePartTimeWithSecondsTest()
72+
{
73+
// Arrange:
74+
var query = new Query(TableName)
75+
.Select()
76+
.WhereDatePart("time", "STAMP", "=", "19:01:10");
77+
78+
// Act:
79+
var ctx = compiler.Compile(query);
80+
81+
// Assert:
82+
Assert.Equal($"SELECT * FROM \"{TableName}\" WHERE TO_CHAR(\"STAMP\", 'HH24:MI:SS') = TO_CHAR(TO_DATE(?, 'HH24:MI:SS'), 'HH24:MI:SS')", ctx.RawSql);
83+
Assert.Equal("19:01:10", ctx.Bindings[0]);
84+
Assert.Single(ctx.Bindings);
85+
}
86+
87+
[Fact]
88+
public void SimpleWhereTimeWithoutSecondsTest()
89+
{
90+
// Arrange:
91+
var query = new Query(TableName)
92+
.Select()
93+
.WhereTime("STAMP", "=", "19:01");
94+
95+
// Act:
96+
var ctx = compiler.Compile(query);
97+
98+
// Assert:
99+
Assert.Equal($"SELECT * FROM \"{TableName}\" WHERE TO_CHAR(\"STAMP\", 'HH24:MI:SS') = TO_CHAR(TO_DATE(?, 'HH24:MI'), 'HH24:MI:SS')", ctx.RawSql);
100+
Assert.Equal("19:01", ctx.Bindings[0]);
101+
Assert.Single(ctx.Bindings);
102+
}
103+
104+
[Fact]
105+
public void SimpleWhereDatePartTimeWithoutSecondsTest()
106+
{
107+
// Arrange:
108+
var query = new Query(TableName)
109+
.Select()
110+
.WhereDatePart("time", "STAMP", "=", "19:01");
111+
112+
// Act:
113+
var ctx = compiler.Compile(query);
114+
115+
// Assert:
116+
Assert.Equal($"SELECT * FROM \"{TableName}\" WHERE TO_CHAR(\"STAMP\", 'HH24:MI:SS') = TO_CHAR(TO_DATE(?, 'HH24:MI'), 'HH24:MI:SS')", ctx.RawSql);
117+
Assert.Equal("19:01", ctx.Bindings[0]);
118+
Assert.Single(ctx.Bindings);
119+
}
120+
121+
[Fact]
122+
public void SimpleWhereDatePartYear()
123+
{
124+
// Arrange:
125+
var query = new Query(TableName)
126+
.Select()
127+
.WhereDatePart("year", "STAMP", "=", "2018");
128+
129+
// Act:
130+
var ctx = compiler.Compile(query);
131+
132+
// Assert:
133+
Assert.Equal($"SELECT * FROM \"{TableName}\" WHERE EXTRACT(YEAR FROM \"STAMP\") = ?", ctx.RawSql);
134+
Assert.Equal("2018", ctx.Bindings[0]);
135+
Assert.Single(ctx.Bindings);
136+
}
137+
138+
[Fact]
139+
public void SimpleWhereDatePartMonth()
140+
{
141+
// Arrange:
142+
var query = new Query(TableName)
143+
.Select()
144+
.WhereDatePart("month", "STAMP", "=", "9");
145+
146+
// Act:
147+
var ctx = compiler.Compile(query);
148+
149+
// Assert:
150+
Assert.Equal($"SELECT * FROM \"{TableName}\" WHERE EXTRACT(MONTH FROM \"STAMP\") = ?", ctx.RawSql);
151+
Assert.Equal("9", ctx.Bindings[0]);
152+
Assert.Single(ctx.Bindings);
153+
}
154+
155+
[Fact]
156+
public void SimpleWhereDatePartDay()
157+
{
158+
// Arrange:
159+
var query = new Query(TableName)
160+
.Select()
161+
.WhereDatePart("day", "STAMP", "=", "15");
162+
163+
// Act:
164+
var ctx = compiler.Compile(query);
165+
166+
// Assert:
167+
Assert.Equal($"SELECT * FROM \"{TableName}\" WHERE EXTRACT(DAY FROM \"STAMP\") = ?", ctx.RawSql);
168+
Assert.Equal("15", ctx.Bindings[0]);
169+
Assert.Single(ctx.Bindings);
170+
}
171+
172+
[Fact]
173+
public void SimpleWhereDatePartHour()
174+
{
175+
// Arrange:
176+
var query = new Query(TableName)
177+
.Select()
178+
.WhereDatePart("hour", "STAMP", "=", "15");
179+
180+
// Act:
181+
var ctx = compiler.Compile(query);
182+
183+
// Assert:
184+
Assert.Equal($"SELECT * FROM \"{TableName}\" WHERE EXTRACT(HOUR FROM \"STAMP\") = ?", ctx.RawSql);
185+
Assert.Equal("15", ctx.Bindings[0]);
186+
Assert.Single(ctx.Bindings);
187+
}
188+
189+
[Fact]
190+
public void SimpleWhereDatePartMinute()
191+
{
192+
// Arrange:
193+
var query = new Query(TableName)
194+
.Select()
195+
.WhereDatePart("minute", "STAMP", "=", "25");
196+
197+
// Act:
198+
var ctx = compiler.Compile(query);
199+
200+
// Assert:
201+
Assert.Equal($"SELECT * FROM \"{TableName}\" WHERE EXTRACT(MINUTE FROM \"STAMP\") = ?", ctx.RawSql);
202+
Assert.Equal("25", ctx.Bindings[0]);
203+
Assert.Single(ctx.Bindings);
204+
}
205+
206+
[Fact]
207+
public void SimpleWhereDatePartSecond()
208+
{
209+
// Arrange:
210+
var query = new Query(TableName)
211+
.Select()
212+
.WhereDatePart("second", "STAMP", "=", "59");
213+
214+
// Act:
215+
var ctx = compiler.Compile(query);
216+
217+
// Assert:
218+
Assert.Equal($"SELECT * FROM \"{TableName}\" WHERE EXTRACT(SECOND FROM \"STAMP\") = ?", ctx.RawSql);
219+
Assert.Equal("59", ctx.Bindings[0]);
220+
Assert.Single(ctx.Bindings);
221+
}
222+
}
223+
}

QueryBuilder/Compilers/OracleCompiler.cs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Diagnostics;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
24
using System.Linq;
35

46
namespace SqlKata.Compilers
@@ -93,5 +95,60 @@ internal void ApplyLegacyLimit(SqlResult ctx)
9395

9496
ctx.RawSql = newSql;
9597
}
98+
99+
protected override string CompileBasicDateCondition(SqlResult ctx, BasicDateCondition condition)
100+
{
101+
102+
var column = Wrap(condition.Column);
103+
var value = Parameter(ctx, condition.Value);
104+
105+
var sql = "";
106+
var valueFormat = "";
107+
108+
var isDateTime = (condition.Value is DateTime dt);
109+
110+
switch (condition.Part)
111+
{
112+
case "date": // assume YY-MM-DD format
113+
if (isDateTime)
114+
valueFormat = $"{value}";
115+
else
116+
valueFormat = $"TO_DATE({value}, 'YY-MM-DD')";
117+
sql = $"TO_CHAR({column}, 'YY-MM-DD') {condition.Operator} TO_CHAR({valueFormat}, 'YY-MM-DD')";
118+
break;
119+
case "time":
120+
if (isDateTime)
121+
valueFormat = $"{value}";
122+
else
123+
{
124+
// assume HH:MM format
125+
if (condition.Value.ToString().Split(':').Count() == 2)
126+
valueFormat = $"TO_DATE({value}, 'HH24:MI')";
127+
else // assume HH:MM:SS format
128+
valueFormat = $"TO_DATE({value}, 'HH24:MI:SS')";
129+
}
130+
sql = $"TO_CHAR({column}, 'HH24:MI:SS') {condition.Operator} TO_CHAR({valueFormat}, 'HH24:MI:SS')";
131+
break;
132+
case "year":
133+
case "month":
134+
case "day":
135+
case "hour":
136+
case "minute":
137+
case "second":
138+
sql = $"EXTRACT({condition.Part.ToUpper()} FROM {column}) {condition.Operator} {value}";
139+
break;
140+
default:
141+
sql = $"{column} {condition.Operator} {value}";
142+
break;
143+
}
144+
145+
if (condition.IsNot)
146+
{
147+
return $"NOT ({sql})";
148+
}
149+
150+
return sql;
151+
152+
}
96153
}
97154
}

sqlkata.sln

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QueryBuilder.Tests", "Query
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SqlKata.Execution", "SqlKata.Execution\SqlKata.Execution.csproj", "{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Program", "Program\Program.csproj", "{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Program", "Program\Program.csproj", "{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}"
1313
EndProject
1414
Global
1515
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -20,46 +20,43 @@ Global
2020
Release|x64 = Release|x64
2121
Release|x86 = Release|x86
2222
EndGlobalSection
23-
GlobalSection(SolutionProperties) = preSolution
24-
HideSolutionNode = FALSE
25-
EndGlobalSection
2623
GlobalSection(ProjectConfigurationPlatforms) = postSolution
2724
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2825
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
29-
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Debug|x64.ActiveCfg = Debug|x64
30-
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Debug|x64.Build.0 = Debug|x64
31-
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Debug|x86.ActiveCfg = Debug|x86
32-
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Debug|x86.Build.0 = Debug|x86
26+
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Debug|x64.ActiveCfg = Debug|Any CPU
27+
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Debug|x64.Build.0 = Debug|Any CPU
28+
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Debug|x86.ActiveCfg = Debug|Any CPU
29+
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Debug|x86.Build.0 = Debug|Any CPU
3330
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
3431
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Release|Any CPU.Build.0 = Release|Any CPU
35-
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Release|x64.ActiveCfg = Release|x64
36-
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Release|x64.Build.0 = Release|x64
37-
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Release|x86.ActiveCfg = Release|x86
38-
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Release|x86.Build.0 = Release|x86
32+
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Release|x64.ActiveCfg = Release|Any CPU
33+
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Release|x64.Build.0 = Release|Any CPU
34+
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Release|x86.ActiveCfg = Release|Any CPU
35+
{2D0657E1-7046-4B45-BAF3-90291BD74E0B}.Release|x86.Build.0 = Release|Any CPU
3936
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
4037
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Debug|Any CPU.Build.0 = Debug|Any CPU
41-
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Debug|x64.ActiveCfg = Debug|x64
42-
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Debug|x64.Build.0 = Debug|x64
43-
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Debug|x86.ActiveCfg = Debug|x86
44-
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Debug|x86.Build.0 = Debug|x86
38+
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Debug|x64.ActiveCfg = Debug|Any CPU
39+
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Debug|x64.Build.0 = Debug|Any CPU
40+
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Debug|x86.ActiveCfg = Debug|Any CPU
41+
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Debug|x86.Build.0 = Debug|Any CPU
4542
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Release|Any CPU.ActiveCfg = Release|Any CPU
4643
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Release|Any CPU.Build.0 = Release|Any CPU
47-
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Release|x64.ActiveCfg = Release|x64
48-
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Release|x64.Build.0 = Release|x64
49-
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Release|x86.ActiveCfg = Release|x86
50-
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Release|x86.Build.0 = Release|x86
44+
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Release|x64.ActiveCfg = Release|Any CPU
45+
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Release|x64.Build.0 = Release|Any CPU
46+
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Release|x86.ActiveCfg = Release|Any CPU
47+
{61B3CBF1-7471-4F7B-B4AE-8D7F1E124371}.Release|x86.Build.0 = Release|Any CPU
5148
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
5249
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
53-
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Debug|x64.ActiveCfg = Debug|x64
54-
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Debug|x64.Build.0 = Debug|x64
55-
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Debug|x86.ActiveCfg = Debug|x86
56-
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Debug|x86.Build.0 = Debug|x86
50+
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Debug|x64.ActiveCfg = Debug|Any CPU
51+
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Debug|x64.Build.0 = Debug|Any CPU
52+
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Debug|x86.ActiveCfg = Debug|Any CPU
53+
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Debug|x86.Build.0 = Debug|Any CPU
5754
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
5855
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Release|Any CPU.Build.0 = Release|Any CPU
59-
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Release|x64.ActiveCfg = Release|x64
60-
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Release|x64.Build.0 = Release|x64
61-
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Release|x86.ActiveCfg = Release|x86
62-
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Release|x86.Build.0 = Release|x86
56+
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Release|x64.ActiveCfg = Release|Any CPU
57+
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Release|x64.Build.0 = Release|Any CPU
58+
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Release|x86.ActiveCfg = Release|Any CPU
59+
{B6DF0569-6040-4EAF-A38B-E4DEB8DC76E0}.Release|x86.Build.0 = Release|Any CPU
6360
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
6461
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
6562
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -73,4 +70,10 @@ Global
7370
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Release|x86.ActiveCfg = Release|Any CPU
7471
{5DEA7DBC-5B8A-44A9-A070-55E95881A4CF}.Release|x86.Build.0 = Release|Any CPU
7572
EndGlobalSection
73+
GlobalSection(SolutionProperties) = preSolution
74+
HideSolutionNode = FALSE
75+
EndGlobalSection
76+
GlobalSection(ExtensibilityGlobals) = postSolution
77+
SolutionGuid = {8FFC834B-6CAA-409C-8E6A-43D4FB187669}
78+
EndGlobalSection
7679
EndGlobal

0 commit comments

Comments
 (0)