using System; using System.Collections.Generic; using System.Linq; using Kreta.Framework.Collections.Generic; namespace Kreta.Framework.Entities.Generic { public abstract class EntityDBHelper : IEntityDBHelper //, IEntityDBHelper where EntityType : Kreta.Framework.Entities.Entity { protected abstract EntityType CreateEntityInstance(); #region IEntityDBHelper Members public abstract string EmptyQueryCommandText { get; } public abstract string DynamicQueryCommandText { get; } public abstract IDictionary DynamicColumns { get; } public SDA.DataProvider.SDACommand CreateEmptyQueryCommand() { return DAUtil.CreateCommand(EmptyQueryCommandText); } public SDA.DataProvider.SDACommand CreateDynamicQueryCommand(ColumnFilterMode columnFilterMode, IEnumerable columns) { if (columns == null) { columns = new List(); } 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 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 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 collection, SDA.DataProvider.SDACommand command) { using (SDA.DataProvider.SDADataReader reader = command.ExecuteReader()) { while (reader.Read()) { EntityType entity = CreateEntityInstance(); LoadFromReader(entity, reader); ((EntityCollection)collection).InternalAdd(entity); } return true; } } public bool LoadEntityCollection(IEntityCollection collection, SDA.DataProvider.SDACommand command, ColumnFilterMode columnFilterMode, IEnumerable columns) { using (SDA.DataProvider.SDADataReader reader = command.ExecuteReader()) { while (reader.Read()) { EntityType entity = CreateEntityInstance(); LoadFromReader(entity, reader, columnFilterMode, columns); ((EntityCollection)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 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 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 commandParameters) { if (commandParameters != null) { foreach (KeyValuePair 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 } }