53 lines
1.8 KiB
C#
53 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace BCG {
|
|
public class AddColumnsStream : ViewStream {
|
|
public static Tuple<string, Type, object> parse_single_tok(string exp) {
|
|
string[] toks = exp.Split(':');
|
|
string col_name = toks[0].Trim();
|
|
string col_value = toks[1].Trim();
|
|
Tuple<Type, object> tup = BlueDataUtility.guess_type(col_value);
|
|
return Tuple.Create(col_name, tup.Item1, tup.Item2);
|
|
}
|
|
|
|
public static List<Tuple<string, Type, object>> parse_exp(string exp) {
|
|
string[] toks = exp.Split(",");
|
|
List<Tuple<string, Type, object>> rv = new List<Tuple<string, Type, object>>();
|
|
foreach (string s in toks) {
|
|
var tup = parse_single_tok(s.Trim());
|
|
rv.Add(tup);
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
private Tuple<string, Type, object> _new_col;
|
|
private List<Tuple<string, Type, object>> _new_col_map;
|
|
private IBlueTableSchema _out_schema;
|
|
|
|
public AddColumnsStream(ViewStream vs, string col_def) {
|
|
_new_col_map = parse_exp(col_def);
|
|
ViewStream rs = new MapStream(vs, (IBlueTableSchema s) => _get_schema(s), (IBlueRow r) => _map(r));
|
|
mySetRealSource(rs);
|
|
}
|
|
|
|
private IBlueTableSchema _get_schema(IBlueTableSchema input_schema) {
|
|
if (_out_schema == null) {
|
|
_out_schema = input_schema;
|
|
foreach (var m in _new_col_map) {
|
|
_out_schema = BlueDataUtility.add_column(_out_schema, m.Item1, m.Item2);
|
|
}
|
|
}
|
|
return _out_schema;
|
|
}
|
|
|
|
private IBlueRow _map(IBlueRow r) {
|
|
MutableBlueRow rv = new MutableBlueRow(_get_schema(r.schema), r);
|
|
foreach (var m in _new_col_map) {
|
|
rv.setValue(m.Item1, m.Item3);
|
|
}
|
|
return rv;
|
|
}
|
|
}
|
|
}
|