243 lines
13 KiB
C#
243 lines
13 KiB
C#
using Microsoft.Data.SqlClient;
|
|
using Sarand.BusinessMgr.Net.Standard.Dbas.Data;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Common;
|
|
using System.Linq;
|
|
|
|
//---------------------------------------------------------------------------
|
|
// Code generated by Sarand Database Utilities Sat 1-Feb-2025 17:49:12
|
|
//---------------------------------------------------------------------------
|
|
namespace Sarand.BusinessMgr.Net.Standard.Dbas.Internal
|
|
{
|
|
internal class CommodityCustomer_Db : SbmDbDataItem
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Column definitions.
|
|
public class ColumnsData
|
|
{
|
|
public ColumnDataInt32 _CommodityId { get; } = new ColumnDataInt32("CommodityId");
|
|
public ColumnDataInt32 _AssocIdCustomer { get; } = new ColumnDataInt32("AssocIdCustomer");
|
|
public ColumnDataString _ProductCode { get; } = new ColumnDataString("ProductCode");
|
|
public ColumnDataDecimal _AmountRetail { get; } = new ColumnDataDecimal("AmountRetail");
|
|
public ColumnDataInt32 _CurrencyIdRetail { get; } = new ColumnDataInt32("CurrencyIdRetail");
|
|
public ColumnDataString _PromotionCode { get; } = new ColumnDataString("PromotionCode");
|
|
public ColumnDataString _Notes { get; } = new ColumnDataString("Notes");
|
|
public ColumnDataInt16 _Flags { get; } = new ColumnDataInt16("Flags");
|
|
public ColumnDataString _Status { get; } = new ColumnDataString("Status");
|
|
public ColumnDataDateTime _CreateOn { get; } = new ColumnDataDateTime("CreateOn");
|
|
public ColumnDataInt32Nullable _CreateBy { get; } = new ColumnDataInt32Nullable("CreateBy");
|
|
public ColumnDataString _CreateHost { get; } = new ColumnDataString("CreateHost");
|
|
public ColumnDataDateTimeNullable _UpdateOn { get; } = new ColumnDataDateTimeNullable("UpdateOn");
|
|
public ColumnDataInt32Nullable _UpdateBy { get; } = new ColumnDataInt32Nullable("UpdateBy");
|
|
public ColumnDataString _UpdateHost { get; } = new ColumnDataString("UpdateHost");
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
protected static List<String> fields { get; } = new List<String>()
|
|
{
|
|
"CommodityId",
|
|
"AssocIdCustomer",
|
|
"ProductCode",
|
|
"AmountRetail",
|
|
"CurrencyIdRetail",
|
|
"PromotionCode",
|
|
"Notes",
|
|
"Flags",
|
|
"Status",
|
|
"CreateOn",
|
|
"CreateBy",
|
|
"CreateHost",
|
|
"UpdateOn",
|
|
"UpdateBy",
|
|
"UpdateHost",
|
|
};
|
|
protected static List<String> fieldsId { get; } = new List<String>()
|
|
{
|
|
};
|
|
protected static List<String> fieldsPrimary { get; } = new List<String>()
|
|
{
|
|
"CommodityId",
|
|
"AssocIdCustomer",
|
|
};
|
|
protected static List<String> fieldsNullable { get; } = new List<String>()
|
|
{
|
|
"ProductCode",
|
|
"PromotionCode",
|
|
"Notes",
|
|
"CreateBy",
|
|
"CreateHost",
|
|
"UpdateOn",
|
|
"UpdateBy",
|
|
"UpdateHost",
|
|
};
|
|
protected static List<String> fieldsIgnoreOnUpdate { get; } = new List<String>()
|
|
{
|
|
"CreateOn",
|
|
"CreateBy",
|
|
"CreateHost",
|
|
};
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
internal CommodityCustomer_Db (IDatabaseInfo dbInfo) : base(dbInfo)
|
|
{
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public override String SchemaName { get; } = "dbo";
|
|
public override String TableName { get; } = "CommodityCustomer";
|
|
public String TableNameFmt { get { return (!String.IsNullOrEmpty(SchemaName) ? "[" + SchemaName + "]." : "") + "[" + TableName + "]"; } }
|
|
public static List<String> Fields { get { return new List<String>(fields); } }
|
|
public static List<String> FieldsId { get { return new List<String>(fieldsId); } }
|
|
public static List<String> FieldsNonId { get { return fields.FindAll(item => !fieldsId.ContainsCI(item)); } }
|
|
// Columns.
|
|
public ColumnsData Cols { get; } = new ColumnsData();
|
|
public Int32 _CommodityId { get { return Cols._CommodityId.Value; } set { Cols._CommodityId.Value = value; } }
|
|
public Int32 _AssocIdCustomer { get { return Cols._AssocIdCustomer.Value; } set { Cols._AssocIdCustomer.Value = value; } }
|
|
public String _ProductCode { get { return Cols._ProductCode.Value; } set { Cols._ProductCode.Value = value; } }
|
|
public Decimal _AmountRetail { get { return Cols._AmountRetail.Value; } set { Cols._AmountRetail.Value = value; } }
|
|
public Int32 _CurrencyIdRetail { get { return Cols._CurrencyIdRetail.Value; } set { Cols._CurrencyIdRetail.Value = value; } }
|
|
public String _PromotionCode { get { return Cols._PromotionCode.Value; } set { Cols._PromotionCode.Value = value; } }
|
|
public String _Notes { get { return Cols._Notes.Value; } set { Cols._Notes.Value = value; } }
|
|
public Int16 _Flags { get { return Cols._Flags.Value; } set { Cols._Flags.Value = value; } }
|
|
public String _Status { get { return Cols._Status.Value; } set { Cols._Status.Value = value; } }
|
|
public DateTime _CreateOn { get { return Cols._CreateOn.Value; } set { Cols._CreateOn.Value = value; } }
|
|
public Int32? _CreateBy { get { return Cols._CreateBy.Value; } set { Cols._CreateBy.Value = value; } }
|
|
public String _CreateHost { get { return Cols._CreateHost.Value; } set { Cols._CreateHost.Value = value; } }
|
|
public DateTime? _UpdateOn { get { return Cols._UpdateOn.Value; } set { Cols._UpdateOn.Value = value; } }
|
|
public Int32? _UpdateBy { get { return Cols._UpdateBy.Value; } set { Cols._UpdateBy.Value = value; } }
|
|
public String _UpdateHost { get { return Cols._UpdateHost.Value; } set { Cols._UpdateHost.Value = value; } }
|
|
// Existing columns - confirmed on database if table exists.
|
|
public List<String> ExistingColumnNames { get { return GetExistingColumnNames(Fields); } }
|
|
public List<String> ExistingColumnNamesNonId { get { return GetExistingColumnNames(FieldsNonId); } }
|
|
public List<String> UpdatableColumns { get { return ExistingColumnNamesNonId.Where(item => !fieldsPrimary.ContainsCI(item) && !fieldsIgnoreOnUpdate.ContainsCI(item)).ToList(); } }
|
|
|
|
//-------------------------------------------------------------------
|
|
// Population.
|
|
public override void PopulateFrom (DbCommand dbCommand, String tableAlias, String filter)
|
|
{
|
|
dbCommand.CommandText =
|
|
"SELECT " + ExistingColumnNames.Concat(", ", (!String.IsNullOrEmpty(tableAlias) ? tableAlias + "." : "") + "[", "]") + " " +
|
|
"FROM " + TableNameFmt + (!String.IsNullOrEmpty(tableAlias) ? " " + tableAlias : "") + " " +
|
|
(!String.IsNullOrEmpty(filter) ? "WHERE " + filter : "");
|
|
Log(GetType().Name + ".PopulateFrom", dbCommand.CommandText);
|
|
using (DbDataReader dbDataReader = dbCommand.ExecuteReader())
|
|
{
|
|
dbDataReader.Read();
|
|
PopulateFrom(dbDataReader);
|
|
}
|
|
}
|
|
public override void PopulateFrom (DbDataReader dbDataReader)
|
|
{
|
|
_CommodityId = GetFieldInt32(dbDataReader, "CommodityId", _CommodityId);
|
|
_AssocIdCustomer = GetFieldInt32(dbDataReader, "AssocIdCustomer", _AssocIdCustomer);
|
|
_ProductCode = GetFieldString(dbDataReader, "ProductCode", _ProductCode);
|
|
_AmountRetail = GetFieldDecimal(dbDataReader, "AmountRetail", _AmountRetail);
|
|
_CurrencyIdRetail = GetFieldInt32(dbDataReader, "CurrencyIdRetail", _CurrencyIdRetail);
|
|
_PromotionCode = GetFieldString(dbDataReader, "PromotionCode", _PromotionCode);
|
|
_Notes = GetFieldString(dbDataReader, "Notes", _Notes);
|
|
_Flags = GetFieldInt16(dbDataReader, "Flags", _Flags);
|
|
_Status = GetFieldString(dbDataReader, "Status", _Status);
|
|
_CreateOn = GetFieldDateTime(dbDataReader, "CreateOn", _CreateOn);
|
|
_CreateBy = GetFieldInt32Nullable(dbDataReader, "CreateBy", _CreateBy);
|
|
_CreateHost = GetFieldString(dbDataReader, "CreateHost", _CreateHost);
|
|
_UpdateOn = GetFieldDateTimeNullable(dbDataReader, "UpdateOn", _UpdateOn);
|
|
_UpdateBy = GetFieldInt32Nullable(dbDataReader, "UpdateBy", _UpdateBy);
|
|
_UpdateHost = GetFieldString(dbDataReader, "UpdateHost", _UpdateHost);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Insert command.
|
|
public override bool ExecuteInsertCommand (DbCommand dbCommand)
|
|
{
|
|
List<String> columnNames = ExistingColumnNamesNonId;
|
|
// Construct query.
|
|
String query =
|
|
"INSERT INTO " + TableNameFmt + " " +
|
|
"("+columnNames.Concat(",","[","]")+") ";
|
|
if (dbCommand is SqlCommand)
|
|
query += "VALUES (" + columnNames.Concat(",", "@", "") + ") ";
|
|
else query += "VALUES (" + StringListUtil.CreateList("?", columnNames.Count).Concat(",") + ") ";
|
|
dbCommand.CommandText = query;
|
|
Log(GetType().Name + ".ExecuteInsertCommand", dbCommand.CommandText);
|
|
// Populate parameters.
|
|
dbCommand.Parameters.Clear();
|
|
if (columnNames.ContainsCI("CommodityId")) AddCommandParameter(dbCommand, "@CommodityId", _CommodityId);
|
|
if (columnNames.ContainsCI("AssocIdCustomer")) AddCommandParameter(dbCommand, "@AssocIdCustomer", _AssocIdCustomer);
|
|
if (columnNames.ContainsCI("ProductCode")) AddCommandParameter(dbCommand, "@ProductCode", _ProductCode);
|
|
if (columnNames.ContainsCI("AmountRetail")) AddCommandParameter(dbCommand, "@AmountRetail", _AmountRetail);
|
|
if (columnNames.ContainsCI("CurrencyIdRetail")) AddCommandParameter(dbCommand, "@CurrencyIdRetail", _CurrencyIdRetail);
|
|
if (columnNames.ContainsCI("PromotionCode")) AddCommandParameter(dbCommand, "@PromotionCode", _PromotionCode);
|
|
if (columnNames.ContainsCI("Notes")) AddCommandParameter(dbCommand, "@Notes", _Notes);
|
|
if (columnNames.ContainsCI("Flags")) AddCommandParameter(dbCommand, "@Flags", _Flags);
|
|
if (columnNames.ContainsCI("Status")) AddCommandParameter(dbCommand, "@Status", _Status);
|
|
if (columnNames.ContainsCI("CreateOn")) AddCommandParameter(dbCommand, "@CreateOn", _CreateOn);
|
|
if (columnNames.ContainsCI("CreateBy")) AddCommandParameter(dbCommand, "@CreateBy", _CreateBy);
|
|
if (columnNames.ContainsCI("CreateHost")) AddCommandParameter(dbCommand, "@CreateHost", _CreateHost);
|
|
if (columnNames.ContainsCI("UpdateOn")) AddCommandParameter(dbCommand, "@UpdateOn", _UpdateOn);
|
|
if (columnNames.ContainsCI("UpdateBy")) AddCommandParameter(dbCommand, "@UpdateBy", _UpdateBy);
|
|
if (columnNames.ContainsCI("UpdateHost")) AddCommandParameter(dbCommand, "@UpdateHost", _UpdateHost);
|
|
// Execute insert command.
|
|
bool commandSuccess = false;
|
|
int executeNonQuery = dbCommand.ExecuteNonQuery();
|
|
if (executeNonQuery == 1)
|
|
{
|
|
commandSuccess = true;
|
|
}
|
|
return commandSuccess;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Update command.
|
|
public override bool ExecuteUpdateCommand (DbCommand dbCommand, List<String> columnsToUpdate = null)
|
|
{
|
|
// Columns to update.
|
|
List<String> columnNames = (columnsToUpdate?.Any() ?? false ? columnsToUpdate : UpdatableColumns).Where(col => UpdatableColumns.ContainsCI(col)).ToList();
|
|
if (!columnNames.Any())
|
|
{
|
|
SaLogger.Warn($"{SysUtil.Fn} No columns to update. [{GetType().Name}]");
|
|
return false;
|
|
}
|
|
// Construct query.
|
|
String query = "UPDATE " + TableNameFmt + " SET ";
|
|
if (dbCommand is SqlCommand)
|
|
{
|
|
query += StringPairList.CreateFrom(columnNames.Bracket("[","]"),columnNames).Concat("=@").Concat(", ") + " ";
|
|
query += "WHERE CommodityId=@CommodityId AND AssocIdCustomer=@AssocIdCustomer;";
|
|
}
|
|
else
|
|
{
|
|
query += columnNames.Concat(", ", "", "=?") + " ";
|
|
query += "WHERE CommodityId=? AND AssocIdCustomer=?;";
|
|
}
|
|
dbCommand.CommandText = query;
|
|
Log(GetType().Name + ".ExecuteUpdateCommand", dbCommand.CommandText);
|
|
// Populate parameters.
|
|
dbCommand.Parameters.Clear();
|
|
if (columnNames.ContainsCI("ProductCode")) AddCommandParameter(dbCommand, "@ProductCode", _ProductCode);
|
|
if (columnNames.ContainsCI("AmountRetail")) AddCommandParameter(dbCommand, "@AmountRetail", _AmountRetail);
|
|
if (columnNames.ContainsCI("CurrencyIdRetail")) AddCommandParameter(dbCommand, "@CurrencyIdRetail", _CurrencyIdRetail);
|
|
if (columnNames.ContainsCI("PromotionCode")) AddCommandParameter(dbCommand, "@PromotionCode", _PromotionCode);
|
|
if (columnNames.ContainsCI("Notes")) AddCommandParameter(dbCommand, "@Notes", _Notes);
|
|
if (columnNames.ContainsCI("Flags")) AddCommandParameter(dbCommand, "@Flags", _Flags);
|
|
if (columnNames.ContainsCI("Status")) AddCommandParameter(dbCommand, "@Status", _Status);
|
|
if (columnNames.ContainsCI("UpdateOn")) AddCommandParameter(dbCommand, "@UpdateOn", _UpdateOn);
|
|
if (columnNames.ContainsCI("UpdateBy")) AddCommandParameter(dbCommand, "@UpdateBy", _UpdateBy);
|
|
if (columnNames.ContainsCI("UpdateHost")) AddCommandParameter(dbCommand, "@UpdateHost", _UpdateHost);
|
|
AddCommandParameter(dbCommand, "@CommodityId", _CommodityId);
|
|
AddCommandParameter(dbCommand, "@AssocIdCustomer", _AssocIdCustomer);
|
|
int executeNonQueryResult = dbCommand.ExecuteNonQuery();
|
|
return (executeNonQueryResult == 1);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
}
|