public override DataRecord Construct() { TableRecord record = new TableRecord(_tableColumnNames, _tableColumnSeparator); record.KeyColumnNos = _keyColumnNos; return record; }
// for wrapping table records for table conversion public ColumnWrapper(TableRecord record) { _separator = record.TableColumnSeparator; _columnNames = ((TableRecord)record).ColumnNames; _keyColumnNos = ((TableRecord)record).KeyColumnNos; _isTableRecord = true; return; }
public override void ProcessRecord(DataRecord record, RecordAccepter accepter) { // caseNum 0: null conversion (key expression == incoming key) // caseNum 1: table record // caseNum 2: everything else // So, the thing is a null conversion is costly, potentially super // costly. Instead of trying to rebuild the whole tree without // conversion (which brought with it potential sorting nodes as // well) we will error and message the user to remove the null // conversion. // initialize the wrapper if (_wrapper == null) { bool hasHeaders = false; int caseNum = 0; HintMessageToConsole(); // case 1: table record. if (record is TableRecord) { TableRecord t = record as TableRecord; _wrapper = new ColumnWrapper(t); caseNum = 1; // table record } else { caseNum = 2; // everything else hasHeaders = ColumnWrapper.HasHeaders(KeyExpression); _wrapper = new ColumnWrapper(record, _sourceName, _columnSeparator, hasHeaders); // if columnNames provided by filter user if (_columnNames != null) { if (_columnNames.Length != _wrapper.ColumnNames.Length) { Console.WriteLine("too few column names provided"); Environment.Exit(1); } } else { _columnNames = _wrapper.ColumnNames; // use default } } // making table records is costly. If the key columns have not // changed there is no reason to do the conversion. (since // sorting and joining work the same for all records). _keyColumnNos = _wrapper.GetColumnNos(KeyExpression); int[] currentKeyColumnNos = _wrapper.KeyColumnNos; // _keyColumnNos == the requested new key columns if (_keyColumnNos.Length == currentKeyColumnNos.Length) { for (int i = 0; i < _keyColumnNos.Length; i++) { if (_keyColumnNos[i] != currentKeyColumnNos[i]) _caseNum = caseNum; } } else _caseNum = caseNum; // we special case flat files converting to tables allowing null conversions // since when they define headers ToTable is evaluating and dropping them // from the input. if (hasHeaders) { _caseNum = 2; return; // eat up this record containing headers } } switch (_caseNum) { case 0: // null conversion see comments above Console.WriteLine("Null-table conversions are costly (i.e. key expression equal to incoming key)."); Console.WriteLine("Remove unnecessary 'ToTable(" + KeyExpression + ")' from expression."); Environment.Exit(1); break; case 1: // table record TableRecord outRecord = record as TableRecord; outRecord.KeyColumnNos = _keyColumnNos; accepter.AddRecord(outRecord); break; case 2: // everything else _wrapper.SetRecord(record); // ignore null records: no key no data if (record.Key.Length == 0 && record.Data == null) return; outRecord = new TableRecord(_columnNames, _columnSeparator); //if (_outRecord == null) { // _outRecord = new TableRecord(_columnNames, _columnSeparator); //} outRecord.KeyColumnNos = _keyColumnNos; _stringBuilder.Length = 0; for (int i = 0; i < _wrapper.ColumnNames.Length; i++) { if (i != 0) _stringBuilder.Append('\t'); _wrapper.AppendColumn(i, _stringBuilder); } outRecord.DelimitedColumns = _stringBuilder.ToString(); accepter.AddRecord(outRecord); break; } }
public TableRecord Join(DataRecord left, DataRecord right, bool match) { _leftWrapper.SetRecord(left); _rightWrapper.SetRecord(right); TableRecord outRecord = new TableRecord(_outputColumnNames, _separator); outRecord.KeyColumnNos = _leftWrapper.KeyColumnNos; _delimitedColumns.Length = 0; // reset // add the left columns for (int i = 0; i < _leftWrapper.ColumnNames.Length; i++) { if (i != 0) _delimitedColumns.Append(_separator); _leftWrapper.AppendColumn(i, _delimitedColumns); } if (!match) { _delimitedColumns.Append(_emptyRightColumns); outRecord.DelimitedColumns = _delimitedColumns.ToString(); return outRecord; } // we matched. // add the right columns int j = 0; for (int i = 0; i < _rightWrapper.ColumnNames.Length; i++) { if (i == _rightWrapper.KeyColumnNos[j]) { // don't add keys if (_rightWrapper.KeyColumnNos.Length > j + 1) j++; } else { _delimitedColumns.Append(_separator); _rightWrapper.AppendColumn(i, _delimitedColumns); } } outRecord.DelimitedColumns = _delimitedColumns.ToString(); return outRecord; }