159 lines
3.8 KiB
C#
159 lines
3.8 KiB
C#
namespace Common.SqlExtensions
|
|
{
|
|
using Common.SqlExtensions.Interfaces;
|
|
|
|
using System;
|
|
using System.Data.SqlClient;
|
|
|
|
public class SQLReader : ISQLReader, IDisposable
|
|
{
|
|
private readonly SqlDataReader sqlDataReader;
|
|
|
|
private int index;
|
|
|
|
private SQLReader(SqlDataReader sqlDataReader)
|
|
=> this.sqlDataReader = sqlDataReader.EnsureNotNull(nameof(sqlDataReader));
|
|
|
|
public void Dispose()
|
|
{
|
|
this.sqlDataReader.Close();
|
|
this.sqlDataReader.Dispose();
|
|
}
|
|
|
|
public bool GetBool(int index = -1)
|
|
{
|
|
this.SetIndex(index);
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
return this.sqlDataReader.GetBoolean(this.index);
|
|
}
|
|
|
|
return default(bool);
|
|
}
|
|
|
|
public DateTime GetDate(int index = -1)
|
|
{
|
|
this.SetIndex(index);
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
return this.sqlDataReader.GetDateTime(this.index);
|
|
}
|
|
|
|
return default(DateTime);
|
|
}
|
|
|
|
public int GetInt(int index = -1)
|
|
{
|
|
this.SetIndex(index);
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
return this.sqlDataReader.GetInt32(this.index);
|
|
}
|
|
|
|
return default(int);
|
|
}
|
|
|
|
public long GetLong(int index = -1)
|
|
{
|
|
this.SetIndex(index);
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
return this.sqlDataReader.GetInt64(this.index);
|
|
}
|
|
|
|
return default(long);
|
|
}
|
|
|
|
public short GetSmallint(int index = -1)
|
|
{
|
|
this.SetIndex(index);
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
return this.sqlDataReader.GetInt16(this.index);
|
|
}
|
|
|
|
return default(short);
|
|
}
|
|
|
|
public string GetString(int index = -1)
|
|
{
|
|
this.SetIndex(index);
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
return $"{this.sqlDataReader.GetValue(this.index)}";
|
|
}
|
|
|
|
return default(string);
|
|
}
|
|
|
|
public byte GetTinyint(int index = -1)
|
|
{
|
|
this.SetIndex(index);
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
return this.sqlDataReader.GetByte(this.index);
|
|
}
|
|
|
|
return default(byte);
|
|
}
|
|
|
|
public T GetValue<T>(int index = -1)
|
|
{
|
|
this.SetIndex(index);
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
if (this.sqlDataReader.GetValue(this.index) is T value)
|
|
{
|
|
return value;
|
|
}
|
|
}
|
|
|
|
return default(T);
|
|
}
|
|
|
|
public object GetValue(string column)
|
|
{
|
|
column.EnsureNotNullOrWhiteSpace(nameof(column));
|
|
|
|
var colIndex = this.sqlDataReader.GetOrdinal(column);
|
|
|
|
if (colIndex >= 0 && colIndex < this.sqlDataReader.FieldCount - 1)
|
|
{
|
|
if (!this.sqlDataReader.IsDBNull(colIndex))
|
|
{
|
|
return this.sqlDataReader.GetValue(colIndex);
|
|
}
|
|
}
|
|
|
|
return default(object);
|
|
}
|
|
|
|
internal bool Read()
|
|
{
|
|
this.index = -1;
|
|
|
|
return this.sqlDataReader.Read();
|
|
}
|
|
|
|
private void SetIndex(int index)
|
|
{
|
|
this.index++;
|
|
|
|
if (index >= 0)
|
|
{
|
|
this.index = index;
|
|
}
|
|
}
|
|
|
|
public static implicit operator SQLReader(SqlDataReader sqlDataReader)
|
|
=> new SQLReader(sqlDataReader);
|
|
}
|
|
} |