ColumnUpdate.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace InABox.Database;
  9. public interface IColumnUpdate
  10. {
  11. IColumn Column { get; }
  12. IComplexFormulaNode Update { get; }
  13. }
  14. public interface IColumnUpdate<T> : IColumnUpdate
  15. {
  16. new Column<T> Column { get; }
  17. IColumn IColumnUpdate.Column => Column;
  18. }
  19. public class ColumnUpdate<T, TValue> : IColumnUpdate<T>
  20. {
  21. public Column<T> Column { get; }
  22. public IComplexFormulaNode<T, TValue> Update { get; }
  23. IComplexFormulaNode IColumnUpdate.Update => Update;
  24. public ColumnUpdate(Expression<Func<T, TValue>> column, IComplexFormulaNode<T, TValue> update)
  25. {
  26. Column = new(CoreUtils.GetFullPropertyName(column));
  27. Update = update;
  28. }
  29. }
  30. public interface IColumnUpdates
  31. {
  32. IEnumerable<IColumnUpdate> Updates();
  33. }
  34. public class ColumnUpdates<T> : List<IColumnUpdate<T>>, IColumnUpdates
  35. {
  36. public IEnumerable<IColumnUpdate> Updates() => this;
  37. public ColumnUpdates<T> Add<TValue>(Expression<Func<T, TValue>> column, IComplexFormulaNode<T, TValue> update)
  38. {
  39. Add(new ColumnUpdate<T, TValue>(column, update));
  40. return this;
  41. }
  42. public ColumnUpdates<T> Add<TValue>(Expression<Func<T, TValue>> column, Func<IComplexFormulaGenerator<T, TValue>, IComplexFormulaNode<T, TValue>> update)
  43. {
  44. Add(new ColumnUpdate<T, TValue>(column, update(IComplexFormulaGenerator.Create<T, TValue>())));
  45. return this;
  46. }
  47. public ColumnUpdates<T> AddProperty<TValue>(Expression<Func<T, TValue>> column, Expression<Func<T, TValue>> field)
  48. {
  49. Add(new ColumnUpdate<T, TValue>(column, ComplexFormulaGenerator.Property(field)));
  50. return this;
  51. }
  52. public ColumnUpdates<T> AddConstant<TValue>(Expression<Func<T, TValue>> column, TValue constant)
  53. {
  54. Add(new ColumnUpdate<T, TValue>(column, ComplexFormulaGenerator.Constant<T, TValue>(constant)));
  55. return this;
  56. }
  57. }