yellowfeather / DbfDataReader

DbfDataReader is a small fast .Net Core library for reading dBase, xBase, Clipper and FoxPro database files
MIT License
134 stars 61 forks source link

GetFieldType throws NotImplementedException, so the reader can't be used with Dapper #47

Closed jnm2 closed 4 years ago

jnm2 commented 4 years ago

We were hoping to use Dapper's dataReader.Parse<T>(); extension method, but Dapper calls IDataReader.GetFieldType and the current implementation crashes:

https://github.com/yellowfeather/DbfDataReader/blob/a68402cf31039807c834641012279d160a6aad8c/src/DbfDataReader/DbfDataReader.cs#L255-L258

This is the call stack from Dapper:

at DbfDataReader.DbfDataReader.GetFieldType(Int32 ordinal)
at Dapper.SqlMapper.GetColumnHash(IDataReader reader, Int32 startBound, Int32 length) in C:\projects\dapper\Dapper\SqlMapper.cs:line 41
at Dapper.SqlMapper.TypeDeserializerCache.GetReader(IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in C:\projects\dapper\Dapper\SqlMapper.TypeDeserializerCache.cs:line 143
at Dapper.SqlMapper.TypeDeserializerCache.GetReader(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in C:\projects\dapper\Dapper\SqlMapper.TypeDeserializerCache.cs:line 50
at Dapper.SqlMapper.GetDeserializer(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing) in C:\projects\dapper\Dapper\SqlMapper.cs:line 1789
at Dapper.SqlMapper.GetRowParser[T](IDataReader reader, Type concreteType, Int32 startIndex, Int32 length, Boolean returnNullIfFirstMissing) in C:\projects\dapper\Dapper\SqlMapper.IDataReader.cs:line 142

Would you take a pull request to implement GetFieldType?

It looks the implementation could be something like a switch on DbfTable.Columns[ordinal].ColumnType similar to the switch in DbfRecord.CreateDbfValue.

chrisrichards commented 4 years ago

A PR would be great, thanks! I wonder if it would be better to add something like Type GetType() to IDbfValue and the implementations of DbfValue

jnm2 commented 4 years ago

@chrisrichards Oh, interesting. Would that allow GetFieldType to work even if there are no values, e.g. when Read() has not yet been called or after Read() returns false?

chrisrichards commented 4 years ago

Yes it would, the values are created in the DbfRecord constructor before any records have been read

chrisrichards commented 4 years ago

Fixed by #49

jnm2 commented 4 years ago

Nice, thank you for the fast release!