EDMI: Add 3-tier database access in DatabaseWithFallback, add client config,

EDMI Service: Version 2.4.0.0
EDMI: API: Version 1.4.0.0
This commit is contained in:
Jonathan Jenne
2022-01-14 11:48:32 +01:00
parent 7d46951b60
commit bcbfba37b2
58 changed files with 1192 additions and 416 deletions

View File

@@ -0,0 +1,66 @@
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Namespace Methods.Database.ExecuteNonQuery
''' <summary>
''' Database method for executing a query from the supplied SQL Command and a Connection Id.
''' </summary>
Public Class ExecuteNonQueryMethod
Inherits BaseMethod
Public Sub New(pLogConfig As LogConfig, pDatabaseIDB As MSSQLServer, pDatabaseECM As MSSQLServer, pGlobalState As GlobalState)
MyBase.New(pLogConfig, pDatabaseIDB, pDatabaseECM, pGlobalState)
End Sub
''' <summary>
''' Executes a Query. If ConnectionId is empty, uses the configured DD_ECM Database.
''' </summary>
Public Function Run(pData As ExecuteNonQueryRequest) As ExecuteNonQueryResponse
Try
Dim oResult As Boolean = False
Select Case pData.DatabaseType
Case DatabaseType.MSSQL
If pData.NamedDatabase = DatabaseName.None Then
oResult = ExecuteNonQueryByConnectionId(pData.SqlCommand, pData.ConnectionId)
ElseIf pData.NamedDatabase = DatabaseName.ECM Then
oResult = DatabaseECM.ExecuteNonQuery(pData.SqlCommand)
ElseIf pData.NamedDatabase = DatabaseName.IDB Then
oResult = DatabaseIDB.ExecuteNonQuery(pData.SqlCommand)
Else
LogAndThrow($"Unsupported Named Database supplied. SQL Command [{pData.SqlCommand}] was not executed!")
End If
Case Else
LogAndThrow($"Only MSSQL Server is supported at this time. SQL Command [{pData.SqlCommand}] was not executed!")
End Select
Return New ExecuteNonQueryResponse(oResult)
Catch ex As Exception
Logger.Warn("Error occurred while getting database scalar value!")
Return New ExecuteNonQueryResponse(ex)
End Try
End Function
Private Function ExecuteNonQueryByConnectionId(pSQLCommand As String, pConnectionId As Integer) As Boolean
If pConnectionId = 0 Then
Return DatabaseECM.ExecuteNonQuery(pSQLCommand)
Else
Dim oConnectionString = DatabaseECM.Get_ConnectionStringforID(pConnectionId)
If oConnectionString <> "" Then
Return DatabaseIDB.ExecuteNonQueryWithConnection(pSQLCommand, oConnectionString)
Else
Logger.Warn("ConnectionId [{0}] could not be translated into ConnectionString. Exiting.")
Return False
End If
End If
End Function
End Class
End Namespace

View File

@@ -0,0 +1,8 @@
Namespace Methods.Database.ExecuteNonQuery
Public Class ExecuteNonQueryRequest
Public Property SqlCommand As String = ""
Public Property ConnectionId As Integer = 0
Public Property DatabaseType As DatabaseType = DatabaseType.MSSQL
Public Property NamedDatabase As DatabaseName = DatabaseName.ECM
End Class
End Namespace

View File

@@ -0,0 +1,24 @@
Imports System.Runtime.Serialization
Namespace Methods.Database.ExecuteNonQuery
<Serializable>
<DataContract>
Public Class ExecuteNonQueryResponse
Inherits Messages.BaseResponse
<DataMember>
Public Property Result As Boolean
Public Sub New(pResult As Object)
MyBase.New()
Result = pResult
End Sub
Public Sub New(pException As Exception, Optional pDetails As String = "")
MyBase.New(pException, pDetails)
End Sub
End Class
End Namespace

View File

@@ -0,0 +1,59 @@
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Language
Imports DigitalData.Modules.Logging
Namespace Methods.Database.GetDatatableFromCache
Public Class GetDatatableFromCacheMethod
Inherits BaseMethod
Public Sub New(pLogConfig As LogConfig, pDatabaseIDB As MSSQLServer, pDatabaseECM As MSSQLServer, pGlobalState As GlobalState)
MyBase.New(pLogConfig, pDatabaseIDB, pDatabaseECM, pGlobalState)
End Sub
Public Function Run(pData As GetDatatableFromCacheRequest) As GetDatatableFromCacheResponse
Try
Logger.Debug($"ReturnDatatableFromCache: Datatable: {pData.DataTable}")
Dim oDataTable As DataTable = Nothing
Logger.Debug("ReturnDatatableFromCache: DataSet contains [{0}] datatables", GlobalState.TableStore.Tables.Count)
If GlobalState.TableStore.Tables.Contains(pData.DataTable) Then
oDataTable = GlobalState.TableStore.Tables.Item(pData.DataTable).Copy()
' Apply filter and sorting to data
Dim oFilterExpression As String = Utils.NotNull(pData.FilterExpression, String.Empty)
Dim oSortByColumn As String = Utils.NotNull(pData.SortByColumn, String.Empty)
Dim oFilteredRows = oDataTable.Select(oFilterExpression, oSortByColumn)
Dim oFilteredTable As DataTable = Nothing
If oFilteredRows.Count > 0 Then
oFilteredTable = oFilteredRows.CopyToDataTable()
oFilteredTable.TableName = pData.DataTable
Else
' Produce empty table
oFilteredTable = oDataTable.Clone()
oFilteredTable.TableName = pData.DataTable
End If
Logger.Debug("ReturnDatatableFromCache: Datatable Stats for [{0}]:", pData.DataTable)
Logger.Debug("Unfiltered: [{0}] rows", oDataTable.Rows.Count)
Logger.Debug("Filtered: [{0}] rows", oFilteredTable.Rows.Count)
Return New GetDatatableFromCacheResponse(oFilteredTable)
Else
Logger.Warn($"ReturnDatatableFromCache: Datatable {pData.DataTable} does not exist")
Return New GetDatatableFromCacheResponse(New KeyNotFoundException($"Datatable {pData.DataTable} does not exist"))
End If
Catch ex As Exception
Logger.Error(ex)
Return New GetDatatableFromCacheResponse(ex)
End Try
End Function
End Class
End Namespace

View File

@@ -0,0 +1,14 @@
Imports System.Runtime.Serialization
Namespace Methods.Database.GetDatatableFromCache
<Serializable>
<DataContract>
Public Class GetDatatableFromCacheRequest
<DataMember>
Public Property DataTable As String
<DataMember>
Public Property FilterExpression As String
<DataMember>
Public Property SortByColumn As String
End Class
End Namespace

View File

@@ -0,0 +1,22 @@
Imports System.Runtime.Serialization
Namespace Methods.Database.GetDatatableFromCache
<Serializable>
<DataContract>
Public Class GetDatatableFromCacheResponse
Inherits Messages.BaseResponse
<DataMember>
Public Property Table As DataTable
Public Sub New(pTable As DataTable)
MyBase.New()
Table = pTable
End Sub
Public Sub New(pException As Exception, Optional pDetails As String = "")
MyBase.New(pException, pDetails)
End Sub
End Class
End Namespace