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:
@@ -8,7 +8,8 @@ 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 _ClientConfig As ConfigClientConfiguration
|
||||
Private ReadOnly _DatabaseECM As MSSQLServer
|
||||
Private ReadOnly _DatabaseIDB As MSSQLServer
|
||||
|
||||
''' <summary>
|
||||
@@ -49,8 +50,11 @@ Public Class DatabaseWithFallback
|
||||
Public Sub New(LogConfig As LogConfig, Client As Client, DatabaseECM As MSSQLServer, DatabaseIDB As MSSQLServer)
|
||||
_Logger = LogConfig.GetLogger()
|
||||
_Client = Client
|
||||
_DatabaseEDM = DatabaseECM
|
||||
_DatabaseECM = DatabaseECM
|
||||
_DatabaseIDB = DatabaseIDB
|
||||
|
||||
' Load client config, will throw if client is not yet connected to service
|
||||
_ClientConfig = Client.TryGetClientConfig()
|
||||
End Sub
|
||||
|
||||
Public Function GetDatatableECM(pSQL As String, Optional pConnectionId As Integer = 0) As DataTable
|
||||
@@ -73,6 +77,14 @@ Public Class DatabaseWithFallback
|
||||
Return GetScalarValue(pSQL, Constants.DatabaseType.IDB, pForceFallback:=False, pConnectionId)
|
||||
End Function
|
||||
|
||||
Public Function ExecuteNonQueryECM(pSQL As String, Optional pConnectionId As Integer = 0) As Boolean
|
||||
Return ExecuteNonQuery(pSQL, Constants.DatabaseType.ECM, pForceFallback:=False, pConnectionId)
|
||||
End Function
|
||||
|
||||
Public Function ExecuteNonQueryIDB(pSQL As String, Optional pConnectionId As Integer = 0) As Boolean
|
||||
Return ExecuteNonQuery(pSQL, Constants.DatabaseType.IDB, pForceFallback:=False, pConnectionId)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' 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.
|
||||
''' </summary>
|
||||
@@ -112,7 +124,7 @@ Public Class DatabaseWithFallback
|
||||
End If
|
||||
|
||||
' If ForceFallback flag is set, we go to database immediately
|
||||
If pForceFallback Then
|
||||
If pForceFallback Or _ClientConfig.ForceDirectDatabaseAccess Then
|
||||
Return GetDatatableFromDatabase(pFallbackSQL, pFallbackType, pConnectionId)
|
||||
End If
|
||||
|
||||
@@ -165,7 +177,7 @@ Public Class DatabaseWithFallback
|
||||
End If
|
||||
|
||||
' If ForceFallback flag is set, we go to database immediately
|
||||
If pForceFallback Then
|
||||
If pForceFallback Or _ClientConfig.ForceDirectDatabaseAccess Then
|
||||
Return GetScalarValueFromDatabase(pSQL, pDatabaseType, pConnectionId)
|
||||
End If
|
||||
|
||||
@@ -178,6 +190,37 @@ Public Class DatabaseWithFallback
|
||||
End Try
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Returns a Scalar Value by querying the database through the service and querying the database directly as fallback.
|
||||
''' </summary>
|
||||
''' <param name="pSQL">SQL Command to execute as fallback</param>
|
||||
''' <param name="pDatabaseType">Named Database to use for the fallback SQL Command</param>
|
||||
''' <param name="pForceFallback">Force the fallback, skipping the service completely</param>
|
||||
''' <param name="pConnectionId">Connection Id to use, references TBDD_CONNECTION</param>
|
||||
Public Function ExecuteNonQuery(pSQL As String, pDatabaseType As Constants.DatabaseType, Optional pForceFallback As Boolean = False, Optional pConnectionId As Integer = 0) As Boolean
|
||||
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 ExecuteNonQueryFromDatabase(pSQL, pDatabaseType, pConnectionId)
|
||||
End If
|
||||
|
||||
' If ForceFallback flag is set, we go to database immediately
|
||||
If pForceFallback Or _ClientConfig.ForceDirectDatabaseAccess Then
|
||||
Return ExecuteNonQueryFromDatabase(pSQL, pDatabaseType, pConnectionId)
|
||||
End If
|
||||
|
||||
Return ExecuteNonQueryFromService(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
|
||||
@@ -186,19 +229,32 @@ Public Class DatabaseWithFallback
|
||||
Return _Client.CachedTables.Contains(pName.ToUpper)
|
||||
End Function
|
||||
|
||||
Private Function GetDatatableFromService(pSQLCommand As String, DatabaseType As Constants.DatabaseType, pConnectionId As Integer)
|
||||
Private Function GetDatatableFromService(pSQLCommand As String, DatabaseType As Constants.DatabaseType, pConnectionId As Integer) As DataTable
|
||||
Try
|
||||
Dim oResult As GetDatatableResponse = Nothing
|
||||
|
||||
Select Case DatabaseType
|
||||
Case Constants.DatabaseType.ECM
|
||||
Return _Client.GetDatatableFromECM(pSQLCommand, pConnectionId)
|
||||
oResult = _Client.GetDatatableFromECM(pSQLCommand, pConnectionId)
|
||||
|
||||
Case Constants.DatabaseType.IDB
|
||||
Return _Client.GetDatatableFromIDB(pSQLCommand, pConnectionId)
|
||||
oResult = _Client.GetDatatableFromIDB(pSQLCommand, pConnectionId)
|
||||
|
||||
Case Else
|
||||
Return Nothing
|
||||
Return GetDatatableFromDatabase(pSQLCommand, DatabaseType, pConnectionId)
|
||||
|
||||
End Select
|
||||
|
||||
If oResult Is Nothing Then
|
||||
Throw New ApplicationException("Unexpected server error ocurred!")
|
||||
End If
|
||||
|
||||
If oResult.OK = False Then
|
||||
Throw New ApplicationException(oResult.ErrorMessage)
|
||||
End If
|
||||
|
||||
Return oResult.Table
|
||||
|
||||
Catch ex As Exception
|
||||
_Logger.Warn("GetDatatableFromService failed. Falling back to direct database access.")
|
||||
_Logger.Error(ex)
|
||||
@@ -207,19 +263,53 @@ Public Class DatabaseWithFallback
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function GetScalarValueFromService(pSQLCommand As String, DatabaseType As Constants.DatabaseType, pConnectionId As Integer)
|
||||
|
||||
Private Function GetDatatableFromDatabase(pSQLCommand As String, DatabaseType As Constants.DatabaseType, pConnectionId As Integer) As DataTable
|
||||
Try
|
||||
Dim oResult As ExecuteNonQueryResponse = Nothing
|
||||
|
||||
Select Case DatabaseType
|
||||
Case Constants.DatabaseType.ECM
|
||||
Return _Client.GetScalarValueFromECM(pSQLCommand, pConnectionId)
|
||||
Return _DatabaseECM.GetDatatable(pSQLCommand)
|
||||
|
||||
Case Constants.DatabaseType.IDB
|
||||
Return _Client.GetScalarValueFromIDB(pSQLCommand, pConnectionId)
|
||||
Return _DatabaseIDB.GetDatatable(pSQLCommand)
|
||||
|
||||
Case Else
|
||||
Return Nothing
|
||||
Dim oConnectionString = _DatabaseECM.Get_ConnectionStringforID(pConnectionId)
|
||||
Return _DatabaseECM.GetDatatableWithConnection(pSQLCommand, oConnectionString)
|
||||
|
||||
End Select
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
Return Nothing
|
||||
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function GetScalarValueFromService(pSQLCommand As String, DatabaseType As Constants.DatabaseType, pConnectionId As Integer) As Object
|
||||
Try
|
||||
Dim oResult As GetScalarValueResponse = Nothing
|
||||
|
||||
Select Case DatabaseType
|
||||
Case Constants.DatabaseType.ECM
|
||||
oResult = _Client.GetScalarValueFromECM(pSQLCommand, pConnectionId)
|
||||
|
||||
Case Constants.DatabaseType.IDB
|
||||
oResult = _Client.GetScalarValueFromIDB(pSQLCommand, pConnectionId)
|
||||
|
||||
End Select
|
||||
|
||||
If oResult Is Nothing Then
|
||||
Throw New ApplicationException("Unexpected server error ocurred!")
|
||||
End If
|
||||
|
||||
If oResult.OK = False Then
|
||||
Throw New ApplicationException(oResult.ErrorMessage)
|
||||
End If
|
||||
|
||||
Return oResult.Scalar
|
||||
|
||||
Catch ex As Exception
|
||||
_Logger.Warn("GetScalarValueFromService failed. Falling back to direct database access.")
|
||||
_Logger.Error(ex)
|
||||
@@ -228,37 +318,19 @@ Public Class DatabaseWithFallback
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function GetDatatableFromDatabase(pSQLCommand As String, DatabaseType As Constants.DatabaseType, pConnectionId As Integer)
|
||||
|
||||
Private Function GetScalarValueFromDatabase(pSQLCommand As String, DatabaseType As Constants.DatabaseType, pConnectionId As Integer) As Object
|
||||
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)
|
||||
Return _DatabaseECM.GetScalarValue(pSQLCommand)
|
||||
|
||||
Case Constants.DatabaseType.IDB
|
||||
Return _DatabaseIDB.GetScalarValue(pSQLCommand)
|
||||
|
||||
Case Else
|
||||
Return Nothing
|
||||
Dim oConnectionString = _DatabaseECM.Get_ConnectionStringforID(pConnectionId)
|
||||
Return _DatabaseECM.GetScalarValueWithConnection(pSQLCommand, oConnectionString)
|
||||
|
||||
End Select
|
||||
Catch ex As Exception
|
||||
@@ -267,5 +339,57 @@ Public Class DatabaseWithFallback
|
||||
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function ExecuteNonQueryFromService(pSQLCommand As String, DatabaseType As Constants.DatabaseType, pConnectionId As Integer) As Boolean
|
||||
Try
|
||||
Dim oResult As ExecuteNonQueryResponse = Nothing
|
||||
|
||||
Select Case DatabaseType
|
||||
Case Constants.DatabaseType.ECM
|
||||
oResult = _Client.ExecuteNonQueryFromECM(pSQLCommand, pConnectionId)
|
||||
|
||||
Case Constants.DatabaseType.IDB
|
||||
oResult = _Client.ExecuteNonQueryFromIDB(pSQLCommand, pConnectionId)
|
||||
|
||||
End Select
|
||||
|
||||
If oResult Is Nothing Then
|
||||
Throw New ApplicationException("Unexpected server error ocurred!")
|
||||
End If
|
||||
|
||||
If oResult.OK = False Then
|
||||
Throw New ApplicationException(oResult.ErrorMessage)
|
||||
End If
|
||||
|
||||
Return oResult.Result
|
||||
|
||||
Catch ex As Exception
|
||||
_Logger.Warn("ExecuteNonQueryFromService failed. Falling back to direct database access.")
|
||||
_Logger.Error(ex)
|
||||
Return ExecuteNonQueryFromDatabase(pSQLCommand, DatabaseType, pConnectionId)
|
||||
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function ExecuteNonQueryFromDatabase(pSQLCommand As String, DatabaseType As Constants.DatabaseType, pConnectionId As Integer) As Boolean
|
||||
Try
|
||||
Select Case DatabaseType
|
||||
Case Constants.DatabaseType.ECM
|
||||
Return _DatabaseECM.ExecuteNonQuery(pSQLCommand)
|
||||
|
||||
Case Constants.DatabaseType.IDB
|
||||
Return _DatabaseIDB.ExecuteNonQuery(pSQLCommand)
|
||||
|
||||
Case Else
|
||||
Dim oConnectionString = _DatabaseECM.Get_ConnectionStringforID(pConnectionId)
|
||||
Return _DatabaseECM.ExecuteNonQueryWithConnection(pSQLCommand, oConnectionString)
|
||||
|
||||
End Select
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
Return False
|
||||
|
||||
End Try
|
||||
End Function
|
||||
End Class
|
||||
|
||||
|
||||
Reference in New Issue
Block a user