using System; using System.Collections; using System.Text; using BCG; using BCG.Utilities; namespace BCG { public class ConcatenateColumnsMapper : IStreamMapper { protected IDictionary myTempColMap = new SortedList (); protected ArrayList myColMap; protected BlueTableSchema mySchema; public ConcatenateColumnsMapper(params string [] columnDefs) { foreach (string cd in columnDefs) { string outColumn = cd.Substring(0,cd.IndexOf(":")); string [] inColumns = cd.Substring(cd.IndexOf(":") + 1).Split(' '); string [] trimedIn = new string[inColumns.Length]; int n = 0; foreach (string s in inColumns) trimedIn[n++] = s.Trim(); myTempColMap[outColumn.Trim()] = trimedIn; } } #region IStreamMapper Members public IBlueTableSchema getOutputSchema (IBlueTableSchema inputSchema) { if (mySchema == null) { /* * The new columns form the first columns, and the existing columns in the input * schema are appened. * */ ArrayList cis = new ArrayList (); foreach (string cn in myTempColMap.Keys) cis.Add (new ColumnInfo (cn, cis.Count, typeof(string))); foreach (ColumnInfo ci in inputSchema.columnInfo) cis.Add (new ColumnInfo (ci.colName, cis.Count, ci.colType)); mySchema = new BlueTableSchema("ConcatenateColumns " + inputSchema.tableName, cis.ToArray (typeof(ColumnInfo)) as ColumnInfo[]); /* * An arraylist is formed which indexes the new columns to the existing ones that form it. Hashtables * are avoided wherever possible (which is natural as well, as the keys are ordered integers) * */ myColMap = new ArrayList (myTempColMap.Count); foreach (DictionaryEntry de in myTempColMap) { string[] cols = de.Value as string[]; ArrayList temp = new ArrayList (); foreach (string col in cols) temp.Add (inputSchema[col].colNo); myColMap.Add (temp.ToArray(typeof(int)) as int[]); } myTempColMap.Clear (); } return mySchema; } #endregion #region IDynamicStreamMapper Members public virtual IBlueRow map (IBlueRow row) { object[] retVal = new object[mySchema.columnInfo.Length]; row.CopyTo (retVal, myColMap.Count); for (int colNo = 0; colNo < myColMap.Count; colNo ++) retVal[colNo] = myGetConcatValue (row, colNo); return new BlueRow (mySchema, retVal); } #endregion /// /// Given a row and a column number, the method extracts the constituent columns indices /// (from the colMap array) and form the resultant value corresponding to colNo and the row passed /// /// /// /// protected virtual object myGetConcatValue (IBlueRow row, int colNo) { StringBuilder b = new StringBuilder(); foreach (int cn in myColMap[colNo] as int[]) b.Append(row[cn].ToString()); return b.ToString (); } } public class ConcatenateColumnsMapperForSameDataSet : ConcatenateColumnsMapper { private HashTableND[] myDicts; public ConcatenateColumnsMapperForSameDataSet (params string [] columnDefs) : base (columnDefs) { myDicts = new HashTableND [columnDefs.Length]; int i = 0; foreach (DictionaryEntry de in myTempColMap) myDicts[i++] = new HashTableND ((de.Value as string[]).Length); } protected override object myGetConcatValue (IBlueRow row, int colNo) { int[] cols = myColMap[colNo] as int[]; object[] vals = new object[cols.Length]; for (int i = 0; i < vals.Length; i++) vals[i] = row[cols[i]]; HashTableND ht = myDicts[colNo]; object rv = ht.getValue (vals); if (rv != null) return rv; else { rv = base.myGetConcatValue (row, colNo); object[] args = new object[vals.Length + 1]; vals.CopyTo (args, 0); args[args.Length-1] = rv; ht.add (args); return rv; } } } }