mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-05-18 19:48:23 +00:00
Bug fix in ExportSalesInvoice and converted more code over to repository and service pattern
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
using bnhtrade.Core.Data.Database._BoilerPlate;
|
||||
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
|
||||
{
|
||||
internal class SqlUpdateBuilder
|
||||
{
|
||||
private Dictionary<string, int> _usedUpdateFields = new Dictionary<string, int>();
|
||||
private Dictionary<string, object> _parameterList = new Dictionary<string, object>();
|
||||
private string _tableName = string.Empty;
|
||||
private int _parameterCount = 0;
|
||||
private string _sqlUpdate = string.Empty;
|
||||
private string _sqlWhere = string.Empty;
|
||||
private bool _updateIsSet = false;
|
||||
private bool _whereIsSet = false;
|
||||
|
||||
public SqlUpdateBuilder() { }
|
||||
|
||||
public void Innit()
|
||||
{
|
||||
_usedUpdateFields.Clear();
|
||||
_parameterList.Clear();
|
||||
_tableName = string.Empty;
|
||||
_parameterCount = 0;
|
||||
_sqlUpdate = string.Empty;
|
||||
_sqlWhere = string.Empty;
|
||||
_updateIsSet = false;
|
||||
_whereIsSet = false;
|
||||
}
|
||||
|
||||
public void SetTableName(string tableName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(tableName))
|
||||
{
|
||||
throw new ArgumentException("Table name cannot be null or empty.", nameof(tableName));
|
||||
}
|
||||
_tableName = tableName;
|
||||
}
|
||||
|
||||
public void AddUpdateArugment(string columnName, object newValue)
|
||||
{
|
||||
if (string.IsNullOrEmpty(columnName) || newValue == null)
|
||||
{
|
||||
throw new ArgumentException("Column name and new value cannot be null or empty.");
|
||||
}
|
||||
if (_usedUpdateFields.ContainsKey(columnName))
|
||||
{
|
||||
throw new ArgumentException($"Column '{columnName}' already exists in the update arguments.", nameof(columnName));
|
||||
}
|
||||
|
||||
// add the update arguments to sql string and parameter list
|
||||
if (_updateIsSet)
|
||||
{
|
||||
_sqlUpdate += @",
|
||||
";
|
||||
}
|
||||
else
|
||||
{
|
||||
_updateIsSet = true;
|
||||
}
|
||||
|
||||
_parameterCount++;
|
||||
_sqlUpdate += columnName + " = @parameter" + _parameterCount;
|
||||
_parameterList.Add("@parameter" + _parameterCount, newValue);
|
||||
}
|
||||
|
||||
public void AddWhereArugment(string columnName, object value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(columnName) || value == null)
|
||||
{
|
||||
throw new ArgumentException("Column name and new value cannot be null or empty.");
|
||||
}
|
||||
|
||||
// add the where arguments to sql string and parameter list
|
||||
if (_whereIsSet)
|
||||
{
|
||||
_sqlWhere += @",
|
||||
";
|
||||
}
|
||||
else
|
||||
{
|
||||
_whereIsSet = true;
|
||||
}
|
||||
|
||||
_parameterCount++;
|
||||
_sqlWhere += columnName + " = @parameter" + _parameterCount;
|
||||
_parameterList.Add("@parameter" + _parameterCount, value);
|
||||
}
|
||||
|
||||
public string GetSqlString()
|
||||
{
|
||||
if (_updateIsSet == false || _whereIsSet == false || string.IsNullOrEmpty(_tableName))
|
||||
{
|
||||
throw new InvalidOperationException("Table name, update arguments, and where arguments must be set before generating SQL string.");
|
||||
}
|
||||
|
||||
string sql = @"
|
||||
UPDATE "+ _tableName + @"
|
||||
SET " + _sqlUpdate + @"
|
||||
WHERE " + _sqlWhere + ";";
|
||||
|
||||
return sql;
|
||||
}
|
||||
|
||||
public void AddParametersToSqlCommand(System.Data.SqlClient.SqlCommand cmd)
|
||||
{
|
||||
if (_parameterList != null)
|
||||
{
|
||||
foreach (var item in _parameterList)
|
||||
{
|
||||
cmd.Parameters.AddWithValue(item.Key, item.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddParametersToSqlCommand(Microsoft.Data.SqlClient.SqlCommand cmd)
|
||||
{
|
||||
if (_parameterList != null)
|
||||
{
|
||||
foreach (var item in _parameterList)
|
||||
{
|
||||
cmd.Parameters.AddWithValue(item.Key, item.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user