Database: remove baseclass

This commit is contained in:
Jonathan Jenne 2019-08-29 11:27:06 +02:00
parent 718c77ccdd
commit 64a195018a
5 changed files with 38 additions and 31 deletions

View File

@ -1,5 +0,0 @@
Public MustInherit Class BaseClass
Public MustOverride Function GetDatatable(SqlCommand As String) As DataTable
Public MustOverride Function GetScalarValue(SqlCommand As String) As Object
Public MustOverride Function ExecuteNonQuery(SqlCommand As String) As Boolean
End Class

View File

@ -90,7 +90,6 @@
<Import Include="System.Threading.Tasks" /> <Import Include="System.Threading.Tasks" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="BaseClass.vb" />
<Compile Include="Constants.vb" /> <Compile Include="Constants.vb" />
<Compile Include="Exceptions.vb" /> <Compile Include="Exceptions.vb" />
<Compile Include="Firebird.vb" /> <Compile Include="Firebird.vb" />

View File

@ -46,8 +46,6 @@ Imports DigitalData.Modules.Logging
''' REMARKS: If the connection fails due to "wrong username or password", the cause might be that the server harddrive is full.. ''' REMARKS: If the connection fails due to "wrong username or password", the cause might be that the server harddrive is full..
''' </summary> ''' </summary>
Public Class Firebird Public Class Firebird
Inherits BaseClass
Private _Logger As Logger Private _Logger As Logger
Private _LogConfig As LogConfig Private _LogConfig As LogConfig
Private _connectionServer As String Private _connectionServer As String
@ -221,7 +219,7 @@ Public Class Firebird
''' </summary> ''' </summary>
''' <param name="SqlCommand">The command to execute</param> ''' <param name="SqlCommand">The command to execute</param>
''' <returns>True, if command was executed sucessfully. Otherwise false.</returns> ''' <returns>True, if command was executed sucessfully. Otherwise false.</returns>
Public Overrides Function ExecuteNonQuery(SqlCommand As String) As Boolean Public Function ExecuteNonQuery(SqlCommand As String) As Boolean
Dim oConnection As FbConnection = GetConnection() Dim oConnection As FbConnection = GetConnection()
Dim oScalarValue As Object = ExecuteNonQueryWithConnection(SqlCommand, oConnection) Dim oScalarValue As Object = ExecuteNonQueryWithConnection(SqlCommand, oConnection)
oConnection.Close() oConnection.Close()
@ -268,7 +266,7 @@ Public Class Firebird
''' </summary> ''' </summary>
''' <param name="SqlQuery">The query to execute</param> ''' <param name="SqlQuery">The query to execute</param>
''' <returns>The scalar value if the command was executed successfully. Nothing otherwise.</returns> ''' <returns>The scalar value if the command was executed successfully. Nothing otherwise.</returns>
Public Overrides Function GetScalarValue(SqlQuery As String) As Object Public Function GetScalarValue(SqlQuery As String) As Object
Dim oConnection As FbConnection = GetConnection() Dim oConnection As FbConnection = GetConnection()
Dim oScalarValue As Object = GetScalarValueWithConnection(SqlQuery, oConnection) Dim oScalarValue As Object = GetScalarValueWithConnection(SqlQuery, oConnection)
oConnection.Close() oConnection.Close()
@ -304,7 +302,8 @@ Public Class Firebird
oAdapter.Fill(oDatatable) oAdapter.Fill(oDatatable)
Catch ex As Exception Catch ex As Exception
_Logger.Error(ex, $"Error in GetDatatableWithConnection while executing command: '{SqlQuery}'") _Logger.Error(ex)
_Logger.Warn("Error in GetDatatableWithConnection while executing command: '{0}'", SqlQuery)
Throw ex Throw ex
Finally Finally
MaybeCommitTransaction(oTransaction, TransactionMode) MaybeCommitTransaction(oTransaction, TransactionMode)
@ -318,11 +317,17 @@ Public Class Firebird
''' </summary> ''' </summary>
''' <param name="SqlQuery">The query to execute</param> ''' <param name="SqlQuery">The query to execute</param>
''' <returns>A datatable containing the results if the command was executed successfully. Nothing otherwise.</returns> ''' <returns>A datatable containing the results if the command was executed successfully. Nothing otherwise.</returns>
Public Overrides Function GetDatatable(SqlQuery As String) As DataTable Public Function GetDatatable(SqlQuery As String, Optional TransactionMode As TransactionMode = TransactionMode.NoTransaction, Optional Transaction As FbTransaction = Nothing) As DataTable
Try
Dim oConnection As FbConnection = GetConnection() Dim oConnection As FbConnection = GetConnection()
Dim oDatatable As DataTable = GetDatatableWithConnection(SqlQuery, oConnection) Dim oDatatable As DataTable = GetDatatableWithConnection(SqlQuery, oConnection, TransactionMode, Transaction)
oConnection.Close() oConnection.Close()
Return oDatatable Return oDatatable
Catch ex As Exception
_Logger.Error(ex)
_Logger.Warn("Error in GetDatatable while executing command: '{0}'", SqlQuery)
Throw ex
End Try
End Function End Function
End Class End Class

