75 lines
2.4 KiB
C#
75 lines
2.4 KiB
C#
using System.Collections.Generic;
|
|
using System;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Linq;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace AyaNova.DataList
|
|
{
|
|
internal static class DataListSqlSelectBuilder
|
|
{
|
|
|
|
//Build the SELECT portion of a list query based on the ListView fields
|
|
internal static string Build(List<AyaDataListFieldDefinition> objectFieldsList, List<string> listViewFieldList)
|
|
{
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.Append("SELECT ");
|
|
|
|
//Default ID column for each row (always is aliased as df)
|
|
AyaDataListFieldDefinition def = objectFieldsList.FirstOrDefault(x => x.FieldKey == "df");
|
|
if (def == null)
|
|
{
|
|
throw new System.ArgumentNullException("DataListSqlSelectBuilder: objectFieldList is missing the df default field");
|
|
}
|
|
if (string.IsNullOrEmpty(def.SqlIdColumnName))
|
|
{
|
|
sb.Append("id");//default when no alternate column is specified
|
|
}
|
|
else
|
|
{
|
|
sb.Append(def.SqlIdColumnName);
|
|
}
|
|
|
|
sb.Append(" AS df");
|
|
|
|
foreach (string ColumnName in listViewFieldList)
|
|
{
|
|
//skip the df column, it's already been processed above
|
|
if (ColumnName == "df")
|
|
continue;
|
|
AyaDataListFieldDefinition o = objectFieldsList.FirstOrDefault(x => x.FieldKey == ColumnName);
|
|
#if (DEBUG)
|
|
//Developers little helper
|
|
if (o == null)
|
|
{
|
|
throw new System.ArgumentNullException($"DEV ERROR in DataListSqlSelectBuilder.cs: field {ColumnName} specified in template was NOT found in ObjectFields list");
|
|
}
|
|
#endif
|
|
if (o != null)
|
|
{//Ignore missing fields in production
|
|
|
|
sb.Append(", ");
|
|
sb.Append(o.GetSqlValueColumnName());
|
|
|
|
//does it also have an ID column?
|
|
if (!string.IsNullOrWhiteSpace(o.SqlIdColumnName))
|
|
{
|
|
sb.Append(", ");
|
|
sb.Append(o.SqlIdColumnName);
|
|
}
|
|
}
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
|
|
|
|
}//eoc
|
|
}//ens
|