59 lines
1.7 KiB
C#
59 lines
1.7 KiB
C#
namespace LaaProduction.Data.SQL
|
|
{
|
|
using LaaProduction.Data.SQL.Interfaces;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
|
|
public class SQLCommand : ISQLCommand
|
|
{
|
|
private readonly SQLConnection sqlConnection;
|
|
private readonly IDictionary<string, SqlParameter> parameters;
|
|
|
|
public SQLCommand(SQLConnection sqlConnection, string commandText)
|
|
{
|
|
this.parameters = new Dictionary<string, SqlParameter>();
|
|
this.sqlConnection = sqlConnection;
|
|
this.CommandText = commandText;
|
|
}
|
|
|
|
internal string CommandText { get; }
|
|
|
|
internal SqlParameter[] Parameters
|
|
=> this.parameters.Values.ToArray();
|
|
|
|
public int ExecuteNonQuery()
|
|
=> this.sqlConnection.ExecuteNonQuery(this);
|
|
|
|
public IEnumerable<T> ExecuteReader<T>(Func<ISQLReader, T> expression)
|
|
{
|
|
foreach (var result in this.sqlConnection.ExecuteReader(this, expression))
|
|
{
|
|
yield return result;
|
|
}
|
|
}
|
|
|
|
public T ExecuteScalar<T>(Func<ISQLReader, T> expression)
|
|
=> this.sqlConnection.ExecuteScalar(this, expression);
|
|
|
|
public T FirstOrDefault<T>(Func<ISQLReader, T> expression)
|
|
=> this.sqlConnection.FirstOrDefault(this, expression);
|
|
|
|
public ISQLCommand SetParameter(string name, object value)
|
|
{
|
|
if (value is null)
|
|
{
|
|
value = DBNull.Value;
|
|
}
|
|
|
|
var parameter = new SqlParameter(name, value);
|
|
|
|
this.parameters[name] = parameter;
|
|
|
|
return this;
|
|
}
|
|
}
|
|
}
|