Skip to content

Commit f9aef2d

Browse files
authored
Merge pull request #143 from d4software/develop
Updates for .net 6, some fixes, and a new "Last N Days" filter
2 parents dbcef3d + 9436ab1 commit f9aef2d

40 files changed

+2097
-11468
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ version: 2
22
jobs:
33
build:
44
docker:
5-
- image: microsoft/dotnet:2.2-sdk-stretch
5+
- image: mcr.microsoft.com/dotnet/sdk:6.0
66
steps:
77
- checkout
88
- run: dotnet build ./Web/QueryTree.csproj -v n
99
test:
1010
docker:
11-
- image: microsoft/dotnet:2.2-sdk-stretch
11+
- image: mcr.microsoft.com/dotnet/sdk:6.0
1212
steps:
1313
- checkout
1414
- run: dotnet test ./Tests/Tests.csproj -v n

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
FROM microsoft/dotnet:2.2-sdk-stretch as builder
1+
FROM mcr.microsoft.com/dotnet/sdk:6.0 as builder
22
WORKDIR /build
33
COPY . .
44
RUN dotnet restore
55
RUN dotnet publish --no-restore -c Release ./Web/QueryTree.csproj -o /dist
66

7-
FROM microsoft/dotnet:2.2-aspnetcore-runtime as runtime
7+
FROM mcr.microsoft.com/dotnet/aspnet:6.0 as runtime
88
WORKDIR /app
99
COPY --from=builder /dist .
1010
COPY --from=builder /build/Web/EmailTemplates ./EmailTemplates

Engine/FilterNode.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public enum FilterOperator
2525
NextMonth,
2626
LastMonth,
2727
Last90Days,
28-
Next90Days
28+
Next90Days,
29+
LastNDays,
30+
NextNDays
2931
}
3032

3133
public class FilterNode : DataProcessorNode
@@ -85,6 +87,7 @@ public override string GetQuerySql()
8587
sql += "BINARY ";
8688

8789
string compareValue;
90+
int days = 0;
8891

8992
var filterColumnSpecifier = string.Format("{0}.Column_{1:D} ",
9093
firstInput.GetNodeAlias(),
@@ -205,7 +208,7 @@ public override string GetQuerySql()
205208
sql += string.Format("{0}::date - NOW()::date BETWEEN -90 AND -1", filterColumnSpecifier);
206209
break;
207210
case DatabaseType.MySQL:
208-
sql += string.Format("DATEDIFF(NOW(), {0}) BETWEEN -90 AND -1", filterColumnSpecifier);
211+
sql += string.Format("DATEDIFF({0}, NOW()) BETWEEN -90 AND -1", filterColumnSpecifier);
209212
break;
210213
}
211214

@@ -220,10 +223,44 @@ public override string GetQuerySql()
220223
sql += string.Format("{0}::date - NOW()::date BETWEEN 0 AND 89", filterColumnSpecifier);
221224
break;
222225
case DatabaseType.MySQL:
223-
sql += string.Format("DATEDIFF(NOW(), {0}) BETWEEN 0 AND 89", filterColumnSpecifier);
226+
sql += string.Format("DATEDIFF({0}, NOW()) BETWEEN 0 AND 89", filterColumnSpecifier);
224227
break;
225228
}
226229

230+
break;
231+
case FilterOperator.LastNDays:
232+
if (int.TryParse(FilterValue1, out days))
233+
{
234+
switch (DatabaseType)
235+
{
236+
case DatabaseType.SQLServer:
237+
sql += $"DATEDIFF(d, GETDATE(), {filterColumnSpecifier}) BETWEEN -{days} AND -1";
238+
break;
239+
case DatabaseType.PostgreSQL:
240+
sql += $"{filterColumnSpecifier}::date - NOW()::date BETWEEN -{days} AND -1";
241+
break;
242+
case DatabaseType.MySQL:
243+
sql += $"DATEDIFF({filterColumnSpecifier}, NOW()) BETWEEN -{days} AND -1";
244+
break;
245+
}
246+
}
247+
break;
248+
case FilterOperator.NextNDays:
249+
if (int.TryParse(FilterValue1, out days))
250+
{
251+
switch (DatabaseType)
252+
{
253+
case DatabaseType.SQLServer:
254+
sql += $"DATEDIFF(d, GETDATE(), {filterColumnSpecifier}) BETWEEN 0 AND {days-1}";
255+
break;
256+
case DatabaseType.PostgreSQL:
257+
sql += $"{filterColumnSpecifier}::date - NOW()::date BETWEEN 0 AND {days-1}";
258+
break;
259+
case DatabaseType.MySQL:
260+
sql += $"DATEDIFF({filterColumnSpecifier}, NOW()) BETWEEN 0 AND {days-1}";
261+
break;
262+
}
263+
}
227264
break;
228265
default:
229266
if (DatabaseType == DatabaseType.PostgreSQL && IsTextType(columnTypes[FilterColumnIndex.Value]) && !CaseSensitive && (Operator == FilterOperator.EqualTo || Operator == FilterOperator.DoesNotEqual))

