feat(MiniExcelDataReaderBase): add MniExcelDataReaderBase class to simplify code (#651)

* feat: 增加 MiniExcelDataReaderBase 基类

* refactor: 使用基类精简代码

* Update MiniExcelDataReaderBase.cs

Signed-off-by: Wei Lin <shps951002@gmail.com>

---------

Signed-off-by: Wei Lin <shps951002@gmail.com>
Co-authored-by: Wei Lin <shps951002@gmail.com>
This commit is contained in:
Argo Zhang 2024-08-15 20:37:15 +08:00 committed by GitHub
parent 4b7f1696db
commit b20ed40f81
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 288 additions and 129 deletions

View File

@ -1,12 +1,11 @@
namespace MiniExcelLibs
{
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
public class MiniExcelDataReader : IDataReader
public class MiniExcelDataReader : MiniExcelDataReaderBase
{
private readonly IEnumerator<IDictionary<string, object>> _source;
private readonly int _fieldCount;
@ -26,22 +25,29 @@
}
}
public void Dispose()
{
_stream.Dispose();
}
public object GetValue(int i)
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override object GetValue(int i)
{
return _source.Current[_keys[i]];
}
public int FieldCount
/// <summary>
/// <inheritdoc/>
/// </summary>
public override int FieldCount
{
get { return _fieldCount; }
}
public bool Read()
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
public override bool Read()
{
if (_isFirst)
{
@ -51,135 +57,37 @@
return _source.MoveNext();
}
public string GetName(int i)
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override string GetName(int i)
{
return _keys[i];
}
public int GetOrdinal(string name)
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public override int GetOrdinal(string name)
{
var i = _keys.IndexOf(name);
return _keys.IndexOf(name);
}
public void Close()
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
return;
}
public int Depth => throw new NotImplementedException();
public bool IsClosed => throw new NotImplementedException();
public int RecordsAffected => throw new NotImplementedException();
public object this[string name] => throw new NotImplementedException();
public object this[int i] => throw new NotImplementedException();
public DataTable GetSchemaTable()
{
throw new NotImplementedException();
}
public bool NextResult()
{
throw new NotImplementedException();
}
public bool GetBoolean(int i)
{
throw new NotImplementedException();
}
public byte GetByte(int i)
{
throw new NotImplementedException();
}
public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
{
throw new NotImplementedException();
}
public char GetChar(int i)
{
throw new NotImplementedException();
}
public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
{
throw new NotImplementedException();
}
public IDataReader GetData(int i)
{
throw new NotImplementedException();
}
public string GetDataTypeName(int i)
{
throw new NotImplementedException();
}
public DateTime GetDateTime(int i)
{
throw new NotImplementedException();
}
public decimal GetDecimal(int i)
{
throw new NotImplementedException();
}
public double GetDouble(int i)
{
throw new NotImplementedException();
}
public Type GetFieldType(int i)
{
throw new NotImplementedException();
}
public float GetFloat(int i)
{
throw new NotImplementedException();
}
public Guid GetGuid(int i)
{
throw new NotImplementedException();
}
public short GetInt16(int i)
{
throw new NotImplementedException();
}
public int GetInt32(int i)
{
throw new NotImplementedException();
}
public long GetInt64(int i)
{
throw new NotImplementedException();
}
public string GetString(int i)
{
throw new NotImplementedException();
}
public int GetValues(object[] values)
{
throw new NotImplementedException();
}
public bool IsDBNull(int i)
{
throw new NotImplementedException();
if (disposing)
{
_stream.Dispose();
}
}
}
}

View File

@ -0,0 +1,251 @@
namespace MiniExcelLibs
{
using System;
using System.Data;
/// <summary>
/// IDataReader Base Class
/// </summary>
public abstract class MiniExcelDataReaderBase : IDataReader
{
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual object this[int i] => null;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public virtual object this[string name] => null;
/// <summary>
/// <inheritdoc/>
/// </summary>
public virtual int Depth { get; } = 0;
/// <summary>
/// <inheritdoc/>
/// </summary>
public virtual bool IsClosed { get; } = false;
/// <summary>
/// <inheritdoc/>
/// </summary>
public virtual int RecordsAffected { get; } = 0;
/// <summary>
/// <inheritdoc/>
/// </summary>
public virtual int FieldCount { get; }
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual bool GetBoolean(int i) => false;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual byte GetByte(int i) => byte.MinValue;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <param name="fieldOffset"></param>
/// <param name="buffer"></param>
/// <param name="bufferOffset"></param>
/// <param name="length"></param>
/// <returns></returns>
public virtual long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferOffset, int length) => 0;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual char GetChar(int i) => char.MinValue;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <param name="fieldOffset"></param>
/// <param name="buffer"></param>
/// <param name="bufferOffset"></param>
/// <param name="length"></param>
/// <returns></returns>
public virtual long GetChars(int i, long fieldOffset, char[] buffer, int bufferOffset, int length) => 0;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual IDataReader GetData(int i) => null;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual string GetDataTypeName(int i) => string.Empty;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual DateTime GetDateTime(int i) => DateTime.MinValue;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual decimal GetDecimal(int i) => 0;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual double GetDouble(int i) => 0;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual Type GetFieldType(int i) => null;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual float GetFloat(int i) => 0f;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual Guid GetGuid(int i) => Guid.Empty;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual short GetInt16(int i) => 0;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual int GetInt32(int i) => 0;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual long GetInt64(int i) => 0;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public virtual int GetOrdinal(string name) => 0;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
public virtual DataTable GetSchemaTable() => null;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual string GetString(int i) => string.Empty;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="values"></param>
/// <returns></returns>
public virtual int GetValues(object[] values) => 0;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public virtual bool IsDBNull(int i) => false;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
public virtual bool NextResult() => false;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public abstract string GetName(int i);
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public abstract object GetValue(int i);
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
public abstract bool Read();
/// <summary>
/// <inheritdoc/>
/// </summary>
public virtual void Close()
{
}
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
}
/// <summary>
/// <inheritdoc/>
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}