Monorepo/Modules.Database/Dispatcher.vb
2022-09-27 15:00:27 +02:00

90 lines
3.4 KiB
VB.net

Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Logging
Public Class Dispatcher
Public ReadOnly Property Connections As New List(Of DispatcherConnection)
Public ReadOnly Logger As Logger
Public ReadOnly LogConfig As LogConfig
Public Sub New(pLogConfig As LogConfig, pConnections As List(Of DispatcherConnection))
LogConfig = pLogConfig
Logger = pLogConfig.GetLogger()
Connections = pConnections
End Sub
Public Function GetDatatable(pSQLCommand As String, pConnectionId As Integer) As DataTable
Dim oAdapter As IDatabase = GetAdapterClass(pConnectionId)
Return oAdapter.GetDatatable(pSQLCommand)
End Function
Public Function ExectueNonQuery(pSQLCommand As String, pConnectionId As Integer) As Boolean
Dim oAdapter As IDatabase = GetAdapterClass(pConnectionId)
Return oAdapter.ExecuteNonQuery(pSQLCommand)
End Function
Public Function GetScalarValue(pSQLCommand As String, pConnectionId As Integer) As Object
Dim oAdapter As IDatabase = GetAdapterClass(pConnectionId)
Return oAdapter.GetScalarValue(pSQLCommand)
End Function
Private Function GetConnection(pConnectionId As Integer) As DispatcherConnection
Dim oConnection As DispatcherConnection = Connections.
Where(Function(conn) conn.Id = pConnectionId).
FirstOrDefault()
If oConnection IsNot Nothing Then
Logger.Debug("Resolved ConnectionId [{0}] into Connection [{1}]", pConnectionId, oConnection.Name)
End If
Return oConnection
End Function
Private Function GetAdapterClass(pConnectionId As Integer) As IDatabase
Dim oConnection = GetConnection(pConnectionId)
Dim oArgs As New List(Of Object) From {LogConfig, oConnection.ConnectionString, Constants.DEFAULT_TIMEOUT}
Logger.Debug("Creating database adapter object for type [{0}]", ConnectionType.MSSQL)
' TODO: Cache Database instances to avoid constructing them for every call
Select Case oConnection.ConnectionType
Case ConnectionType.MSSQL
Return New MSSQLServer(LogConfig, oConnection.ConnectionString, Constants.DEFAULT_TIMEOUT)
Case ConnectionType.Oracle
Return New Oracle(LogConfig, oConnection.ConnectionString, Constants.DEFAULT_TIMEOUT)
Case ConnectionType.Firebird
Dim oBuilder As New FirebirdSql.Data.FirebirdClient.FbConnectionStringBuilder(oConnection.ConnectionString)
Return New Firebird(LogConfig, oBuilder.DataSource, oBuilder.Database, oBuilder.UserID, oBuilder.Password)
Case ConnectionType.ODBC
'Dim oBuilder As New Data.Odbc.OdbcConnectionStringBuilder(pConnection.ConnectionString)
'Return New ODBC(LogConfig)
Return Nothing
Case Else
Return Nothing
End Select
End Function
Public Enum ConnectionType
MSSQL
Oracle
ODBC
Firebird
End Enum
Public Class DispatcherOptions
Public Property QueryTimeout As Integer = Constants.DEFAULT_TIMEOUT
End Class
Public Class DispatcherConnection
Public Property Id As Integer
Public Property Name As String
Public Property ConnectionString As String
Public Property ConnectionType As ConnectionType
End Class
End Class