protected GetReader ( ClassMapping mapping ) : |
||
mapping | ClassMapping | The mapping for the object we intend to read. |
Résultat |
protected internal StreamReader GetReader(ClassMapping mapping)
{
switch (_connDesc.Type)
{
case CsvConnectionType.Directory:
case CsvConnectionType.FileName:
return new StreamReader(GetFileName(mapping));
case CsvConnectionType.Reader:
// We have a reader specifically set.
// Make sure we're back at the beginning.
_connDesc.Reader.BaseStream.Seek(0, SeekOrigin.Begin);
_connDesc.Reader.DiscardBufferedData();
return _connDesc.Reader;
case CsvConnectionType.Writer:
throw new LoggingException("Connection for class " +
mapping + " was configured as read-only: " + _connDesc.Type);
default:
throw new LoggingException("Unable to get reader for class " +
mapping + " for unsupported connection type: " + _connDesc.Type);
}
}
private static DataReaderConfig GetConfig(CsvDaLayer layer, ClassMapping mapping) { CsvDataReaderConfig retVal = new CsvDataReaderConfig(); try { retVal.Reader = layer.GetReader(mapping); if (CsvDaLayer.UseNamedColumns(mapping)) { // If the CSV has row headers, read the header row. IList colNameRow = ReadRawCsvRow(retVal.Reader); for (int x = 0; x < colNameRow.Count; x++) { retVal.IndexesByName[colNameRow[x].ToString()] = x; } } else { // No row headers, so we must be mapped to column numbers. // In that case, just map the column number strings to the ints. foreach (string colStr in mapping.AllDataColsInOrder) { // Remember the mapping column numbers are 1-based but // the internal column numbers are 0-based. retVal.IndexesByName[colStr] = int.Parse(colStr) - 1; } } return retVal; } catch (Exception e) { // Problem setting up, close the reader. if (retVal.Reader != null) { layer.DoneWithReader(retVal.Reader); } throw new LoggingException("Unable to begin reading from CSV file.", e); } }