152 lines
3.6 KiB
C#
152 lines
3.6 KiB
C#
namespace LaaProductionWeb.Data
|
|
{
|
|
using LaaProductionWeb.Data.Interfaces;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
public class SqlReader : ISqlReader, IDisposable
|
|
{
|
|
private readonly SqlDataReader sqlDataReader;
|
|
private readonly IDictionary<string, int> columns;
|
|
|
|
private int column;
|
|
|
|
private SqlReader(SqlDataReader sqlDataReader)
|
|
{
|
|
this.sqlDataReader = sqlDataReader;
|
|
this.columns = new Dictionary<string, int>();
|
|
|
|
var columnsCount = this.sqlDataReader.FieldCount;
|
|
var columns = new string[columnsCount];
|
|
|
|
for (int i = 0; i < columnsCount; i++)
|
|
{
|
|
var columnName = this.sqlDataReader.GetName(i);
|
|
this.columns[columnName] = i;
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
this.sqlDataReader.Close();
|
|
this.sqlDataReader.Dispose();
|
|
}
|
|
|
|
public byte[] GetBytes(int index = -1)
|
|
{
|
|
var bytes = default(byte[]);
|
|
|
|
if (index < 0)
|
|
{
|
|
index = this.column++;
|
|
}
|
|
|
|
if (!this.sqlDataReader.IsDBNull(index))
|
|
{
|
|
bytes = this.sqlDataReader.GetFieldValue<byte[]>(0);
|
|
}
|
|
|
|
return bytes ?? Array.Empty<byte>();
|
|
}
|
|
|
|
internal string[] GetColumns()
|
|
=> this.columns.Keys.ToArray();
|
|
|
|
public string GetString(int index = -1)
|
|
{
|
|
if (index < 0)
|
|
{
|
|
index = this.column++;
|
|
}
|
|
|
|
if (!this.sqlDataReader.IsDBNull(index))
|
|
{
|
|
return $"{this.sqlDataReader.GetValue(index)}";
|
|
}
|
|
|
|
return default(string);
|
|
}
|
|
|
|
public T GetValue<T>(int index = -1)
|
|
{
|
|
if (index < 0)
|
|
{
|
|
index = this.column++;
|
|
}
|
|
|
|
if (!this.sqlDataReader.IsDBNull(index))
|
|
{
|
|
if (this.sqlDataReader.GetValue(index) is T value)
|
|
{
|
|
return value;
|
|
}
|
|
}
|
|
|
|
return default(T);
|
|
}
|
|
|
|
internal bool Read()
|
|
{
|
|
this.column = 0;
|
|
|
|
return this.sqlDataReader.Read();
|
|
}
|
|
|
|
internal Task<bool> ReadAsync()
|
|
{
|
|
this.column = 0;
|
|
|
|
return this.sqlDataReader.ReadAsync();
|
|
}
|
|
|
|
public object GetValue(string field)
|
|
{
|
|
if (this.columns.TryGetValue($"{field}", out int index))
|
|
{
|
|
if (!this.sqlDataReader.IsDBNull(index))
|
|
{
|
|
return this.sqlDataReader.GetValue(index);
|
|
}
|
|
}
|
|
|
|
return default(object);
|
|
}
|
|
|
|
public int GetInt(int index = -1)
|
|
{
|
|
if (index < 0)
|
|
{
|
|
index = this.column++;
|
|
}
|
|
|
|
if (!this.sqlDataReader.IsDBNull(index))
|
|
{
|
|
return this.sqlDataReader.GetInt32(index);
|
|
}
|
|
|
|
return default(int);
|
|
}
|
|
|
|
public short GetSmallint(int index = -1)
|
|
{
|
|
if (index < 0)
|
|
{
|
|
index = this.column++;
|
|
}
|
|
|
|
if (!this.sqlDataReader.IsDBNull(index))
|
|
{
|
|
return this.sqlDataReader.GetInt16(index);
|
|
}
|
|
|
|
return default(short);
|
|
}
|
|
|
|
public static implicit operator SqlReader(SqlDataReader sqlDataReader)
|
|
=> new SqlReader(sqlDataReader);
|
|
}
|
|
} |