From b20ed40f81e472f6b36eae6ddba4f09a7587a25c Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 15 Aug 2024 20:37:15 +0800 Subject: [PATCH] feat(MiniExcelDataReaderBase): add MniExcelDataReaderBase class to simplify code (#651) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 增加 MiniExcelDataReaderBase 基类 * refactor: 使用基类精简代码 * Update MiniExcelDataReaderBase.cs Signed-off-by: Wei Lin --------- Signed-off-by: Wei Lin Co-authored-by: Wei Lin --- src/MiniExcel/MiniExcelDataReader.cs | 166 ++++----------- src/MiniExcel/MiniExcelDataReaderBase.cs | 251 +++++++++++++++++++++++ 2 files changed, 288 insertions(+), 129 deletions(-) create mode 100644 src/MiniExcel/MiniExcelDataReaderBase.cs diff --git a/src/MiniExcel/MiniExcelDataReader.cs b/src/MiniExcel/MiniExcelDataReader.cs index 754ed83..9370897 100644 --- a/src/MiniExcel/MiniExcelDataReader.cs +++ b/src/MiniExcel/MiniExcelDataReader.cs @@ -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> _source; private readonly int _fieldCount; @@ -26,22 +25,29 @@ } } - public void Dispose() - { - _stream.Dispose(); - } - - public object GetValue(int i) + /// + /// + /// + /// + /// + public override object GetValue(int i) { return _source.Current[_keys[i]]; } - public int FieldCount + /// + /// + /// + public override int FieldCount { get { return _fieldCount; } } - public bool Read() + /// + /// + /// + /// + public override bool Read() { if (_isFirst) { @@ -51,135 +57,37 @@ return _source.MoveNext(); } - public string GetName(int i) + /// + /// + /// + /// + /// + public override string GetName(int i) { return _keys[i]; } - public int GetOrdinal(string name) + /// + /// + /// + /// + /// + public override int GetOrdinal(string name) { var i = _keys.IndexOf(name); return _keys.IndexOf(name); } - public void Close() + /// + /// + /// + /// + 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(); + } } } } diff --git a/src/MiniExcel/MiniExcelDataReaderBase.cs b/src/MiniExcel/MiniExcelDataReaderBase.cs new file mode 100644 index 0000000..3832bf6 --- /dev/null +++ b/src/MiniExcel/MiniExcelDataReaderBase.cs @@ -0,0 +1,251 @@ +namespace MiniExcelLibs +{ + using System; + using System.Data; + + /// + /// IDataReader Base Class + /// + public abstract class MiniExcelDataReaderBase : IDataReader + { + /// + /// + /// + /// + /// + public virtual object this[int i] => null; + + /// + /// + /// + /// + /// + public virtual object this[string name] => null; + + /// + /// + /// + public virtual int Depth { get; } = 0; + + /// + /// + /// + public virtual bool IsClosed { get; } = false; + + /// + /// + /// + public virtual int RecordsAffected { get; } = 0; + + /// + /// + /// + public virtual int FieldCount { get; } + + /// + /// + /// + /// + /// + public virtual bool GetBoolean(int i) => false; + + /// + /// + /// + /// + /// + public virtual byte GetByte(int i) => byte.MinValue; + + /// + /// + /// + /// + /// + /// + /// + /// + /// + public virtual long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferOffset, int length) => 0; + + /// + /// + /// + /// + /// + public virtual char GetChar(int i) => char.MinValue; + + /// + /// + /// + /// + /// + /// + /// + /// + /// + public virtual long GetChars(int i, long fieldOffset, char[] buffer, int bufferOffset, int length) => 0; + + /// + /// + /// + /// + /// + public virtual IDataReader GetData(int i) => null; + + /// + /// + /// + /// + /// + public virtual string GetDataTypeName(int i) => string.Empty; + + /// + /// + /// + /// + /// + public virtual DateTime GetDateTime(int i) => DateTime.MinValue; + + /// + /// + /// + /// + /// + public virtual decimal GetDecimal(int i) => 0; + + /// + /// + /// + /// + /// + public virtual double GetDouble(int i) => 0; + + /// + /// + /// + /// + /// + public virtual Type GetFieldType(int i) => null; + + /// + /// + /// + /// + /// + public virtual float GetFloat(int i) => 0f; + + /// + /// + /// + /// + /// + public virtual Guid GetGuid(int i) => Guid.Empty; + + /// + /// + /// + /// + /// + public virtual short GetInt16(int i) => 0; + + /// + /// + /// + /// + /// + public virtual int GetInt32(int i) => 0; + + /// + /// + /// + /// + /// + public virtual long GetInt64(int i) => 0; + + /// + /// + /// + /// + /// + public virtual int GetOrdinal(string name) => 0; + + /// + /// + /// + /// + public virtual DataTable GetSchemaTable() => null; + + /// + /// + /// + /// + /// + public virtual string GetString(int i) => string.Empty; + + /// + /// + /// + /// + /// + public virtual int GetValues(object[] values) => 0; + + /// + /// + /// + /// + /// + public virtual bool IsDBNull(int i) => false; + + /// + /// + /// + /// + public virtual bool NextResult() => false; + + /// + /// + /// + /// + /// + public abstract string GetName(int i); + + /// + /// + /// + /// + /// + public abstract object GetValue(int i); + + /// + /// + /// + /// + public abstract bool Read(); + + /// + /// + /// + public virtual void Close() + { + + } + + /// + /// + /// + /// + protected virtual void Dispose(bool disposing) + { + + } + + /// + /// + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + } +}