mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-19 06:27:15 +00:00
Various bug fixs and improvements to stock SKU reconciliation
This commit is contained in:
103
src/bnhtrade.Core/Data/Database/SqlWhereBuilder.cs
Normal file
103
src/bnhtrade.Core/Data/Database/SqlWhereBuilder.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bnhtrade.Core.Data.Database
|
||||
{
|
||||
public class SqlWhereBuilder
|
||||
{
|
||||
private int parameterCount;
|
||||
|
||||
public SqlWhereBuilder()
|
||||
{
|
||||
Innit();
|
||||
}
|
||||
|
||||
public string SqlWhereString { get; private set; }
|
||||
|
||||
public Dictionary<string, object> ParameterList { get; private set; }
|
||||
|
||||
public void Innit()
|
||||
{
|
||||
SqlWhereString = null;
|
||||
ParameterList = new Dictionary<string, object>();
|
||||
parameterCount = 0;
|
||||
}
|
||||
|
||||
public void AddParametersToSqlCommand(SqlCommand cmd)
|
||||
{
|
||||
foreach (var item in ParameterList)
|
||||
{
|
||||
cmd.Parameters.AddWithValue(item.Key, item.Value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used to create a string for an SQL where condition 'In' statement
|
||||
/// </summary>
|
||||
/// <param name="columnReference">Name of the column to used to for the condition statement</param>
|
||||
/// <param name="orValueList">List of values to test in condition statement</param>
|
||||
/// <param name="wherePrefix">Optional string to prefix to the returned result</param>
|
||||
public void In(string columnReference, List<string> orValueList, string wherePrefix = null)
|
||||
{
|
||||
Innit();
|
||||
|
||||
if (orValueList == null || !orValueList.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string sqlWhere = @"
|
||||
";
|
||||
|
||||
if (wherePrefix != null)
|
||||
{
|
||||
sqlWhere += wherePrefix;
|
||||
}
|
||||
|
||||
sqlWhere += " " + columnReference + " IN ( ";
|
||||
|
||||
var paramters = new Dictionary<string, object>();
|
||||
int listCount = orValueList.Count();
|
||||
for (int i = 0; i < listCount; i++, parameterCount++)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
sqlWhere += ", ";
|
||||
}
|
||||
|
||||
string param = "@parameter" + parameterCount;
|
||||
sqlWhere += param;
|
||||
paramters.Add(param, orValueList[i]);
|
||||
}
|
||||
sqlWhere += " )";
|
||||
|
||||
SqlWhereString = sqlWhere;
|
||||
ParameterList = paramters;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used to create a string for an SQL where condition 'In' statement
|
||||
/// </summary>
|
||||
/// <param name="columnReference">Name of the column to used to for the condition statement</param>
|
||||
/// <param name="orValueList">List of values to test in condition statement</param>
|
||||
/// <param name="wherePrefix">Optional string to prefix to the returned result</param>
|
||||
public void In(string columnReference, List<int> orValueList, string wherePrefix = null)
|
||||
{
|
||||
var stringList = new List<string>();
|
||||
|
||||
if (orValueList != null || !orValueList.Any())
|
||||
{
|
||||
foreach (int value in orValueList)
|
||||
{
|
||||
stringList.Add(value.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
In(columnReference, stringList, wherePrefix);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user