88 lines
2.8 KiB
C#
88 lines
2.8 KiB
C#
#nullable enable
|
|
namespace Bts.DataStream;
|
|
|
|
using BCG;
|
|
|
|
using bts.collections;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
public class BlueTableSchemaAdaptor<T> : IMutableBlueRowFactory<T> {
|
|
readonly string name;
|
|
readonly List<ColumnAdaptor> columns = new();
|
|
BlueTableSchema? schema;
|
|
|
|
public BlueTableSchemaAdaptor(string name) =>
|
|
this.name = name;
|
|
|
|
public IBlueTableSchema Schema =>
|
|
schema ??= new BlueTableSchema(columns.WrapSelectorRO(c => c.Info).ToArray(), name);
|
|
|
|
public bool Contains(string name) =>
|
|
columns.Any(c => c.Info.colName == name);
|
|
|
|
public void AddColumn(string name, Func<T, int> getValue) =>
|
|
AddColumn(name, r => getValue(r), typeof(int));
|
|
|
|
public void AddColumn(string name, Func<T, int?> getValue) =>
|
|
AddColumn(name, r => getValue(r) ?? (object)DBNull.Value, typeof(int));
|
|
|
|
public void AddColumn(string name, Func<T, long> getValue) =>
|
|
AddColumn(name, r => getValue(r), typeof(long));
|
|
|
|
public void AddColumn(string name, Func<T, long?> getValue) =>
|
|
AddColumn(name, r => getValue(r) ?? (object)DBNull.Value, typeof(long));
|
|
|
|
public void AddColumn(string name, Func<T, double> getValue) =>
|
|
AddColumn(name, r => getValue(r), typeof(double));
|
|
|
|
public void AddColumn(string name, Func<T, double?> getValue) =>
|
|
AddColumn(name, r => getValue(r) ?? (object)DBNull.Value, typeof(double));
|
|
|
|
public void AddColumn(string name, Func<T, DateTime> getValue) =>
|
|
AddColumn(name, r => getValue(r), typeof(DateTime));
|
|
|
|
public void AddColumn(string name, Func<T, DateTime?> getValue) =>
|
|
AddColumn(name, r => getValue(r) ?? (object)DBNull.Value, typeof(DateTime));
|
|
|
|
public void AddColumn(string name, Func<T, string?> getValue) =>
|
|
AddColumn(name, r => getValue(r) ?? (object)DBNull.Value, typeof(string));
|
|
|
|
public void AddColumn(string name, Func<T, object?> getValue, Type dataType) {
|
|
if (!ColumnInfo.isAllowedType(dataType)) {
|
|
throw new ArgumentException(
|
|
$"'{dataType}' is not a supported column type", nameof(dataType));
|
|
}
|
|
// Invalidate cached schema (set it to null)
|
|
schema = null;
|
|
columns.Add(new ColumnAdaptor(new(name, columns.Count, dataType), r => getValue(r)));
|
|
}
|
|
|
|
public IBlueRow GetBlueRow(T item) =>
|
|
GetMutableBlueRow(item);
|
|
|
|
public MutableBlueRow GetMutableBlueRow(T item) {
|
|
var mbr = new MutableBlueRow(Schema);
|
|
foreach (var col in columns) {
|
|
mbr.setValue(col.Info.colNo, col.GetValue(item));
|
|
}
|
|
return mbr;
|
|
}
|
|
|
|
class ColumnAdaptor {
|
|
readonly Func<T, object?> getValue;
|
|
|
|
public ColumnAdaptor(ColumnInfo info, Func<T, object?> getValue) {
|
|
Info = info;
|
|
this.getValue = getValue;
|
|
}
|
|
|
|
public ColumnInfo Info { get; }
|
|
|
|
public object? GetValue(T row) =>
|
|
getValue(row);
|
|
}
|
|
}
|