Imports DigitalData.Modules.Database
Imports DigitalData.Modules.EDMI.API
Imports DigitalData.Modules.EDMI.API.Constants
Imports DigitalData.Modules.EDMI.API.EDMIServiceReference
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Language.Utils
Public Class DatabaseWithFallback
Private ReadOnly _Logger As Logger
Private ReadOnly _Client As Client
Private ReadOnly _DatabaseEDM As MSSQLServer
Private ReadOnly _DatabaseIDB As MSSQLServer
'''
''' Options for GetDatatable
'''
Public Class GetDatatableOptions
Public ReadOnly FallbackSQL
Public ReadOnly FallbackType
'''
''' Filter expression for the cached Datatable
'''
Public Property FilterExpression As String = ""
'''
''' Columns to sort the cached Datatable by
'''
Public Property SortByColumn As String = ""
'''
''' Force the fallback, skipping the service completely
'''
Public Property ForceFallback As Boolean = False
'''
''' Connection Id to use, references TBDD_CONNECTION
'''
Public Property ConnectionId As Integer = 0
'''
''' Creates a new options object for GetDatatable options
'''
''' SQL Command to execute as fallback
''' Named Database to use for the fallback SQL Command
Public Sub New(pFallbackSQL As String, pFallbackType As Constants.DatabaseType)
FallbackSQL = pFallbackSQL
FallbackType = pFallbackType
End Sub
End Class
Public Sub New(LogConfig As LogConfig, Client As Client, DatabaseECM As MSSQLServer, DatabaseIDB As MSSQLServer)
_Logger = LogConfig.GetLogger()
_Client = Client
_DatabaseEDM = DatabaseECM
_DatabaseIDB = DatabaseIDB
End Sub
Public Function GetDatatableECM(pSQL As String, Optional pConnectionId As Integer = 0) As DataTable
Return GetDatatable(New GetDatatableOptions(pSQL, Constants.DatabaseType.ECM) With {
.ConnectionId = pConnectionId
})
End Function
Public Function GetDatatableIDB(pSQL As String, Optional pConnectionId As Integer = 0) As DataTable
Return GetDatatable(New GetDatatableOptions(pSQL, Constants.DatabaseType.IDB) With {
.ConnectionId = pConnectionId
})
End Function
Public Function GetScalarValueECM(pSQL As String, Optional pConnectionId As Integer = 0) As Object
Return GetScalarValue(pSQL, Constants.DatabaseType.ECM, pForceFallback:=False, pConnectionId)
End Function
Public Function GetScalarValueIDB(pSQL As String, Optional pConnectionId As Integer = 0) As Object
Return GetScalarValue(pSQL, Constants.DatabaseType.IDB, pForceFallback:=False, pConnectionId)
End Function
'''
''' Returns a Datatable by trying to fetch a cached version from the service, then querying the database through the service and querying the database directly as fallback.
'''
''' Name of the Cached Datatable
''' Options object
Public Function GetDatatable(pDataTableName As String, pOptions As GetDatatableOptions) As DataTable
Return GetDatatable(pDataTableName, pOptions.FallbackSQL, pOptions.FallbackType, pOptions.FilterExpression, pOptions.SortByColumn, pOptions.ForceFallback, pOptions.ConnectionId)
End Function
'''
''' Returns a datatable directly from the database.
'''
''' Options object
Public Function GetDatatable(pOptions As GetDatatableOptions) As DataTable
Dim oForceFallback = True
Return GetDatatable("FORCE_FALLBACK", pOptions.FallbackSQL, pOptions.FallbackType, pOptions.FilterExpression, pOptions.SortByColumn, oForceFallback, pOptions.ConnectionId)
End Function
'''
''' Returns a Datatable by trying to fetch a cached version from the service, then querying the database through the service and querying the database directly as fallback.
'''
''' Name of the Cached Datatable
''' SQL Command to execute as fallback
''' Named Database to use for the fallback SQL Command
''' Filter expression for the cached Datatable
''' Columns to sort the cached Datatable by
''' Force the fallback, skipping the service completely
''' Connection Id to use, references TBDD_CONNECTION
Public Function GetDatatable(pDataTableName As String, pFallbackSQL As String, pFallbackType As Constants.DatabaseType, Optional pFilterExpression As String = "", Optional pSortByColumn As String = "", Optional pForceFallback As Boolean = False, Optional pConnectionId As Integer = 0) As DataTable
Try
Dim oResult As DataTable = Nothing
Dim oTableResult As TableResult = Nothing
' If there is no client, we assume there is no service (configured)
If _Client Is Nothing Then
Return GetDatatableFromDatabase(pFallbackSQL, pFallbackType, pConnectionId)
End If
' If ForceFallback flag is set, we go to database immediately
If pForceFallback Then
Return GetDatatableFromDatabase(pFallbackSQL, pFallbackType, pConnectionId)
End If
' If the table is not cached, we try going through the service
If Not IsTableCached(pDataTableName) Then
Return GetDatatableFromService(pFallbackSQL, pFallbackType, pConnectionId)
End If
' If there is a proper ConnectionId, we try going through the service
If pConnectionId > 0 Then
Return GetDatatableFromService(pFallbackSQL, pFallbackType, pConnectionId)
End If
Try
oTableResult = _Client.GetDatatableByName(pDataTableName, pFilterExpression, pSortByColumn)
Catch ex As Exception
_Logger.Error(ex)
oTableResult = Nothing
End Try
If oTableResult Is Nothing OrElse oTableResult.OK = False Then
_Logger.Warn("Datatable [{0}] could not be fetched from AppServer Cache. Falling back to direct Database Access.", pDataTableName)
Return GetDatatableFromDatabase(pFallbackSQL, pFallbackType, pConnectionId)
Else
Return oTableResult.Table
End If
Catch ex As Exception
_Logger.Error(ex)
Return Nothing
End Try
End Function
'''
''' Returns a Scalar Value by querying the database through the service and querying the database directly as fallback.
'''
''' SQL Command to execute as fallback
''' Named Database to use for the fallback SQL Command
''' Force the fallback, skipping the service completely
''' Connection Id to use, references TBDD_CONNECTION
Public Function GetScalarValue(pSQL As String, pDatabaseType As Constants.DatabaseType, Optional pForceFallback As Boolean = False, Optional pConnectionId As Integer = 0) As DataTable
Try
Dim oResult As DataTable = Nothing
Dim oTableResult As TableResult = Nothing
' If there is no client, we assume there is no service (configured)
If _Client Is Nothing Then
Return GetScalarValueFromDatabase(pSQL, pDatabaseType, pConnectionId)
End If
' If ForceFallback flag is set, we go to database immediately
If pForceFallback Then
Return GetScalarValueFromDatabase(pSQL, pDatabaseType, pConnectionId)
End If
Return GetScalarValueFromService(pSQL, pDatabaseType, pConnectionId)
Catch ex As Exception
_Logger.Error(ex)
Return Nothing
End Try
End Function
Private Function IsTableCached(pName As String) As Boolean
If _Client Is Nothing Then
Return False
End If
Return _Client.CachedTables.Contains(pName.ToUpper)
End Function
Private Function GetDatatableFromService(pSQLCommand As String, DatabaseType As Constants.DatabaseType, pConnectionId As Integer)
Try
Select Case DatabaseType
Case Constants.DatabaseType.ECM
Return _Client.GetDatatableFromECM(pSQLCommand, pConnectionId)
Case Constants.DatabaseType.IDB
Return _Client.GetDatatableFromIDB(pSQLCommand, pConnectionId)
Case Else
Return Nothing
End Select
Catch ex As Exception
_Logger.Warn("GetDatatableFromService failed. Falling back to direct database access.")
_Logger.Error(ex)
Return GetDatatableFromDatabase(pSQLCommand, DatabaseType, pConnectionId)
End Try
End Function
Private Function GetScalarValueFromService(pSQLCommand As String, DatabaseType As Constants.DatabaseType, pConnectionId As Integer)
Try
Select Case DatabaseType
Case Constants.DatabaseType.ECM
Return _Client.GetScalarValueFromECM(pSQLCommand, pConnectionId)
Case Constants.DatabaseType.IDB
Return _Client.GetScalarValueFromIDB(pSQLCommand, pConnectionId)
Case Else
Return Nothing
End Select
Catch ex As Exception
_Logger.Warn("GetScalarValueFromService failed. Falling back to direct database access.")
_Logger.Error(ex)
Return GetDatatableFromDatabase(pSQLCommand, DatabaseType, pConnectionId)
End Try
End Function
Private Function GetDatatableFromDatabase(pSQLCommand As String, DatabaseType As Constants.DatabaseType, pConnectionId As Integer)
Try
Select Case DatabaseType
Case Constants.DatabaseType.ECM
Return _DatabaseEDM.GetDatatable(pSQLCommand)
Case Constants.DatabaseType.IDB
Return _DatabaseIDB.GetDatatable(pSQLCommand)
Case Else
Return Nothing
End Select
Catch ex As Exception
_Logger.Error(ex)
Return Nothing
End Try
End Function
Private Function GetScalarValueFromDatabase(pSQLCommand As String, DatabaseType As Constants.DatabaseType, pConnectionId As Integer)
Try
Select Case DatabaseType
Case Constants.DatabaseType.ECM
Return _DatabaseEDM.GetScalarValue(pSQLCommand)
Case Constants.DatabaseType.IDB
Return _DatabaseIDB.GetScalarValue(pSQLCommand)
Case Else
Return Nothing
End Select
Catch ex As Exception
_Logger.Error(ex)
Return Nothing
End Try
End Function
End Class