View File

@ -2,13 +2,11 @@
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Public Class MSSQLServer Public Class MSSQLServer
Inherits BaseClass
Public DBInitialized As Boolean = False Public DBInitialized As Boolean = False
Public CurrentSQLConnectionString As String = "" Public CurrentSQLConnectionString As String = ""
Private _Timeout As Integer Private ReadOnly _Timeout As Integer
Private _Logger As Logger Private ReadOnly _Logger As Logger
Public Sub New(LogConfig As LogConfig, ConnectionString As String, Optional Timeout As Integer = 120) Public Sub New(LogConfig As LogConfig, ConnectionString As String, Optional Timeout As Integer = 120)
_Logger = LogConfig.GetLogger() _Logger = LogConfig.GetLogger()
@ -77,7 +75,7 @@ Public Class MSSQLServer
''' </summary> ''' </summary>
''' <param name="SqlCommand">sqlcommand for datatable (select XYZ from TableORView)</param> ''' <param name="SqlCommand">sqlcommand for datatable (select XYZ from TableORView)</param>
''' <returns>Returns a datatable</returns> ''' <returns>Returns a datatable</returns>
Public Overrides Function GetDatatable(SqlCommand As String) As DataTable Public Function GetDatatable(SqlCommand As String, Timeout As Integer) As DataTable
Try Try
If TestCanConnect() = False Then If TestCanConnect() = False Then
Return Nothing Return Nothing
@ -86,7 +84,7 @@ Public Class MSSQLServer
Using oConnection = GetSQLConnection() Using oConnection = GetSQLConnection()
Using oSQLCOmmand = oConnection.CreateCommand() Using oSQLCOmmand = oConnection.CreateCommand()
oSQLCOmmand.CommandText = SqlCommand oSQLCOmmand.CommandText = SqlCommand
oSQLCOmmand.CommandTimeout = _Timeout oSQLCOmmand.CommandTimeout = Timeout
Dim dt As DataTable = New DataTable() Dim dt As DataTable = New DataTable()
Dim oAdapter As SqlDataAdapter = New SqlDataAdapter(oSQLCOmmand) Dim oAdapter As SqlDataAdapter = New SqlDataAdapter(oSQLCOmmand)
@ -101,6 +99,10 @@ Public Class MSSQLServer
End Try End Try
End Function End Function
Public Function GetDatatable(SqlCommand As String) As DataTable
Return GetDatatable(SqlCommand, _Timeout)
End Function
''' <summary> ''' <summary>
''' Executes the passed sql-statement ''' Executes the passed sql-statement
''' </summary> ''' </summary>
@ -112,7 +114,7 @@ Public Class MSSQLServer
Return ExecuteNonQuery(executeStatement) Return ExecuteNonQuery(executeStatement)
End Function End Function
Public Overrides Function ExecuteNonQuery(SQLCommand As String) As Boolean Public Function ExecuteNonQuery(SQLCommand As String, Timeout As Integer) As Boolean
Try Try
If TestCanConnect() = False Then If TestCanConnect() = False Then
Return Nothing Return Nothing
@ -121,7 +123,7 @@ Public Class MSSQLServer
Using oConnection = GetSQLConnection() Using oConnection = GetSQLConnection()
Using oSQLCOmmand = oConnection.CreateCommand() Using oSQLCOmmand = oConnection.CreateCommand()
oSQLCOmmand.CommandText = SQLCommand oSQLCOmmand.CommandText = SQLCommand
oSQLCOmmand.CommandTimeout = _Timeout oSQLCOmmand.CommandTimeout = Timeout
oSQLCOmmand.ExecuteNonQuery() oSQLCOmmand.ExecuteNonQuery()
Return True Return True
End Using End Using
@ -133,6 +135,10 @@ Public Class MSSQLServer
End Try End Try
End Function End Function
Public Function ExecuteNonQuery(SQLCommand As String) As Boolean
Return ExecuteNonQuery(SQLCommand, _Timeout)
End Function
''' <summary> ''' <summary>
''' Executes the passed sql-statement as Scalar ''' Executes the passed sql-statement as Scalar
''' </summary> ''' </summary>
@ -143,7 +149,7 @@ Public Class MSSQLServer
Return GetScalarValue(ScalarSQL) Return GetScalarValue(ScalarSQL)
End Function End Function
Public Overrides Function GetScalarValue(SQLQuery As String) As Object Public Function GetScalarValue(SQLQuery As String, Timeout As Integer) As Object
Try Try
If TestCanConnect() = False Then If TestCanConnect() = False Then
Return Nothing Return Nothing
@ -152,7 +158,7 @@ Public Class MSSQLServer
Using oConnection As SqlConnection = GetSQLConnection() Using oConnection As SqlConnection = GetSQLConnection()
Using oSQLCOmmand = oConnection.CreateCommand() Using oSQLCOmmand = oConnection.CreateCommand()
oSQLCOmmand.CommandText = SQLQuery oSQLCOmmand.CommandText = SQLQuery
oSQLCOmmand.CommandTimeout = _Timeout oSQLCOmmand.CommandTimeout = Timeout
Dim oResult As Object = oSQLCOmmand.ExecuteScalar() Dim oResult As Object = oSQLCOmmand.ExecuteScalar()
Return oResult Return oResult
End Using End Using
@ -163,6 +169,10 @@ Public Class MSSQLServer
End Try End Try
End Function End Function
Public Function GetScalarValue(SQLQuery As String) As Object
Return GetScalarValue(SQLQuery, _Timeout)
End Function
''' <summary> ''' <summary>
''' Executes the passed sql-statement in asyncmode ''' Executes the passed sql-statement in asyncmode
''' </summary> ''' </summary>

