EDMIService: Fix replacing tables

This commit is contained in:
Jonathan Jenne 2020-12-14 11:20:19 +01:00
parent 39f4cc5152
commit 16193b08cf
3 changed files with 56 additions and 18 deletions

View File

@ -22,16 +22,29 @@ Partial Class frmRelations
'Das Bearbeiten mit dem Code-Editor ist nicht möglich.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(12, 12)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
'
'frmRelations
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(800, 450)
Me.Controls.Add(Me.Button1)
Me.Name = "frmRelations"
Me.Text = "frmRelations"
Me.ResumeLayout(False)
End Sub
Friend WithEvents Button1 As Button
End Class

View File

@ -5,6 +5,9 @@ Imports DigitalData.Services.EDMIService
Public Class frmRelations
Private DB As MSSQLServer
Private LogConfig As LogConfig
Private JobResult As JobResult
Private JobListener As JobListener
Private DetailRow As DataRow
Private Sub frmRelations_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LogConfig = New LogConfig(LogConfig.PathType.CustomPath, Application.StartupPath)
@ -18,13 +21,18 @@ Public Class frmRelations
oGroupUser.TableName = "TBDD_GROUPS_USER"
Dim oCronDetails As DataTable = DB.GetDatatable("SELECT * FROM TBAPPSERV_CRON_DETAIL WHERE DT_NAME = 'TBDD_USER'")
JobListener = New JobListener(LogConfig, DB, oDataSet)
Dim oListener As New DigitalData.Services.EDMIService.JobListener(LogConfig, DB, oDataSet)
oListener.SaveDataTables(New DigitalData.Services.EDMIService.JobResult() With {
DetailRow = oCronDetails.Rows.Item(0)
JobResult = New JobResult() With {
.Table = oUsers,
.TableRelationColumn = "GUID",
.ChildTable = oGroupUser,
.ChildRelationColumn = "USER_ID"
}, oCronDetails.Rows.Item(0))
}
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
JobListener.SaveDataTables(JobResult, DetailRow)
End Sub
End Class

View File

@ -26,16 +26,35 @@ Public Class JobListener
Dataset = ResultDataSet
End Sub
Public Sub ReplaceExistingTable(Name As String, Table As DataTable, DataSet As DataSet)
Public Sub ReplaceExistingTable(Name As String, Table As DataTable, DataSet As DataSet, Optional ChildTable As DataTable = Nothing)
Dim oDatatableNameTemp As String = Name & "-TEMP"
_Logger.Debug("DataTable [{0}] exists, renaming and replacing in DataSet", Name)
' Rename the new table, add TEMP suffix
Table.TableName = oDatatableNameTemp
' If child table exists, remove all connections to it
If ChildTable IsNot Nothing Then
' Get name for relations and constraints
Dim oRelationName = GetRelationName(Name, ChildTable.TableName)
' Remove the relation
Dim oRelation As DataRelation = DataSet.Relations.Item(oRelationName)
DataSet.Relations.Remove(oRelation)
' Remove the constraint
Dim oConstraint As Constraint = ChildTable.Constraints.Item(oRelationName)
ChildTable.Constraints.Remove(oConstraint)
End If
' Remove the temp table if it exists
If DataSet.Tables.Contains(oDatatableNameTemp) Then
DataSet.Tables.Remove(Table)
End If
' Add the new table to the dataset
DataSet.Tables.Add(Table)
' Remove the old table
DataSet.Tables.Remove(Name)
' Rename the new table
DataSet.Tables.Item(oDatatableNameTemp).TableName = Name
End Sub
@ -46,11 +65,15 @@ Public Class JobListener
DataSet.Tables.Add(Table)
End Sub
Public Function GetRelationName(ParentTableName As String, ChildTableName As String) As String
Return $"{ParentTableName}-{ChildTableName}"
End Function
Public Sub AddRelation(ParentTableName As String, ParentColumnName As String, ChildTableName As String, ChildColumnName As String)
Dim oChild As DataTable = Dataset.Tables.Item(ChildTableName)
Dim oParent As DataTable = Dataset.Tables.Item(ParentTableName)
Dim oRelationName As String = $"{ParentTableName}-{ChildTableName}"
Dim oRelationName As String = GetRelationName(ParentTableName, ChildTableName)
Dim oParentColumn As DataColumn = oParent.Columns.Item(ParentColumnName)
Dim oChildColumn As DataColumn = oChild.Columns.Item(ChildColumnName)
@ -90,24 +113,18 @@ Public Class JobListener
Public Sub SaveDataTables(Result As JobResult, DetailRow As DataRow)
Try
Dim oTable As DataTable = Result.Table
Dim oName As String = DetailRow.Item("DT_NAME")
Dim oDetailId As Integer = DetailRow.Item("GUID")
Dim oDatatableNameTemp As String = oName & "-TEMP"
If Dataset.Tables.Contains(oName) Then
' Replace existing table
If Result.ChildTable IsNot Nothing Then
ReplaceExistingTable(Result.ChildTable.TableName, Result.ChildTable, Dataset)
End If
ReplaceExistingTable(oName, oTable, Dataset)
If Result.ChildTable IsNot Nothing Then
Dim oRelation = Dataset.Relations.Item($"{oTable.TableName}-{Result.ChildTable.TableName}")
If oRelation IsNot Nothing Then
Dataset.Relations.Remove(oRelation)
End If
ReplaceExistingTable(oName, oTable, Dataset, Result.ChildTable)
AddRelation(oName, Result.TableRelationColumn, Result.ChildTable.TableName, Result.ChildRelationColumn)
Else
ReplaceExistingTable(oName, oTable, Dataset)
End If
Else
AddNewTable(oName, oTable, Dataset)