This commit is contained in:
skidoodle 2024-03-13 00:33:46 +01:00
commit e124a47765
19374 changed files with 9806149 additions and 0 deletions

View file

@ -0,0 +1,215 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Kreta.Framework.Collections.Generic;
namespace Kreta.Framework.Entities.Generic
{
public abstract class EntityDBHelper<EntityType> : IEntityDBHelper<EntityType> //, IEntityDBHelper
where EntityType : Kreta.Framework.Entities.Entity
{
protected abstract EntityType CreateEntityInstance();
#region IEntityDBHelper<EntityType> Members
public abstract string EmptyQueryCommandText
{
get;
}
public abstract string DynamicQueryCommandText
{
get;
}
public abstract IDictionary<string, string> DynamicColumns
{
get;
}
public SDA.DataProvider.SDACommand CreateEmptyQueryCommand()
{
return DAUtil.CreateCommand(EmptyQueryCommandText);
}
public SDA.DataProvider.SDACommand CreateDynamicQueryCommand(ColumnFilterMode columnFilterMode, IEnumerable<string> columns)
{
if (columns == null)
{
columns = new List<string>();
}
var columnText = "";
foreach (var column in DynamicColumns.Where(dc => (columnFilterMode == ColumnFilterMode.DEFAULT_ALLOWED) ^ (columns.Contains(dc.Key))))
{
columnText += column.Value + ",";
}
var queryCommandText = DynamicQueryCommandText.Replace("{COLUMNS}", columnText);
return DAUtil.CreateCommand(queryCommandText);
}
public abstract void LoadEntityFields(EntityType entity, SDA.DataProvider.SDADataReader reader, ColumnFilterMode columnFilterMode, IEnumerable<string> columns);
public abstract void LoadEntityFields(EntityType entity, SDA.DataProvider.SDADataReader reader);
public void LoadFromReader(EntityType entity, SDA.DataProvider.SDADataReader reader)
{
entity.ID = ((int)reader.GetDecimal(0));
LoadEntityFields(entity, reader);
int index = reader.FieldCount - 6;
entity.Torolt = reader[index] != DBNull.Value && reader.GetBoolean(index);
index++;
entity.Serial = ((int)reader.GetDecimal(index++));
entity.EntityCreated = DAUtil.ReadDateTime(reader, index++);
entity.EntityCreator = reader[index] != DBNull.Value ? reader.GetInt32(index) : 0;
index++;
entity.EntityLastChanged = DAUtil.ReadDateTime(reader, index++);
entity.EntityModifier = reader[index] != DBNull.Value ? reader.GetInt32(index) : new int?();
entity.SetLoaded();
}
public void LoadFromReader(EntityType entity, SDA.DataProvider.SDADataReader reader, ColumnFilterMode columnFilterMode, IEnumerable<string> columns)
{
entity.ID = ((int)reader.GetDecimal(0));
LoadEntityFields(entity, reader, columnFilterMode, columns);
int index = reader.FieldCount - 6;
entity.Torolt = reader[index] != DBNull.Value && reader.GetBoolean(index);
index++;
entity.Serial = ((int)reader.GetDecimal(index++));
entity.EntityCreated = DAUtil.ReadDateTime(reader, index++);
entity.EntityCreator = reader[index] != DBNull.Value ? reader.GetInt32(index) : 0;
index++;
entity.EntityLastChanged = DAUtil.ReadDateTime(reader, index++);
entity.EntityModifier = reader[index] != DBNull.Value ? reader.GetInt32(index++) : new int?();
entity.SetLoaded();
}
public bool LoadEntityCollection(IEntityCollection<EntityType> collection, SDA.DataProvider.SDACommand command)
{
using (SDA.DataProvider.SDADataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
EntityType entity = CreateEntityInstance();
LoadFromReader(entity, reader);
((EntityCollection<EntityType>)collection).InternalAdd(entity);
}
return true;
}
}
public bool LoadEntityCollection(IEntityCollection<EntityType> collection, SDA.DataProvider.SDACommand command, ColumnFilterMode columnFilterMode, IEnumerable<string> columns)
{
using (SDA.DataProvider.SDADataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
EntityType entity = CreateEntityInstance();
LoadFromReader(entity, reader, columnFilterMode, columns);
((EntityCollection<EntityType>)collection).InternalAdd(entity);
}
return true;
}
}
public bool LoadSingleEntity(EntityType entity, SDA.DataProvider.SDACommand command)
{
using (SDA.DataProvider.SDADataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
LoadFromReader(entity, reader);
if (reader.Read())
{
throw new Kreta.Framework.DataIntegrityException("Egyediség megsértése: " + entity.GetEntityName());
}
return true;
}
return false;
}
}
public bool LoadSingleEntity(EntityType entity, SDA.DataProvider.SDACommand command, ColumnFilterMode columnFilterMode, IEnumerable<string> columns)
{
using (SDA.DataProvider.SDADataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
LoadFromReader(entity, reader, columnFilterMode, columns);
if (reader.Read())
{
throw new Kreta.Framework.DataIntegrityException("Egyediség megsértése: " + entity.GetEntityName());
}
return true;
}
return false;
}
}
public bool LoadByPartnerId(EntityType entity, string filterText, int partnerId)
{
using (SDA.DataProvider.SDACommand command = CreateEmptyQueryCommand())
{
command.CommandText += filterText;
command.Parameters.Add("pID", SDA.DataProvider.SDADBType.Int).Value = partnerId;
return LoadSingleEntity(entity, command, ColumnFilterMode.DEFAULT_ALLOWED, null);
}
}
public bool LoadByPartnerId(IEntityCollection<EntityType> collection, string filterText, int partnerId)
{
using (SDA.DataProvider.SDACommand command = CreateEmptyQueryCommand())
{
command.CommandText += filterText;
command.Parameters.Add("pID", SDA.DataProvider.SDADBType.Int).Value = partnerId;
return LoadEntityCollection(collection, command, ColumnFilterMode.DEFAULT_ALLOWED, null);
}
}
public abstract void BindAttributes(EntityType entity, SDA.DataProvider.SDACommand command);
public abstract void BindAssociations(EntityType entity, SDA.DataProvider.SDACommand command);
public abstract void DynamicBindAttributes(EntityType entity, SDA.DataProvider.SDACommand command);
public void CreateParameterBinding(SDA.DataProvider.SDACommand command, Dictionary<string, object> commandParameters)
{
if (commandParameters != null)
{
foreach (KeyValuePair<string, object> kvp in commandParameters)
{
// mssql nem szereti parameter nevben a ":"-ot
string pName = kvp.Key;
if (pName.StartsWith(":"))
{
pName = pName.TrimStart(new char[] { ':' });
}
if (kvp.Value.GetType() == typeof(int))
{
command.Parameters.Add(pName, SDA.DataProvider.SDADBType.Int).Value = Convert.ToInt32(kvp.Value);
}
if (kvp.Value.GetType() == typeof(string))
{
command.Parameters.Add(pName, SDA.DataProvider.SDADBType.String).Value = Convert.ToString(kvp.Value);
}
if (kvp.Value.GetType() == typeof(DateTime))
{
command.Parameters.Add(pName, SDA.DataProvider.SDADBType.DateTime).Value = Convert.ToDateTime(kvp.Value, LanguageContext.Current.RegionSettings);
}
}
}
}
#endregion
}
}