Move Saxonite’s projects into a dedicated `Workspace` folder and align the solution with .NET 10 and the current Common workspace. Update encryption integration, test project structure and coverage, and repository guidance and upgrade notes. - Relocate application and library projects while retaining Git rename history. - Split tests into Admin, Online, and Shared projects; add representative behavioral and serialization tests. - Update Common integration, application version, and repository documentation. Application version: 1.3.0 Reviewed-on: #1 Co-authored-by: andy <andy@sarand.com>
730 lines
37 KiB
C#
730 lines
37 KiB
C#
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using Sarand.Net.Standard.Dbas.Data;
|
|
using Saxonite.Base.App;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using Saxonite.Net.Standard.Base.Bus;
|
|
using Saxonite.Net.Standard.Base.Bus.Reference;
|
|
using Saxonite.Net.Standard.Dbas.Bus.Reference;
|
|
using Saxonite.Net.Standard.Dbas.Internal.Reference;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Common;
|
|
using System.Linq;
|
|
|
|
namespace Saxonite.Net.Standard.Dbas.Mgr.Reference
|
|
{
|
|
//=======================================================================
|
|
partial class SaxReferenceGolfMgr_Dbas
|
|
{
|
|
//*******************************************************************
|
|
// Support Mgr - User Report
|
|
//*******************************************************************
|
|
// Utilities.
|
|
private List<StringPair> GetLogProperties(SaxGolfUsgaCountry usgaCountry)
|
|
{
|
|
return new List<StringPair>()
|
|
{
|
|
new StringPair("Id",SysId.ToString(usgaCountry.Id)),
|
|
new StringPair("Country code",usgaCountry.CountryCode),
|
|
new StringPair("Country name",usgaCountry.CountryName),
|
|
new StringPair("Status",usgaCountry.StatusCode)
|
|
};
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Create item.
|
|
public override SaxGolfUsgaCountry CreateUsgaCountry()
|
|
{
|
|
ClearErrorState();
|
|
return new SaxGolfUsgaCountry_Dbas(ReferenceDatabase, null, 0);
|
|
}
|
|
private SaxGolfUsgaCountry_Dbas CreateUsgaCountry(Golf_UsgaCountry_Db dbUsgaCountry)
|
|
{
|
|
ClearErrorState();
|
|
SaxGolfUsgaCountry_Dbas usgaCountry = new SaxGolfUsgaCountry_Dbas(ReferenceDatabase, new SysId(dbUsgaCountry._Id), (ushort)dbUsgaCountry._Flags);
|
|
PopulateFrom(dbUsgaCountry, usgaCountry);
|
|
return usgaCountry;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get item.
|
|
public override SaxGolfUsgaCountry GetUsgaCountry(SaxGolfUsgaCountry.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
return GetUsgaCountry(GetUsgaCountryQueryFilter(prm).Concat(" And ", "(", ")"));
|
|
}
|
|
private SaxGolfUsgaCountry GetUsgaCountry(String filter)
|
|
{
|
|
ClearErrorState();
|
|
long count = GetUsgaCountryCount(filter);
|
|
if (count == 0) return null;
|
|
if (count != 1)
|
|
{
|
|
SetErrorState("Multiple items found");
|
|
return null;
|
|
}
|
|
SaxGolfUsgaCountry usgaCountry = null;
|
|
// Query data.
|
|
using (var dbConnection = ReferenceDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
Golf_UsgaCountry_Db dbUsgaCountry = new Golf_UsgaCountry_Db(ReferenceDbInfo);
|
|
dbUsgaCountry.PopulateFrom(dbCommand, aliasUsgaCountry, filter);
|
|
usgaCountry = CreateUsgaCountry(dbUsgaCountry);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SetErrorState("Database query exception");
|
|
SaLogger.Warn($"{SysUtil.Fn}\n\tQuery: {dbCommand.CommandText}\n\t{ExceptionUtil.GetErrorStateMessage(exc)}");
|
|
}
|
|
}
|
|
return usgaCountry;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Validate item.
|
|
public override bool IsValidUsgaCountryData(SaxGolfUsgaCountry.Data data, SaxGolfUsgaCountry.ErrField errField)
|
|
{
|
|
ClearErrorState();
|
|
if (!base.IsValidUsgaCountryData(data, errField)) return false;
|
|
return true;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Insert item.
|
|
public override bool InsertUsgaCountry(SaxGolfUsgaCountry usgaCountry, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
String fn = GetType().Name + ".InsertUsgaCountry";
|
|
ClearErrorState();
|
|
if (!IsValidUsgaCountry(usgaCountry)) return false;
|
|
if (SysId.IsValid(usgaCountry.Id)) return SetErrorState(ErrorMessage.ValidItemId);
|
|
using (var dbConnection = ReferenceDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
Golf_UsgaCountry_Db dbUsgaCountry = new Golf_UsgaCountry_Db(ReferenceDbInfo);
|
|
PopulateTo(dbUsgaCountry, usgaCountry, dbUpdateInfo);
|
|
if (Golf_UsgaCountry_Db.FieldsNonId.Contains("Id"))
|
|
{
|
|
// Generate next Id.
|
|
dbUsgaCountry._Id = ReferenceDatabase.GetNextIdFromTable("UsgaCountry");
|
|
}
|
|
dbUsgaCountry.ExecuteInsertCommand(dbCommand);
|
|
usgaCountry.Id = new SysId(dbUsgaCountry._Id);
|
|
usgaCountry.Changed = false;
|
|
if (DebugMode) SaLogger.Debug(fn + " Inserted. " + GetLogProperties(usgaCountry).Concat(": ").Concat("", "\n\t"));
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Update item.
|
|
public override bool UpdateUsgaCountry(SaxGolfUsgaCountry usgaCountry, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
String fn = GetType().Name + ".UpdateUsgaCountry";
|
|
ClearErrorState();
|
|
if (!IsValidUsgaCountry(usgaCountry)) return false;
|
|
if (!SysId.IsValid(usgaCountry.Id)) return SetErrorState(ErrorMessage.InvalidItemId);
|
|
if (!usgaCountry.Changed)
|
|
{
|
|
if (DebugMode) SaLogger.Debug(fn + " Data not changed. " + GetLogProperties(usgaCountry).Concat(": ").Concat("", "\n\t"));
|
|
return true;
|
|
}
|
|
using (var dbConnection = ReferenceDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
// Retrieve item.
|
|
dbConnection.Open();
|
|
Golf_UsgaCountry_Db dbUsgaCountry = new Golf_UsgaCountry_Db(ReferenceDbInfo);
|
|
dbUsgaCountry.PopulateFrom(dbCommand, aliasUsgaCountry, "Id=" + SysId.ToString(usgaCountry.Id));
|
|
// ToDo SaxUpdateLogItemMonitor_UsgaCountry updateLogMonitor = new SaxUpdateLogItemMonitor_UsgaCountry(SessionContext, CreateUsgaCountry(dbUsgaCountry), dbUpdateInfo);
|
|
// Update item.
|
|
PopulateTo(dbUsgaCountry, usgaCountry, dbUpdateInfo);
|
|
dbUsgaCountry.ExecuteUpdateCommand(dbCommand);
|
|
usgaCountry.Changed = false;
|
|
if (DebugMode) SaLogger.Debug(fn + " Updated. " + GetLogProperties(usgaCountry).Concat(": ").Concat("", "\n\t"));
|
|
// Record updates.
|
|
// ToDo updateLogMonitor.RecordUpdatedValues(usgaCountry);
|
|
// ToDo updateLogMonitor.LogUpdates();
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Delete item.
|
|
public override bool UsgaCountryCanBeDeleted(SaxGolfUsgaCountry usgaCountry)
|
|
{
|
|
ClearErrorState();
|
|
if (!base.UsgaCountryCanBeDeleted(usgaCountry)) return false;
|
|
return true;
|
|
}
|
|
public override bool DeleteUsgaCountry(SaxGolfUsgaCountry usgaCountry, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
String fn = GetType().Name + ".DeleteUsgaCountry";
|
|
ClearErrorState();
|
|
if (!UsgaCountryCanBeDeleted(usgaCountry)) return false;
|
|
|
|
// User Roles are logically deleted by changing the status.
|
|
usgaCountry.Status = SaStatus.Deleted;
|
|
return UpdateUsgaCountry(usgaCountry, dbUpdateInfo);
|
|
|
|
/*
|
|
* Physical deletion.
|
|
using (var dbConnection = ReferenceDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
// Retrieve item.
|
|
dbConnection.Open();
|
|
Golf_UsgaCountry_Db dbUsgaCountry = new Golf_UsgaCountry_Db(ReferenceDbInfo);
|
|
dbUsgaCountry.PopulateFrom(dbCommand, aliasUsgaCountry, "Id=" + usgaCountry.Id.ToString());
|
|
// Delete item.
|
|
dbCommand.CommandType = System.Data.CommandType.Text;
|
|
dbCommand.CommandText = "DELETE FROM " + SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaCountry) + " WHERE Id=" + SysId.ToString(usgaCountry.Id) + ";";
|
|
dbCommand.ExecuteNonQuery();
|
|
usgaCountry.Changed = false;
|
|
if (DebugMode) SaLogger.Debug(fn + " Deleted. " + GetLogProperties(usgaCountry).Concat(": ").Concat("", "\n\t"));
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
*/
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Copy data properties to and from database.
|
|
private void PopulateTo(Golf_UsgaCountry_Db dbUsgaCountry, SaxGolfUsgaCountry usgaCountry, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
dbUsgaCountry._Id = SysId.ToLong(usgaCountry.Id);
|
|
dbUsgaCountry._CountryCode = usgaCountry.CountryCode;
|
|
dbUsgaCountry._EntCountryCode = usgaCountry.EntCountryCode;
|
|
dbUsgaCountry._CountryName = usgaCountry.CountryName;
|
|
dbUsgaCountry._ProvincesUpdateAt = usgaCountry.ProvincesUpdateAt;
|
|
// Common.
|
|
dbUsgaCountry._Notes = usgaCountry.Notes;
|
|
dbUsgaCountry._Status = SacsSystem.GetStatusCode(usgaCountry.Status);
|
|
dbUsgaCountry._Flags = (short)usgaCountry.Flags;
|
|
if (!SysId.IsValid(usgaCountry.Id))
|
|
{
|
|
dbUsgaCountry._CreateOn = DateTime.Now;
|
|
dbUsgaCountry._CreateBy = dbUpdateInfo.UserId.ToInt();
|
|
dbUsgaCountry._CreateHost = dbUpdateInfo.Host;
|
|
}
|
|
dbUsgaCountry._UpdateOn = DateTime.Now;
|
|
dbUsgaCountry._UpdateBy = dbUpdateInfo.UserId.ToInt();
|
|
dbUsgaCountry._UpdateHost = dbUpdateInfo.Host;
|
|
}
|
|
private void PopulateFrom(Golf_UsgaCountry_Db dbUsgaCountry, SaxGolfUsgaCountry usgaCountry)
|
|
{
|
|
//usgaCountry.Id = new SysId(dbUsgaCountry._Id);
|
|
usgaCountry.CountryCode = dbUsgaCountry._CountryCode;
|
|
usgaCountry.EntCountryCode = dbUsgaCountry._EntCountryCode;
|
|
usgaCountry.CountryName = dbUsgaCountry._CountryName;
|
|
usgaCountry.ProvincesUpdateAt = dbUsgaCountry._ProvincesUpdateAt;
|
|
// Common.
|
|
usgaCountry.Notes = dbUsgaCountry._Notes;
|
|
usgaCountry.Status = SacsSystem.GetStatusForCode(dbUsgaCountry._Status);
|
|
usgaCountry.SetCreateStamp(dbUsgaCountry._CreateOn, new SysId(dbUsgaCountry._CreateBy), dbUsgaCountry._CreateHost);
|
|
usgaCountry.SetUpdateStamp(dbUsgaCountry._UpdateOn, new SysId(dbUsgaCountry._UpdateBy), dbUsgaCountry._UpdateHost);
|
|
usgaCountry.AcceptChanges();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Bulk updates.
|
|
/// <summary>
|
|
/// Bulk update USGA Countries.
|
|
/// <para>- Country code must be supplied to act as identifier.</para>
|
|
/// <para>- Other properties are validated as per country rules - check error state.</para>
|
|
/// <para>- Other properties are left as is if null.</para>
|
|
/// </summary>
|
|
public override bool BulkUpdateUsgaCountries(List<UsgaCountryData> countries, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
ClearErrorState();
|
|
List<UsgaCountryData> countriesToUpdate = new List<UsgaCountryData>();
|
|
|
|
// Validate supplied data.
|
|
foreach (UsgaCountryData country in countries)
|
|
{
|
|
// Validate country properties.
|
|
// Code is mandatory, other values only validated if non-null.
|
|
if (!IsValidValue(country.CountryCode, "code", (long)SaxDbDfn_Reference.GetColTextLen(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryCode), true)) return false;
|
|
if (country.EntCountryCode != null && !IsValidValue(country.EntCountryCode, "ent code", (long)SaxDbDfn_Reference.GetColTextLen(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_EntCountryCode))) return false;
|
|
if (country.CountryName != null && !IsValidValue(country.CountryName, "name", (long)SaxDbDfn_Reference.GetColTextLen(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryName))) return false;
|
|
countriesToUpdate.Add(country);
|
|
}
|
|
int MaxBulkUpdateBatchSize = 64;
|
|
int batchSize = countriesToUpdate.Any() ? (int)Math.Ceiling(countriesToUpdate.Count() / Math.Ceiling((double)countriesToUpdate.Count() / MaxBulkUpdateBatchSize)) : MaxBulkUpdateBatchSize;
|
|
while (countriesToUpdate.Any())
|
|
{
|
|
List<String> query = new List<string>();
|
|
query.Add($"Declare @statusActive VarChar(3) = '{SacsSystem.GetStatusCode(SaxGolfUsgaCountry.DefaultStatus)}';");
|
|
query.Add($"Declare @updateOn DateTime = GetDate();");
|
|
query.Add($"Declare @updateBy Int = {SysId.ToLong(dbUpdateInfo.UserId)};");
|
|
query.Add($"Declare @updateHost VarChar(100) = '{DbQuery.FormatForCondition(dbUpdateInfo.Host)}';");
|
|
query.Add("Merge");
|
|
query.Add($"Into {SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaCountry)} uCty");
|
|
query.Add("Using");
|
|
query.Add("(");
|
|
query.Add("Select '' CountryCode, '' EntCountryCode, '' CountryName");
|
|
foreach (UsgaCountryData country in countriesToUpdate.ExtractRange(0, batchSize))
|
|
query.Add($"Union Select '{DbQuery.FormatForCondition(country.CountryCode)}', " + (country.EntCountryCode != null ? $"'{DbQuery.FormatForCondition(country.EntCountryCode)}'" : "Null") + ", " + (country.CountryName != null ? $"'{DbQuery.FormatForCondition(country.CountryName)}'" : "Null"));
|
|
query.Add(") dCty");
|
|
query.Add("On uCty.CountryCode=dCty.CountryCode");
|
|
// Always update countries so that they are time stamped.
|
|
// Was: query.Add("When Matched And ((dCty.EntCountryCode Is Not Null And uCty.EntCountryCode!=dCty.EntCountryCode) Or (dCty.CountryName Is Not Null And uCty.CountryName!=dCty.CountryName) Or uCty.Status!=@statusActive)");
|
|
query.Add("When Matched");
|
|
query.Add("Then Update Set");
|
|
query.Add(" EntCountryCode=Coalesce(dCty.EntCountryCode,uCty.EntCountryCode),");
|
|
query.Add(" CountryName=Coalesce(dCty.CountryName,uCty.CountryName),");
|
|
query.Add(" Status=@statusActive,");
|
|
query.Add(" UpdateOn=@updateOn,");
|
|
query.Add(" UpdateBy=@updateBy,");
|
|
query.Add(" UpdateHost=@updateHost");
|
|
query.Add("When Not Matched And Len(IsNull(dCty.CountryName,''))>0");
|
|
query.Add("Then Insert (CountryCode,EntCountryCode,CountryName,Notes,Flags,Status,CreateOn,CreateBy,CreateHost,UpdateOn,UpdateBy,UpdateHost)");
|
|
query.Add($"Values (dCty.CountryCode,Coalesce(dCty.EntCountryCode,''),dCty.CountryName,'',0,@statusActive,@updateOn,@updateBy,@updateHost,@updateOn,@updateBy,@updateHost)");
|
|
query.Add(";");
|
|
//SaLogger.Info($"{SysUtil.Fn} Bulk USGA Country update query:\n\t{query.Concat("\n\t")}");
|
|
|
|
try
|
|
{
|
|
int result = ReferenceDatabase.ExecuteNonQuery(query.Concat("\n"));
|
|
if (ReferenceDatabase.HasErrorState())
|
|
SaLogger.Warn($"{SysUtil.Fn} Error executing bulk USGA Country update - {ReferenceDatabase.ErrorStateMessage}");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Warn(SysUtil.Fn + " " + ExceptionUtil.GetLogMessage(exc, " executing bulk USGA Country update"));
|
|
}
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item column definitions.
|
|
private static readonly String aliasUsgaCountry = "UsgPrv";
|
|
private static readonly String aliasUsgaCountry_Dummy = "UsgPrv_Cty";
|
|
private static readonly String prefixUsgaCountry_Dummy = aliasUsgaCountry_Dummy + "_";
|
|
private static readonly String colCountry_Province_Count = "Cty_Prv_Count";
|
|
private static readonly String colCountry_Province_Active_Count = "Cty_Prv_Act_Count";
|
|
private static readonly String colCountry_Facility_Count = "Cty_Fty_Count";
|
|
private static readonly String colCountry_Facility_Active_Count = "Cty_Fty_Act_Count";
|
|
private static readonly String colCountry_Courses_Percent = "Cty_Crs_Perc";
|
|
private void SetUsgaCountryColDfn
|
|
(ColumnDfn col, String caption, int width, SaxDbDfn_Reference.ColumnIdx dbCol,
|
|
ColumnAlignment align = ColumnAlignment.Default, DataValue.DataType dataType = DataValue.DataType.String,
|
|
String alias = "", String prefix = "")
|
|
{
|
|
if (String.IsNullOrEmpty(alias) && SaxDbDfn_Reference.GetTableForColumn(dbCol) == SaxDbDfn_Reference.TableIdx.UsgaCountry)
|
|
alias = aliasUsgaCountry;
|
|
col.Set(caption, width, (int)dbCol, prefix + SaxDbDfn_Reference.GetColumnName(dbCol, false), SaxDbDfn_Reference.GetColumnNameQual(dbCol, alias), dataType, align);
|
|
if (dataType == DataValue.DataType.String && dbCol != SaxDbDfn_Reference.ColumnIdx.Unknown)
|
|
col.MaxLength = (int)SaxDbDfn_Reference.GetColTextLen(dbCol);
|
|
}
|
|
public override ColumnDfn GetUsgaCountryColumnDfn(SaxGolfUsgaCountry.Column columnId, string caption = "", int width = 0)
|
|
{
|
|
ColumnDfn columnDfn = new ColumnDfn((int)columnId, caption, width);
|
|
switch (columnId)
|
|
{
|
|
//-----------------------------------------------------------
|
|
case SaxGolfUsgaCountry.Column.Pos:
|
|
SetUsgaCountryColDfn(columnDfn, "Pos", 60, SaxDbDfn_Reference.ColumnIdx.Unknown, ColumnAlignment.Right);
|
|
columnDfn.SortMode = ColumnDfn.ColumnSortMode.Off;
|
|
columnDfn.ReadOnly = true;
|
|
columnDfn.Resizable = false;
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.Id:
|
|
SetUsgaCountryColDfn(columnDfn, "Id", 80, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Id, ColumnAlignment.Right, DataValue.DataType.Long);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
|
|
//-------------------------------------------------------
|
|
case SaxGolfUsgaCountry.Column.CountryCode:
|
|
SetUsgaCountryColDfn(columnDfn, "Code", 80, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryCode, ColumnAlignment.Center);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.CountryName:
|
|
SetUsgaCountryColDfn(columnDfn, "Name", 120, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryName, ColumnAlignment.Center);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.ProvincesUpdateAt_Date:
|
|
SetUsgaCountryColDfn(columnDfn, "Provinces updated", 120, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_ProvincesUpdateAt, ColumnAlignment.Center, DataValue.DataType.Date);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
|
|
//-----------------------------------------------------------
|
|
// Derived properties.
|
|
case SaxGolfUsgaCountry.Column.ProvinceCount_All:
|
|
SetUsgaCountryColDfn(columnDfn, "Provinces", 60, SaxDbDfn_Reference.ColumnIdx.Unknown, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
columnDfn.DataName = colCountry_Province_Count;
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.ProvinceCount_Active:
|
|
SetUsgaCountryColDfn(columnDfn, "Provinces (act)", 60, SaxDbDfn_Reference.ColumnIdx.Unknown, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
columnDfn.DataName = colCountry_Province_Active_Count;
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.FacilityCount_All:
|
|
SetUsgaCountryColDfn(columnDfn, "Facilities", 60, SaxDbDfn_Reference.ColumnIdx.Unknown, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
columnDfn.DataName = colCountry_Facility_Count;
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.FacilityCount_Active:
|
|
SetUsgaCountryColDfn(columnDfn, "Facilities (act)", 60, SaxDbDfn_Reference.ColumnIdx.Unknown, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
columnDfn.DataName = colCountry_Facility_Active_Count;
|
|
break;
|
|
|
|
case SaxGolfUsgaCountry.Column.FacilityPercent:
|
|
SetUsgaCountryColDfn(columnDfn, "Courses %", 60, SaxDbDfn_Reference.ColumnIdx.Unknown, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
columnDfn.DataName = colCountry_Courses_Percent;
|
|
break;
|
|
|
|
//-----------------------------------------------------------
|
|
// Common properties.
|
|
case SaxGolfUsgaCountry.Column.Notes:
|
|
SetUsgaCountryColDfn(columnDfn, "Notes", 240, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Notes);
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.Flags:
|
|
SetUsgaCountryColDfn(columnDfn, "Flags", 60, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Flags, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.Status:
|
|
SetUsgaCountryColDfn(columnDfn, "Status", 80, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Status, ColumnAlignment.Center);
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.Status_Desc:
|
|
SetUsgaCountryColDfn(columnDfn, "Status", 90, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Status, ColumnAlignment.Center);
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.Status_Ind:
|
|
SetUsgaCountryColDfn(columnDfn, "Status", 80, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Status, ColumnAlignment.Center, DataValue.DataType.TriState);
|
|
columnDfn.TriStateFormat = ColumnTriStateFormat.Lights;
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.CreateOn_Date:
|
|
SetUsgaCountryColDfn(columnDfn, "Created", 80, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CreateOn, ColumnAlignment.Center, DataValue.DataType.Date);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SaxGolfUsgaCountry.Column.UpdateOn_Date:
|
|
SetUsgaCountryColDfn(columnDfn, "Updated", 80, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_UpdateOn, ColumnAlignment.Center, DataValue.DataType.Date);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
//-----------------------------------------------------------
|
|
}
|
|
if (!String.IsNullOrEmpty(caption))
|
|
columnDfn.Caption = caption;
|
|
if (width > 0)
|
|
columnDfn.Width = width;
|
|
return columnDfn;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item query filter.
|
|
public override List<string> GetUsgaCountryQueryFilter(SaxGolfUsgaCountry.Params prm)
|
|
{
|
|
List<String> filter = new List<String>();
|
|
String col_Id = SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Id, aliasUsgaCountry);
|
|
|
|
// Item Ids.
|
|
if (prm.IdsIncluded.HasValidIds())
|
|
filter.Add($"{col_Id} IN ({prm.IdsIncluded.Concat(",")})");
|
|
if (prm.IdsExcluded.HasValidIds())
|
|
filter.Add($"{col_Id} NOT IN ({prm.IdsExcluded.Concat(",")})");
|
|
|
|
// Country code.
|
|
if (prm.CountryCodesIncluded.Any(item => !StringUtil.IsBlank(item)))
|
|
filter.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryCode, aliasUsgaCountry)} In ({prm.CountryCodesIncluded.Select(item => DbQuery.FormatForCondition(item)).Concat(",","'","'")})");
|
|
|
|
if (!StringUtil.IsBlank(prm.CountryFilter))
|
|
{
|
|
// Split filter into words.
|
|
List<String> filterWords = prm.CountryFilter.Split((char[])null).ToList();
|
|
filterWords.RemoveDuplicatesCI();
|
|
filterWords.RemoveBlanks();
|
|
foreach (String filterWord in filterWords)
|
|
{
|
|
List<String> countryFilter = new List<String>();
|
|
countryFilter.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryCode, aliasUsgaCountry)} Like '%{DbQuery.FormatForLike(filterWord, true)}%'");
|
|
countryFilter.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryName, aliasUsgaCountry)} Like '%{DbQuery.FormatForLike(filterWord, true)}%'");
|
|
filter.Add(countryFilter.Concat(" OR ", "(", ")"));
|
|
}
|
|
}
|
|
|
|
// Name.
|
|
if (!StringUtil.IsEmpty(prm.CountryNameFull))
|
|
filter.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryName, aliasUsgaCountry)}='{DbQuery.FormatForCondition(prm.CountryNameFull)}'");
|
|
if (!StringUtil.IsEmpty(prm.CountryNamePartial))
|
|
filter.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryName, aliasUsgaCountry)} LIKE '%{DbQuery.FormatForLike(prm.CountryNamePartial)}%'");
|
|
|
|
// Flags.
|
|
if (prm.PriorityFlagState != SacsHierarchyItem.Params.FlagState.Either)
|
|
filter.Add(DbQuery.BuildBitMappedFilter(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Flags, aliasUsgaCountry), SaxGolfUsgaCountry.Flag_Priority, prm.PriorityFlagState == SacsHierarchyItem.Params.FlagState.True));
|
|
|
|
// Associated data.
|
|
if (prm.HasProvinces != SacsHierarchyItem.Params.BoolState.Either)
|
|
{
|
|
String existsPrefix = (prm.HasProvinces == SacsHierarchyItem.Params.BoolState.True ? "" : "Not ");
|
|
filter.Add($"{existsPrefix} Exists (Select 1 From {SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaProvince)} Where {SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_CountryId)}={col_Id})");
|
|
}
|
|
|
|
// Notes.
|
|
if (!StringUtil.IsEmpty(prm.NotesPartial))
|
|
filter.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Notes, aliasUsgaCountry)} LIKE '%{DbQuery.FormatForLike(prm.NotesPartial)}%'");
|
|
|
|
// Status.
|
|
if (prm.StatusCat != SaxGolfUsgaCountry.StatusCat.Any)
|
|
filter.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Status, aliasUsgaCountry)} {GetUsgaCountryStatusCatFilterCondition(prm.StatusCat)}");
|
|
|
|
if (filter.Count < 1 && prm.IncludeDefaultFilter) filter.Add("1=1");
|
|
return filter;
|
|
}
|
|
internal static String GetUsgaCountryStatusCatFilterCondition(SaxGolfUsgaCountry.StatusCat statusCat)
|
|
{
|
|
if (statusCat == SaxGolfUsgaCountry.StatusCat.ActiveOnly)
|
|
return $"='{SacsSystem.GetStatusCode(SaStatus.Active)}'";
|
|
if (statusCat == SaxGolfUsgaCountry.StatusCat.InactiveOnly)
|
|
return $"='{SacsSystem.GetStatusCode(SaStatus.Inactive)}'";
|
|
if (statusCat == SaxGolfUsgaCountry.StatusCat.NotDeleted)
|
|
return $"!='{SacsSystem.GetStatusCode(SaStatus.Deleted)}'";
|
|
if (statusCat == SaxGolfUsgaCountry.StatusCat.DeletedOnly)
|
|
return $"='{SacsSystem.GetStatusCode(SaStatus.Deleted)}'";
|
|
return "";
|
|
}
|
|
// Item sort columns.
|
|
public List<String> GetUsgaCountrySortColumns(SaxGolfUsgaCountry.Params prm)
|
|
{
|
|
// Sort sequence:
|
|
HashSet<String> sortColumnNames = new HashSet<String>();
|
|
foreach (var columnDfn in prm.SortColumns)
|
|
{
|
|
// Need to ensure correct alias used for UsgaCountry columns in case of conflict.
|
|
String sortCol = String.Empty;
|
|
if (SaxDbDfn_Reference.GetTableForColumn((SaxDbDfn_Reference.ColumnIdx)columnDfn.DbColumn) == SaxDbDfn_Reference.TableIdx.UsgaCountry)
|
|
{
|
|
sortCol = SaxDbDfn_Reference.GetColumnNameQual((SaxDbDfn_Reference.ColumnIdx)columnDfn.DbColumn, aliasUsgaCountry);
|
|
}
|
|
else
|
|
{
|
|
sortCol = columnDfn.DataName;
|
|
if (!StringUtil.IsEmpty(columnDfn.SortDataName))
|
|
sortCol = columnDfn.SortDataName;
|
|
}
|
|
if (!sortCol.IsEmpty())
|
|
{
|
|
DbOrderByNullMode orderByNullMode = DbOrderByNullMode.Default; // eSaOrderByDemoteNulls, eSaOrderByPromoteNulls
|
|
sortColumnNames.Add(DbQuery.FormatForOrderBy(sortCol, columnDfn.SortAscending, orderByNullMode).Concat(", "));
|
|
}
|
|
}
|
|
sortColumnNames.RemoveBlanks();
|
|
AddRandomSortColumn(prm, sortColumnNames);
|
|
if (sortColumnNames.IsEmpty())
|
|
{
|
|
// Default sort columns.
|
|
sortColumnNames.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Id, aliasUsgaCountry) + " ASC");
|
|
}
|
|
return sortColumnNames.ToList();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item count.
|
|
private long GetUsgaCountryCount(String filter)
|
|
{
|
|
ClearErrorState();
|
|
return ReferenceDatabase.GetRowCount(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaCountry) + " As " + aliasUsgaCountry, filter);
|
|
}
|
|
public override long GetUsgaCountryCount(SaxGolfUsgaCountry.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
return GetUsgaCountryCount(GetUsgaCountryQueryFilter(prm).Concat(" AND ", "(", ")"));
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item list.
|
|
public override List<SaxGolfUsgaCountry> GetUsgaCountryList(SaxGolfUsgaCountry.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
List<SaxGolfUsgaCountry> userRoles = new List<SaxGolfUsgaCountry>();
|
|
DbQuery qry = new DbQuery(ReferenceDatabase.DbmsType);
|
|
qry.Tables.Add(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaCountry), aliasUsgaCountry);
|
|
Golf_UsgaCountry_Db dbUsgaCountry = new Golf_UsgaCountry_Db(ReferenceDbInfo);
|
|
qry.Columns.AddRange(dbUsgaCountry.ExistingColumnNames);
|
|
qry.Filters.AddRange(GetUsgaCountryQueryFilter(prm));
|
|
qry.OrderBy.AddRange(GetUsgaCountrySortColumns(prm));
|
|
// Query data.
|
|
using (var dbConnection = ReferenceDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
dbCommand.CommandText = qry.BuildQuery(prm.ResultsPaging);
|
|
using (DbDataReader dbDataReader = dbCommand.ExecuteReader())
|
|
{
|
|
while (dbDataReader.Read())
|
|
{
|
|
dbUsgaCountry = new Golf_UsgaCountry_Db(ReferenceDbInfo);
|
|
dbUsgaCountry.PopulateFrom(dbDataReader);
|
|
userRoles.Add(CreateUsgaCountry(dbUsgaCountry));
|
|
}
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SetErrorState("Database query exception");
|
|
SaLogger.Warn($"{SysUtil.Fn}\n\tQuery: {dbCommand.CommandText}\n\t{ExceptionUtil.GetErrorStateMessage(exc)}");
|
|
}
|
|
}
|
|
return userRoles;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item query execution.
|
|
public override DbResultSet ExecuteUsgaCountryQuery(List<ColumnDfn> columns, SaxGolfUsgaCountry.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
|
|
//---------------------------------------------------------------------
|
|
// Construct query.
|
|
DbQuery query = new DbQuery(ReferenceDatabase.DbmsType);
|
|
query.Tables.Add(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaCountry), aliasUsgaCountry);
|
|
|
|
// Add columns as defined in query list.
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.Id))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Id, aliasUsgaCountry));
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.CountryCode))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryCode, aliasUsgaCountry));
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.CountryName))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryName, aliasUsgaCountry));
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.ProvincesUpdateAt_Date))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_ProvincesUpdateAt, aliasUsgaCountry));
|
|
// Common properties.
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.Notes))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Notes, aliasUsgaCountry));
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.Flags))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Flags, aliasUsgaCountry));
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.Status) || columns.HasId(SaxGolfUsgaCountry.Column.Status_Desc) || columns.HasId(SaxGolfUsgaCountry.Column.Status_Ind))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Status, aliasUsgaCountry));
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.CreateOn_Date))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CreateOn, aliasUsgaCountry));
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.UpdateOn_Date))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_UpdateOn, aliasUsgaCountry));
|
|
|
|
//---------------------------------------------------------------------
|
|
// Province count.
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.ProvinceCount_All))
|
|
{
|
|
String aliasProvince = "aCtyPrv_Count";
|
|
DbQuery queryProvince = new DbQuery(CoreDatabase.DbmsType);
|
|
queryProvince.Tables.Add(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaProvince), aliasProvince);
|
|
queryProvince.Columns.Add("Count(*)");
|
|
queryProvince.Filters.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_CountryId, aliasProvince) + "=" + SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Id, aliasUsgaCountry));
|
|
query.Columns.Add("(" + queryProvince.BuildQuery() + ")", colCountry_Province_Count);
|
|
}
|
|
|
|
// Active Province count.
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.ProvinceCount_Active))
|
|
{
|
|
String aliasProvince = "aCtyPrvAct_Count";
|
|
DbQuery queryProvince = new DbQuery(CoreDatabase.DbmsType);
|
|
queryProvince.Tables.Add(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaProvince), aliasProvince);
|
|
queryProvince.Columns.Add("Count(*)");
|
|
queryProvince.Filters.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_CountryId, aliasProvince) + "=" + SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Id, aliasUsgaCountry));
|
|
queryProvince.Filters.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Status, aliasProvince) + "='" + SacsSystem.GetStatusCode(SaStatus.Active) + "'");
|
|
query.Columns.Add("(" + queryProvince.BuildQuery() + ")", colCountry_Province_Active_Count);
|
|
}
|
|
|
|
//---------------------------------------------------------------------
|
|
// Facility count.
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.ProvinceCount_All))
|
|
{
|
|
String aliasProvince = "aCtyFty_Count_Prv";
|
|
DbQuery queryProvince = new DbQuery(CoreDatabase.DbmsType);
|
|
queryProvince.Tables.Add(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaProvince), aliasProvince);
|
|
queryProvince.Columns.Add("Count(*)");
|
|
queryProvince.Filters.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_CountryId, aliasProvince) + "=" + SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Id, aliasUsgaCountry));
|
|
// Join to Facility.
|
|
String aliasFacility = "aCtyFty_Count_Fty";
|
|
DbQueryJoin joinFacility = new DbQueryJoin(DbJoinType.Inner);
|
|
joinFacility.Table = new StringPair(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaFacility), aliasFacility);
|
|
joinFacility.Filters.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaFacility_ProvinceId, aliasFacility) + "=" + SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Id, aliasProvince));
|
|
queryProvince.Joins.Add(joinFacility);
|
|
// Construct sub-query.
|
|
query.Columns.Add("(" + queryProvince.BuildQuery() + ")", colCountry_Facility_Count);
|
|
}
|
|
|
|
// Active Facility count.
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.ProvinceCount_Active))
|
|
{
|
|
String aliasProvince = "aCtyFty_Count_Prv";
|
|
DbQuery queryProvince = new DbQuery(CoreDatabase.DbmsType);
|
|
queryProvince.Tables.Add(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaProvince), aliasProvince);
|
|
queryProvince.Columns.Add("Count(*)");
|
|
queryProvince.Filters.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_CountryId, aliasProvince)}={SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Id, aliasUsgaCountry)}");
|
|
queryProvince.Filters.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Status, aliasProvince)}='{SacsSystem.GetStatusCode(SaStatus.Active)}'");
|
|
// Join to Facility.
|
|
String aliasFacility = "aCtyFty_Count_Fty";
|
|
DbQueryJoin joinFacility = new DbQueryJoin(DbJoinType.Inner);
|
|
joinFacility.Table = new StringPair(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaFacility), aliasFacility);
|
|
joinFacility.Filters.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaFacility_ProvinceId, aliasFacility)}={SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Id, aliasProvince)}");
|
|
joinFacility.Filters.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaFacility_Status, aliasFacility)}='{SacsSystem.GetStatusCode(SaStatus.Active)}'");
|
|
queryProvince.Joins.Add(joinFacility);
|
|
// Construct sub-query.
|
|
query.Columns.Add("(" + queryProvince.BuildQuery() + ")", colCountry_Facility_Active_Count);
|
|
}
|
|
|
|
//---------------------------------------------------------------------
|
|
// Facility population %.
|
|
if (columns.HasId(SaxGolfUsgaCountry.Column.FacilityPercent))
|
|
{
|
|
// Sub query to count provinces & provinces with facilities, and work out the percent with facilities.
|
|
String alias_Province = "prv";
|
|
String alias_Facility = "fty";
|
|
String table_Province = $"{SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaProvince)} {alias_Province}";
|
|
String table_Facility = $"{SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaFacility)} {alias_Facility}";
|
|
String col_Country_Id = SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Id, aliasUsgaCountry);
|
|
String col_Province_Id = SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Id, alias_Province);
|
|
String col_Province_CountryId = SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_CountryId, alias_Province);
|
|
String col_Facility_ProvinceId = SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaFacility_ProvinceId, alias_Facility);
|
|
String name_ProvinceCount = "ProvinceCount";
|
|
String name_ProvincesWithFacilities = "ProvincesWithFacilities";
|
|
List<String> subQuery = new List<String>();
|
|
subQuery.Add($"Select Convert (decimal(9,2), Case When {name_ProvinceCount} > 0 Then 100.0 * (1.0 * {name_ProvincesWithFacilities} / {name_ProvinceCount}) Else 0 End)");
|
|
subQuery.Add($"From");
|
|
subQuery.Add($"(");
|
|
subQuery.Add($"Select");
|
|
subQuery.Add($"{name_ProvinceCount} = (select Count(*) From {table_Province} Where {col_Province_CountryId}={col_Country_Id}),");
|
|
subQuery.Add($"{name_ProvincesWithFacilities} = (select Count(*) From {table_Province} Where {col_Province_CountryId}={col_Country_Id} and {col_Province_Id} in (Select {col_Facility_ProvinceId} from {table_Facility}))");
|
|
subQuery.Add($") dat");
|
|
query.Columns.Add($"({subQuery.Concat(" ")})", colCountry_Courses_Percent);
|
|
}
|
|
|
|
//---------------------------------------------------------------------
|
|
// Populate filter.
|
|
query.Filters.AddRange(GetUsgaCountryQueryFilter(prm));
|
|
|
|
if (prm.IdsIncluded.Count != 1)
|
|
{
|
|
// Configure sort sequence.
|
|
foreach (String sortColumn in GetUsgaCountrySortColumns(prm))
|
|
query.OrderBy.Add(sortColumn);
|
|
}
|
|
|
|
//---------------------------------------------------------------------
|
|
//return new DbResultSet_DataTableReader(db, query.BuildQuery(prm.ResultsPaging));
|
|
return DbResultSet_DataReader.ExecuteQuery(ReferenceDatabase, query.BuildQuery(prm.ResultsPaging));
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|