73 lines
2.0 KiB
C#
73 lines
2.0 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.Biz
|
|
{
|
|
public static class SqlSelectBuilder
|
|
{
|
|
|
|
//Build the SELECT portion of a list query based on the template, mini or full and the object key in question
|
|
public static string Build(string objectKey, string template, bool mini)
|
|
{
|
|
|
|
//parse the template
|
|
var jtemplate = JObject.Parse(template);
|
|
|
|
//get the fields list
|
|
var FieldsList = ObjectFields.ObjectFieldsList(objectKey);
|
|
|
|
//convert to strings array (https://stackoverflow.com/a/33836599/8939)
|
|
string[] templateFieldList;
|
|
if (mini)
|
|
{
|
|
templateFieldList = ((JArray)jtemplate["mini"]).ToObject<string[]>();
|
|
}
|
|
else
|
|
{
|
|
templateFieldList = ((JArray)jtemplate["full"]).ToObject<string[]>();
|
|
}
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.Append("SELECT ");
|
|
|
|
foreach (string ColumnName in templateFieldList)
|
|
{
|
|
sb.Append(ColumnName);
|
|
}
|
|
|
|
//iterate the datafilter and concatenate a sql query from it
|
|
var SortArray = JArray.Parse(dataFilter.Sort);
|
|
for (int i = 0; i < SortArray.Count; i++)
|
|
{
|
|
|
|
var SortItem = SortArray[i];
|
|
var fld = SortItem["fld"].Value<string>();
|
|
var dir = SortItem["dir"].Value<string>();
|
|
|
|
sb.Append(" ");
|
|
sb.Append(fld);
|
|
sb.Append(" ");
|
|
sb.Append(dir == "+" ? "ASC" : "DESC");
|
|
|
|
if (i < SortArray.Count - 1)
|
|
{
|
|
sb.Append(",");
|
|
}
|
|
}
|
|
|
|
return " ORDER BY" + sb.ToString();
|
|
}
|
|
|
|
|
|
|
|
}//eoc
|
|
}//ens
|