From 375a67bbb44df2a9e50d21de26a86723377f1ea1 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Mon, 17 Oct 2022 10:38:37 +0200 Subject: [PATCH] Database: Improve dispatcher --- Database/Constants.vb | 2 + Database/Database.vbproj | 4 + Database/Dispatcher.vb | 177 ++++++++++++++++++++++++++++++++++----- Database/Queries.vb | 11 +++ Database/packages.config | 1 + 5 files changed, 176 insertions(+), 19 deletions(-) create mode 100644 Database/Queries.vb diff --git a/Database/Constants.vb b/Database/Constants.vb index 3827c68b..02496c41 100644 --- a/Database/Constants.vb +++ b/Database/Constants.vb @@ -2,7 +2,9 @@ Public Const PROVIDER_MSSQL = "MS-SQL" Public Const PROVIDER_ORACLE = "ORACLE" Public Const PROVIDER_ODBC = "ODBC" + Public Const PROVIDER_FIREBIRD = "FIREBIRD" Public Const DEFAULT_TIMEOUT = 120 Public Const DEFAULT_TABLE = "DDRESULT" + Public Const DEFAULT_CONNECTION_ID = 1 End Class diff --git a/Database/Database.vbproj b/Database/Database.vbproj index 4847fb02..b9f2d82c 100644 --- a/Database/Database.vbproj +++ b/Database/Database.vbproj @@ -69,6 +69,9 @@ + + ..\packages\System.Data.Odbc.6.0.1\lib\net461\System.Data.Odbc.dll + @@ -115,6 +118,7 @@ Settings.settings True + diff --git a/Database/Dispatcher.vb b/Database/Dispatcher.vb index aa53df79..bf42a94f 100644 --- a/Database/Dispatcher.vb +++ b/Database/Dispatcher.vb @@ -1,5 +1,5 @@ -Imports DigitalData.Modules.Base -Imports DigitalData.Modules.Logging +Imports DigitalData.Modules.Logging +Imports Oracle.ManagedDataAccess.Client Public Class Dispatcher Public ReadOnly Property Connections As New List(Of DispatcherConnection) @@ -7,27 +7,121 @@ Public Class Dispatcher Public ReadOnly Logger As Logger Public ReadOnly LogConfig As LogConfig + ''' + ''' Create a new instance of Dispatcher. This is the preferred way to create the dispatcher. + ''' + ''' An instance of LogConfig + ''' Initial connectionstring for connecting to DD_ECM database. + ''' An instance of Dispatcher with connections + Public Shared Function Create(pLogConfig As LogConfig, pConnectionString As String) As Dispatcher + Dim oDecryptedConnectionString As String = MSSQLServer.DecryptConnectionString(pConnectionString) + Dim oDatabase As MSSQLServer = New MSSQLServer(pLogConfig, oDecryptedConnectionString) + Dim oTable As DataTable = oDatabase.GetDatatable(Queries.DD_ECM.Connections.AllConnections) + + Dim oConnections As New List(Of DispatcherConnection) + For Each oRow As DataRow In oTable.Rows + Dim oConnection As DispatcherConnection = CreateFromDataRow(oRow) + + If oConnection IsNot Nothing Then + oConnections.Add(oConnection) + End If + Next + + Return New Dispatcher(pLogConfig, oConnections) + End Function + + ''' + ''' Create a new instance of Dispatcher. Needs a manually constructed list of connection objects. + ''' + ''' An instance of LogConfig + ''' A list of DispatcherConnection objects + ''' Public Sub New(pLogConfig As LogConfig, pConnections As List(Of DispatcherConnection)) LogConfig = pLogConfig Logger = pLogConfig.GetLogger() Connections = pConnections End Sub + 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 + + ''' + ''' Returns a Datatable from the database with the specified connection id + ''' + ''' The SQL query + ''' The connection id + ''' A datatable with the results or nothing if an error occurred 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 + ''' + ''' Returns a Datatable from the database + ''' + ''' The SQL query + ''' A datatable with the results or nothing if an error occurred + Public Function GetDatatable(pSQLCommand As String) As DataTable + Return GetDatatable(pSQLCommand, Constants.DEFAULT_CONNECTION_ID) + End Function + + ''' + ''' Executes a query without return value like INSERT or UPDATE from the database with the specified connection id and + ''' returns a boolean value indicating success or failure of the query + ''' + ''' The SQL query + ''' The connection id + ''' True if the query was successful, otherwise false + Public Function ExecuteNonQuery(pSQLCommand As String, pConnectionId As Integer) As Boolean Dim oAdapter As IDatabase = GetAdapterClass(pConnectionId) Return oAdapter.ExecuteNonQuery(pSQLCommand) End Function + ''' + ''' Executes a query without return value like INSERT or UPDATE from the database and + ''' returns a boolean value indicating success or failure of the query + ''' + ''' The SQL query + ''' True if the query was successful, otherwise false + Public Function ExecuteNonQuery(pSQLCommand As String) As Boolean + Return ExecuteNonQuery(pSQLCommand, Constants.DEFAULT_CONNECTION_ID) + End Function + + ''' + ''' Returns a single value from the database specified by the connection id + ''' + ''' The SQL query + ''' The connection id + ''' A value of type object Public Function GetScalarValue(pSQLCommand As String, pConnectionId As Integer) As Object Dim oAdapter As IDatabase = GetAdapterClass(pConnectionId) Return oAdapter.GetScalarValue(pSQLCommand) End Function + ''' + ''' Returns a single value from the database + ''' + ''' The SQL query + ''' A value of type object + Public Function GetScalarValue(pSQLCommand As String) As Object + Return GetScalarValue(pSQLCommand, Constants.DEFAULT_CONNECTION_ID) + End Function + Private Function GetConnection(pConnectionId As Integer) As DispatcherConnection Dim oConnection As DispatcherConnection = Connections. Where(Function(conn) conn.Id = pConnectionId). @@ -49,7 +143,8 @@ Public Class Dispatcher Select Case oConnection.ConnectionType Case ConnectionType.MSSQL - Return New MSSQLServer(LogConfig, oConnection.ConnectionString, Constants.DEFAULT_TIMEOUT) + Dim oConnectionString = MSSQLServer.DecryptConnectionString(oConnection.ConnectionString) + Return New MSSQLServer(LogConfig, oConnectionString, Constants.DEFAULT_TIMEOUT) Case ConnectionType.Oracle Return New Oracle(LogConfig, oConnection.ConnectionString, Constants.DEFAULT_TIMEOUT) @@ -68,22 +163,66 @@ Public Class Dispatcher End Select End Function - Public Enum ConnectionType - MSSQL - Oracle - ODBC - Firebird - End Enum + Private Shared Function CreateFromDataRow(pConnectionDataRow As DataRow) As DispatcherConnection + Try + Dim oGuid = pConnectionDataRow.Item("GUID") + Dim oName = pConnectionDataRow.Item("BEZEICHNUNG") + Dim oUser = pConnectionDataRow.Item("USERNAME") + Dim oPassword = pConnectionDataRow.Item("PASSWORD") + Dim oServer = pConnectionDataRow.Item("SERVER") + Dim oDatabase = pConnectionDataRow.Item("DATENBANK") + + Dim oConnectionType As ConnectionType + Dim oConnectionString As String + + Select Case pConnectionDataRow.Item("SQL_PROVIDER") + Case "Oracle" + ' TODO: Test Oracle Connection + oConnectionType = ConnectionType.Oracle + oConnectionString = New OracleConnectionStringBuilder() With { + .UserID = oUser, + .Password = oPassword, + .DataSource = oServer + }.ToString() + + Case "ODBC" + ' TODO: Test ODBC Connection + oConnectionType = ConnectionType.ODBC + oConnectionString = New Data.Odbc.OdbcConnectionStringBuilder() With { + .ConnectionString = oServer + }.ToString() + + Case "Firebird" + oConnectionType = ConnectionType.Firebird + oConnectionString = New FirebirdSql.Data.FirebirdClient.FbConnectionStringBuilder() With { + .UserID = oUser, + .Password = oPassword, + .DataSource = oServer, + .Database = oDatabase + }.ToString() + + Case Else ' MSSQL + oConnectionType = ConnectionType.MSSQL + oConnectionString = New SqlClient.SqlConnectionStringBuilder() With { + .UserID = oUser, + .Password = oPassword, + .DataSource = oServer, + .InitialCatalog = oDatabase + }.ToString() + + End Select + + Return New DispatcherConnection With { + .Id = oGuid, + .Name = oName, + .ConnectionType = oConnectionType, + .ConnectionString = oConnectionString + } + Catch ex As Exception + Return Nothing + End Try + End Function - 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 diff --git a/Database/Queries.vb b/Database/Queries.vb new file mode 100644 index 00000000..a7e7a2dc --- /dev/null +++ b/Database/Queries.vb @@ -0,0 +1,11 @@ +Public Class Queries + Public Class DD_ECM + Public Class ThirdPartyModules + Public Const GdPictureLicense As String = "SELECT LICENSE FROM TBDD_3RD_PARTY_MODULES WHERE NAME = 'GDPICTURE'" + End Class + + Public Class Connections + Public Const AllConnections As String = "SELECT * FROM TBDD_CONNECTION" + End Class + End Class +End Class diff --git a/Database/packages.config b/Database/packages.config index 2a3264d5..08656661 100644 --- a/Database/packages.config +++ b/Database/packages.config @@ -4,4 +4,5 @@ + \ No newline at end of file