Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making things static and preparing sqls at startup. #127

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions ManualTests/ConfigTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Configuration;

using PCAxis.Sql.ApiUtils;
using PCAxis.Sql.DbConfig;

namespace ManualTests
@@ -11,7 +12,6 @@ public class ConfigTest
private readonly string _mainLanguage;
private readonly string _okVS;
private readonly string _okGrouping;
private readonly PCAxis.Sql.ApiUtils.ApiUtil entryPoint = new();

public ConfigTest()
{
@@ -32,7 +32,6 @@ public ConfigTest()
[TestMethod]
public void TestReadsConfig()
{

var k = ConfigurationManager.AppSettings["dbconfigFile"];
Assert.AreEqual("SqlDb.config", k);
}
@@ -43,7 +42,6 @@ public void TestReadsConfig()
public void TestGetsDefaultDatabase()
{
var dbConf = SqlDbConfigsStatic.DefaultDatabase;

var langCount = dbConf.GetAllLanguages().Count;
Assert.IsTrue(langCount > 1);
}
@@ -53,7 +51,7 @@ public void TestOkValueSet()
{
string getId = _okVS;

PCAxis.Sql.Models.ValueSet actual_data = entryPoint.GetValueSet(getId, _mainLanguage);
PCAxis.Sql.Models.ValueSet actual_data = ApiUtilStatic.GetValueSet(getId, _mainLanguage);
Assert.IsNotNull(actual_data);
Assert.AreEqual(2, actual_data.AvailableLanguages.Count);
Assert.IsTrue(actual_data.AvailableLanguages.Contains(_mainLanguage));
@@ -66,7 +64,7 @@ public void TestOkValueSet()
Assert.IsFalse(String.IsNullOrEmpty(actual_data.Values[0].Text));


actual_data = entryPoint.GetValueSet(getId, "en");
actual_data = ApiUtilStatic.GetValueSet(getId, "en");
Assert.IsNotNull(actual_data);
Assert.AreEqual(2, actual_data.AvailableLanguages.Count);
Assert.IsTrue(actual_data.AvailableLanguages.Contains(_mainLanguage));
@@ -89,16 +87,15 @@ public void TestGetGrouping()
//string getId = "GrkretsBydel2002";
string getId = _okGrouping;

PCAxis.Sql.ApiUtils.ApiUtil entryPoint = new();
PCAxis.Sql.Models.Grouping actual_data = entryPoint.GetGrouping(getId, _mainLanguage);
PCAxis.Sql.Models.Grouping actual_data = ApiUtilStatic.GetGrouping(getId, _mainLanguage);

Assert.IsNotNull(actual_data);
Assert.AreEqual(2, actual_data.AvailableLanguages.Count);
Assert.IsTrue(actual_data.AvailableLanguages.Contains(_mainLanguage));
Assert.IsTrue(actual_data.AvailableLanguages.Contains("en"));


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


}
@@ -110,7 +107,7 @@ public void TestValidNames()
// ApplicationException means not found (does no exist in this db)

string vsId = "Spa3ce ok. _ :sdfsdDSÆØ";
Assert.ThrowsException<ApplicationException>(() => entryPoint.GetValueSet(vsId, "en")
Assert.ThrowsException<ApplicationException>(() => ApiUtilStatic.GetValueSet(vsId, "en")
);

List<string> badIds = new();
@@ -124,7 +121,7 @@ public void TestValidNames()

foreach (string badId in badIds)
{
Assert.ThrowsException<ArgumentException>(() => entryPoint.GetValueSet(badId, "en"), "Does not fail for: " + badId
Assert.ThrowsException<ArgumentException>(() => ApiUtilStatic.GetValueSet(badId, "en"), "Does not fail for: " + badId
);
}

Original file line number Diff line number Diff line change
@@ -10,38 +10,43 @@ 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
//returned data should be defined in PCAxis.Sql.Models if complex

//In Pxwebapi2 only 1 database is possible, so SqlDbConfigsStatic.DefaultDatabase is the only one.
//It is therefore simpler to prepare some of the sqls at startup.

public static class ApiUtilStatic
{
readonly List<string> _languagesInDbConfig;
public ApiUtil()

private static readonly List<string> LanguagesInDbConfig;
static ApiUtilStatic()
{
Console.WriteLine("Start ApiUtilStatic");
var config = SqlDbConfigsStatic.DefaultDatabase;
_languagesInDbConfig = config.ListAllLanguages();
LanguagesInDbConfig = config.ListAllLanguages();
}

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

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

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

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


//TODO:
// liste med tabellid og publ dato
// tabellid er unik, mens publ dato er det vi spørr mot

@@ -75,13 +80,13 @@ public static class SqlDbConfigExtensions

//dump to pxfile ?

private static string ValidateLangCodeString(string input, List<string> languagesInDbConfig)
private static string ValidateLangCodeString(string input)
{
if (input == null)
{
throw new ArgumentException("The language cannot be null.");
}
if (!languagesInDbConfig.Contains(input))
if (!LanguagesInDbConfig.Contains(input))
{
throw new ArgumentException("Cant find language in config.");
}
Original file line number Diff line number Diff line change
@@ -8,44 +8,58 @@

namespace PCAxis.Sql.Repositories
{
internal class GroupingRepository
internal static class GroupingRepositoryStatic
{
readonly List<string> _languagesInDbConfig;
internal GroupingRepository()
private static readonly List<string> LanguagesInDbConfig;

private static readonly Dictionary<string, List<string>> SqlquerysByLanguage;
static GroupingRepositoryStatic()
{
_languagesInDbConfig = SqlDbConfigsStatic.DefaultDatabase.ListAllLanguages();
LanguagesInDbConfig = SqlDbConfigsStatic.DefaultDatabase.ListAllLanguages();

//Prepares the sqls. 3 per language.
SqlquerysByLanguage = new Dictionary<string, List<string>>();

var config = SqlDbConfigsStatic.DefaultDatabase;

InfoForDbConnection info = config.GetInfoForDbConnection(config.GetDefaultConnString());
var cmd = new PxSqlCommandForTempTables(info.DataBaseType, info.DataProvider, info.ConnectionString);

foreach (var language in LanguagesInDbConfig)
{
GetQueries(language, out string sqlGrouping, out string sqlValues, out string sqlGroupingExistsInLang, config, cmd);
List<string> sqlquerys = new List<string> { sqlGrouping, sqlValues, sqlGroupingExistsInLang };
SqlquerysByLanguage[language] = sqlquerys;
}

}

internal Models.Grouping GetGrouping(string name, string language)
internal static Models.Grouping GetGrouping(string name, string language)
{
//validate input
if (name == null || language == null)
{
return null;
}

var grouping = new Models.Grouping();
string sqlGrouping;
string sqlValues;
string sqlGrouping = SqlquerysByLanguage[language][0];
string sqlValues = SqlquerysByLanguage[language][1];
string sqlGroupingExistsInLang = SqlquerysByLanguage[language][2];

var config = SqlDbConfigsStatic.DefaultDatabase;

InfoForDbConnection info;

info = config.GetInfoForDbConnection(config.GetDefaultConnString());
InfoForDbConnection info = config.GetInfoForDbConnection(config.GetDefaultConnString());
var cmd = new PxSqlCommandForTempTables(info.DataBaseType, info.DataProvider, info.ConnectionString);

GetQueries(language, out sqlGrouping, out sqlValues, out string sqlGroupingExistsInLang, config, cmd);

System.Data.Common.DbParameter[] parameters = new System.Data.Common.DbParameter[1];
parameters[0] = cmd.GetStringParameter("aGrouping", name);

var valueGroup = cmd.ExecuteSelect(sqlGrouping, parameters);
var vsValue = cmd.ExecuteSelect(sqlValues, parameters);
var groupingDS = cmd.ExecuteSelect(sqlGrouping, parameters);
var valuesDS = cmd.ExecuteSelect(sqlValues, parameters);

DataSet extraLangsDS = String.IsNullOrEmpty(sqlGroupingExistsInLang) ? null : cmd.ExecuteSelect(sqlGroupingExistsInLang, parameters);

grouping = Parse(valueGroup, vsValue, extraLangsDS);

Grouping grouping = Parse(groupingDS, valuesDS, extraLangsDS);

//Adding langs we know exists without checking the DB
grouping.AvailableLanguages.Add(config.MainLanguage.code);
@@ -70,7 +84,7 @@ private static void GetQueries(string language, out string sqlGrouping, out stri
sqlValues = QueryLib_21.Queries.GetGroupingValuesQuery(cfg, language, sqlCommand);

string glue = String.Empty;
foreach (var lang in config.ListAllLanguages())
foreach (var lang in LanguagesInDbConfig)
{
if (!lang.Equals(config.MainLanguage.code) && !lang.Equals(language))
{
@@ -89,7 +103,7 @@ private static void GetQueries(string language, out string sqlGrouping, out stri
sqlValues = QueryLib_22.Queries.GetGroupingValuesQuery(cfg, language, sqlCommand);

string glue = String.Empty;
foreach (var lang in config.ListAllLanguages())
foreach (var lang in LanguagesInDbConfig)
{
if (!lang.Equals(config.MainLanguage.code) && !lang.Equals(language))
{
@@ -107,7 +121,7 @@ private static void GetQueries(string language, out string sqlGrouping, out stri
sqlValues = QueryLib_23.Queries.GetGroupingValuesQuery(cfg, language, sqlCommand);

string glue = String.Empty;
foreach (var lang in config.ListAllLanguages())
foreach (var lang in LanguagesInDbConfig)
{
if (!lang.Equals(config.MainLanguage.code) && !lang.Equals(language))
{
@@ -126,7 +140,7 @@ private static void GetQueries(string language, out string sqlGrouping, out stri
sqlValues = QueryLib_24.Queries.GetGroupingValuesQuery(cfg, language, sqlCommand);

string glue = String.Empty;
foreach (var lang in config.ListAllLanguages())
foreach (var lang in LanguagesInDbConfig)
{
if (!lang.Equals(config.MainLanguage.code) && !lang.Equals(language))
{
@@ -140,7 +154,7 @@ private static void GetQueries(string language, out string sqlGrouping, out stri

}

private static PCAxis.Sql.Models.Grouping Parse(DataSet valueGroup, DataSet vsValue, DataSet extraLangsDS)
private static Models.Grouping Parse(DataSet valueGroup, DataSet vsValue, DataSet extraLangsDS)
{
//Make sure we have a grouping
if (valueGroup.Tables.Count == 0 || valueGroup.Tables[0].Rows.Count < 1 || vsValue.Tables.Count == 0)
@@ -149,7 +163,7 @@ private static PCAxis.Sql.Models.Grouping Parse(DataSet valueGroup, DataSet vsVa
}

var grouping = new PCAxis.Sql.Models.Grouping();
grouping.Id = valueGroup.Tables[0].Rows[0][0].ToString(); ;
grouping.Id = valueGroup.Tables[0].Rows[0][0].ToString();
grouping.Name = valueGroup.Tables[0].Rows[0][1].ToString();


Loading
Loading