IProvider.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using InABox.Core;
  2. namespace InABox.Database;
  3. public delegate void LogEvent(LogType type, string message);
  4. public interface IProviderFactory
  5. {
  6. string URL { get; set; }
  7. Type[] Types { get; set; }
  8. void ForceRecreateViews();
  9. void Start();
  10. IProvider NewProvider(Logger logger);
  11. }
  12. public interface IProvider
  13. {
  14. Logger Logger { get; set; }
  15. IEnumerable<object[]> List<T>(Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null, CoreRange? range = null) where T : Entity, new();
  16. bool TableExists<T>();
  17. bool TableExists(Type t);
  18. bool TableExists(string name);
  19. CoreTable? GetTable<T>();
  20. CoreTable? GetTable(Type t);
  21. CoreTable? GetTable(string name);
  22. void DropTable<T>();
  23. void DropTable(Type t);
  24. void DropTable(string name);
  25. IEnumerable<object[]> List(string sql);
  26. CoreTable Query(string sql);
  27. int Update(string sql);
  28. CoreTable Query<T>(Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null,
  29. CoreRange? range = null, bool log = false, bool distinct = false) where T : Entity, new();
  30. CoreTable Query(Type type, IFilter? filter = null, IColumns? columns = null, ISortOrder? sort = null,
  31. CoreRange? range = null, bool log = false, bool distinct = false);
  32. /// <summary>
  33. /// Same as <see cref="Query{T}(Filter{T}, Columns{T}, SortOrder{T}, CoreRange, bool, bool)"/>, but only for deleted items
  34. /// </summary>
  35. CoreTable QueryDeleted<T>(Deletion deletion, Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null, CoreRange? range = null, bool deleted = false) where T : Entity, new();
  36. T[] Load<T>(Filter<T>? filter = null, SortOrder<T>? sort = null, CoreRange? range = null) where T : Entity, new();
  37. void Save<T>(T entity) where T : Entity;
  38. void Save<T>(IEnumerable<T> entities) where T : Entity;
  39. void Save(Type type, Entity entity);
  40. void Save(Type type, IEnumerable<Entity> entities);
  41. void Delete<T>(T entity, string userID) where T : Entity, new();
  42. void Delete<T>(IEnumerable<T> entities, string userID) where T : Entity, new();
  43. void Purge<T>(T entity) where T : Entity;
  44. void Purge<T>(IEnumerable<T> entities) where T : Entity;
  45. void Purge(Deletion deletion);
  46. void Recover(Deletion deletion);
  47. List<Type> GetDeletionTypes(Deletion deletion);
  48. }
  49. public static class ProviderExtensions
  50. {
  51. public static void EnsureColumns<TEntity>(this IProvider provider, TEntity entity, Columns<TEntity> columns)
  52. where TEntity : Entity, new()
  53. {
  54. var newColumns = Columns.None<TEntity>()
  55. .AddRange(columns.Where(x => !entity.HasColumn(x.Property)));
  56. if (newColumns.Count > 0)
  57. {
  58. var row = provider.Query(new Filter<TEntity>(x => x.ID).IsEqualTo(entity.ID), newColumns).Rows.FirstOrDefault();
  59. row?.FillObject(entity);
  60. }
  61. }
  62. public static void EnsureColumns<TEntity>(this IProvider provider, ICollection<TEntity> entities, Columns<TEntity> columns)
  63. where TEntity : Entity, IRemotable, new()
  64. {
  65. var newColumns = Columns.None<TEntity>()
  66. .AddRange(columns.Where(x => entities.Any(entity => !entity.HasColumn(x.Property))));
  67. if (newColumns.Count > 0)
  68. {
  69. newColumns.Add(x => x.ID);
  70. var table = provider.Query(new Filter<TEntity>(x => x.ID).InList(entities.Select(x => x.ID).ToArray()), newColumns);
  71. foreach(var row in table.Rows)
  72. {
  73. var id = row.Get<TEntity, Guid>(x => x.ID);
  74. var entity = entities.FirstOrDefault(x => x.ID == id);
  75. if(entity is null)
  76. {
  77. // Shouldn't happen, but just in case.
  78. continue;
  79. }
  80. row?.FillObject(entity);
  81. }
  82. }
  83. }
  84. }