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 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 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 lSelect = new List(); List lWhere = new List(); //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