ValueMapping speichern in der Datenbank
This commit is contained in:
parent
d37a92be6f
commit
8c38001e42
42
SQL/table.ValueMapping.eni.7.12.sql
Normal file
42
SQL/table.ValueMapping.eni.7.12.sql
Normal 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
|
||||
@ -34,6 +34,19 @@ namespace bsmd.database
|
||||
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
|
||||
|
||||
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();
|
||||
vm.PrepareLoadCommand(cmd, Message.LoadFilter.BY_TYPE, mappingType);
|
||||
SqlDataReader reader = await PerformCommandAsync(cmd);
|
||||
return await vm.LoadListAsync(reader);
|
||||
return (await vm.LoadListAsync(reader)).ConvertAll(x => (ValueMapping)x);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@ -29,5 +29,19 @@ namespace bsmd.database
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -247,9 +247,9 @@ namespace bsmd.database
|
||||
public bool Equals(MaerskData other)
|
||||
{
|
||||
if (other == null) return false;
|
||||
if (this.ColG == null) return (other.ColG == null);
|
||||
if (this.ColH == null) return (other.ColH == null);
|
||||
if (this.ColF == null) return (other.ColF == null);
|
||||
if (this.ColG == null) return other.ColG == null;
|
||||
if (this.ColH == null) return other.ColH == 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);
|
||||
}
|
||||
|
||||
|
||||
@ -31,7 +31,7 @@ namespace bsmd.database
|
||||
|
||||
#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
|
||||
|
||||
@ -48,6 +48,10 @@ namespace bsmd.database
|
||||
|
||||
public string Value { get; private set; }
|
||||
|
||||
public DateTime? Created { get; private set; }
|
||||
|
||||
public DateTime? Changed { get; private set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region public funcs
|
||||
@ -56,9 +60,22 @@ namespace bsmd.database
|
||||
/// creates and saves a new entry and adds it to the internal dictionaries
|
||||
/// </summary>
|
||||
/// <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;
|
||||
}
|
||||
|
||||
@ -66,20 +83,23 @@ namespace bsmd.database
|
||||
/// deletes an entry and removes it from the database
|
||||
/// </summary>
|
||||
/// <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)
|
||||
{
|
||||
|
||||
return false;
|
||||
if (!_dicts.ContainsKey(type)) 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>
|
||||
/// updates an existing value and saves it
|
||||
/// </summary>
|
||||
/// <returns>true if successful</returns>
|
||||
public bool Update(string newValue)
|
||||
public async Task<bool> Update(string newValue)
|
||||
{
|
||||
|
||||
return false;
|
||||
this.Value = newValue;
|
||||
return await DBManagerAsync.SaveAsync(this) == 1;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -88,18 +108,38 @@ namespace bsmd.database
|
||||
|
||||
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)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override List<DatabaseEntity> LoadList(System.Data.IDataReader reader)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
string query = $"SELECT Id, EntryType, MappingKey, MappingValue, Created, Changed FROM {Tablename}";
|
||||
switch (filter)
|
||||
{
|
||||
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)
|
||||
{
|
||||
@ -110,11 +150,12 @@ namespace bsmd.database
|
||||
|
||||
vm.id = reader.GetGuid(0);
|
||||
|
||||
//if (!reader.IsDBNull(1)) md.MessageCoreId = reader.GetGuid(1);
|
||||
//if (!reader.IsDBNull(2)) md.ColA = reader.GetString(2);
|
||||
|
||||
if (!reader.IsDBNull(1)) vm.EntryType = (ValueMapping.MappingType)reader.GetByte(1);
|
||||
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;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user