VEU.WathBody/3-app/1-lib/Kit.Helpers/SqlHelperAbstract/SqlHelperFluent.cs

278 lines
8.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Kit.Helpers.SqlHelperAbstract
{
internal class OutputValue
{
public object Value;
}
public class SqlHelperFluent
{
protected readonly IConnectionString _connectionString;
protected readonly string _sqlText;
protected bool _isStoredProcedure;
protected bool _isOutputParameter;
protected bool _withStrictSyntax;
protected List<CustomType> _customTypes;
protected List<KeyValuePair<string, object>> _parameters;
public SqlHelperFluent(IConnectionString connectionString, string sqlText)
{
_connectionString = connectionString;
_sqlText = sqlText;
_isStoredProcedure = true;
_isOutputParameter = false;
_withStrictSyntax = false;
_customTypes = new List<CustomType>();
_parameters = new List<KeyValuePair<string, object>>();
}
#region fluent methods
public SqlHelperFluent AsStoredProcedure()
{
_isStoredProcedure = true;
return this;
}
public SqlHelperFluent AsSqlText()
{
_isStoredProcedure = false;
return this;
}
public SqlHelperFluent WithStrictSyntax()
{
_withStrictSyntax = true;
return this;
}
public SqlHelperFluent AddCustomType(string name, Type type)
{
if (type != null)
{
_customTypes.Add(new CustomType(name, type));
}
return this;
}
public SqlHelperFluent AddCustomType<TType>(string name)
{
_customTypes.Add(new CustomType(name, typeof(TType)));
return this;
}
public SqlHelperFluent AddFilter(string name, object value)
{
if (value != null)
{
_parameters.Add(new KeyValuePair<string, object>(name, value));
}
return this;
}
public SqlHelperFluent AddFilter(string name, string value)
{
if (!value.IsNullOrEmpty())
{
_parameters.Add(new KeyValuePair<string, object>(name, value));
}
return this;
}
public SqlHelperFluent AddParameter(string name, object value)
{
_parameters.Add(new KeyValuePair<string, object>(name, value));
return this;
}
public SqlHelperFluent AddParameter(string name, string value)
{
_parameters.Add(new KeyValuePair<string, object>(name, value ?? string.Empty));
return this;
}
public SqlHelperFluent AddParameterNullable(string name, object value)
{
_parameters.Add(new KeyValuePair<string, object>(name, value ?? DBNull.Value));
return this;
}
public SqlHelperFluent AddParameterIfNotNull(string name, object value)
{
if (value != null)
{
_parameters.Add(new KeyValuePair<string, object>(name, value));
}
return this;
}
public SqlHelperFluent AddOutputParameter(string name, object value)
{
_parameters.Add(new KeyValuePair<string, object>(name, new OutputValue { Value = value ?? DBNull.Value }));
return this;
}
public SqlHelperFluent AddParameters(IEnumerable<KeyValuePair<string, object>> parameters)
{
_parameters.AddRange(parameters);
return this;
}
public SqlHelperFluent AddFilters(IEnumerable<KeyValuePair<string, object>> parameters)
{
_parameters.AddRange(parameters.Where(x => x.Value != null));
return this;
}
#endregion
#region finish methods
public RequestParams BuildParams() => new RequestParams(_connectionString, _sqlText,
_isStoredProcedure, _withStrictSyntax, _parameters, _customTypes);
public void ExecuteNonQuery()
{
_connectionString.GetSqlHelper().ExecuteNonQuery(BuildParams());
}
public TResult ExecuteScalar<TResult>()
{
return _connectionString.GetSqlHelper().ExecuteScalar<TResult>(BuildParams());
}
#endregion
}
public class SqlHelperFluent<TEntity> : SqlHelperFluent
{
protected List<Action<IDataRecordSpecified, IList<TEntity>>> _converters;
public SqlHelperFluent(IConnectionString connectionString, string sqlText) : base(connectionString, sqlText)
{
_converters = new List<Action<IDataRecordSpecified, IList<TEntity>>>();
}
#region base fluent methods
public new SqlHelperFluent<TEntity> AddCustomType(string name, Type type)
{
base.AddCustomType(name, type);
return this;
}
public new SqlHelperFluent<TEntity> AddCustomType<TType>(string name)
{
base.AddCustomType<TType>(name);
return this;
}
public new SqlHelperFluent<TEntity> AddFilter(string name, object value)
{
base.AddFilter(name, value);
return this;
}
public new SqlHelperFluent<TEntity> AddFilter(string name, string value)
{
base.AddFilter(name, value);
return this;
}
public new SqlHelperFluent<TEntity> AddParameter(string name, object value)
{
base.AddParameter(name, value);
return this;
}
public new SqlHelperFluent<TEntity> AddParameter(string name, string value)
{
base.AddParameter(name, value);
return this;
}
public new SqlHelperFluent<TEntity> AddParameterNullable(string name, object value)
{
base.AddParameterNullable(name, value);
return this;
}
public new SqlHelperFluent<TEntity> AddParameterIfNotNull(string name, object value)
{
base.AddParameterIfNotNull(name, value);
return this;
}
public new SqlHelperFluent<TEntity> AddOutputParameter(string name, object value)
{
base.AddParameter(name, value);
return this;
}
public new SqlHelperFluent<TEntity> AddParameters(IEnumerable<KeyValuePair<string, object>> parameters)
{
base.AddParameters(parameters);
return this;
}
public new SqlHelperFluent<TEntity> AddFilters(IEnumerable<KeyValuePair<string, object>> parameters)
{
base.AddFilters(parameters);
return this;
}
public new SqlHelperFluent<TEntity> AsStoredProcedure()
{
base.AsStoredProcedure();
return this;
}
public new SqlHelperFluent<TEntity> AsSqlText()
{
base.AsSqlText();
return this;
}
public new SqlHelperFluent<TEntity> WithStrictSyntax()
{
base.WithStrictSyntax();
return this;
}
#endregion
private RequestParamsSelect<TEntity> BuildParams() => new RequestParamsSelect<TEntity>(_connectionString, _sqlText,
_isStoredProcedure, _withStrictSyntax, _parameters, _customTypes, _converters.ToArray());
public SqlHelperFluent<TEntity> AddConverter(Action<IDataRecordSpecified, IList<TEntity>> converter)
{
_converters.Add(converter);
return this;
}
public IEnumerable<TEntity> ExecuteSelectMany()
{
return _connectionString.GetSqlHelper().ExecuteSelectMany(BuildParams());
}
public IEnumerableWithPage<TEntity> ExecuteSelectManyWithPage()
{
return _connectionString.GetSqlHelper().ExecuteSelectManyWithPage(BuildParams());
}
public IEnumerableWithOffer<TEntity> ExecuteSelectManyWithOffer()
{
return _connectionString.GetSqlHelper().ExecuteSelectManyWithOffer(BuildParams());
}
public TEntity ExecuteScalar()
{
return _connectionString.GetSqlHelper().ExecuteScalar<TEntity>(BuildParams());
}
}
}