Files
raven/server/AyaNova/biz/SqlSelectBuilder.cs
2020-01-16 00:39:44 +00:00

78 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.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 objectFieldsList = 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 ");
//Default ID column for each row (always is _df_)
ObjectField def = objectFieldsList.FirstOrDefault(x => x.Key == "_df_");
if (def == null)
{
throw new System.ArgumentNullException($"SqlSelectBuilder: objectFieldList for key \"{objectKey}\" is missing the _df_ default field");
}
sb.Append(def.SqlIdColumn);
sb.Append(" AS _df_ ");
foreach (string ColumnName in templateFieldList)
{
ObjectField o = objectFieldsList.FirstOrDefault(x => x.Key == s);
if (o != null)
{//Here is where we can vet the field name, if it doesn't exist though, for now we'll just ignore those ones
sb.Append(",");
sb.Append("{");
//Build required part of column definition
sb.Append($"\"cm\":\"{o.Key}\",\"dt\":{(int)o.DataType}");
//Has a AyObjectType? (linkable / openable)
if (o.AyObjectType != 0)
sb.Append($",\"ay\":{(int)o.AyObjectType}");
sb.Append("}");
}
}
return sb.ToString();
}
}//eoc
}//ens