using Bts.ColumnMapExpressions;
using Spark.Core.Diagnostics;
using System;
using System.Collections;
namespace BCG
{
///
/// ColumnMap governs what columns that are to be projected, hidden or aliased
///
public class ColumnMap
{
private string myColMapExpression;
///
/// ColumnMap governs what columns that are to be projected, hidden or aliased. If all columns
/// are to be preserved with their names, ColumnMap("", false) should be passed
///
///
/// Includes the mapping of the columns to their respective aliases as well as the columns that
/// need to be included/excluded depending on the second parameter. e.g. "a:b c" means to map
/// a onto b, and include or exclude c depending on the value of the second parameter
///
///
/// isInclusive decides whether to include the unmapped columns in argument one or to exclude it
/// from the resulting table
///
public ColumnMap (string colMapExpression)
{
myColMapExpression = colMapExpression == null ? "!" : colMapExpression;
}
public IDictionary getColumnMap (IBlueTableSchema schema)
{
IDictionary retVal = new Hashtable ();
var e = ColumnMapExpression.Parse(myColMapExpression) ??
throw new Exception($"Failed to parse column expression '{myColMapExpression}'");
if (e.IsInclusive) {
retVal = new Hashtable(e.Columns) {
["id"] = "id"
};
}
else
foreach (ColumnInfo ci in schema.columnInfo)
if (e.Columns[ci.colName] == null)
retVal[ci.colName] = ci.colName;
else if (e.Columns[ci.colName] != ci.colName)
retVal[ci.colName] = e.Columns[ci.colName];
return invertDictionary (retVal);
}
public static IDictionary invertDictionary (IDictionary dic)
{
IDictionary retVal = new Hashtable ();
foreach (DictionaryEntry de in dic)
retVal[ExceptionAssert.NotNull(de.Value)] = de.Key;
return retVal;
}
public string columnExpression {get{return myColMapExpression;}}
}
}