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 _customTypes; protected List> _parameters; public SqlHelperFluent(IConnectionString connectionString, string sqlText) { _connectionString = connectionString; _sqlText = sqlText; _isStoredProcedure = true; _isOutputParameter = false; _withStrictSyntax = false; _customTypes = new List(); _parameters = new List>(); } #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(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(name, value)); } return this; } public SqlHelperFluent AddFilter(string name, string value) { if (!value.IsNullOrEmpty()) { _parameters.Add(new KeyValuePair(name, value)); } return this; } public SqlHelperFluent AddParameter(string name, object value) { _parameters.Add(new KeyValuePair(name, value)); return this; } public SqlHelperFluent AddParameter(string name, string value) { _parameters.Add(new KeyValuePair(name, value ?? string.Empty)); return this; } public SqlHelperFluent AddParameterNullable(string name, object value) { _parameters.Add(new KeyValuePair(name, value ?? DBNull.Value)); return this; } public SqlHelperFluent AddParameterIfNotNull(string name, object value) { if (value != null) { _parameters.Add(new KeyValuePair(name, value)); } return this; } public SqlHelperFluent AddOutputParameter(string name, object value) { _parameters.Add(new KeyValuePair(name, new OutputValue { Value = value ?? DBNull.Value })); return this; } public SqlHelperFluent AddParameters(IEnumerable> parameters) { _parameters.AddRange(parameters); return this; } public SqlHelperFluent AddFilters(IEnumerable> 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() { return _connectionString.GetSqlHelper().ExecuteScalar(BuildParams()); } #endregion } public class SqlHelperFluent : SqlHelperFluent { protected List>> _converters; public SqlHelperFluent(IConnectionString connectionString, string sqlText) : base(connectionString, sqlText) { _converters = new List>>(); } #region base fluent methods public new SqlHelperFluent AddCustomType(string name, Type type) { base.AddCustomType(name, type); return this; } public new SqlHelperFluent AddCustomType(string name) { base.AddCustomType(name); return this; } public new SqlHelperFluent AddFilter(string name, object value) { base.AddFilter(name, value); return this; } public new SqlHelperFluent AddFilter(string name, string value) { base.AddFilter(name, value); return this; } public new SqlHelperFluent AddParameter(string name, object value) { base.AddParameter(name, value); return this; } public new SqlHelperFluent AddParameter(string name, string value) { base.AddParameter(name, value); return this; } public new SqlHelperFluent AddParameterNullable(string name, object value) { base.AddParameterNullable(name, value); return this; } public new SqlHelperFluent AddParameterIfNotNull(string name, object value) { base.AddParameterIfNotNull(name, value); return this; } public new SqlHelperFluent AddOutputParameter(string name, object value) { base.AddParameter(name, value); return this; } public new SqlHelperFluent AddParameters(IEnumerable> parameters) { base.AddParameters(parameters); return this; } public new SqlHelperFluent AddFilters(IEnumerable> parameters) { base.AddFilters(parameters); return this; } public new SqlHelperFluent AsStoredProcedure() { base.AsStoredProcedure(); return this; } public new SqlHelperFluent AsSqlText() { base.AsSqlText(); return this; } public new SqlHelperFluent WithStrictSyntax() { base.WithStrictSyntax(); return this; } #endregion private RequestParamsSelect BuildParams() => new RequestParamsSelect(_connectionString, _sqlText, _isStoredProcedure, _withStrictSyntax, _parameters, _customTypes, _converters.ToArray()); public SqlHelperFluent AddConverter(Action> converter) { _converters.Add(converter); return this; } public IEnumerable ExecuteSelectMany() { return _connectionString.GetSqlHelper().ExecuteSelectMany(BuildParams()); } public IEnumerableWithPage ExecuteSelectManyWithPage() { return _connectionString.GetSqlHelper().ExecuteSelectManyWithPage(BuildParams()); } public IEnumerableWithOffer ExecuteSelectManyWithOffer() { return _connectionString.GetSqlHelper().ExecuteSelectManyWithOffer(BuildParams()); } public TEntity ExecuteScalar() { return _connectionString.GetSqlHelper().ExecuteScalar(BuildParams()); } } }