View File

@ -2,8 +2,6 @@
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Public Class ODBC Public Class ODBC
Inherits BaseClass
Private _Logger As Logger Private _Logger As Logger
Private _LogConfig As LogConfig Private _LogConfig As LogConfig
@ -88,7 +86,7 @@ Public Class ODBC
''' </summary> ''' </summary>
''' <param name="SqlCommand">The command to execute</param> ''' <param name="SqlCommand">The command to execute</param>
''' <returns>True, if command was executed sucessfully. Otherwise false.</returns> ''' <returns>True, if command was executed sucessfully. Otherwise false.</returns>
Public Overrides Function ExecuteNonQuery(SqlCommand As String) As Boolean Public Function ExecuteNonQuery(SqlCommand As String) As Boolean
Dim oConnection As OdbcConnection = GetConnection() Dim oConnection As OdbcConnection = GetConnection()
Dim oScalarValue As Object = ExecuteNonQueryWithConnection(SqlCommand, oConnection) Dim oScalarValue As Object = ExecuteNonQueryWithConnection(SqlCommand, oConnection)
oConnection.Close() oConnection.Close()
@ -128,7 +126,7 @@ Public Class ODBC
''' </summary> ''' </summary>
''' <param name="SqlQuery">The query to execute</param> ''' <param name="SqlQuery">The query to execute</param>
''' <returns>A datatable containing the results if the command was executed successfully. Nothing otherwise.</returns> ''' <returns>A datatable containing the results if the command was executed successfully. Nothing otherwise.</returns>
Public Overrides Function GetScalarValue(SqlQuery As String) As Object Public Function GetScalarValue(SqlQuery As String) As Object
Dim oConnection As OdbcConnection = GetConnection() Dim oConnection As OdbcConnection = GetConnection()
Dim oDatatable As DataTable = GetDatatableWithConnection(SqlQuery, oConnection) Dim oDatatable As DataTable = GetDatatableWithConnection(SqlQuery, oConnection)
oConnection.Close() oConnection.Close()
@ -171,7 +169,7 @@ Public Class ODBC
''' </summary> ''' </summary>
''' <param name="SqlQuery">The query to execute</param> ''' <param name="SqlQuery">The query to execute</param>
''' <returns>A datatable containing the results if the command was executed successfully. Nothing otherwise.</returns> ''' <returns>A datatable containing the results if the command was executed successfully. Nothing otherwise.</returns>
Public Overrides Function GetDatatable(SqlQuery As String) As DataTable Public Function GetDatatable(SqlQuery As String) As DataTable
Dim oConnection As OdbcConnection = GetConnection() Dim oConnection As OdbcConnection = GetConnection()
Dim oDatatable As DataTable = GetDatatableWithConnection(SqlQuery, oConnection) Dim oDatatable As DataTable = GetDatatableWithConnection(SqlQuery, oConnection)
oConnection.Close() oConnection.Close()