This commit is contained in:
2020-02-13 23:03:05 +00:00
parent 144e92dabe
commit d032ceb125
4 changed files with 104 additions and 45 deletions

View File

@@ -48,7 +48,9 @@ DataListView JSON:
Sort property definition
ID VS NAME
if it's an name *and* id field
if it's an name *and* id field it won't matter because it will sort by name only
I've thought about it and can't see a use case for needing to sort by the underlying ID value, but if people want that they can just not sort the list to get
the most recent records at the top (for example)
Filter object definition:
Has an "any" boolean property which if true means make an OR query of the conditions if more than one, if nore present or false it means AND each condition (if more than one)

View File

@@ -14,34 +14,31 @@ namespace AyaNova.DataList
public static string DataFilterToSQLCriteria(List<AyaDataListFieldDefinition> objectFieldsList, JArray listViewArray, long userId)
{
if (string.IsNullOrWhiteSpace(filterJson))
if (listViewArray == null || listViewArray.Count == 0)
{
return "";
}
StringBuilder sb = new StringBuilder();
//iterate the datafilter and concatenate a sql query from it
var FilterArray = JArray.Parse(filterJson);
for (int i = 0; i < FilterArray.Count; i++)
//iterate the list view fields and concatenate a sql query from it
//// [{key:"COLUMN UNIQUE KEY ID",sort:"-" or "+",filter:{any:true/false,items:[{FILTER OBJECT SEE BELOW}]} }, {key:"second column unique key"},{...etc...}]
for (int i = 0; i < listViewArray.Count; i++)
{
var filterItem = FilterArray[i];
var fld = filterItem["fld"].Value<string>();
var opType = filterItem["op"].Value<string>();
List<string> tagList = new List<string>();
string val = string.Empty;
if (filterItem["value"].Type != JTokenType.Array)
val = filterItem["value"].Value<string>();
else
var cm = listViewArray[i];
//skip it if sort is not defined
if (cm["filter"] == null)
{
tagList = filterItem["value"].ToObject<List<String>>();
continue;
}
var dataType = objectFieldsList.Find(x => x.FieldKey.ToLowerInvariant() == fld).UiFieldDataType;
//Yup there's at least one filter here for this column
sb.Append("(");
//Get the correct sql column name
//Get some info about this column / field
var fld = cm["fld"].Value<string>();
var dataType = objectFieldsList.Find(x => x.FieldKey.ToLowerInvariant() == fld).UiFieldDataType;
AyaDataListFieldDefinition DataListField = objectFieldsList.FirstOrDefault(x => x.FieldKey == fld);
#if (DEBUG)
//Developers little helper
@@ -51,14 +48,71 @@ namespace AyaNova.DataList
}
#endif
var SQLValueColumnName = DataListField.GetSqlValueColumnName();
sb.Append(DataFilterToColumnCriteria(SQLValueColumnName, (UiFieldDataType)dataType, opType, val, tagList, userId));
if (i < FilterArray.Count - 1)
var SQLIdColumnName = DataListField.SqlIdColumnName;
//get filter items collection for this field view definition
var filterItems = (JArray)cm["filter"]["items"];
var IsOrFilter = false;
if (cm["filter"]["any"] != null)
{
IsOrFilter = cm["filter"]["any"].Value<bool>();
}
//Is there more than one filter for this column?
if (filterItems.Count > 1)
{
//Yup, so put the whole group in paranthesis
sb.Append("(");
}
//Iterate filter items building this WHERE segment
for (int y = 0; y < filterItems.Count; y++)
{
//Put this item in parenthesis
sb.Append("(");
var filterItem = (JObject)filterItems[y];
var opType = filterItem["op"].Value<string>();
List<string> tagList = new List<string>();
string val = string.Empty;
if (filterItem["value"].Type != JTokenType.Array)
val = filterItem["value"].Value<string>();
else
{
tagList = filterItem["value"].ToObject<List<String>>();
}
sb.Append(DataFilterToColumnCriteria(SQLValueColumnName, (UiFieldDataType)dataType, opType, val, tagList, userId));
//close this item parenthesis
if (y < filterItems.Count - 1)
{
if (IsOrFilter)
sb.Append(") OR ");
else
sb.Append(") AND ");
}
else
{
sb.Append(")");
}
}
if (filterItems.Count > 1)
{
//close this group
sb.Append(")");
}
if (i < listViewArray.Count - 1)
{
sb.Append(") AND ");
}
}
return " where " + sb.ToString() + ")";
return " where (" + sb.ToString() + ")";
}
/// <summary>

View File

@@ -15,31 +15,20 @@ namespace AyaNova.DataList
public static string DataFilterToSQLOrderBy(List<AyaDataListFieldDefinition> objectFieldsList, JArray listViewArray)
{
if ( string.IsNullOrWhiteSpace(SortJson))
{
//sort by default field descending which should in theory always be the id column of the main object in the list
//which should return the results to user with most recent records at the top if no sort order was specified
if (objectFieldsList[0].FieldKey == "df")
{
return $"ORDER BY {objectFieldsList[0].SqlIdColumnName} DESC";
}
else
{
//no default column so no idea how to sort
return string.Empty;
}
}
StringBuilder sb = new StringBuilder();
//iterate the datafilter and concatenate a sql query from it
var SortArray = JArray.Parse(SortJson);
for (int i = 0; i < SortArray.Count; i++)
// [{key:"COLUMN UNIQUE KEY ID",sort:"-" or "+",filter:{any:true/false,items:[{FILTER OBJECT SEE BELOW}]} }, {key:"second column unique key"},{...etc...}]
for (int i = 0; i < listViewArray.Count; i++)
{
var SortItem = SortArray[i];
var fld = SortItem["fld"].Value<string>();
var dir = SortItem["dir"].Value<string>();
var cm = listViewArray[i];
//skip it if sort is not defined
if (cm["sort"] == null)
{
continue;
}
var fld = cm["fld"].Value<string>();
var dir = cm["sort"].Value<string>();
//Get the correct sql column name
AyaDataListFieldDefinition DataListField = objectFieldsList.FirstOrDefault(x => x.FieldKey == fld);
#if (DEBUG)
@@ -56,13 +45,29 @@ namespace AyaNova.DataList
sb.Append(" ");
sb.Append(dir == "+" ? "ASC" : "DESC");
if (i < SortArray.Count - 1)
if (i < listViewArray.Count - 1)
{
sb.Append(",");
}
}
return "ORDER BY" + sb.ToString();
if (sb.Length == 0)
{
//no sort specified so default it
if (objectFieldsList[0].FieldKey == "df")
{
return $"ORDER BY {objectFieldsList[0].SqlIdColumnName} DESC";
}
else
{
//no default column so no idea how to sort
return string.Empty;
}
}
else
{
return "ORDER BY" + sb.ToString();
}
}

View File

@@ -15,9 +15,7 @@ namespace AyaNova.DataList
//Build the SELECT portion of a list query based on the template, mini or full and the object key in question
internal static string Build(List<AyaDataListFieldDefinition> objectFieldsList, JArray listViewArray)
{
//parse the template
var jtemplate = JObject.Parse(template);
//convert to strings array (https://stackoverflow.com/a/33836599/8939)