ValueMapping speichern in der Datenbank

This commit is contained in:
Daniel Schick 2023-05-09 14:15:20 +02:00
parent d37a92be6f
commit 8c38001e42
5 changed files with 134 additions and 24 deletions

View File

@ -0,0 +1,42 @@
/****** Object: Table [dbo].[ValueMapping] Script Date: 09.05.2023 13:21:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ValueMapping](
[Id] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[EntryType] [tinyint] NULL,
[MappingKey] [nvarchar](64) NULL,
[MappingValue] [nvarchar](128) NULL,
[Created] [datetime] NOT NULL,
[Changed] [datetime] NULL,
CONSTRAINT [PK_ValueMapping] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ValueMapping] ADD CONSTRAINT [DF_ValueMapping_Id] DEFAULT (newid()) FOR [Id]
GO
ALTER TABLE [dbo].[ValueMapping] ADD CONSTRAINT [DF_ValueMapping_Created] DEFAULT (getdate()) FOR [Created]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE trigger [dbo].[ValueMapping_Trigger_Change_Log]
ON [dbo].[ValueMapping]
FOR UPDATE
AS
SET NOCOUNT ON
IF NOT UPDATE([Changed])
UPDATE ValueMapping SET [Changed] = GETDATE() WHERE Id IN (SELECT Id FROM [inserted])
GO
ALTER TABLE [dbo].[ValueMapping] ENABLE TRIGGER [ValueMapping_Trigger_Change_Log]
GO

View File

@ -34,6 +34,19 @@ namespace bsmd.database
return await PerformNonQueryAsync(cmd); return await PerformNonQueryAsync(cmd);
} }
public static async Task<int> DeleteAsync(DatabaseEntity entity)
{
if (!entity.IsNew)
{
using (SqlCommand cmd = new SqlCommand())
{
entity.PrepareDelete(cmd);
return await PerformNonQueryAsync(cmd);
}
}
return 0;
}
#region convenience loading functions #region convenience loading functions
public static async Task<List<MessageCore>> LoadMaerskCoresByIntervalAsync(Dictionary<MessageCore.SearchFilterType, string> filterDict, bool loadXtraData = false) public static async Task<List<MessageCore>> LoadMaerskCoresByIntervalAsync(Dictionary<MessageCore.SearchFilterType, string> filterDict, bool loadXtraData = false)
@ -85,7 +98,7 @@ namespace bsmd.database
ValueMapping vm = new ValueMapping(); ValueMapping vm = new ValueMapping();
vm.PrepareLoadCommand(cmd, Message.LoadFilter.BY_TYPE, mappingType); vm.PrepareLoadCommand(cmd, Message.LoadFilter.BY_TYPE, mappingType);
SqlDataReader reader = await PerformCommandAsync(cmd); SqlDataReader reader = await PerformCommandAsync(cmd);
return await vm.LoadListAsync(reader); return (await vm.LoadListAsync(reader)).ConvertAll(x => (ValueMapping)x);
} }
#endregion #endregion

View File

@ -29,5 +29,19 @@ namespace bsmd.database
return result; return result;
} }
public override List<DatabaseEntity> LoadList(IDataReader reader)
{
List<DatabaseEntity> result = new List<DatabaseEntity>();
if (reader != null)
{
while (reader.Read())
{
result.Add(ReadRowFromReader(reader));
}
reader.Close();
}
return result;
}
} }
} }

View File

@ -247,9 +247,9 @@ namespace bsmd.database
public bool Equals(MaerskData other) public bool Equals(MaerskData other)
{ {
if (other == null) return false; if (other == null) return false;
if (this.ColG == null) return (other.ColG == null); if (this.ColG == null) return other.ColG == null;
if (this.ColH == null) return (other.ColH == null); if (this.ColH == null) return other.ColH == null;
if (this.ColF == null) return (other.ColF == null); if (this.ColF == null) return other.ColF == null;
return this.ColF.Equals(other.ColF) && this.ColG.Equals(other.ColG) && this.ColH.Equals(other.ColH); return this.ColF.Equals(other.ColF) && this.ColG.Equals(other.ColG) && this.ColH.Equals(other.ColH);
} }

View File

@ -31,7 +31,7 @@ namespace bsmd.database
#region Fields #region Fields
private static Dictionary<MappingType, Dictionary<string, ValueMapping>> _dicts = new Dictionary<MappingType, Dictionary<string, ValueMapping>>(); private static readonly Dictionary<MappingType, Dictionary<string, ValueMapping>> _dicts = new Dictionary<MappingType, Dictionary<string, ValueMapping>>();
#endregion #endregion
@ -48,6 +48,10 @@ namespace bsmd.database
public string Value { get; private set; } public string Value { get; private set; }
public DateTime? Created { get; private set; }
public DateTime? Changed { get; private set; }
#endregion #endregion
#region public funcs #region public funcs
@ -56,9 +60,22 @@ namespace bsmd.database
/// creates and saves a new entry and adds it to the internal dictionaries /// creates and saves a new entry and adds it to the internal dictionaries
/// </summary> /// </summary>
/// <returns>true if entry was actually created, false if already present</returns> /// <returns>true if entry was actually created, false if already present</returns>
public static bool Create(MappingType type, string key, string value) public static async Task<bool> Create(MappingType type, string key, string value)
{ {
if (!_dicts.ContainsKey(type))
_dicts[type] = new Dictionary<string, ValueMapping>();
if (!_dicts[type].ContainsKey(key))
{
ValueMapping vm = new ValueMapping()
{
EntryType = type,
Key = key,
Value = value
};
_dicts[type][key] = vm;
return await DBManagerAsync.SaveAsync(vm) == 1;
}
return false; return false;
} }
@ -66,20 +83,23 @@ namespace bsmd.database
/// deletes an entry and removes it from the database /// deletes an entry and removes it from the database
/// </summary> /// </summary>
/// <returns>true if successful, false if value was not found</returns> /// <returns>true if successful, false if value was not found</returns>
public static bool Delete(MappingType type, string key) public static async Task<bool> Delete(MappingType type, string key)
{ {
if (!_dicts.ContainsKey(type)) return false;
return false; if (!_dicts[type].ContainsKey(key)) return false;
ValueMapping vm = _dicts[type][key];
_dicts[type].Remove(key);
return await DBManagerAsync.DeleteAsync(vm) == 1;
} }
/// <summary> /// <summary>
/// updates an existing value and saves it /// updates an existing value and saves it
/// </summary> /// </summary>
/// <returns>true if successful</returns> /// <returns>true if successful</returns>
public bool Update(string newValue) public async Task<bool> Update(string newValue)
{ {
this.Value = newValue;
return false; return await DBManagerAsync.SaveAsync(this) == 1;
} }
#endregion #endregion
@ -88,17 +108,37 @@ namespace bsmd.database
public override void PrepareSave(System.Data.IDbCommand cmd) public override void PrepareSave(System.Data.IDbCommand cmd)
{ {
throw new NotImplementedException(); SqlCommand scmd = cmd as SqlCommand;
scmd.Parameters.AddWithValue("@TYPE", this.EntryType);
scmd.Parameters.AddWithNullableValue("@KEY", this.Key);
scmd.Parameters.AddWithNullableValue("@VALUE", this.Value);
if(this.IsNew)
{
this.CreateId();
scmd.Parameters.AddWithValue("@ID", this.Id);
scmd.CommandText = $"INSERT INTO {Tablename} (Id, EntryType, MappingKey, MappingValue) VALUES (@ID, @TYPE, @KEY, @VALUE)";
}
else
{
scmd.Parameters.AddWithValue("@ID", this.Id);
scmd.CommandText = $"UPDATE {Tablename} SET EntryType = @TYPE, MappingKey = @KEY, MappingValue = @VALUE WHERE Id = @ID";
}
} }
public override void PrepareLoadCommand(System.Data.IDbCommand cmd, Message.LoadFilter filter, params object[] criteria) public override void PrepareLoadCommand(System.Data.IDbCommand cmd, Message.LoadFilter filter, params object[] criteria)
{ {
throw new NotImplementedException(); string query = $"SELECT Id, EntryType, MappingKey, MappingValue, Created, Changed FROM {Tablename}";
} switch (filter)
public override List<DatabaseEntity> LoadList(System.Data.IDataReader reader)
{ {
throw new NotImplementedException(); case Message.LoadFilter.BY_TYPE:
query += " WHERE EntryType = @TYPE";
((SqlCommand)cmd).Parameters.AddWithValue("@TYPE", criteria[0]);
break;
default:
break;
}
} }
protected override DatabaseEntityAsync ReadRowFromReader(System.Data.IDataReader reader) protected override DatabaseEntityAsync ReadRowFromReader(System.Data.IDataReader reader)
@ -110,10 +150,11 @@ namespace bsmd.database
vm.id = reader.GetGuid(0); vm.id = reader.GetGuid(0);
//if (!reader.IsDBNull(1)) md.MessageCoreId = reader.GetGuid(1); if (!reader.IsDBNull(1)) vm.EntryType = (ValueMapping.MappingType)reader.GetByte(1);
//if (!reader.IsDBNull(2)) md.ColA = reader.GetString(2); if (!reader.IsDBNull(2)) vm.Key = reader.GetString(2);
if (!reader.IsDBNull(3)) vm.Value = reader.GetString(3);
if (!reader.IsDBNull(4)) vm.Created = reader.GetDateTime(4);
if (!reader.IsDBNull(5)) vm.Changed = reader.GetDateTime(5);
} }
return vm; return vm;