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>
773 lines
41 KiB
C#
773 lines
41 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(SaxGolfUsgaProvince usgaProvince)
|
|
{
|
|
return new List<StringPair>()
|
|
{
|
|
new StringPair("Id",SysId.ToString(usgaProvince.Id)),
|
|
new StringPair("Country Id",SysId.ToString(usgaProvince.CountryId)),
|
|
new StringPair("Province code",usgaProvince.ProvinceCode),
|
|
new StringPair("Province name",usgaProvince.ProvinceName),
|
|
new StringPair("Status",usgaProvince.StatusCode)
|
|
};
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Create item.
|
|
public override SaxGolfUsgaProvince CreateUsgaProvince(SysId countryId)
|
|
{
|
|
ClearErrorState();
|
|
return new SaxGolfUsgaProvince_Dbas(ReferenceDatabase, null, countryId, 0);
|
|
}
|
|
private SaxGolfUsgaProvince_Dbas CreateUsgaProvince(Golf_UsgaProvince_Db dbUsgaProvince)
|
|
{
|
|
ClearErrorState();
|
|
SaxGolfUsgaProvince_Dbas usgaProvince = new SaxGolfUsgaProvince_Dbas(ReferenceDatabase, new SysId(dbUsgaProvince._Id), new SysId(dbUsgaProvince._CountryId), (ushort)dbUsgaProvince._Flags);
|
|
PopulateFrom(dbUsgaProvince, usgaProvince);
|
|
return usgaProvince;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get item.
|
|
public override SaxGolfUsgaProvince GetUsgaProvince(SaxGolfUsgaProvince.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
return GetUsgaProvince(GetUsgaProvinceQueryFilter(prm).Concat(" And ", "(", ")"));
|
|
}
|
|
private SaxGolfUsgaProvince GetUsgaProvince(String filter)
|
|
{
|
|
ClearErrorState();
|
|
long count = GetUsgaProvinceCount(filter);
|
|
if (count == 0) return null;
|
|
if (count != 1)
|
|
{
|
|
SetErrorState("Multiple items found");
|
|
return null;
|
|
}
|
|
SaxGolfUsgaProvince usgaProvince = null;
|
|
// Query data.
|
|
using (var dbConnection = ReferenceDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
Golf_UsgaProvince_Db dbUsgaProvince = new Golf_UsgaProvince_Db(ReferenceDbInfo);
|
|
dbUsgaProvince.PopulateFrom(dbCommand, aliasUsgaProvince, filter);
|
|
usgaProvince = CreateUsgaProvince(dbUsgaProvince);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SetErrorState("Database query exception");
|
|
SaLogger.Warn($"{SysUtil.Fn}\n\tQuery: {dbCommand.CommandText}\n\t{ExceptionUtil.GetErrorStateMessage(exc)}");
|
|
}
|
|
}
|
|
return usgaProvince;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Validate item.
|
|
public override bool IsValidUsgaProvinceData(SaxGolfUsgaProvince.Data data, SaxGolfUsgaProvince.ErrField errField)
|
|
{
|
|
ClearErrorState();
|
|
if (!base.IsValidUsgaProvinceData(data, errField)) return false;
|
|
return true;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Insert item.
|
|
public override bool InsertUsgaProvince(SaxGolfUsgaProvince usgaProvince, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
String fn = GetType().Name + ".InsertUsgaProvince";
|
|
ClearErrorState();
|
|
if (!IsValidUsgaProvince(usgaProvince)) return false;
|
|
if (SysId.IsValid(usgaProvince.Id)) return SetErrorState(ErrorMessage.ValidItemId);
|
|
using (var dbConnection = ReferenceDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
dbConnection.Open();
|
|
Golf_UsgaProvince_Db dbUsgaProvince = new Golf_UsgaProvince_Db(ReferenceDbInfo);
|
|
PopulateTo(dbUsgaProvince, usgaProvince, dbUpdateInfo);
|
|
if (Golf_UsgaProvince_Db.FieldsNonId.Contains("Id"))
|
|
{
|
|
// Generate next Id.
|
|
dbUsgaProvince._Id = ReferenceDatabase.GetNextIdFromTable("UsgaProvince");
|
|
}
|
|
dbUsgaProvince.ExecuteInsertCommand(dbCommand);
|
|
usgaProvince.Id = new SysId(dbUsgaProvince._Id);
|
|
usgaProvince.Changed = false;
|
|
if (DebugMode) SaLogger.Debug(fn + " Inserted. " + GetLogProperties(usgaProvince).Concat(": ").Concat("", "\n\t"));
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Update item.
|
|
public override bool UpdateUsgaProvince(SaxGolfUsgaProvince usgaProvince, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
String fn = GetType().Name + ".UpdateUsgaProvince";
|
|
ClearErrorState();
|
|
if (!IsValidUsgaProvince(usgaProvince)) return false;
|
|
if (!SysId.IsValid(usgaProvince.Id)) return SetErrorState(ErrorMessage.InvalidItemId);
|
|
if (!usgaProvince.Changed)
|
|
{
|
|
if (DebugMode) SaLogger.Debug(fn + " Data not changed. " + GetLogProperties(usgaProvince).Concat(": ").Concat("", "\n\t"));
|
|
return true;
|
|
}
|
|
using (var dbConnection = ReferenceDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
// Retrieve item.
|
|
dbConnection.Open();
|
|
Golf_UsgaProvince_Db dbUsgaProvince = new Golf_UsgaProvince_Db(ReferenceDbInfo);
|
|
dbUsgaProvince.PopulateFrom(dbCommand, aliasUsgaProvince, "Id=" + SysId.ToString(usgaProvince.Id));
|
|
// ToDo SaxUpdateLogItemMonitor_UsgaProvince updateLogMonitor = new SaxUpdateLogItemMonitor_UsgaProvince(SessionContext, CreateUsgaProvince(dbUsgaProvince), dbUpdateInfo);
|
|
// Update item.
|
|
PopulateTo(dbUsgaProvince, usgaProvince, dbUpdateInfo);
|
|
dbUsgaProvince.ExecuteUpdateCommand(dbCommand);
|
|
usgaProvince.Changed = false;
|
|
if (DebugMode) SaLogger.Debug(fn + " Updated. " + GetLogProperties(usgaProvince).Concat(": ").Concat("", "\n\t"));
|
|
// Record updates.
|
|
// ToDo updateLogMonitor.RecordUpdatedValues(usgaProvince);
|
|
// ToDo updateLogMonitor.LogUpdates();
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Delete item.
|
|
public override bool UsgaProvinceCanBeDeleted(SaxGolfUsgaProvince usgaProvince)
|
|
{
|
|
ClearErrorState();
|
|
if (!base.UsgaProvinceCanBeDeleted(usgaProvince)) return false;
|
|
return true;
|
|
}
|
|
public override bool DeleteUsgaProvince(SaxGolfUsgaProvince usgaProvince, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
String fn = GetType().Name + ".DeleteUsgaProvince";
|
|
ClearErrorState();
|
|
if (!UsgaProvinceCanBeDeleted(usgaProvince)) return false;
|
|
|
|
// User Roles are logically deleted by changing the status.
|
|
usgaProvince.Status = SaStatus.Deleted;
|
|
return UpdateUsgaProvince(usgaProvince, dbUpdateInfo);
|
|
|
|
/*
|
|
* Physical deletion.
|
|
using (var dbConnection = ReferenceDatabase.CreateConnection())
|
|
using (var dbCommand = dbConnection.CreateCommand())
|
|
{
|
|
try
|
|
{
|
|
// Retrieve item.
|
|
dbConnection.Open();
|
|
Golf_UsgaProvince_Db dbUsgaProvince = new Golf_UsgaProvince_Db(ReferenceDbInfo);
|
|
dbUsgaProvince.PopulateFrom(dbCommand, aliasUsgaProvince, "Id=" + usgaProvince.Id.ToString());
|
|
// Delete item.
|
|
dbCommand.CommandType = System.Data.CommandType.Text;
|
|
dbCommand.CommandText = "DELETE FROM " + SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaProvince) + " WHERE Id=" + SysId.ToString(usgaProvince.Id) + ";";
|
|
dbCommand.ExecuteNonQuery();
|
|
usgaProvince.Changed = false;
|
|
if (DebugMode) SaLogger.Debug(fn + " Deleted. " + GetLogProperties(usgaProvince).Concat(": ").Concat("", "\n\t"));
|
|
}
|
|
catch (Exception exc) { SetErrorState(exc.Message); }
|
|
}
|
|
return !HasErrorState();
|
|
*/
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Copy data properties to and from database.
|
|
private void PopulateTo(Golf_UsgaProvince_Db dbUsgaProvince, SaxGolfUsgaProvince usgaProvince, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
dbUsgaProvince._Id = SysId.ToLong(usgaProvince.Id);
|
|
dbUsgaProvince._CountryId = SysId.ToInt(usgaProvince.CountryId);
|
|
dbUsgaProvince._ProvinceCode = usgaProvince.ProvinceCode;
|
|
dbUsgaProvince._EntProvinceCode = usgaProvince.EntProvinceCode;
|
|
dbUsgaProvince._ProvinceName = usgaProvince.ProvinceName;
|
|
dbUsgaProvince._CoursesUpdateAt = usgaProvince.CoursesUpdateAt;
|
|
// Common.
|
|
dbUsgaProvince._Notes = usgaProvince.Notes;
|
|
dbUsgaProvince._Status = SacsSystem.GetStatusCode(usgaProvince.Status);
|
|
dbUsgaProvince._Flags = (short)usgaProvince.Flags;
|
|
if (!SysId.IsValid(usgaProvince.Id))
|
|
{
|
|
dbUsgaProvince._CreateOn = DateTime.Now;
|
|
dbUsgaProvince._CreateBy = dbUpdateInfo.UserId.ToInt();
|
|
dbUsgaProvince._CreateHost = dbUpdateInfo.Host;
|
|
}
|
|
dbUsgaProvince._UpdateOn = DateTime.Now;
|
|
dbUsgaProvince._UpdateBy = dbUpdateInfo.UserId.ToInt();
|
|
dbUsgaProvince._UpdateHost = dbUpdateInfo.Host;
|
|
}
|
|
private void PopulateFrom(Golf_UsgaProvince_Db dbUsgaProvince, SaxGolfUsgaProvince usgaProvince)
|
|
{
|
|
//usgaProvince.Id = new SysId(dbUsgaProvince._Id);
|
|
usgaProvince.CountryId = new SysId(dbUsgaProvince._CountryId);
|
|
usgaProvince.ProvinceCode = dbUsgaProvince._ProvinceCode;
|
|
usgaProvince.EntProvinceCode = dbUsgaProvince._EntProvinceCode;
|
|
usgaProvince.ProvinceName = dbUsgaProvince._ProvinceName;
|
|
usgaProvince.CoursesUpdateAt = dbUsgaProvince._CoursesUpdateAt;
|
|
// Common.
|
|
usgaProvince.Notes = dbUsgaProvince._Notes;
|
|
usgaProvince.Status = SacsSystem.GetStatusForCode(dbUsgaProvince._Status);
|
|
usgaProvince.SetCreateStamp(dbUsgaProvince._CreateOn, new SysId(dbUsgaProvince._CreateBy), dbUsgaProvince._CreateHost);
|
|
usgaProvince.SetUpdateStamp(dbUsgaProvince._UpdateOn, new SysId(dbUsgaProvince._UpdateBy), dbUsgaProvince._UpdateHost);
|
|
usgaProvince.AcceptChanges();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Bulk updates.
|
|
/// <summary>
|
|
/// Bulk update USGA Provinces.
|
|
/// <para>- Country code must be supplied to identify parent.</para>
|
|
/// <para>- Province code must be supplied to act as identifier.</para>
|
|
/// <para>- Other properties are validated as per province rules - check error state.</para>
|
|
/// <para>- Other properties are left as is if null.</para>
|
|
/// </summary>
|
|
public override bool BulkUpdateUsgaProvinces(List<UsgaProvinceData> provinces, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
ClearErrorState();
|
|
List<UsgaProvinceData> provincesToUpdate = new List<UsgaProvinceData>();
|
|
|
|
// Validate supplied data.
|
|
foreach (UsgaProvinceData province in provinces)
|
|
{
|
|
// Validate province properties.
|
|
// Country and Province Codes are mandatory, other values only validated if non-null.
|
|
if (!IsValidValue(province.CountryCode, "country", (long)SaxDbDfn_Reference.GetColTextLen(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryCode), true)) return false;
|
|
if (!IsValidValue(province.ProvinceCode, "code", (long)SaxDbDfn_Reference.GetColTextLen(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_ProvinceCode), true)) return false;
|
|
if (!IsValidValue(province.EntProvinceCode, "ENT code", (long)SaxDbDfn_Reference.GetColTextLen(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_EntProvinceCode))) return false;
|
|
if (province.ProvinceName != null && !IsValidValue(province.ProvinceName, "name", (long)SaxDbDfn_Reference.GetColTextLen(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_ProvinceName))) return false;
|
|
provincesToUpdate.Add(province);
|
|
}
|
|
int MaxBulkUpdateBatchSize = 64;
|
|
int batchSize = provincesToUpdate.Any() ? (int)Math.Ceiling(provincesToUpdate.Count() / Math.Ceiling((double)provincesToUpdate.Count() / MaxBulkUpdateBatchSize)) : MaxBulkUpdateBatchSize;
|
|
while (provincesToUpdate.Any())
|
|
{
|
|
List<String> query = new List<string>();
|
|
query.Add($"Declare @statusActive VarChar(3) = '{SacsSystem.GetStatusCode(SaxGolfUsgaProvince.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.UsgaProvince)} uPrv");
|
|
query.Add("Using");
|
|
query.Add("(");
|
|
query.Add("Select dCty.Id CountryId, dPrv.CountryCode, dPrv.ProvinceCode, dPrv.EntProvinceCode, dPrv.ProvinceName");
|
|
query.Add("From");
|
|
query.Add("(");
|
|
query.Add("Select '' CountryCode, '' ProvinceCode, '' EntProvinceCode, '' ProvinceName");
|
|
foreach (UsgaProvinceData province in provincesToUpdate.ExtractRange(0, batchSize))
|
|
query.Add($"Union Select '{DbQuery.FormatForCondition(province.CountryCode)}', '{DbQuery.FormatForCondition(province.ProvinceCode)}', " + (province.EntProvinceCode != null ? $"'{DbQuery.FormatForCondition(province.EntProvinceCode)}'" : "Null") + "," + (province.ProvinceName != null ? $"'{DbQuery.FormatForCondition(province.ProvinceName)}'" : "Null"));
|
|
query.Add(") dPrv");
|
|
query.Add($"Inner Join {SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaCountry)} dCty On dCty.CountryCode=dPrv.CountryCode");
|
|
query.Add(") dPrv");
|
|
query.Add("On uPrv.CountryId=dPrv.CountryId And uPrv.ProvinceCode=dPrv.ProvinceCode");
|
|
query.Add("When Matched And ((dPrv.EntProvinceCode Is Not Null And uPrv.EntProvinceCode!=dPrv.EntProvinceCode) Or (dPrv.ProvinceName Is Not Null And uPrv.ProvinceName!=dPrv.ProvinceName) Or uPrv.Status!=@statusActive)");
|
|
query.Add("Then Update Set");
|
|
query.Add(" EntProvinceCode=Coalesce(dPrv.EntProvinceCode,uPrv.EntProvinceCode),");
|
|
query.Add(" ProvinceName=Coalesce(dPrv.ProvinceName,uPrv.ProvinceName),");
|
|
query.Add(" Status=@statusActive,");
|
|
query.Add(" UpdateOn=@updateOn,");
|
|
query.Add(" UpdateBy=@updateBy,");
|
|
query.Add(" UpdateHost=@updateHost");
|
|
query.Add("When Not Matched And Len(IsNull(dPrv.ProvinceName,''))>0");
|
|
query.Add("Then Insert (CountryId,ProvinceCode,EntProvinceCode,ProvinceName,Notes,Flags,Status,CreateOn,CreateBy,CreateHost,UpdateOn,UpdateBy,UpdateHost)");
|
|
query.Add($"Values (dPrv.CountryId,dPrv.ProvinceCode,Coalesce(dPrv.EntProvinceCode,''),dPrv.ProvinceName,'',0,@statusActive,@updateOn,@updateBy,@updateHost,@updateOn,@updateBy,@updateHost)");
|
|
query.Add(";");
|
|
//SaLogger.Info($"{SysUtil.Fn} Bulk USGA Province 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 Province update - {ReferenceDatabase.ErrorStateMessage}");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Warn(SysUtil.Fn + " " + ExceptionUtil.GetLogMessage(exc, " executing bulk USGA Province update"));
|
|
}
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item column definitions.
|
|
private static readonly String aliasUsgaProvince = "UsgPrv";
|
|
private static readonly String aliasUsgaProvince_Country = "UsgPrv_Cty";
|
|
private static readonly String prefixUsgaProvince_Country = aliasUsgaProvince_Country + "_";
|
|
private static readonly String colProvince_Facility_Count = "Prv_Fty_Count";
|
|
private static readonly String colProvince_Facility_Active_Count = "Prv_Fty_Act_Count";
|
|
private static readonly String colProvince_Course_Count = "Prv_Crs_Count";
|
|
private static readonly String colProvince_Course_Active_Count = "Prv_Crs_Act_Count";
|
|
private static readonly String colProvince_Markers_Percent = "Prv_Mkr_Perc";
|
|
private void SetUsgaProvinceColDfn
|
|
(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.UsgaProvince)
|
|
alias = aliasUsgaProvince;
|
|
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 GetUsgaProvinceColumnDfn(SaxGolfUsgaProvince.Column columnId, string caption = "", int width = 0)
|
|
{
|
|
ColumnDfn columnDfn = new ColumnDfn((int)columnId, caption, width);
|
|
switch (columnId)
|
|
{
|
|
//-----------------------------------------------------------
|
|
case SaxGolfUsgaProvince.Column.Pos:
|
|
SetUsgaProvinceColDfn(columnDfn, "Pos", 60, SaxDbDfn_Reference.ColumnIdx.Unknown, ColumnAlignment.Right);
|
|
columnDfn.SortMode = ColumnDfn.ColumnSortMode.Off;
|
|
columnDfn.ReadOnly = true;
|
|
columnDfn.Resizable = false;
|
|
break;
|
|
case SaxGolfUsgaProvince.Column.Id:
|
|
SetUsgaProvinceColDfn(columnDfn, "Id", 80, SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Id, ColumnAlignment.Right, DataValue.DataType.Long);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
|
|
//-------------------------------------------------------
|
|
case SaxGolfUsgaProvince.Column.CountryId:
|
|
SetUsgaProvinceColDfn(columnDfn, "Country #", 80, SaxDbDfn_Reference.ColumnIdx.UsgaProvince_CountryId, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SaxGolfUsgaProvince.Column.Country_Name:
|
|
SetUsgaProvinceColDfn(columnDfn, "Country", 120, SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryName, ColumnAlignment.Center, DataValue.DataType.String, aliasUsgaProvince_Country, prefixUsgaProvince_Country);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
|
|
//-------------------------------------------------------
|
|
case SaxGolfUsgaProvince.Column.ProvinceCode:
|
|
SetUsgaProvinceColDfn(columnDfn, "Code", 80, SaxDbDfn_Reference.ColumnIdx.UsgaProvince_ProvinceCode, ColumnAlignment.Center);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SaxGolfUsgaProvince.Column.ProvinceName:
|
|
SetUsgaProvinceColDfn(columnDfn, "Name", 120, SaxDbDfn_Reference.ColumnIdx.UsgaProvince_ProvinceName, ColumnAlignment.Center);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SaxGolfUsgaProvince.Column.CoursesUpdateAt_Date:
|
|
SetUsgaProvinceColDfn(columnDfn, "Courses updated", 120, SaxDbDfn_Reference.ColumnIdx.UsgaProvince_CoursesUpdateAt, ColumnAlignment.Center, DataValue.DataType.Date);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
|
|
//-----------------------------------------------------------
|
|
// Derived properties.
|
|
case SaxGolfUsgaProvince.Column.FacilityCount_All:
|
|
SetUsgaProvinceColDfn(columnDfn, "Facilities", 60, SaxDbDfn_Reference.ColumnIdx.Unknown, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
columnDfn.DataName = colProvince_Facility_Count;
|
|
break;
|
|
case SaxGolfUsgaProvince.Column.FacilityCount_Active:
|
|
SetUsgaProvinceColDfn(columnDfn, "Facilities (act)", 60, SaxDbDfn_Reference.ColumnIdx.Unknown, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
columnDfn.DataName = colProvince_Facility_Active_Count;
|
|
break;
|
|
case SaxGolfUsgaProvince.Column.CourseCount_All:
|
|
SetUsgaProvinceColDfn(columnDfn, "Courses", 60, SaxDbDfn_Reference.ColumnIdx.Unknown, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
columnDfn.DataName = colProvince_Course_Count;
|
|
break;
|
|
case SaxGolfUsgaProvince.Column.CourseCount_Active:
|
|
SetUsgaProvinceColDfn(columnDfn, "Courses (act)", 60, SaxDbDfn_Reference.ColumnIdx.Unknown, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
columnDfn.DataName = colProvince_Course_Active_Count;
|
|
break;
|
|
|
|
case SaxGolfUsgaProvince.Column.MarkerPercent:
|
|
SetUsgaCountryColDfn(columnDfn, "Markers %", 60, SaxDbDfn_Reference.ColumnIdx.Unknown, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
columnDfn.DataName = colProvince_Markers_Percent;
|
|
break;
|
|
|
|
//-----------------------------------------------------------
|
|
// Common properties.
|
|
case SaxGolfUsgaProvince.Column.Notes:
|
|
SetUsgaProvinceColDfn(columnDfn, "Notes", 240, SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Notes);
|
|
break;
|
|
case SaxGolfUsgaProvince.Column.Flags:
|
|
SetUsgaProvinceColDfn(columnDfn, "Flags", 60, SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Flags, ColumnAlignment.Center, DataValue.DataType.Long);
|
|
break;
|
|
case SaxGolfUsgaProvince.Column.Status:
|
|
SetUsgaProvinceColDfn(columnDfn, "Status", 80, SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Status, ColumnAlignment.Center);
|
|
break;
|
|
case SaxGolfUsgaProvince.Column.Status_Desc:
|
|
SetUsgaProvinceColDfn(columnDfn, "Status", 90, SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Status, ColumnAlignment.Center);
|
|
break;
|
|
case SaxGolfUsgaProvince.Column.Status_Ind:
|
|
SetUsgaProvinceColDfn(columnDfn, "Status", 80, SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Status, ColumnAlignment.Center, DataValue.DataType.TriState);
|
|
columnDfn.TriStateFormat = ColumnTriStateFormat.Lights;
|
|
break;
|
|
case SaxGolfUsgaProvince.Column.CreateOn_Date:
|
|
SetUsgaProvinceColDfn(columnDfn, "Created", 80, SaxDbDfn_Reference.ColumnIdx.UsgaProvince_CreateOn, ColumnAlignment.Center, DataValue.DataType.Date);
|
|
columnDfn.ReadOnly = true;
|
|
break;
|
|
case SaxGolfUsgaProvince.Column.UpdateOn_Date:
|
|
SetUsgaProvinceColDfn(columnDfn, "Updated", 80, SaxDbDfn_Reference.ColumnIdx.UsgaProvince_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> GetUsgaProvinceQueryFilter(SaxGolfUsgaProvince.Params prm)
|
|
{
|
|
List<String> filter = new List<String>();
|
|
String col_Id = SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Id, aliasUsgaProvince);
|
|
|
|
// 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(",")})");
|
|
|
|
// Countries.
|
|
if (prm.CountryIdsIncluded.HasValidIds())
|
|
filter.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_CountryId, aliasUsgaProvince)} IN ({prm.CountryIdsIncluded.Concat(",")})");
|
|
|
|
// Province code.
|
|
if (!StringUtil.IsEmpty(prm.ProvinceCodeFull))
|
|
filter.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_ProvinceCode, aliasUsgaProvince)}='{DbQuery.FormatForCondition(prm.ProvinceCodeFull)}'");
|
|
|
|
if (!StringUtil.IsBlank(prm.ProvinceFilter))
|
|
{
|
|
// Split filter into words.
|
|
List<String> filterWords = prm.ProvinceFilter.Split((char[])null).ToList();
|
|
filterWords.RemoveDuplicatesCI();
|
|
filterWords.RemoveBlanks();
|
|
foreach (String filterWord in filterWords)
|
|
{
|
|
List<String> provinceFilter = new List<String>();
|
|
provinceFilter.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_ProvinceCode, aliasUsgaProvince)} Like '%{DbQuery.FormatForLike(filterWord, true)}%'");
|
|
provinceFilter.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_ProvinceName, aliasUsgaProvince)} Like '%{DbQuery.FormatForLike(filterWord, true)}%'");
|
|
filter.Add(provinceFilter.Concat(" OR ", "(", ")"));
|
|
}
|
|
}
|
|
|
|
// Name.
|
|
if (!StringUtil.IsEmpty(prm.ProvinceNameFull))
|
|
filter.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_ProvinceName, aliasUsgaProvince)}='{DbQuery.FormatForCondition(prm.ProvinceNameFull)}'");
|
|
if (!StringUtil.IsEmpty(prm.ProvinceNamePartial))
|
|
filter.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_ProvinceName, aliasUsgaProvince)} LIKE '%{DbQuery.FormatForLike(prm.ProvinceNamePartial)}%'");
|
|
|
|
// Flags.
|
|
if (prm.PriorityFlagState != SacsHierarchyItem.Params.FlagState.Either)
|
|
filter.Add(DbQuery.BuildBitMappedFilter(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Flags, aliasUsgaProvince), SaxGolfUsgaProvince.Flag_Priority, prm.PriorityFlagState == SacsHierarchyItem.Params.FlagState.True));
|
|
|
|
// Associated data.
|
|
if (prm.HasFacilities != SacsHierarchyItem.Params.BoolState.Either)
|
|
{
|
|
String existsPrefix = (prm.HasFacilities == SacsHierarchyItem.Params.BoolState.True ? "" : "Not ");
|
|
filter.Add($"{existsPrefix} Exists (Select 1 From {SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaFacility)} Where {SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaFacility_ProvinceId)}={col_Id})");
|
|
}
|
|
|
|
// Notes.
|
|
if (!StringUtil.IsEmpty(prm.NotesPartial))
|
|
filter.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Notes, aliasUsgaProvince)} LIKE '%{DbQuery.FormatForLike(prm.NotesPartial)}%'");
|
|
|
|
// Status.
|
|
if (prm.StatusCat != SaxGolfUsgaProvince.StatusCat.Any)
|
|
filter.Add($"{SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Status, aliasUsgaProvince)} {GetUsgaProvinceStatusCatFilterCondition(prm.StatusCat)}");
|
|
|
|
if (filter.Count < 1 && prm.IncludeDefaultFilter) filter.Add("1=1");
|
|
return filter;
|
|
}
|
|
internal static String GetUsgaProvinceStatusCatFilterCondition(SaxGolfUsgaProvince.StatusCat statusCat)
|
|
{
|
|
if (statusCat == SaxGolfUsgaProvince.StatusCat.ActiveOnly)
|
|
return $"='{SacsSystem.GetStatusCode(SaStatus.Active)}'";
|
|
if (statusCat == SaxGolfUsgaProvince.StatusCat.InactiveOnly)
|
|
return $"='{SacsSystem.GetStatusCode(SaStatus.Inactive)}'";
|
|
if (statusCat == SaxGolfUsgaProvince.StatusCat.NotDeleted)
|
|
return $"!='{SacsSystem.GetStatusCode(SaStatus.Deleted)}'";
|
|
if (statusCat == SaxGolfUsgaProvince.StatusCat.DeletedOnly)
|
|
return $"='{SacsSystem.GetStatusCode(SaStatus.Deleted)}'";
|
|
return "";
|
|
}
|
|
// Item sort columns.
|
|
public HashSet<String> GetUsgaProvinceSortColumns(SaxGolfUsgaProvince.Params prm)
|
|
{
|
|
// Sort sequence:
|
|
HashSet<String> sortColumnNames = new HashSet<String>();
|
|
foreach (var columnDfn in prm.SortColumns)
|
|
{
|
|
// Need to ensure correct alias used for UsgaProvince columns in case of conflict.
|
|
String sortCol = String.Empty;
|
|
if (SaxDbDfn_Reference.GetTableForColumn((SaxDbDfn_Reference.ColumnIdx)columnDfn.DbColumn) == SaxDbDfn_Reference.TableIdx.UsgaProvince)
|
|
{
|
|
sortCol = SaxDbDfn_Reference.GetColumnNameQual((SaxDbDfn_Reference.ColumnIdx)columnDfn.DbColumn, aliasUsgaProvince);
|
|
}
|
|
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.UsgaProvince_Id, aliasUsgaProvince) + " ASC");
|
|
}
|
|
return sortColumnNames;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item count.
|
|
private long GetUsgaProvinceCount(String filter)
|
|
{
|
|
ClearErrorState();
|
|
return ReferenceDatabase.GetRowCount(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaProvince) + " As " + aliasUsgaProvince, filter);
|
|
}
|
|
public override long GetUsgaProvinceCount(SaxGolfUsgaProvince.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
return GetUsgaProvinceCount(GetUsgaProvinceQueryFilter(prm).Concat(" AND ", "(", ")"));
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Item list.
|
|
public override List<SaxGolfUsgaProvince> GetUsgaProvinceList(SaxGolfUsgaProvince.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
List<SaxGolfUsgaProvince> userRoles = new List<SaxGolfUsgaProvince>();
|
|
DbQuery qry = new DbQuery(ReferenceDatabase.DbmsType);
|
|
qry.Tables.Add(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaProvince), aliasUsgaProvince);
|
|
Golf_UsgaProvince_Db dbUsgaProvince = new Golf_UsgaProvince_Db(ReferenceDbInfo);
|
|
qry.Columns.AddRange(dbUsgaProvince.ExistingColumnNames);
|
|
qry.Filters.AddRange(GetUsgaProvinceQueryFilter(prm));
|
|
qry.OrderBy.AddRange(GetUsgaProvinceSortColumns(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())
|
|
{
|
|
dbUsgaProvince = new Golf_UsgaProvince_Db(ReferenceDbInfo);
|
|
dbUsgaProvince.PopulateFrom(dbDataReader);
|
|
userRoles.Add(CreateUsgaProvince(dbUsgaProvince));
|
|
}
|
|
}
|
|
}
|
|
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 ExecuteUsgaProvinceQuery(List<ColumnDfn> columns, SaxGolfUsgaProvince.Params prm)
|
|
{
|
|
ClearErrorState();
|
|
|
|
//---------------------------------------------------------------------
|
|
// Construct query.
|
|
DbQuery query = new DbQuery(ReferenceDatabase.DbmsType);
|
|
query.Tables.Add(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaProvince), aliasUsgaProvince);
|
|
|
|
// Add columns as defined in query list.
|
|
if (columns.HasId(SaxGolfUsgaProvince.Column.Id))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Id, aliasUsgaProvince));
|
|
bool hasCountryCols = (columns.HasId(SaxGolfUsgaProvince.Column.Country_Name));
|
|
if (columns.HasId(SaxGolfUsgaProvince.Column.CountryId) || hasCountryCols)
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_CountryId, aliasUsgaProvince));
|
|
if (columns.HasId(SaxGolfUsgaProvince.Column.ProvinceCode))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_ProvinceCode, aliasUsgaProvince));
|
|
if (columns.HasId(SaxGolfUsgaProvince.Column.ProvinceName))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_ProvinceName, aliasUsgaProvince));
|
|
if (columns.HasId(SaxGolfUsgaProvince.Column.CoursesUpdateAt_Date))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_CoursesUpdateAt, aliasUsgaProvince));
|
|
// Common properties.
|
|
if (columns.HasId(SaxGolfUsgaProvince.Column.Notes))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Notes, aliasUsgaProvince));
|
|
if (columns.HasId(SaxGolfUsgaProvince.Column.Flags))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Flags, aliasUsgaProvince));
|
|
if (columns.HasId(SaxGolfUsgaProvince.Column.Status) || columns.HasId(SaxGolfUsgaProvince.Column.Status_Desc) || columns.HasId(SaxGolfUsgaProvince.Column.Status_Ind))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Status, aliasUsgaProvince));
|
|
if (columns.HasId(SaxGolfUsgaProvince.Column.CreateOn_Date))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_CreateOn, aliasUsgaProvince));
|
|
if (columns.HasId(SaxGolfUsgaProvince.Column.UpdateOn_Date))
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_UpdateOn, aliasUsgaProvince));
|
|
|
|
//---------------------------------------------------------------------
|
|
// Country.
|
|
if (hasCountryCols)
|
|
{
|
|
DbQueryJoin joinCountry = new DbQueryJoin(DbJoinType.LeftOuter);
|
|
joinCountry.Table = new StringPair(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaCountry), aliasUsgaProvince_Country);
|
|
joinCountry.Filters.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_Id, aliasUsgaProvince_Country) + "=" + SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_CountryId, aliasUsgaProvince));
|
|
query.Joins.Add(joinCountry);
|
|
List<SaxDbDfn_Reference.ColumnIdx> cols = new List<SaxDbDfn_Reference.ColumnIdx>();
|
|
cols.Add(SaxDbDfn_Reference.ColumnIdx.UsgaCountry_CountryName);
|
|
foreach (var col in cols)
|
|
query.Columns.Add(SaxDbDfn_Reference.GetColumnNameQual(col, aliasUsgaProvince_Country), prefixUsgaProvince_Country + SaxDbDfn_Reference.GetColumnName(col, false));
|
|
prm.Alias_Country = aliasUsgaProvince_Country;
|
|
}
|
|
|
|
//---------------------------------------------------------------------
|
|
// Facility count.
|
|
if (columns.HasId(SaxGolfUsgaProvince.Column.FacilityCount_All))
|
|
{
|
|
String aliasFacility = "aPrvFty_Count";
|
|
DbQuery queryFacility = new DbQuery(CoreDatabase.DbmsType);
|
|
queryFacility.Tables.Add(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaFacility), aliasFacility);
|
|
queryFacility.Columns.Add("Count(*)");
|
|
queryFacility.Filters.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaFacility_ProvinceId, aliasFacility) + "=" + SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Id, aliasUsgaProvince));
|
|
query.Columns.Add("(" + queryFacility.BuildQuery() + ")", colProvince_Facility_Count);
|
|
}
|
|
|
|
// Active Facility count.
|
|
if (columns.HasId(SaxGolfUsgaProvince.Column.FacilityCount_Active))
|
|
{
|
|
String aliasFacility = "aPrvFtyAct_Count";
|
|
DbQuery queryFacility = new DbQuery(CoreDatabase.DbmsType);
|
|
queryFacility.Tables.Add(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaFacility), aliasFacility);
|
|
queryFacility.Columns.Add("Count(*)");
|
|
queryFacility.Filters.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaFacility_ProvinceId, aliasFacility) + "=" + SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Id, aliasUsgaProvince));
|
|
queryFacility.Filters.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaFacility_Status, aliasFacility) + "='" + SacsSystem.GetStatusCode(SaStatus.Active) + "'");
|
|
query.Columns.Add("(" + queryFacility.BuildQuery() + ")", colProvince_Facility_Active_Count);
|
|
}
|
|
|
|
//---------------------------------------------------------------------
|
|
// Course count.
|
|
if (columns.HasId(SaxGolfUsgaProvince.Column.FacilityCount_All))
|
|
{
|
|
String aliasFacility = "aPrvFty_Count_Fty";
|
|
DbQuery queryFacility = new DbQuery(CoreDatabase.DbmsType);
|
|
queryFacility.Tables.Add(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaFacility), aliasFacility);
|
|
queryFacility.Columns.Add("Count(*)");
|
|
queryFacility.Filters.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaFacility_ProvinceId, aliasFacility) + "=" + SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Id, aliasUsgaProvince));
|
|
// Join to Course.
|
|
String aliasCourse = "aPrvFty_Count_Crs";
|
|
DbQueryJoin joinCourse = new DbQueryJoin(DbJoinType.Inner);
|
|
joinCourse.Table = new StringPair(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaCourse), aliasCourse);
|
|
joinCourse.Filters.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCourse_FacilityId, aliasCourse) + "=" + SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaFacility_Id, aliasFacility));
|
|
queryFacility.Joins.Add(joinCourse);
|
|
// Construct sub-query.
|
|
query.Columns.Add("(" + queryFacility.BuildQuery() + ")", colProvince_Course_Count);
|
|
}
|
|
|
|
// Active Course count.
|
|
if (columns.HasId(SaxGolfUsgaProvince.Column.FacilityCount_Active))
|
|
{
|
|
String aliasFacility = "aPrvFty_Count_Fty";
|
|
DbQuery queryFacility = new DbQuery(CoreDatabase.DbmsType);
|
|
queryFacility.Tables.Add(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaFacility), aliasFacility);
|
|
queryFacility.Columns.Add("Count(*)");
|
|
queryFacility.Filters.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaFacility_ProvinceId, aliasFacility) + "=" + SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Id, aliasUsgaProvince));
|
|
// Join to Course.
|
|
String aliasCourse = "aPrvFty_Count_Crs";
|
|
DbQueryJoin joinCourse = new DbQueryJoin(DbJoinType.Inner);
|
|
joinCourse.Table = new StringPair(SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaCourse), aliasCourse);
|
|
joinCourse.Filters.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCourse_FacilityId, aliasCourse) + "=" + SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaFacility_Id, aliasFacility));
|
|
joinCourse.Filters.Add(SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCourse_Status, aliasCourse) + "='" + SacsSystem.GetStatusCode(SaStatus.Active) + "'");
|
|
queryFacility.Joins.Add(joinCourse);
|
|
// Construct sub-query.
|
|
query.Columns.Add("(" + queryFacility.BuildQuery() + ")", colProvince_Course_Active_Count);
|
|
}
|
|
|
|
//---------------------------------------------------------------------
|
|
// Marker population %.
|
|
if (columns.HasId(SaxGolfUsgaProvince.Column.MarkerPercent))
|
|
{
|
|
// Sub query to count facilities & privinces with markers, and work out the percent with markers.
|
|
String alias_Facility = "prv";
|
|
String alias_Course = "crs";
|
|
String alias_Marker = "fty";
|
|
String table_Facility = $"{SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaFacility)} {alias_Facility}";
|
|
String table_Course = $"{SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaCourse)} {alias_Course}";
|
|
String table_Marker = $"{SaxDbDfn_Reference.GetTableNameQual(SaxDbDfn_Reference.TableIdx.UsgaMarker)} {alias_Marker}";
|
|
String col_Province_Id = SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaProvince_Id, aliasUsgaProvince);
|
|
String col_Facility_Id = SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaFacility_Id, alias_Facility);
|
|
String col_Facility_ProvinceId = SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaFacility_ProvinceId, alias_Facility);
|
|
String col_Course_Id = SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCourse_Id, alias_Course);
|
|
String col_Course_FacilityId = SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaCourse_FacilityId, alias_Course);
|
|
String col_Marker_CourseId = SaxDbDfn_Reference.GetColumnNameQual(SaxDbDfn_Reference.ColumnIdx.UsgaMarker_CourseId, alias_Marker);
|
|
String name_CourseCount = "CourseCount";
|
|
String name_CoursesWithMarkers = "CoursesWithMarkers";
|
|
List<String> subQuery = new List<String>();
|
|
subQuery.Add($"Select Convert (decimal(9,2), Case When {name_CourseCount} > 0 Then 100.0 * (1.0 * {name_CoursesWithMarkers} / {name_CourseCount}) Else 0 End)");
|
|
subQuery.Add($"From");
|
|
subQuery.Add($"(");
|
|
subQuery.Add($"Select");
|
|
subQuery.Add($"{name_CourseCount} = (select Count(*) From {table_Course} Where {col_Course_FacilityId} In (Select {col_Facility_Id} From {table_Facility} Where {col_Facility_ProvinceId}={col_Province_Id})),");
|
|
subQuery.Add($"{name_CoursesWithMarkers} = (select Count(*) From {table_Course} Where {col_Course_FacilityId} In (Select {col_Facility_Id} From {table_Facility} Where {col_Facility_ProvinceId}={col_Province_Id}) And {col_Course_Id} in (Select {col_Marker_CourseId} from {table_Marker}))");
|
|
subQuery.Add($") dat");
|
|
query.Columns.Add($"({subQuery.Concat(" ")})", colProvince_Markers_Percent);
|
|
}
|
|
|
|
//---------------------------------------------------------------------
|
|
// Populate filter.
|
|
query.Filters.AddRange(GetUsgaProvinceQueryFilter(prm));
|
|
|
|
if (prm.IdsIncluded.Count != 1)
|
|
{
|
|
// Configure sort sequence.
|
|
foreach (String sortColumn in GetUsgaProvinceSortColumns(prm))
|
|
query.OrderBy.Add(sortColumn);
|
|
}
|
|
|
|
//---------------------------------------------------------------------
|
|
//return new DbResultSet_DataTableReader(db, query.BuildQuery(prm.ResultsPaging));
|
|
return DbResultSet_DataReader.ExecuteQuery(ReferenceDatabase, query.BuildQuery(prm.ResultsPaging));
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|