279 lines
13 KiB
C#
279 lines
13 KiB
C#
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Linq;
|
|
using AyaNova.Models;
|
|
|
|
namespace AyaNova.DataList
|
|
{
|
|
internal class SqlSelectBuilderResult
|
|
{
|
|
internal Dictionary<string, int> map { get; set; }
|
|
internal string Select { get; set; }
|
|
}
|
|
internal static class DataListSqlSelectBuilder
|
|
{
|
|
|
|
//Build the SELECT portion of a list query based on the columns
|
|
internal static SqlSelectBuilderResult BuildForDataTableListResponse(List<DataListFieldDefinition> objectFieldsList, List<string> columns)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.Append("SELECT ");
|
|
|
|
//keep track of which custom fields columns were added already
|
|
//this ensures that if there is more than one set of custom fields like from two different objects in the list
|
|
//only unique ones will be returned by query
|
|
//map sql column name to ordinal name
|
|
Dictionary<string, int> map = new Dictionary<string, int>();
|
|
int nOrdinal = 0;
|
|
|
|
var firstColumnAdded = false;
|
|
foreach (string ColumnName in columns)
|
|
{
|
|
DataListFieldDefinition o = objectFieldsList.FirstOrDefault(z => z.FieldKey == ColumnName);
|
|
#if (DEBUG)
|
|
//Developers little helper
|
|
if (o == null)
|
|
{
|
|
throw new System.ArgumentNullException($"## DEV ERROR in DataListSqlSelectBuilder.cs:BuildForDataTableListResponse() field {ColumnName} specified in columns was NOT found in the data list's ObjectFields list, a defined fieldkey name differs from the columns key name");
|
|
}
|
|
#endif
|
|
if (o != null)
|
|
{//Ignore missing fields in production
|
|
if (o.IsCustomField)
|
|
{ //if any are custom field then add custom fields column to query
|
|
var CustomFieldSqlColumnName = o.GetSqlValueColumnName();
|
|
//has it been added yet?
|
|
if (!map.ContainsKey(CustomFieldSqlColumnName))
|
|
{ //nope
|
|
if (firstColumnAdded)
|
|
sb.Append(", ");
|
|
sb.Append(CustomFieldSqlColumnName);
|
|
firstColumnAdded = true;
|
|
map.Add(CustomFieldSqlColumnName, nOrdinal++);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var valueColumnName = o.GetSqlValueColumnName();
|
|
if (!map.ContainsKey(valueColumnName))
|
|
{
|
|
if (firstColumnAdded)
|
|
sb.Append(", ");
|
|
sb.Append(valueColumnName);
|
|
firstColumnAdded = true;
|
|
map.Add(valueColumnName, nOrdinal++);
|
|
}
|
|
|
|
//does it also have an ID column?
|
|
var idColumnName = o.SqlIdColumnName;
|
|
if (!string.IsNullOrWhiteSpace(idColumnName))
|
|
{
|
|
if (!map.ContainsKey(idColumnName))
|
|
{
|
|
if (firstColumnAdded)
|
|
sb.Append(", ");
|
|
sb.Append(idColumnName);
|
|
firstColumnAdded = true;
|
|
map.Add(idColumnName, nOrdinal++);
|
|
}
|
|
}
|
|
|
|
//does it also have an openable AyaType column?
|
|
var ayaTypeColumnName = o.SqlAyTypeColumnName;
|
|
if (!string.IsNullOrWhiteSpace(ayaTypeColumnName))
|
|
{
|
|
if (!map.ContainsKey(ayaTypeColumnName))
|
|
{
|
|
if (firstColumnAdded)
|
|
sb.Append(", ");
|
|
sb.Append(ayaTypeColumnName);
|
|
firstColumnAdded = true;
|
|
map.Add(ayaTypeColumnName, nOrdinal++);
|
|
}
|
|
}
|
|
|
|
//does it also have a Color column?
|
|
var ayaColorColumnName = o.SqlColorColumnName;
|
|
if (!string.IsNullOrWhiteSpace(ayaColorColumnName))
|
|
{
|
|
if (!map.ContainsKey(ayaColorColumnName))
|
|
{
|
|
if (firstColumnAdded)
|
|
sb.Append(", ");
|
|
sb.Append(ayaColorColumnName);
|
|
firstColumnAdded = true;
|
|
map.Add(ayaColorColumnName, nOrdinal++);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return new SqlSelectBuilderResult() { map = map, Select = sb.ToString() };
|
|
}//eom
|
|
|
|
|
|
//Build the SELECT portion of a list query but only to return rowid's
|
|
|
|
internal static string BuildForIdListResponse(List<DataListFieldDefinition> fieldDefinitions, DataListSelectedProcessingOptions dataListSelectionOptions)
|
|
{
|
|
//BugBug - is not including internalcriteria OR user filtered columns so they don't filter properly
|
|
//needs to be there and also return correct 0th column for id filtering or make sure it's always the zeroth I guess
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.Append("SELECT ");
|
|
//map sql column name to ordinal name
|
|
// Dictionary<string, int> map = new Dictionary<string, int>();
|
|
|
|
//FIRST: find the rowid in the objectfields list and insert it
|
|
//this is now the zeroth column
|
|
//Note: IsRowId field should *always* exist for any list that is intended to be used in an idlist response
|
|
var o = fieldDefinitions.FirstOrDefault(z => z.IsRowId == true);
|
|
sb.Append(o.SqlIdColumnName);
|
|
// map.Add(o.SqlIdColumnName, 0);
|
|
|
|
//Now add any rows found in filter
|
|
foreach(var filter in dataListSelectionOptions.Filter){
|
|
//add all columns being filtered but not rowId
|
|
var fld=fieldDefinitions.FirstOrDefault(z=>z.FieldKey==filter.Column);
|
|
if(!fld.IsRowId){
|
|
sb.Append($",{fld.SqlIdColumnName}");
|
|
//map.Add(fld.SqlIdColumnName);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
}//eom
|
|
|
|
|
|
// internal static SqlSelectBuilderResult BuildForReportIdListOnly(List<DataListFieldDefinition> objectFieldsList)
|
|
// {
|
|
// //BugBug - is not including internalcriteria columns so they don't filter properly
|
|
// //needs to be there and also return correct 0th column for id filtering or make sure it's always the zeroth I guess
|
|
|
|
// StringBuilder sb = new StringBuilder();
|
|
// sb.Append("SELECT ");
|
|
// //map sql column name to ordinal name
|
|
// Dictionary<string, int> map = new Dictionary<string, int>();
|
|
|
|
// //find the rowid in the objectfields list and insert it
|
|
// var o = objectFieldsList.FirstOrDefault(z => z.IsRowId == true);
|
|
// if (o != null)
|
|
// {
|
|
// var idColumnName = o.SqlIdColumnName;
|
|
// if (!string.IsNullOrWhiteSpace(idColumnName))
|
|
// {
|
|
// sb.Append(idColumnName);
|
|
// map.Add(idColumnName, 0);
|
|
// }
|
|
// }
|
|
|
|
// return new SqlSelectBuilderResult() { map = map, Select = sb.ToString() };
|
|
// }//eom
|
|
|
|
|
|
// //Build the SELECT portion of a list query but only to return rowid's
|
|
// internal static SqlSelectBuilderResult BuildForReportIdListOnly(List<AyaDataListFieldDefinition> objectFieldsList, List<string> listViewFieldList)
|
|
// {
|
|
// StringBuilder sb = new StringBuilder();
|
|
// sb.Append("SELECT ");
|
|
// //map sql column name to ordinal name
|
|
// Dictionary<string, int> map = new Dictionary<string, int>();
|
|
// int nOrdinal = 0;
|
|
|
|
// var firstColumnAdded = false;
|
|
// foreach (string ColumnName in listViewFieldList)
|
|
// {
|
|
// AyaDataListFieldDefinition o = objectFieldsList.FirstOrDefault(z => z.FieldKey == ColumnName);
|
|
// #if (DEBUG)
|
|
// //Developers little helper
|
|
// if (o == null)
|
|
// {
|
|
// throw new System.ArgumentNullException($"DEV ERROR in DataListSqlSelectBuilder.cs:BuildForReportIdListOnly() field {ColumnName} specified in template was NOT found in ObjectFields list");
|
|
// }
|
|
// #endif
|
|
// if (o != null && o.IsRowId)//only return the rowid column
|
|
// {//Ignore missing fields in production
|
|
// AddTheColumn(sb, map, ref nOrdinal, ref firstColumnAdded, o);
|
|
// }
|
|
// }
|
|
|
|
|
|
// //insert rowid if it wasn't specified already in the list view
|
|
// //this is legit as some datalists don't have a rowid visible to user e.g. PartInventoryOnHand
|
|
// if (map.Count == 0)
|
|
// {
|
|
// //find the rowid in the objectfields list and insert it since it wasn't in the view already
|
|
// var o = objectFieldsList.FirstOrDefault(z => z.IsRowId == true);
|
|
// if (o != null)
|
|
// {
|
|
// // if(!string.IsNullOrWhiteSpace(rowIdColumn.SqlIdColumnName)){
|
|
// // map.Add(rowIdColumn.SqlIdColumnName, nOrdinal++);
|
|
// // }else{
|
|
// // map.Add(rowIdColumn.GetSqlValueColumnName(), nOrdinal++);
|
|
// // }
|
|
// AddTheColumn(sb, map, ref nOrdinal, ref firstColumnAdded, o);
|
|
// }
|
|
|
|
// }
|
|
// return new SqlSelectBuilderResult() { map = map, Select = sb.ToString() };
|
|
// }//eof
|
|
|
|
|
|
|
|
|
|
// private static void AddTheColumn(StringBuilder sb, Dictionary<string, int> map, ref int nOrdinal, ref bool firstColumnAdded, AyaDataListFieldDefinition o)
|
|
// {
|
|
// // var valueColumnName = o.GetSqlValueColumnName();
|
|
// // if (!map.ContainsKey(valueColumnName))
|
|
// // {
|
|
// // if (firstColumnAdded)
|
|
// // sb.Append(", ");
|
|
// // sb.Append(valueColumnName);
|
|
// // firstColumnAdded = true;
|
|
// // map.Add(valueColumnName, nOrdinal++);
|
|
// // }
|
|
|
|
// // //does it also have an ID column?
|
|
// // var idColumnName = o.SqlIdColumnName;
|
|
// // if (!string.IsNullOrWhiteSpace(idColumnName))
|
|
// // {
|
|
// // if (!map.ContainsKey(idColumnName))
|
|
// // {
|
|
// // if (firstColumnAdded)
|
|
// // sb.Append(", ");
|
|
// // sb.Append(idColumnName);
|
|
// // firstColumnAdded = true;
|
|
// // map.Add(idColumnName, nOrdinal++);
|
|
// // }
|
|
// // }
|
|
|
|
// // var valueColumnName = o.GetSqlValueColumnName();
|
|
// // if (!map.ContainsKey(valueColumnName))
|
|
// // {
|
|
// // if (firstColumnAdded)
|
|
// // sb.Append(", ");
|
|
// // sb.Append(valueColumnName);
|
|
// // firstColumnAdded = true;
|
|
// // map.Add(valueColumnName, nOrdinal++);
|
|
// // }
|
|
|
|
// //does it also have an ID column?
|
|
// var idColumnName = o.SqlIdColumnName;
|
|
// if (!string.IsNullOrWhiteSpace(idColumnName))
|
|
// {
|
|
// // if (!map.ContainsKey(idColumnName))
|
|
// // {
|
|
// // if (firstColumnAdded)
|
|
// // sb.Append(", ");
|
|
// sb.Append(idColumnName);
|
|
// // firstColumnAdded = true;
|
|
// map.Add(idColumnName, 0);
|
|
// // }
|
|
// }
|
|
// }
|
|
}//eoc
|
|
}//ens
|