EDMIService: Fix replacing tables
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user