153 lines
3.5 KiB
C#
153 lines
3.5 KiB
C#
namespace LaaProduction.SQL
|
|
{
|
|
using LaaProduction.SQL.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()
|
|
{
|
|
this.index++;
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
return this.sqlDataReader.GetBoolean(this.index);
|
|
}
|
|
|
|
return default(bool);
|
|
}
|
|
|
|
public byte[] GetBytes()
|
|
{
|
|
this.index++;
|
|
|
|
var bytes = default(byte[]);
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
bytes = this.sqlDataReader.GetFieldValue<byte[]>(this.index);
|
|
}
|
|
|
|
return bytes ?? Array.Empty<byte>();
|
|
}
|
|
|
|
public DateTime GetDate()
|
|
{
|
|
this.index++;
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
return this.sqlDataReader.GetDateTime(this.index);
|
|
}
|
|
|
|
return default(DateTime);
|
|
}
|
|
|
|
public int GetInt()
|
|
{
|
|
this.index++;
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
return this.sqlDataReader.GetInt32(this.index);
|
|
}
|
|
|
|
return default(int);
|
|
}
|
|
|
|
public long GetLong()
|
|
{
|
|
this.index++;
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
return this.sqlDataReader.GetInt64(this.index);
|
|
}
|
|
|
|
return default(long);
|
|
}
|
|
|
|
public short GetSmallint()
|
|
{
|
|
this.index++;
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
return this.sqlDataReader.GetInt16(this.index);
|
|
}
|
|
|
|
return default(short);
|
|
}
|
|
|
|
public string GetString()
|
|
{
|
|
this.index++;
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
return $"{this.sqlDataReader.GetValue(this.index)}";
|
|
}
|
|
|
|
return default(string);
|
|
}
|
|
|
|
public T GetValue<T>()
|
|
{
|
|
this.index++;
|
|
|
|
if (!this.sqlDataReader.IsDBNull(this.index))
|
|
{
|
|
var value = this.sqlDataReader.GetValue(this.index);
|
|
|
|
if (value is T _value)
|
|
{
|
|
return _value;
|
|
}
|
|
}
|
|
|
|
return default(T);
|
|
}
|
|
|
|
public object GetValue(string column)
|
|
{
|
|
column.EnsureNotNullOrWhiteSpace(nameof(column));
|
|
|
|
var colIndex = this.sqlDataReader.GetOrdinal(column);
|
|
|
|
if (0 <= colIndex && colIndex < this.sqlDataReader.FieldCount)
|
|
{
|
|
if (!this.sqlDataReader.IsDBNull(colIndex))
|
|
{
|
|
return this.sqlDataReader.GetValue(colIndex);
|
|
}
|
|
}
|
|
|
|
return default(object);
|
|
}
|
|
|
|
internal bool Read()
|
|
{
|
|
this.index = -1;
|
|
|
|
return this.sqlDataReader.Read();
|
|
}
|
|
|
|
public static implicit operator SQLReader(SqlDataReader sqlDataReader)
|
|
=> new SQLReader(sqlDataReader);
|
|
}
|
|
} |