Files
raven/server/AyaNova/PickList/PickListSqlBuilder.cs
2020-03-13 19:11:29 +00:00

71 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.PickList
{
internal static class PickListSqlBuilder
{
//Build the query for a picklist request
internal static string Build(IAyaPickList pickList, List<string> templateColumnNames, string autoCompleteQuery)
{
//TODO: if no autocompletequery returns the first XX results without querying in natural order by column names
//if autocomplete returns teh first XX results with query in natural order by column names
//TODO: build a sql List<AyaPickListFieldDefinition> columnDefinitionsselect and order by and a where clause that searches appropriately in each field (tags)
//it should return results based on the query where there is a single name (display) column and an id column for rowid
//and the fields should be combined in a standard way separated by spaces "Widget widgetserial username" for compactness
//lists to collect the clauses so to avoid comma fuckery
List<string> lSelect = new List<string>();
List<string> lWhere = new List<string>();
//Add rowid column as it's always required
AyaPickListFieldDefinition rowIdColumn = pickList.ColumnDefinitions.FirstOrDefault(x => x.IsRowId == true);
lSelect.Add(rowIdColumn.SqlIdColumnName + " AS rowid");
foreach (string ColumnName in templateColumnNames)
{
AyaPickListFieldDefinition o = pickList.ColumnDefinitions.FirstOrDefault(x => x.FieldKey == ColumnName);
#if (DEBUG)
if (o == null)
{
throw new System.ArgumentNullException($"DEV ERROR in PickListSqlBuilder.cs: field {ColumnName} specified in template was NOT found in columnDefinitions list");
}
#endif
if (o != null)
{//Ignore missing fields in production
var valueColumnName = o.GetSqlValueColumnName();
lSelect.Add(valueColumnName);
string sWhere=$"{valueColumnName} LIKE '%{autoCompleteQuery"
}
}
return sbSelect.ToString() + pickList.SQLFrom + sbWhere.ToString();
}
}//eoc
}//ens