public async Task TestBig() { var tr = TestData.GetTextReader(); string str = null; var csv = await CsvDataReader.CreateAsync(tr); while (await csv.ReadAsync()) { var s = csv.GetString(0); var v = csv.GetInt32(0); var n = csv.GetString(10); for (int i = 0; i < csv.FieldCount; i++) { str = csv.GetString(i); } var v1 = csv.GetInt32(20); } // there was previously a bug, where the last value was read as "103\n". Assert.Equal("103", str); }
public async Task BinaryValues() { using (var reader = File.OpenText("Data\\Binary.csv")) { var schema = new TypedCsvSchema(); schema.Add(1, typeof(byte[])); var opts = new CsvDataReaderOptions() { Schema = schema }; var csv = await CsvDataReader.CreateAsync(reader, opts); csv.Read(); var vals = new object[2]; csv.GetValues(vals); var expected = Encoding.ASCII.GetBytes(BinaryValue1); Assert.Equal(expected, (byte[])vals[1]); csv.Read(); csv.GetValues(vals); expected = Encoding.ASCII.GetBytes(BinaryValue2); Assert.Equal(expected, (byte[])vals[1]); } }
static async Task ProcessDataAsync(CsvDataReader dr) { var types = new TypeCode[dr.FieldCount]; for (int i = 0; i < types.Length; i++) { types[i] = Type.GetTypeCode(dr.GetFieldType(i)); } while (await dr.ReadAsync()) { for (int i = 0; i < dr.FieldCount; i++) { switch (types[i]) { case TypeCode.Int32: var v = dr.GetInt32(i); break; case TypeCode.Double: if (i == 4 && dr.IsDBNull(i)) { break; } var d = dr.GetDouble(i); break; case TypeCode.String: var s = dr.GetString(i); break; default: break; } } } }
public async Task NoHeadersWithSchema() { var schema = new ExcelHeaders(); var opts = new CsvDataReaderOptions { HasHeaders = false, Schema = schema }; using (var reader = File.OpenText("Data\\DataOnly.csv")) { var csv = await CsvDataReader.CreateAsync(reader, opts); Assert.Equal(4, csv.FieldCount); Assert.True(csv.HasRows); Assert.Equal(0, csv.RowNumber); Assert.Equal("C", csv.GetName(2)); Assert.Equal(3, csv.GetOrdinal("D")); Assert.Equal("A", csv.GetName(0)); Assert.Throws <IndexOutOfRangeException>(() => csv.GetOrdinal("Id")); Assert.True(await csv.ReadAsync()); Assert.Equal(1, csv.RowNumber); Assert.Equal("1", csv[0]); Assert.Equal("John", csv[1]); Assert.Equal("Low", csv[2]); Assert.Equal("2000-11-11", csv[3]); Assert.True(await csv.ReadAsync()); Assert.Equal(2, csv.RowNumber); Assert.Equal("2", csv[0]); Assert.Equal("Jane", csv[1]); Assert.Equal("High", csv[2]); Assert.Equal("1989-03-14", csv[3]); Assert.False(await csv.ReadAsync()); } }
public async Task Quoted() { using (var reader = File.OpenText("Data\\Quote.csv")) { var csv = await CsvDataReader.CreateAsync(reader); Assert.Equal(5, csv.FieldCount); Assert.True(csv.HasRows); Assert.Equal(0, csv.RowNumber); Assert.Equal("Id", csv.GetName(0)); Assert.Equal("Name", csv.GetName(1)); Assert.Equal("Value", csv.GetName(2)); Assert.Equal("Date", csv.GetName(3)); Assert.Equal("Original, Origin", csv.GetName(4)); Assert.True(await csv.ReadAsync()); Assert.Equal(1, csv.RowNumber); Assert.Equal("1", csv[0]); Assert.Equal("John", csv[1]); Assert.Equal("Very\r\nLow\r\n", csv[2]); Assert.Equal("2000-11-11", csv[3]); Assert.True(await csv.ReadAsync()); Assert.Equal(2, csv.RowNumber); Assert.Equal("2", csv[0]); Assert.Equal("Jane", csv[1]); Assert.Equal("\"High\"", csv[2]); Assert.Equal("1989-03-14", csv[3]); Assert.True(await csv.ReadAsync()); Assert.Equal(3, csv.RowNumber); Assert.Equal("3", csv[0]); Assert.Equal("Comma", csv[1]); Assert.Equal("Quite, Common", csv[2]); Assert.Equal("2020-05-29", csv[3]); Assert.Equal("", csv[4]); Assert.False(await csv.ReadAsync()); } }
public static DbDataReader GetData() { return(CsvDataReader.Create(GetTextReader())); }
public Enumerator(CsvDataReader reader) { this.reader = reader; }
public async Task SylvanSchema() { using var tr = TestData.GetTextReader(); using var dr = await CsvDataReader.CreateAsync(tr, new CsvDataReaderOptions { Schema = TestData.TestDataSchema }); await ProcessDataAsync(dr); }
public void Create() { Assert.ThrowsAsync <ArgumentNullException>(() => CsvDataReader.CreateAsync((TextReader)null)); }
public void MissingHeaders() { Assert.Throws <CsvMissingHeadersException>(() => CsvDataReader.Create(new StringReader(""))); }
void InitializeMetadata() { { var te = zip.GetEntry(TablesMetaEntryName); if (te == null) { // TODO set conformance no meta tables } else { try { using var sw = new StreamReader(te.Open()); var tablesCsv = CsvDataReader.Create(sw, Options); // todo: handle extended metadata? punt for now. var filenameIdx = tablesCsv.GetOrdinal("filename"); var bytesIdx = tablesCsv.GetOrdinal("bytes"); var rowsIdx = tablesCsv.GetOrdinal("rows"); var colsIdx = tablesCsv.GetOrdinal("columns"); var descriptionIdx = tablesCsv.GetOrdinal("description"); var publishedIdx = tablesCsv.GetOrdinal("published"); var sourceIdx = tablesCsv.GetOrdinal("source"); while (tablesCsv.Read()) { string filename = tablesCsv.GetString(filenameIdx); long? bytes = tablesCsv.ReadInt64(bytesIdx); long? rows = tablesCsv.ReadInt64(rowsIdx); int? cols = tablesCsv.ReadInt32(colsIdx); string? description = tablesCsv.ReadString(descriptionIdx); DateTime?published = tablesCsv.ReadDateTime(publishedIdx); string? source = tablesCsv.ReadString(sourceIdx); var t = new TableInfo { filename = filename, bytes = bytes, rowCount = rows, colCount = cols, description = description, published = published, source = source, }; this.tables.Add(filename, t); } } catch (Exception) { // TODO ? ignore and treat as csvz-0 ? } } } { var te = zip.GetEntry(ColumnsMetaEntryName); if (te == null) { // TODO set conformance no meta columns } else { try { using var sw = new StreamReader(te.Open()); var csv = CsvDataReader.Create(sw, Options); // todo: handle extended metadata? punt for now. var filenameIdx = csv.GetOrdinal("filename"); var columnIdx = csv.GetOrdinal("column"); var ordinalIdx = csv.GetOrdinal("ordinal"); var typeIdx = csv.GetOrdinal("type"); var nullableIdx = csv.GetOrdinal("nullable"); var maxLengthIdx = csv.GetOrdinal("max-length"); var uniqueIdx = csv.GetOrdinal("unique"); var primaryKeyIdx = csv.GetOrdinal("primary-key"); var descriptionIdx = csv.GetOrdinal("description"); var unitsIdx = csv.GetOrdinal("units"); var publishedIdx = csv.GetOrdinal("published"); var sourceIdx = csv.GetOrdinal("source"); while (csv.Read()) { string filename = csv.GetString(filenameIdx); var c = new ColumnInfo { filename = filename, column = csv.ReadString(columnIdx), ordinal = csv.ReadInt32(ordinalIdx), type = csv.ReadString(typeIdx), nullable = csv.ReadBoolean(nullableIdx), maxLength = csv.ReadInt32(maxLengthIdx), unique = csv.ReadBoolean(uniqueIdx), primaryKey = csv.ReadBoolean(primaryKeyIdx), description = csv.ReadString(descriptionIdx), units = csv.ReadString(unitsIdx), published = csv.ReadDateTime(publishedIdx), source = csv.ReadString(sourceIdx), }; c.Init(); this.columns.Add(c); } } catch (Exception) { // TODO ? ignore and treat as csvz-0 ? } } } }