Skip to content

Commit

Permalink
Merge pull request #116 from PxTools/vs_and_Grouping
Browse files Browse the repository at this point in the history
Always uses SqlDbConfigsStatic.DefaultDatabase, all pxwebapi2 export …
  • Loading branch information
JohannesFinsveen authored Dec 13, 2024
2 parents 72d7b0b + 47874e4 commit 6984f75
Show file tree
Hide file tree
Showing 14 changed files with 353 additions and 123 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -482,3 +482,4 @@ $RECYCLE.BIN/

# Vim temporary swap files
*.swp
/ManualTests/sqlDb.config
85 changes: 85 additions & 0 deletions ManualTests/ConfigTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Configuration;

using PCAxis.Sql.DbConfig;

namespace ManualTests
{
[Ignore]
[TestClass]
public class ConfigTest
{

[TestMethod]
public void TestMethod1()
{
//System.Configuration.ConfigurationFileMap configMap = new ConfigurationFileMap("./app.config");
//System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(configMap);
//var sdfdsz = configuration.AppSettings["dsa"];
//string value = configuration.AppSettings["TestKey"];
string path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
var k = ConfigurationManager.AppSettings["TestKey"];
Assert.AreEqual("testvalue", k);
}

[TestMethod]
public void TestMethod2()
{

var k = ConfigurationManager.AppSettings["dbconfigFile"];
Assert.AreEqual("SqlDb.config", k);
}



[TestMethod]
public void TestGetsDefaultDatabase()
{
var dbConf = SqlDbConfigsStatic.DefaultDatabase;

var langCount = dbConf.GetAllLanguages().Count;
Assert.IsTrue(langCount > 1);
}

[TestMethod]
public void TestGetValueSet()
{
string vsId = "KOKkommuneregion0000008";
PCAxis.Sql.ApiUtils.ApiUtil entryPoint = new PCAxis.Sql.ApiUtils.ApiUtil();
PCAxis.Sql.Models.ValueSet vs = entryPoint.GetValueSet(vsId, "no");
PCAxis.Sql.Models.ValueSet vs_en = entryPoint.GetValueSet(vsId, "en");


}



[TestMethod]
public void TestGet2ValueSet()
{
string vsId = "NoSuchVS";

PCAxis.Sql.ApiUtils.ApiUtil entryPoint = new PCAxis.Sql.ApiUtils.ApiUtil();
PCAxis.Sql.Models.ValueSet vs = entryPoint.GetValueSet(vsId, "no");

PCAxis.Sql.Models.ValueSet vs_en = entryPoint.GetValueSet(vsId, "en");


}

[TestMethod]
public void TestGetGrouping()
{
//string getId = "AldGrupp19Grp5";
//string getId = "GrkretsBydel2002";
string getId = "EUlandNY";

PCAxis.Sql.ApiUtils.ApiUtil entryPoint = new();
PCAxis.Sql.Models.Grouping data_no = entryPoint.GetGrouping(getId, "no");

PCAxis.Sql.Models.Grouping data_en = entryPoint.GetGrouping(getId, "en");


}

}
}
35 changes: 35 additions & 0 deletions ManualTests/ManualTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.3" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.3" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PCAxis.Sql\PCAxis.Sql.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="sqlDb.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="testhost.dll.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions ManualTests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
7 changes: 7 additions & 0 deletions ManualTests/testhost.dll.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="TestKey" value="testvalue"/>
<add key="dbconfigFile" value="SqlDb.config"/>
</appSettings>
</configuration>
6 changes: 6 additions & 0 deletions PCAxis.Sql.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PCAxis.Sql", "PCAxis.Sql\PC
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PCAxis.Sql.UnitTest", "PCAxis.Sql.UnitTest\PCAxis.Sql.UnitTest.csproj", "{5A2DF24D-3515-4C1F-BE56-ED8637A0A87E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ManualTests", "ManualTests\ManualTests.csproj", "{40EF913D-1A04-49EA-8624-6B1CD8B13EE7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{5A2DF24D-3515-4C1F-BE56-ED8637A0A87E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A2DF24D-3515-4C1F-BE56-ED8637A0A87E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A2DF24D-3515-4C1F-BE56-ED8637A0A87E}.Release|Any CPU.Build.0 = Release|Any CPU
{40EF913D-1A04-49EA-8624-6B1CD8B13EE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{40EF913D-1A04-49EA-8624-6B1CD8B13EE7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40EF913D-1A04-49EA-8624-6B1CD8B13EE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40EF913D-1A04-49EA-8624-6B1CD8B13EE7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
107 changes: 107 additions & 0 deletions PCAxis.Sql/ApiUtils/ApiUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

using PCAxis.Sql.DbConfig;
using PCAxis.Sql.Models;
using PCAxis.Sql.Repositories;

namespace PCAxis.Sql.ApiUtils
{
//For things not found in PXSQLBuilder
//needed by Pxwebapi2
//returned data should be defined in PCAxis.Sql.Models if complex
public class ApiUtil
{
readonly List<string> _languagesInDbConfig;
public ApiUtil()
{
var config = SqlDbConfigsStatic.DefaultDatabase;
//stringCollection --> List<string>
_languagesInDbConfig = config.GetAllLanguages().Cast<string>().ToList();
}

//Exceptions ? What if the valueset only exists in another language: Exceptions!
public ValueSet GetValueSet(string name, string language)
{
//validate input
string valueSetId = ValidateIdString(name);
string languageCode = ValidateLangCodeString(language, _languagesInDbConfig);

ValueSetRepository mValueSetRepository = new ValueSetRepository();
return mValueSetRepository.GetValueSet(valueSetId, languageCode);
}

public Grouping GetGrouping(string name, string language)
{
//validate input
string groupingId = ValidateIdString(name);
string languageCode = ValidateLangCodeString(language, _languagesInDbConfig);

GroupingRepository mGroupingRepository = new GroupingRepository();
return mGroupingRepository.GetGrouping(groupingId, languageCode);
}


// liste med tabellid og publ dato

//GetMenuLookupTables
//GetMenuLookupFolders
/*namespace PxWeb.Code.Api2.DataSource.Cnmm
{
public static class SqlDbConfigExtensions
{
public static Dictionary<string, ItemSelection>? GetMenuLookupTables(this SqlDbConfig DB, string language, IOptions<PxApiConfigurationOptions> configOptions)
{
return GetMenuLookup(DB, language, false, configOptions);
}
public static Dictionary<string, ItemSelection>? GetMenuLookupFolders(this SqlDbConfig DB, string language, IOptions<PxApiConfigurationOptions> configOptions)
{
return GetMenuLookup(DB, language, true, configOptions);
}
private static Dictionary<string, ItemSelection>? GetMenuLookup(this SqlDbConfig DB, string language, bool folders, IOptions<PxApiConfigurationOptions> configOptions)
{
// Check language to avoid SQL injection
if (!configOptions.Value.Languages.Any(l => l.Id == language))
{
throw new ArgumentException($"Illegal language {language}");
}
*/

//dump to pxfile ?

private static string ValidateLangCodeString(string input, List<string> languagesInDbConfig)
{
if (input == null)
{
throw new ArgumentException("The language cannot be null.");
}
if (!languagesInDbConfig.Contains(input))
{
throw new ArgumentException("Cant find language in config.");
}

return input;

}
private static string ValidateIdString(string input)
{
if (input == null)
{
throw new ArgumentException("The id string cannot be null.");
}

if (!Regex.IsMatch(input, @"^[\w\t \-:.]+$", RegexOptions.None, TimeSpan.FromSeconds(2)))
{
throw new ArgumentException("The string contains invalid characters. Only letters, digits, underscores, tabs, spaces, hyphens, colons and periods are allowed.");
}
return input;
}
}
}
7 changes: 4 additions & 3 deletions PCAxis.Sql/Models/ValueSet.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;

namespace PCAxis.Sql.Models
{
Expand All @@ -11,9 +9,12 @@ public class ValueSet

public List<Value> Values { get; set; }

//To do: public List<string> AvailableLanguages { get; set; }

public ValueSet()
{
Values = new List<Value>();
// AvailableLanguages = new List<string>();
}

}
Expand Down
28 changes: 12 additions & 16 deletions PCAxis.Sql/QueryLib_21/Queries.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
using PCAxis.Sql.DbClient;
using PCAxis.Sql;
using System;

using PCAxis.Sql.DbConfig;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PCAxis.Sql.QueryLib_21
{
public class Queries
{

public static string GetValueSetQuery(SqlDbConfig_21 db, string lang)
public static string GetValueSetQuery(SqlDbConfig_21 db, string lang, PxSqlCommand sqlCommand)
{
if (db == null) throw new ArgumentNullException("db");

Expand All @@ -23,14 +18,14 @@ public static string GetValueSetQuery(SqlDbConfig_21 db, string lang)
from
{db.ValueSetLang2.GetNameAndAlias(lang).RemoveUnderscoreForDefaultLanguage()}
where
{db.ValueSetLang2.ValueSetCol.Id(lang)} = @valueSet";
{db.ValueSetLang2.ValueSetCol.Id(lang)} = {sqlCommand.GetParameterRef("aValueSet")}";
}

public static string GetValueSetValuesQuery(SqlDbConfig_21 db, string lang)
public static string GetValueSetValuesQuery(SqlDbConfig_21 db, string lang, PxSqlCommand sqlCommand)
{
if (db == null) throw new ArgumentNullException("db");

return $@"SELECT
return $@"SELECT
{db.ValueLang2.ValueCodeCol.Id(lang)} AS valuecode,
{db.ValueLang2.ValuePoolCol.Id(lang)} AS valuepool,
{db.VSValueLang2.ValueCodeCol.Id(lang)} AS valueset,
Expand All @@ -44,15 +39,15 @@ public static string GetValueSetValuesQuery(SqlDbConfig_21 db, string lang)
{db.VSValueLang2.GetNameAndAlias(lang).RemoveUnderscoreForDefaultLanguage()}
ON ( {db.ValueLang2.ValuePoolCol.Id(lang)} = {db.VSValueLang2.ValuePoolCol.Id(lang)}
AND {db.ValueLang2.ValueCodeCol.Id(lang)} = {db.VSValueLang2.ValueCodeCol.Id(lang)} )
where {db.VSValueLang2.ValueSetCol.Id(lang)} = @valueSet
where {db.VSValueLang2.ValueSetCol.Id(lang)} = {sqlCommand.GetParameterRef("aValueSet")}
ORDER BY
{db.VSValueLang2.SortCodeCol.Id(lang)},
{db.ValueLang2.SortCodeCol.Id(lang)},
{db.ValueLang2.ValueCodeCol.Id(lang)}";

}
public static string GetGroupingQuery(SqlDbConfig_21 db, string lang)
public static string GetGroupingQuery(SqlDbConfig_21 db, string lang, PxSqlCommand sqlCommand)
{
if (db == null) throw new ArgumentNullException("db");

Expand All @@ -62,11 +57,12 @@ public static string GetGroupingQuery(SqlDbConfig_21 db, string lang)
FROM
{db.GroupingLang2.GetNameAndAlias(lang).RemoveUnderscoreForDefaultLanguage()}
WHERE
{db.GroupingLang2.GroupingCol.Id(lang)} = @grouping";
{db.GroupingLang2.GroupingCol.Id(lang)} = {sqlCommand.GetParameterRef("aGrouping")}";
}

public static string GetGroupingValuesQuery(SqlDbConfig_21 db, string lang)
public static string GetGroupingValuesQuery(SqlDbConfig_21 db, string lang, PxSqlCommand sqlCommand)
{
//?
throw new NotImplementedException();
}
}
Expand All @@ -78,7 +74,7 @@ public static string RemoveUnderscoreForDefaultLanguage(this string name)
{
if (name.Contains("_ "))
{
return name.Replace("_ "," ");
return name.Replace("_ ", " ");
}
return name;
}
Expand Down
Loading

0 comments on commit 6984f75

Please sign in to comment.