Engine/NodeBase.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ private static string BuildWithSql(IEnumerable<NodeBase> orderedUpstreamDependen
210210

211211
internal virtual void FetchOrderedDependencies(IList<NodeBase> dependencies)
212212
{
213+
if (dependencies.Contains(this))
214+
{
215+
dependencies.Remove(this);
216+
}
217+
213218
dependencies.Insert(0, this);
214219
}
215220

@@ -286,7 +291,7 @@ internal string GetDependencySql()
286291
{
287292
if (DatabaseType == DatabaseType.SQLServer || DatabaseType == DatabaseType.PostgreSQL)
288293
{
289-
return GetNodeAlias() + " ";
294+
return GetNodeAlias();
290295
}
291296
else
292297
{

Engine/SummarizeNode.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,11 @@ public override IList<string> GetColumns()
173173
}
174174
}
175175

176-
for (int i = 0; i < AggColumnIndexes.Count; i++)
176+
for (int i = 0; i < AggFunctions.Count; i++)
177177
{
178-
string columnName = input1Cols[AggColumnIndexes[i]];
178+
string columnName = AggFunctions[i] != AggregationFunction.Count
179+
? input1Cols[AggColumnIndexes[i]]
180+
: null;
179181

180182
if (i < AggFunctions.Count)
181183
{

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ for more information.
4444

4545
### Prerequisites
4646

47-
To build binaries or run from source you need the [.NET Core SDK v2.2](https://www.microsoft.com/net/download) installed.
47+
To build binaries or run from source you need the [.NET 6.0](https://www.microsoft.com/net/download) installed.
4848

4949
### Running from Source
5050

@@ -83,7 +83,7 @@ This will create a release folder in `dist` of all the unpacked QueryTree binari
8383

8484
### Running from Binaries
8585

86-
To run QueryTree on your server you will need to install the .NET Core 2.2.x runtime. (It is not necessary to install the full .NET SDK, just the runtime.) You can download the installer [here](https://www.microsoft.com/net/download/core#/runtime).
86+
To run QueryTree on your server you will need to install the ASP.NET Core Runtime 6.0.x. (It is not necessary to install the full .NET SDK, just the runtime.) You can download the installer [here](https://dotnet.microsoft.com/en-us/download/dotnet/6.0).
8787

8888
To verify that you have the .NET runtime installed, open a terminal/cmd window and type
8989

Tests/AppendTests.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,102 @@ namespace QueryTree.Engine.Tests
77
{
88
public class AppendTests
99
{
10+
private List<ITableInfo> DatabaseInfo
11+
{
12+
get
13+
{
14+
return new List<ITableInfo>()
15+
{
16+
new MockTableInfo()
17+
{
18+
DisplayName = "employees",
19+
Columns = new List<IColumnInfo>()
20+
{
21+
new MockColumnInfo() { DataType = "int", Name = "ID" },
22+
new MockColumnInfo() { DataType = "varchar", Name = "Name" },
23+
new MockColumnInfo() { DataType = "int", Name = "department_id" }
24+
}
25+
},
26+
new MockTableInfo()
27+
{
28+
DisplayName = "departments",
29+
Columns = new List<IColumnInfo>()
30+
{
31+
new MockColumnInfo() { DataType = "int", Name = "ID" },
32+
new MockColumnInfo() { DataType = "varchar", Name = "Name" }
33+
}
34+
}
35+
};
36+
}
37+
}
1038

39+
private string NodesJsonWithComplexAppend
40+
{
41+
get
42+
{
43+
return @"[
44+
{
45+
""Id"": ""1"",
46+
""Type"": ""Data Table"",
47+
""Table"": ""employees""
48+
},
49+
{
50+
""Id"": ""2"",
51+
""Type"": ""Data Table"",
52+
""Table"": ""departments""
53+
},
54+
{
55+
""Id"": ""3"",
56+
""Inputs"": [""1"",""2""],
57+
""Type"": ""Join"",
58+
""JoinType"": ""Inner"",
59+
""Table1Column"": ""department_id"",
60+
""Table2Column"": ""ID""
61+
},
62+
{
63+
""Id"": ""4"",
64+
""Inputs"": [""3""],
65+
""Type"": ""Filter"",
66+
""FilterColumnIndex"": 0,
67+
""Operator"": ""EqualTo"",
68+
""FilterValue1"": ""1""
69+
},
70+
{
71+
""Id"": ""5"",
72+
""Inputs"": [""3""],
73+
""Type"": ""Filter"",
74+
""FilterColumnIndex"": 0,
75+
""Operator"": ""EqualTo"",
76+
""FilterValue1"": ""2""
77+
},
78+
{
79+
""Id"": ""6"",
80+
""Inputs"": [""4"",""5""],
81+
""Type"": ""Append"",
82+
""IncludeUniqueColumns"": true
83+
}
84+
]";
85+
}
86+
}
87+
88+
[Fact]
89+
public void TestMultipleRoutesToDataTablesDoesntDefineThemTwice()
90+
{
91+
var query = new Query(
92+
DatabaseType.PostgreSQL,
93+
NodesJsonWithComplexAppend,
94+
DatabaseInfo);
95+
96+
var sql = query.GetSql("6");
97+
98+
// count how many times "node_1" is defined
99+
int c = 0;
100+
int i = -1;
101+
while ((i = sql.IndexOf("node_1 AS", i+1)) >= 0)
102+
c++;
103+
104+
// The "node_1" datatable should only be defined once in the query
105+
Assert.Equal(1, c);
106+
}
11107
}
12108
}

Tests/FilterTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,28 @@ private string NodesJsonGreaterThanDate
117117
}
118118
}
119119

120+
private string NodesJsonLastNDays
121+
{
122+
get
123+
{
124+
return @"[
125+
{
126+
""Id"": ""1"",
127+
""Type"": ""Data Table"",
128+
""Table"": ""employees""
129+
},
130+
{
131+
""Id"": ""2"",
132+
""Inputs"": [""1""],
133+
""Type"": ""Filter"",
134+
""FilterColumnIndex"": 4,
135+
""Operator"": ""LastNDays"",
136+
""FilterValue1"": ""365""
137+
}
138+
]";
139+
}
140+
}
141+
120142
private List<ITableInfo> DatabaseInfo
121143
{
122144
get
@@ -200,5 +222,17 @@ public void TestTimestampGreaterThan()
200222
var sql = query.GetSql("2");
201223
Assert.True(sql.Contains("node_1.Column_4 > '2017-01-01 00:00'"), "SQL Value was: " + sql);
202224
}
225+
226+
[Fact]
227+
public void TestLastNDays()
228+
{
229+
var query = new Query(
230+
DatabaseType.MySQL,
231+
NodesJsonLastNDays,
232+
DatabaseInfo);
233+
234+
var sql = query.GetSql("2");
235+
Assert.True(sql.Contains("DATEDIFF(node_1.Column_4 , NOW()) BETWEEN -365 AND -1"), "SQL Value was: " + sql);
236+
}
203237
}
204238
}

Tests/SummarizeTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,18 @@ public void TestSum()
8585

8686
Assert.True(sql.Contains("SELECT SUM(Column_1) AS Column_0"), "SQL Value was: " + sql);
8787
}
88+
89+
[Fact]
90+
public void TestCountAddsAnAdditionalColumnToTheSelectList()
91+
{
92+
var query = new Query(
93+
DatabaseType.MySQL,
94+
NodesJson,
95+
DatabaseInfo);
96+
97+
var sql = query.GetSql("2");
98+
99+
Assert.True(sql.StartsWith("SELECT Column_0 AS `Count` FROM"), "SQL Value was: " + sql);
100+
}
88101
}
89102
}

Tests/Tests.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
9-
<PackageReference Include="xunit" Version="2.3.1" />
10-
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
9+
<PackageReference Include="xunit" Version="2.4.2" />
10+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
11+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
12+
<PrivateAssets>all</PrivateAssets>
13+
</PackageReference>
1114
</ItemGroup>
1215

1316
<ItemGroup>

0 commit comments

Comments
 (0)