66 lines
2.0 KiB
C#
66 lines
2.0 KiB
C#
namespace LaaProductionWeb.Data
|
|
{
|
|
using LaaProductionWeb.Data.Interfaces;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
|
|
public class FluentSQLCommand : IFluentSQLCommand
|
|
{
|
|
private readonly FluentSQL fluentSql;
|
|
private readonly IDictionary<string, SqlParameter> parameters;
|
|
|
|
public FluentSQLCommand(FluentSQL fluentSql, string commandText)
|
|
{
|
|
this.fluentSql = fluentSql.EnsureNotNull(nameof(fluentSql));
|
|
this.CommandText = commandText.EnsureNotNullOrWhiteSpace(nameof(commandText));
|
|
this.parameters = new Dictionary<string, SqlParameter>();
|
|
}
|
|
|
|
internal string CommandText { get; }
|
|
|
|
internal SqlParameter[] Parameters
|
|
=> this.parameters.Values.ToArray();
|
|
|
|
public int ExecuteNonQuery()
|
|
=> this.fluentSql.ExecuteNonQuery(this);
|
|
|
|
public IEnumerable<T> ExecuteReader<T>(Func<IFluentSQLReader, T> expression)
|
|
=> this.fluentSql.ExecuteReader(this, expression);
|
|
|
|
public T ExecuteScalar<T>(Func<IFluentSQLReader, T> expression)
|
|
=> this.fluentSql.ExecuteScalar(this, expression);
|
|
|
|
public T FirstOrDefault<T>(Func<IFluentSQLReader, T> expression)
|
|
=> this.fluentSql.FirstOrDefault(this, expression);
|
|
|
|
public IFluentSQLCommand SetParameter(string name, object value)
|
|
{
|
|
name.ThrowIfNullOrWhiteSpace(nameof(name));
|
|
|
|
if (value is null)
|
|
{
|
|
value = DBNull.Value;
|
|
}
|
|
|
|
var parameter = new SqlParameter(name, value);
|
|
|
|
this.parameters[name] = parameter;
|
|
|
|
return this;
|
|
}
|
|
|
|
public IFluentSQLCommand SetParameters(IEnumerable<KeyValuePair<string, object>> parameters)
|
|
{
|
|
foreach (var kvp in parameters)
|
|
{
|
|
this.SetParameter(kvp.Key, kvp.Value);
|
|
}
|
|
|
|
return this;
|
|
}
|
|
}
|
|
}
|