jj: Database Version 0.0.3

This commit is contained in:
Jonathan Jenne 2018-11-09 10:49:59 +01:00
parent 084fa29f3d
commit c27cf5e8a0
2 changed files with 53 additions and 92 deletions

View File

@ -5,9 +5,9 @@ Imports DigitalData.Modules.Logging
''' <summary> ''' <summary>
''' MODULE: Firebird ''' MODULE: Firebird
''' '''
''' VERSION: 0.0.0.3 ''' VERSION: 0.0.0.2
''' '''
''' DATE: 08.11.2018 ''' DATE: 03.09.2018
''' '''
''' DESCRIPTION: ''' DESCRIPTION:
''' '''
@ -17,14 +17,14 @@ Imports DigitalData.Modules.Logging
''' '''
''' FirebirdSql.Data.FirebirdClient, >= 6.0.0 ''' FirebirdSql.Data.FirebirdClient, >= 6.0.0
''' '''
''' PARAMETERS: LogConfig, DigitalData.Modules.Logging.LogConfig ''' PARAMETERS: LogFactory, NLog.LogFactory
''' The LogFactory containing the current log config. Used to instanciate the class logger for this and any dependent class ''' The LogFactory containing the current log config. Used to instanciate the class logger for this and any dependent class
''' '''
''' DataSource, String ''' DataSource, String
''' The server where the database lives, for example 127.0.0.1 or dd-vmx09-vm03 ''' The location of the Database in the format `127.0.0.1:E:\Path\To\Database.FDB`
''' '''
''' Database, String ''' Database, String
''' The location of the Database in the format `127.0.0.1:E:\Path\To\Database.FDB` ''' The server where the database lives, for example 127.0.0.1 or dd-vmx09-vm03
''' '''
''' User, String ''' User, String
''' The user name to connect as ''' The user name to connect as
@ -52,26 +52,23 @@ Public Class Firebird
Private _connectionDatabase As String Private _connectionDatabase As String
Private _connectionUsername As String Private _connectionUsername As String
Private _connectionPassword As String Private _connectionPassword As String
Private _connectionString As String
Public ReadOnly Property ConnectionEstablished As Boolean
Public ReadOnly Property ConnectionFailed As Boolean
Public ReadOnly Property ConnectionString As String Public ReadOnly Property ConnectionString As String
Get
Return _connectionString
End Get
End Property
Public ReadOnly Property DatabaseName As String Public ReadOnly Property DatabaseName As String
Get Get
Dim oRegex As New Regex("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:") Dim oRegex As New Regex("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:")
Dim oPath As String = oRegex.Replace(_connectionDatabase, "") Dim oPath As String = oRegex.Replace(_connectionDatabase, "")
Dim oFileInfo As New IO.FileInfo(oPath) Dim fi As New IO.FileInfo(oPath)
Return oFileInfo.Name Return fi.Name
End Get End Get
End Property End Property
''' <summary> ''' <summary>
''' '''
''' </summary> ''' </summary>
''' <param name="LogFactory"></param>
''' <param name="Datasource"></param> ''' <param name="Datasource"></param>
''' <param name="Database"></param> ''' <param name="Database"></param>
''' <param name="User"></param> ''' <param name="User"></param>
@ -87,7 +84,7 @@ Public Class Firebird
_connectionDatabase = Database _connectionDatabase = Database
_connectionUsername = User _connectionUsername = User
_connectionPassword = Password _connectionPassword = Password
_connectionString = oConnectionString _ConnectionString = oConnectionString
' Test the connection ' Test the connection
Dim oConnection = GetConnection() Dim oConnection = GetConnection()
@ -101,11 +98,15 @@ Public Class Firebird
Public Function GetConnection() As FbConnection Public Function GetConnection() As FbConnection
Try Try
Dim oConnection = New FbConnection(_connectionString) Dim oConnection = New FbConnection(_ConnectionString)
oConnection.Open() oConnection.Open()
_ConnectionEstablished = True
_ConnectionFailed = False
Return oConnection Return oConnection
Catch ex As Exception Catch ex As Exception
_ConnectionFailed = True
_ConnectionEstablished = False
_logger.Error(ex) _logger.Error(ex)
Return Nothing Return Nothing
@ -133,18 +134,16 @@ Public Class Firebird
''' Executes a non-query command. ''' Executes a non-query command.
''' </summary> ''' </summary>
''' <param name="SqlCommand">The command to execute</param> ''' <param name="SqlCommand">The command to execute</param>
''' <param name="Connection">The Firebird connection to use</param>
''' <returns>True, if command was executed sucessfully. Otherwise false.</returns> ''' <returns>True, if command was executed sucessfully. Otherwise false.</returns>
Public Function ExecuteNonQueryWithConnection(SqlCommand As String, Connection As FbConnection) As Boolean Public Function ExecuteNonQuery(SqlCommand As String) As Boolean
Dim oConnection As FbConnection = GetConnection()
Dim oTransaction As FbTransaction
If oConnection Is Nothing Then
Return False
End If
Try Try
oTransaction = oConnection.BeginTransaction() Dim oConnection As FbConnection = GetConnection()
If oConnection Is Nothing Then
Return False
End If
Dim oTransaction As FbTransaction = oConnection.BeginTransaction()
Dim oCommand As New FbCommand With { Dim oCommand As New FbCommand With {
.CommandText = SqlCommand, .CommandText = SqlCommand,
.Connection = oConnection, .Connection = oConnection,
@ -161,41 +160,29 @@ Public Class Firebird
End Try End Try
End Function End Function
''' <summary>
''' Executes a non-query command.
''' </summary>
''' <param name="SqlCommand">The command to execute</param>
''' <returns>True, if command was executed sucessfully. Otherwise false.</returns>
Public Function ExecuteNonQuery(SqlCommand As String) As Boolean
Dim oConnection As FbConnection = GetConnection()
Dim oScalarValue As Object = ExecuteNonQueryWithConnection(SqlCommand, oConnection)
oConnection.Close()
Return oScalarValue
End Function
''' <summary> ''' <summary>
''' Executes a sql query resulting in a scalar value. ''' Executes a sql query resulting in a scalar value.
''' </summary> ''' </summary>
''' <param name="SqlQuery">The query to execute</param> ''' <param name="SqlQuery">The query to execute</param>
''' <param name="Connection">The Firebird connection to use</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 Function GetScalarValueWithConnection(SqlQuery As String, Connection As FbConnection) As Object Public Function GetScalarValue(SqlQuery As String) As Object
Try Try
If Connection Is Nothing Then Dim oConnection As FbConnection = GetConnection()
If oConnection Is Nothing Then
Return Nothing Return Nothing
End If End If
Dim oTransaction As FbTransaction = Connection.BeginTransaction() Dim oTransaction As FbTransaction = oConnection.BeginTransaction()
Dim oCommand As New FbCommand With { Dim oCommand As New FbCommand With {
.CommandText = SqlQuery, .CommandText = SqlQuery,
.Connection = Connection, .Connection = oConnection,
.Transaction = oTransaction .Transaction = oTransaction
} }
Dim oResult As Object = oCommand.ExecuteScalar() Dim oResult As Object = oCommand.ExecuteScalar()
oTransaction.Commit() oTransaction.Commit()
oConnection.Close()
Return oResult Return oResult
Catch ex As Exception Catch ex As Exception
@ -204,59 +191,33 @@ Public Class Firebird
End Try End Try
End Function End Function
''' <summary>
''' Executes a sql query resulting in a scalar value.
''' </summary>
''' <param name="SqlQuery">The query to execute</param>
''' <returns>The scalar value if the command was executed successfully. Nothing otherwise.</returns>
Public Function GetScalarValue(SqlQuery As String) As Object
Dim oConnection As FbConnection = GetConnection()
Dim oScalarValue As Object = GetScalarValueWithConnection(SqlQuery, oConnection)
oConnection.Close()
Return oScalarValue
End Function
''' <summary>
''' Executes a sql query resulting in a table of values.
''' </summary>
''' <param name="SqlQuery">The query to execute</param>
''' <param name="Connection">The Firebird connection to use</param>
''' <returns>A datatable containing the results if the command was executed successfully. Nothing otherwise.</returns>
Public Function GetDatatableWithConnection(SqlQuery As String, Connection As FbConnection) As DataTable
Try
If Connection Is Nothing Then
Return Nothing
End If
Dim oCommand As New FbCommand With {
.CommandText = SqlQuery,
.Connection = Connection
}
Dim oAdapter As New FbDataAdapter(oCommand)
Dim oDatatable As New DataTable() With {.TableName = "DDRESULT"}
oAdapter.Fill(oDatatable)
Return oDatatable
Catch ex As Exception
_logger.Error(ex, $"Error in GetDatatableWithConnection while executing command: '{SqlQuery}'")
Throw ex
End Try
End Function
''' <summary> ''' <summary>
''' Executes a sql query resulting in a table of values. ''' Executes a sql query resulting in a table of values.
''' </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 Function GetDatatable(SqlQuery As String) As DataTable Public Function GetDatatable(SqlQuery As String) As DataTable
Dim oConnection As FbConnection = GetConnection() Try
Dim oDatatable As DataTable = GetDatatableWithConnection(SqlQuery, oConnection) Dim oConnection As FbConnection = GetConnection()
oConnection.Close() If oConnection Is Nothing Then
Return Nothing
End If
Return oDatatable Dim oCommand As New FbCommand With {
.CommandText = SqlQuery,
.Connection = oConnection
}
Dim oAdapter As New FbDataAdapter(oCommand)
Dim oDatatable As New DataTable()
oAdapter.Fill(oDatatable)
oConnection.Close()
Return oDatatable
Catch ex As Exception
_logger.Error(ex, $"Error in ReturnDatatable while executing command: '{SqlQuery}'")
Return Nothing
End Try
End Function End Function
End Class End Class

View File

@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
' übernehmen, indem Sie "*" eingeben: ' übernehmen, indem Sie "*" eingeben:
' <Assembly: AssemblyVersion("1.0.*")> ' <Assembly: AssemblyVersion("1.0.*")>
<Assembly: AssemblyVersion("0.0.0.3")> <Assembly: AssemblyVersion("0.0.0.2")>
<Assembly: AssemblyFileVersion("1.0.0.0")> <Assembly: AssemblyFileVersion("1.0.0.0")>