SQLTableDefinition.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace InABox.Core
  8. {
  9. /*
  10. *
  11. * Incomplete class - an attempt to simplify and separate the SQLiteProvider funtionality into a generalised
  12. * (reflection-driven) model that can be created once, and a streamlined processor that genrerates valid
  13. * SQL to throw at the database.
  14. *
  15. * Currently does not handle aggregates, formulas, conditions, filters or sorts, but it does create a nice
  16. * simple JOIN model based on Entity Properties, EntityLinks and EnclosedEntities
  17. *
  18. * If we can get this working, it will ease the way towards using a non-SQLite database (eg SQL Server, MySQL, etc)
  19. * but there is still some way to go!
  20. *
  21. */
  22. public class SQLFieldDefinition
  23. {
  24. public SQLTableDefinition Parent { get; private set; }
  25. public String ColumnName { get; private set; }
  26. public String FullName { get; private set; }
  27. public IFormula Formula { get; set; }
  28. public SQLFieldDefinition(SQLTableDefinition parent, String columnName, IFormula formula)
  29. {
  30. Parent = parent;
  31. ColumnName = columnName;
  32. FullName = ColumnName;
  33. var curparent = parent;
  34. while ((curparent != null) && (!String.IsNullOrWhiteSpace(curparent.LinkName)))
  35. {
  36. FullName = $"{curparent.LinkName}.{FullName}";
  37. curparent = curparent.Parent;
  38. }
  39. Formula = null;
  40. }
  41. public override string ToString() => FullName;
  42. }
  43. public class SQLTableDefinition
  44. {
  45. public Type Type { get; private set; }
  46. public int TableNumber { get; private set; }
  47. public String TableName { get; private set; }
  48. public String LinkName { get; set; }
  49. public SQLTableDefinition Parent { get; private set; }
  50. public List<SQLFieldDefinition> Fields { get; private set; }
  51. public List<SQLTableDefinition> Joins { get; private set; }
  52. public override string ToString() => $"{TableName}";
  53. public SQLTableDefinition(Type type, SQLTableDefinition parent)
  54. {
  55. Type = type;
  56. LinkName = "";
  57. Parent = parent;
  58. TableName = type.EntityName().Split('.').Last();
  59. Fields = new List<SQLFieldDefinition>();
  60. Joins = new List<SQLTableDefinition>();
  61. }
  62. private IEnumerable<PropertyInfo> PropertyList(Type type, Func<PropertyInfo,bool> predicate)
  63. {
  64. return CoreUtils.PropertyList(type,
  65. x => x.GetCustomAttribute<DoNotPersist>() == null
  66. && x.GetCustomAttribute<DoNotSerialize>() == null
  67. && x.PropertyType != typeof(UserProperties)
  68. && x.GetCustomAttribute<AggregateAttribute>() == null
  69. //&& x.GetCustomAttribute<FormulaAttribute>() == null
  70. && x.GetCustomAttribute<ConditionAttribute>() == null
  71. ).Where(x => predicate(x));
  72. }
  73. private void LoadEnclosedEntity(PropertyInfo enclosedentity, String prefix, ref int tableNumber)
  74. {
  75. var props = PropertyList(enclosedentity.PropertyType, x => true);
  76. foreach (var prop in props)
  77. {
  78. if (prop.PropertyType.GetInterfaces().Contains(typeof(IEnclosedEntity)))
  79. LoadEnclosedEntity(
  80. prop,
  81. $"{prefix}.{prop.Name}",
  82. ref tableNumber
  83. );
  84. else if (prop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink)))
  85. LoadEntityLink(
  86. prop,
  87. $"{prefix}.{prop.Name}",
  88. ref tableNumber
  89. );
  90. else
  91. Fields.Add(new SQLFieldDefinition(this, $"{prefix}.{prop.Name}", prop.GetCustomAttribute<FormulaAttribute>()));
  92. }
  93. }
  94. private void LoadEntityLink(PropertyInfo entitylink, String prefix, ref int tableNumber)
  95. {
  96. var linktype = entitylink.PropertyType.GetInterfaces()
  97. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEntityLink<>))
  98. ?.GenericTypeArguments[0];
  99. if (linktype != null)
  100. {
  101. Fields.Add(new SQLFieldDefinition(this, $"{entitylink.Name}.ID", null));
  102. var props = PropertyList(entitylink.PropertyType, x => !String.Equals(x.Name, "ID"));
  103. if (props.Any())
  104. {
  105. SQLTableDefinition join = new SQLTableDefinition(linktype, this) { LinkName = prefix };
  106. tableNumber++;
  107. join.Load(entitylink.PropertyType, ref tableNumber);
  108. Joins.Add(join);
  109. }
  110. }
  111. }
  112. public void Load(Type type, ref int tableNumber)
  113. {
  114. TableNumber = tableNumber;
  115. var props = PropertyList(type, x => true);
  116. foreach (var prop in props)
  117. {
  118. if (prop.PropertyType.GetInterfaces().Contains(typeof(IEnclosedEntity)))
  119. LoadEnclosedEntity(
  120. prop,
  121. prop.Name,
  122. ref tableNumber
  123. );
  124. else if (prop.PropertyType.GetInterfaces().Contains(typeof(IEntityLink)))
  125. LoadEntityLink(
  126. prop,
  127. prop.Name,
  128. ref tableNumber
  129. );
  130. else
  131. LoadField(prop);
  132. }
  133. }
  134. private void LoadField(PropertyInfo prop)
  135. {
  136. IFormula formula = prop.GetCustomAttribute<FormulaAttribute>();
  137. if (formula != null)
  138. {
  139. // Because Formulas can themselves contain properties from EntityLinks
  140. // or Enclosed Entities, we need to deal with the requisite nesting of tables
  141. // although I'm not sure how this will work out in practice
  142. }
  143. Fields.Add(new SQLFieldDefinition(this, prop.Name, formula));
  144. }
  145. }
  146. public abstract class SQLCompiler<T> where T : Entity
  147. {
  148. protected static Dictionary<Type, SQLTableDefinition> _cache = new Dictionary<Type, SQLTableDefinition>();
  149. protected static SQLTableDefinition GetDefinition(Type type)
  150. {
  151. if (!_cache.TryGetValue(type, out SQLTableDefinition result))
  152. {
  153. lock (((ICollection)_cache).SyncRoot)
  154. {
  155. result = new SQLTableDefinition(type, null);
  156. int tablenumber = 0;
  157. result.Load(type, ref tablenumber);
  158. _cache[type] = result;
  159. }
  160. }
  161. return result;
  162. }
  163. protected IEnumerable<SQLTableDefinition> GetTables(SQLTableDefinition table, String[] columns)
  164. {
  165. return GetFields(table, columns).Select(x => x.Parent).Distinct();
  166. }
  167. protected SQLFieldDefinition[] GetFields(SQLTableDefinition table, String[] columns)
  168. {
  169. List<SQLFieldDefinition> result = new List<SQLFieldDefinition>();
  170. foreach (var field in table.Fields.Where(x =>columns.Contains(x.FullName)))
  171. result.Add(field);
  172. foreach (var join in table.Joins)
  173. {
  174. var subfields = GetFields(join, columns);
  175. result.AddRange(subfields);
  176. }
  177. return result.ToArray();
  178. }
  179. protected IEnumerable<SQLFieldDefinition> GetFields(SQLTableDefinition table, SQLFieldDefinition[] columns)
  180. {
  181. List<SQLFieldDefinition> result = new List<SQLFieldDefinition>();
  182. foreach (var field in table.Fields.Where(x =>columns.Contains(x)))
  183. result.Add(field);
  184. foreach (var join in table.Joins)
  185. {
  186. var subfields = GetFields(join, columns);
  187. result.AddRange(subfields);
  188. }
  189. return result;
  190. }
  191. protected abstract String DoCompile(String prefix, SQLTableDefinition table, SQLTableDefinition[] activetables,
  192. SQLFieldDefinition[] visiblecolumns);
  193. public string Compile(Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null)
  194. {
  195. var root = GetDefinition(typeof(T));
  196. var cols = (columns ?? new Columns<T>().Default(ColumnType.All)).ColumnNames().ToList();
  197. var visiblecolumns = GetFields(root, cols.ToArray());
  198. if (filter != null)
  199. cols.AddRange(filter.ColumnNames());
  200. if (sort != null)
  201. cols.AddRange(sort.ColumnNames());
  202. var activetables = GetFields(root, cols.ToArray()).Select(x => x.Parent).Distinct().ToArray();
  203. return DoCompile("", root, activetables, visiblecolumns);
  204. }
  205. }
  206. public class SQLiteCompiler<T> : SQLCompiler<T> where T : Entity
  207. {
  208. private void DoCompileJoins(SQLTableDefinition table, String prefix, SQLTableDefinition[] activetables, StringBuilder builder)
  209. {
  210. foreach (var join in table.Joins.Where(x=>activetables.Contains(x)))
  211. {
  212. builder.AppendLine($"{prefix}LEFT OUTER JOIN");
  213. builder.AppendLine($"{prefix}\t{join.TableName} T{join.TableNumber} ON T{table.TableNumber}.[{join.LinkName}.ID] = T{join.TableNumber}.[ID]");
  214. DoCompileJoins(join, prefix, activetables, builder);
  215. }
  216. }
  217. protected override String DoCompile(String prefix, SQLTableDefinition table, SQLTableDefinition[] activetables, SQLFieldDefinition[] visiblecolumns)
  218. {
  219. StringBuilder result = new StringBuilder();
  220. result.Append($"{prefix}SELECT\n{prefix}\t");
  221. var fields = visiblecolumns.Select(x=> $@"T{x.Parent.TableNumber}.[{x.ColumnName}] as [{(x.Parent != table ? x.FullName : x.ColumnName)}]").ToList();
  222. if (!visiblecolumns.Any(x=>String.Equals(x.ColumnName,"ID")))
  223. fields.Insert(0,$@"T{table.TableNumber}.[ID] as [ID]");
  224. result.AppendLine(String.Join($",\n{prefix}\t",fields));
  225. result.AppendLine($"{prefix}FROM");
  226. result.AppendLine($"{prefix}\t{table.TableName} T{table.TableNumber}");
  227. DoCompileJoins(table, prefix, activetables, result);
  228. return result.ToString();
  229. }
  230. }
  231. }