From 7aaae89d0192930b10333c1669d985e7b19c6e3f Mon Sep 17 00:00:00 2001 From: Johannes Date: Thu, 19 Dec 2024 13:34:44 +0100 Subject: [PATCH] Making things static and preparing sqls at startup. --- ManualTests/ConfigTest.cs | 17 +++--- .../ApiUtils/{ApiUtil.cs => ApiUtilStatic.cs} | 35 ++++++----- ...ository.cs => GroupingRepositoryStatic.cs} | 60 ++++++++++++------- ...ository.cs => ValueSetRepositoryStatic.cs} | 53 +++++++++------- 4 files changed, 97 insertions(+), 68 deletions(-) rename PCAxis.Sql/ApiUtils/{ApiUtil.cs => ApiUtilStatic.cs} (79%) rename PCAxis.Sql/Repositories/{GroupingRepository.cs => GroupingRepositoryStatic.cs} (76%) rename PCAxis.Sql/Repositories/{ValueSetRepository.cs => ValueSetRepositoryStatic.cs} (77%) diff --git a/ManualTests/ConfigTest.cs b/ManualTests/ConfigTest.cs index 6e58d95..b5c19e4 100644 --- a/ManualTests/ConfigTest.cs +++ b/ManualTests/ConfigTest.cs @@ -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,8 +87,7 @@ 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); @@ -98,7 +95,7 @@ public void TestGetGrouping() 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(() => entryPoint.GetValueSet(vsId, "en") + Assert.ThrowsException(() => ApiUtilStatic.GetValueSet(vsId, "en") ); List badIds = new(); @@ -124,7 +121,7 @@ public void TestValidNames() foreach (string badId in badIds) { - Assert.ThrowsException(() => entryPoint.GetValueSet(badId, "en"), "Does not fail for: " + badId + Assert.ThrowsException(() => ApiUtilStatic.GetValueSet(badId, "en"), "Does not fail for: " + badId ); } diff --git a/PCAxis.Sql/ApiUtils/ApiUtil.cs b/PCAxis.Sql/ApiUtils/ApiUtilStatic.cs similarity index 79% rename from PCAxis.Sql/ApiUtils/ApiUtil.cs rename to PCAxis.Sql/ApiUtils/ApiUtilStatic.cs index 95a7756..c26be26 100644 --- a/PCAxis.Sql/ApiUtils/ApiUtil.cs +++ b/PCAxis.Sql/ApiUtils/ApiUtilStatic.cs @@ -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 _languagesInDbConfig; - public ApiUtil() + + private static readonly List 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 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."); } diff --git a/PCAxis.Sql/Repositories/GroupingRepository.cs b/PCAxis.Sql/Repositories/GroupingRepositoryStatic.cs similarity index 76% rename from PCAxis.Sql/Repositories/GroupingRepository.cs rename to PCAxis.Sql/Repositories/GroupingRepositoryStatic.cs index cbb45b0..19cf376 100644 --- a/PCAxis.Sql/Repositories/GroupingRepository.cs +++ b/PCAxis.Sql/Repositories/GroupingRepositoryStatic.cs @@ -8,15 +8,33 @@ namespace PCAxis.Sql.Repositories { - internal class GroupingRepository + internal static class GroupingRepositoryStatic { - readonly List _languagesInDbConfig; - internal GroupingRepository() + private static readonly List LanguagesInDbConfig; + + private static readonly Dictionary> SqlquerysByLanguage; + static GroupingRepositoryStatic() { - _languagesInDbConfig = SqlDbConfigsStatic.DefaultDatabase.ListAllLanguages(); + LanguagesInDbConfig = SqlDbConfigsStatic.DefaultDatabase.ListAllLanguages(); + + //Prepares the sqls. 3 per language. + SqlquerysByLanguage = new Dictionary>(); + + 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 sqlquerys = new List { 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) @@ -24,28 +42,24 @@ internal Models.Grouping GetGrouping(string name, string language) 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(); diff --git a/PCAxis.Sql/Repositories/ValueSetRepository.cs b/PCAxis.Sql/Repositories/ValueSetRepositoryStatic.cs similarity index 77% rename from PCAxis.Sql/Repositories/ValueSetRepository.cs rename to PCAxis.Sql/Repositories/ValueSetRepositoryStatic.cs index 9709877..0bb77c1 100644 --- a/PCAxis.Sql/Repositories/ValueSetRepository.cs +++ b/PCAxis.Sql/Repositories/ValueSetRepositoryStatic.cs @@ -8,15 +8,33 @@ namespace PCAxis.Sql.Repositories { - internal class ValueSetRepository + internal static class ValueSetRepositoryStatic { - readonly List _languagesInDbConfig; - internal ValueSetRepository() + private static readonly List LanguagesInDbConfig; + + private static readonly Dictionary> SqlquerysByLanguage; + static ValueSetRepositoryStatic() { - _languagesInDbConfig = SqlDbConfigsStatic.DefaultDatabase.ListAllLanguages(); + LanguagesInDbConfig = SqlDbConfigsStatic.DefaultDatabase.ListAllLanguages(); + + //Prepares the sqls. 3 per language. + SqlquerysByLanguage = new Dictionary>(); + + 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 sqlValueset, out string sqlValues, out string sqlValuesetExistsInLang, config, cmd); + List sqlquerys = new List { sqlValueset, sqlValues, sqlValuesetExistsInLang }; + SqlquerysByLanguage[language] = sqlquerys; + } + } - internal ValueSet GetValueSet(string name, string language) + internal static Models.ValueSet GetValueSet(string name, string language) { //validate input if (name == null || language == null) @@ -24,19 +42,14 @@ internal ValueSet GetValueSet(string name, string language) return null; } - ValueSet valueset = null; - string sqlValueset; - string sqlValues; + string sqlValueset = SqlquerysByLanguage[language][0]; + string sqlValues = SqlquerysByLanguage[language][1]; + string sqlValuesetExistsInLang = 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 sqlValueset, out sqlValues, out string sqlValuesetExistsInLang, config, cmd); - System.Data.Common.DbParameter[] parameters = new System.Data.Common.DbParameter[1]; parameters[0] = cmd.GetStringParameter("aValueSet", name); @@ -45,7 +58,7 @@ internal ValueSet GetValueSet(string name, string language) DataSet extraLangsDS = String.IsNullOrEmpty(sqlValuesetExistsInLang) ? null : cmd.ExecuteSelect(sqlValuesetExistsInLang, parameters); - valueset = Parse(name, valuesetDS, valueDS, extraLangsDS); + ValueSet valueset = Parse(name, valuesetDS, valueDS, extraLangsDS); //Adding langs we know exists without checking the DB valueset.AvailableLanguages.Add(config.MainLanguage.code); @@ -67,7 +80,7 @@ private static void GetQueries(string language, out string sqlValueset, out stri sqlValueset = QueryLib_21.Queries.GetValueSetQuery((SqlDbConfig_21)config, language, sqlCommand); sqlValues = QueryLib_21.Queries.GetValueSetValuesQuery((SqlDbConfig_21)config, 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)) { @@ -84,7 +97,7 @@ private static void GetQueries(string language, out string sqlValueset, out stri sqlValues = QueryLib_22.Queries.GetValueSetValuesQuery((SqlDbConfig_22)config, 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)) { @@ -101,7 +114,7 @@ private static void GetQueries(string language, out string sqlValueset, out stri sqlValues = QueryLib_23.Queries.GetValueSetValuesQuery((SqlDbConfig_23)config, 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)) { @@ -119,7 +132,7 @@ private static void GetQueries(string language, out string sqlValueset, out stri sqlValues = QueryLib_24.Queries.GetValueSetValuesQuery((SqlDbConfig_24)config, 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)) { @@ -132,7 +145,7 @@ private static void GetQueries(string language, out string sqlValueset, out stri } } - private static ValueSet Parse(string name, DataSet valuesetDS, DataSet vsValue, DataSet extraLangsDS) + private static Models.ValueSet Parse(string name, DataSet valuesetDS, DataSet vsValue, DataSet extraLangsDS) { if (valuesetDS.Tables.Count == 0 || valuesetDS.Tables[0].Rows.Count < 1 || vsValue.Tables.Count == 0) {