103 lines
2.5 KiB
C#
103 lines
2.5 KiB
C#
using mbts;
|
|
|
|
using System;
|
|
using System.Collections;
|
|
|
|
namespace BCG
|
|
{
|
|
public class NullToConstantMapper : IStreamMapper
|
|
{
|
|
public NullToConstantMapper(string field, object constant)
|
|
{
|
|
myField = field;
|
|
myConstant = constant;
|
|
}
|
|
|
|
#region IStreamMapper Members
|
|
|
|
public IBlueTableSchema getOutputSchema(IBlueTableSchema inputSchema)
|
|
{
|
|
return inputSchema;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IDynamicStreamMapper Members
|
|
|
|
public IBlueRow map(IBlueRow row) =>
|
|
row[myField] switch {
|
|
null or DBNull => new BlueRow(row.schema, new Hashtable(row) { { myField, myConstant } }),
|
|
_ => row,
|
|
};
|
|
|
|
#endregion
|
|
|
|
private string myField;
|
|
private object myConstant;
|
|
}
|
|
|
|
public class AllNullToConstantMapper : IStreamMapper
|
|
{
|
|
public AllNullToConstantMapper(object constant)
|
|
{
|
|
myConstant = constant;
|
|
}
|
|
|
|
#region IStreamMapper Members
|
|
|
|
public IBlueTableSchema getOutputSchema(IBlueTableSchema inputSchema)
|
|
{
|
|
return inputSchema;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IDynamicStreamMapper Members
|
|
|
|
public IBlueRow map(IBlueRow row)
|
|
{
|
|
bool change = false;
|
|
Hashtable newRow = new Hashtable(row);
|
|
foreach(ColumnInfo info in row.schema.columnInfo)
|
|
{
|
|
switch (row[info.colName]) {
|
|
case null or DBNull:
|
|
change = true;
|
|
newRow[info.colName] = Convert.ChangeType(myConstant, info.colType);
|
|
break;
|
|
}
|
|
}
|
|
if(change)
|
|
return new BlueRow(row.schema, newRow);
|
|
else
|
|
return row;
|
|
}
|
|
|
|
#endregion
|
|
private object myConstant;
|
|
}
|
|
|
|
public class SpecifiedNullToConstantMapper : IStreamMapper
|
|
{
|
|
private Pair<string, object>[] _col_val_pair_list;
|
|
public SpecifiedNullToConstantMapper(params Pair<string, object>[] col_val_pair_list)
|
|
{
|
|
_col_val_pair_list = col_val_pair_list;
|
|
}
|
|
|
|
public IBlueTableSchema getOutputSchema(IBlueTableSchema s) {return s;}
|
|
|
|
public IBlueRow map(IBlueRow r)
|
|
{
|
|
MutableBlueRow rv = new MutableBlueRow(getOutputSchema(r.schema), r);
|
|
foreach (Pair<string, object> p in _col_val_pair_list)
|
|
{
|
|
object v = BlueDataUtility.get_row_value<object>(r, p.first, DBNull.Value);
|
|
if (v == null || v == DBNull.Value)
|
|
rv.setValue(p.first, p.second);
|
|
}
|
|
return rv;
|
|
}
|
|
}
|
|
}
|