jj: Database Version 0.0.3 - finally

This commit is contained in:
Jonathan Jenne 2018-11-09 11:18:00 +01:00
parent c27cf5e8a0
commit 5748d37227
2 changed files with 81 additions and 42 deletions

View File

@ -5,9 +5,9 @@ Imports DigitalData.Modules.Logging
''' <summary> ''' <summary>
''' MODULE: Firebird ''' MODULE: Firebird
''' '''
''' VERSION: 0.0.0.2 ''' VERSION: 0.0.0.3
''' '''
''' DATE: 03.09.2018 ''' DATE: 08.11.2018
''' '''
''' DESCRIPTION: ''' DESCRIPTION:
''' '''
@ -17,15 +17,15 @@ Imports DigitalData.Modules.Logging
''' '''
''' FirebirdSql.Data.FirebirdClient, >= 6.0.0 ''' FirebirdSql.Data.FirebirdClient, >= 6.0.0
''' '''
''' PARAMETERS: LogFactory, NLog.LogFactory ''' PARAMETERS: LogConfig, DigitalData.Modules.Logging.LogConfig
''' 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 location of the Database in the format `127.0.0.1:E:\Path\To\Database.FDB`
'''
''' Database, String
''' The server where the database lives, for example 127.0.0.1 or dd-vmx09-vm03 ''' The server where the database lives, for example 127.0.0.1 or dd-vmx09-vm03
''' '''
''' Database, String
''' The location of the Database in the format `127.0.0.1:E:\Path\To\Database.FDB`
'''
''' User, String ''' User, String
''' The user name to connect as ''' The user name to connect as
''' '''
@ -52,23 +52,26 @@ 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 fi As New IO.FileInfo(oPath) Dim oFileInfo As New IO.FileInfo(oPath)
Return fi.Name Return oFileInfo.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>
@ -84,7 +87,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()
@ -98,15 +101,11 @@ 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
@ -134,16 +133,18 @@ 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 ExecuteNonQuery(SqlCommand As String) As Boolean Public Function ExecuteNonQueryWithConnection(SqlCommand As String, Connection As FbConnection) As Boolean
Dim oConnection As FbConnection = GetConnection()
Dim oTransaction As FbTransaction
If oConnection Is Nothing Then
Return False
End If
Try Try
Dim oConnection As FbConnection = GetConnection() oTransaction = oConnection.BeginTransaction()
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,
@ -160,29 +161,41 @@ 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 GetScalarValue(SqlQuery As String) As Object Public Function GetScalarValueWithConnection(SqlQuery As String, Connection As FbConnection) As Object
Try Try
Dim oConnection As FbConnection = GetConnection() If Connection Is Nothing Then
If oConnection Is Nothing Then
Return Nothing Return Nothing
End If End If
Dim oTransaction As FbTransaction = oConnection.BeginTransaction() Dim oTransaction As FbTransaction = Connection.BeginTransaction()
Dim oCommand As New FbCommand With { Dim oCommand As New FbCommand With {
.CommandText = SqlQuery, .CommandText = SqlQuery,
.Connection = oConnection, .Connection = Connection,
.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
@ -191,33 +204,59 @@ 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> ''' <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>
''' <param name="Connection">The Firebird connection to use</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 GetDatatableWithConnection(SqlQuery As String, Connection As FbConnection) As DataTable
Try Try
Dim oConnection As FbConnection = GetConnection() If Connection Is Nothing Then
If oConnection Is Nothing Then
Return Nothing Return Nothing
End If End If
Dim oCommand As New FbCommand With { Dim oCommand As New FbCommand With {
.CommandText = SqlQuery, .CommandText = SqlQuery,
.Connection = oConnection .Connection = Connection
} }
Dim oAdapter As New FbDataAdapter(oCommand) Dim oAdapter As New FbDataAdapter(oCommand)
Dim oDatatable As New DataTable() Dim oDatatable As New DataTable() With {.TableName = "DDRESULT"}
oAdapter.Fill(oDatatable) oAdapter.Fill(oDatatable)
oConnection.Close()
Return oDatatable Return oDatatable
Catch ex As Exception Catch ex As Exception
_logger.Error(ex, $"Error in ReturnDatatable while executing command: '{SqlQuery}'") _logger.Error(ex, $"Error in GetDatatableWithConnection while executing command: '{SqlQuery}'")
Return Nothing Throw ex
End Try End Try
End Function End Function
''' <summary>
''' Executes a sql query resulting in a table of values.
''' </summary>
''' <param name="SqlQuery">The query to execute</param>
''' <returns>A datatable containing the results if the command was executed successfully. Nothing otherwise.</returns>
Public Function GetDatatable(SqlQuery As String) As DataTable
Dim oConnection As FbConnection = GetConnection()
Dim oDatatable As DataTable = GetDatatableWithConnection(SqlQuery, oConnection)
oConnection.Close()
Return oDatatable
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.2")> <Assembly: AssemblyVersion("0.0.0.3")>
<Assembly: AssemblyFileVersion("1.0.0.0")> <Assembly: AssemblyFileVersion("1.0.0.0")>