Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/mattosaurus/CsvHelper int…
Browse files Browse the repository at this point in the history
…o mattosaurus-master
  • Loading branch information
JoshClose committed Mar 23, 2021
2 parents eae9c4f + 359a1e5 commit 7b5167c
Showing 1 changed file with 53 additions and 22 deletions.
75 changes: 53 additions & 22 deletions src/CsvHelper/CsvDataReader.cs
Expand Up @@ -16,8 +16,53 @@ namespace CsvHelper
public class CsvDataReader : IDataReader
{
private readonly CsvReader csv;
private readonly DataTable schemaTable;
private bool skipNextRead;

/// <summary>
/// Initializes a new instance of the <see cref="CsvDataReader"/> class.
/// </summary>
/// <param name="csv">The CSV.</param>
public CsvDataReader(CsvReader csv)
{
this.csv = csv;

csv.Read();

if (csv.Configuration.HasHeaderRecord)
{
csv.ReadHeader();
}
else
{
skipNextRead = true;
}

schemaTable = GetSchemaTable();
}

/// <summary>
/// Initializes a new instance of the <see cref="CsvDataReader"/> class.
/// </summary>
/// <param name="csv">The CSV.</param>
/// <param name="schemaTable">The DataTable representing the file schema.</param>
public CsvDataReader(CsvReader csv, DataTable schemaTable)
{
this.csv = csv;
this.schemaTable = schemaTable;

csv.Read();

if (csv.Configuration.HasHeaderRecord)
{
csv.ReadHeader();
}
else
{
skipNextRead = true;
}
}

/// <summary>
/// Gets the column with the specified index.
/// </summary>
Expand Down Expand Up @@ -88,26 +133,6 @@ public int FieldCount
}
}

/// <summary>
/// Initializes a new instance of the <see cref="CsvDataReader"/> class.
/// </summary>
/// <param name="csv">The CSV.</param>
public CsvDataReader(CsvReader csv)
{
this.csv = csv;

csv.Read();

if (csv.Configuration.HasHeaderRecord)
{
csv.ReadHeader();
}
else
{
skipNextRead = true;
}
}

/// <summary>
/// Closes the <see cref="T:System.Data.IDataReader"></see> Object.
/// </summary>
Expand Down Expand Up @@ -387,9 +412,14 @@ public int GetOrdinal(string name)
/// </returns>
public DataTable GetSchemaTable()
{
if (this.schemaTable != null)
{
return this.schemaTable;
}
else
{
// https://docs.microsoft.com/en-us/dotnet/api/system.data.datatablereader.getschematable?view=netframework-4.7.2

var dt = new DataTable("SchemaTable");
DataTable dt = new DataTable("SchemaTable");
dt.Columns.Add("AllowDBNull", typeof(bool));
dt.Columns.Add("AutoIncrementSeed", typeof(long));
dt.Columns.Add("AutoIncrementStep", typeof(long));
Expand Down Expand Up @@ -454,6 +484,7 @@ public DataTable GetSchemaTable()

return dt;
}
}

/// <summary>
/// Gets the string value of the specified field.
Expand Down

0 comments on commit 7b5167c

Please sign in to comment.