Compare commits

...

2 Commits

Author SHA1 Message Date
Roland Fieger
3abaf417a9 merge commit 2021-11-26 10:02:35 +01:00
Roland Fieger
31540dbfe6 SearchLogic extended 2021-11-26 10:00:58 +01:00
5 changed files with 104 additions and 24 deletions

View File

@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0-rc.2.21480.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0-rc.2.21480.5" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0-rc.2.21480.5" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Datamodels\Datamodels.csproj" />

View File

@@ -10,6 +10,7 @@ using Newtonsoft.Json;
using System.Linq;
using System.Linq.Expressions;
using SearchApi.Exceptions;
using Datamodels.Enums;
namespace SearchApi.SearchLogic
{
@@ -18,11 +19,11 @@ namespace SearchApi.SearchLogic
private readonly ILogger<SearchLogic> _logger;
private readonly IFwMariaSearchRepo _searchRepo;
private List<string> _tableNames;
private List<string> _joinConditions;
private List<string> _fieldNames;
private List<string> _whereConditions;
private List<string> _sortFields;
private List<string> _tableNames = new List<string>();
private List<string> _joinConditions = new List<string>();
private List<string> _fieldNames = new List<string>();
private List<string> _whereConditions = new List<string>();
private List<string> _sortFields = new List<string>();
public SearchLogic(ILogger<SearchLogic> logger, IFwMariaSearchRepo searchRepo)
{
@@ -41,7 +42,7 @@ namespace SearchApi.SearchLogic
var result = new DataResult<ResultModel>();
result.Data = new ResultModel();
result.Data.SearchModel = filter;
var sql = await Task.Run(() => { return AssembleSqlQuery(filter); } );
return result;
}
@@ -73,6 +74,7 @@ namespace SearchApi.SearchLogic
throw new FieldAliasMissingException($"No Alias defined for Field {field.FieldName} in Table {table.TableName}");
}
_fieldNames.Add($"{table.TableAlias}.{field.FieldName} {field.FieldAlias}");
}
}
}
@@ -84,11 +86,63 @@ namespace SearchApi.SearchLogic
}
// Extract Where Conditions
private void GetWhereConditions(FilterModel filter)
// ToDo: wenn es mehrere Values gibt, werden die generell mit OR verknüpft? Oder nehmen wir lieber AND?
private void GetWhereConditions(string tableName, FieldModel field)
{
if (field.SearchDescriptions.Count > 0)
{
foreach (var condition in field.SearchDescriptions)
{
var op = Operator(condition.SearchData.PhraseOperator);
if (op != "NOTHING")
{
var usedField = $"{tableName}.{field.FieldName}{op}{condition.SearchData.Values[0]}";
_whereConditions.Add(usedField);
}
else
{
if (condition.SearchData.PhraseOperator == PhraseOperators.Contains)
{
var usedField = $"{tableName}.{field.FieldName} like '%{condition.SearchData.Values[0]}%'";
_whereConditions.Add(usedField);
}
if (condition.SearchData.PhraseOperator == PhraseOperators.StartsWith)
{
var usedField = $"{tableName}.{field.FieldName} like '%{condition.SearchData.Values[0]}'";
_whereConditions.Add(usedField);
}
if (condition.SearchData.PhraseOperator == PhraseOperators.EndsWith)
{
var usedField = $"{tableName}.{field.FieldName} like '{condition.SearchData.Values[0]} + %'";
_whereConditions.Add(usedField);
}
}
}
}
}
public string Operator(PhraseOperators ops)
{
switch (ops)
{
case PhraseOperators.Equal:
return "=";
case PhraseOperators.NotEqual:
return "!=";
case PhraseOperators.GreaterThan:
return ">";
case PhraseOperators.GreaterThanOrEqual:
return ">=";
case PhraseOperators.LowerThan:
return "<";
case PhraseOperators.LowerThanOrEqual:
return "<=";
default:
return "NOTHING";
}
}
// Extract Sort Items
private void GetSortFields(FilterModel filter)
{
@@ -96,9 +150,35 @@ namespace SearchApi.SearchLogic
}
// Assemble SQL Query
private string AssembleSqlQuery()
private string AssembleSqlQuery(FilterModel filter)
{
return null;
GetTables(filter);
GetFields(filter);
var result = BuildSql();
return result;
}
//Build SQL String itself
private string BuildSql()
{
var sql = "SELECT ";
foreach (var field in _fieldNames)
{
sql += field.ToUpper();
if (_fieldNames.IndexOf(field) < _fieldNames.Count - 1) sql += ",";
}
sql += " FROM ";
foreach (var table in _tableNames)
{
sql += table.ToUpper();
if (_tableNames.IndexOf(table) < _tableNames.Count - 1)
{
sql += ",";
}
}
return sql;
}
}
}

View File

@@ -5,7 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0-rc.2.21480.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

View File

@@ -3,15 +3,15 @@
<ProjectReference Include="..\Datamodels\Datamodels.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0-preview.7.21378.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0-preview.7.21378.4">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0-rc.2.21480.5" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0-preview.7" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0-rc.2.21480.5" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0-rc.2.21480.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>

View File

@@ -6,15 +6,15 @@
<ItemGroup>
<PackageReference Include="Elastic.Apm.EntityFrameworkCore" Version="1.11.1" />
<PackageReference Include="Elastic.Apm.NetCoreAll" Version="1.11.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.0-rc.2.21480.10" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0-rc.2.21480.5" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="OpenTelemetry.Contrib.Instrumentation.EntityFrameworkCore" Version="1.0.0-beta2" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc7" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc7" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc7" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc8" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc8" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc8" />
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
<PackageReference Include="Serilog.Enrichers.ExceptionStackTraceHash" Version="1.3.0" />