WIP: EDM Designer, Create Tables
This commit is contained in:
@@ -1,55 +1,102 @@
|
||||
Imports FirebirdSql.Data.FirebirdClient
|
||||
Public Class Firebird
|
||||
Private Shared Logger As NLog.Logger = NLog.LogManager.GetCurrentClassLogger
|
||||
Public DBInitialized As Boolean = False
|
||||
Private _connectionEstablished As Boolean = False
|
||||
Private _connectionFailed As Boolean = False
|
||||
Private ReadOnly dataSource As String
|
||||
Private ReadOnly database As String
|
||||
Private ReadOnly user As String
|
||||
Private ReadOnly password As String
|
||||
Public CurrentFBDConnectionString As String = ""
|
||||
Public ReadOnly Property ConnectionString As String
|
||||
Public ReadOnly Property ConnectionEstablished As Boolean
|
||||
Get
|
||||
Return _connectionEstablished
|
||||
End Get
|
||||
End Property
|
||||
Imports NLog
|
||||
Imports FirebirdSql.Data.FirebirdClient
|
||||
|
||||
|
||||
''' <summary>
|
||||
''' MODULE: Firebird
|
||||
'''
|
||||
''' VERSION: 0.0.0.1
|
||||
'''
|
||||
''' DATE: 03.09.2018
|
||||
'''
|
||||
''' DESCRIPTION:
|
||||
'''
|
||||
''' DEPENDENCIES: NLog, >= 4.5.8
|
||||
'''
|
||||
''' EntityFramework.Firebird, >= 6.1.0
|
||||
'''
|
||||
''' FirebirdSql.Data.FirebirdClient, >= 6.0.0
|
||||
'''
|
||||
''' PARAMETERS: LogFactory, NLog.LogFactory
|
||||
''' The LogFactory containing the current log config. Used to instanciate the class logger for this and any dependent class
|
||||
'''
|
||||
''' 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
|
||||
'''
|
||||
''' User, String
|
||||
''' The user name to connect as
|
||||
'''
|
||||
''' Password, String
|
||||
''' The user's password
|
||||
'''
|
||||
''' PROPERTIES: ConnectionEstablished, Boolean
|
||||
''' If the last opened connection was successful
|
||||
'''
|
||||
''' ConnectionFailed, Boolean
|
||||
''' If the last opened connection failed
|
||||
'''
|
||||
''' ConnectionString, String
|
||||
''' The used connectionstring
|
||||
'''
|
||||
''' EXAMPLES:
|
||||
'''
|
||||
''' REMARKS: If the connection fails due to "wrong username or password", the cause might be that the server harddrive is full..
|
||||
''' </summary>
|
||||
Public Class Firebird
|
||||
Private _logger As Logger
|
||||
Private _connectionServer As String
|
||||
Private _connectionDatabase As String
|
||||
Private _connectionUsername As String
|
||||
Private _connectionPassword As String
|
||||
|
||||
Public ReadOnly Property ConnectionEstablished As Boolean
|
||||
Public ReadOnly Property ConnectionFailed As Boolean
|
||||
Get
|
||||
Return _connectionFailed
|
||||
End Get
|
||||
End Property
|
||||
Public Sub New(dataSource As String, database As String, user As String, password As String)
|
||||
ConnectionString = BuildConnectionString(dataSource, database, user, password)
|
||||
' Save connection credentials
|
||||
dataSource = dataSource
|
||||
database = database
|
||||
user = user
|
||||
password = password
|
||||
Public ReadOnly Property ConnectionString As String
|
||||
|
||||
''' <summary>
|
||||
'''
|
||||
''' </summary>
|
||||
''' <param name="LogFactory"></param>
|
||||
''' <param name="Datasource"></param>
|
||||
''' <param name="Database"></param>
|
||||
''' <param name="User"></param>
|
||||
''' <param name="Password"></param>
|
||||
''' <exception cref="Exceptions.DatabaseException"></exception>
|
||||
Public Sub New(LogFactory As LogFactory, Datasource As String, Database As String, User As String, Password As String)
|
||||
_logger = LogFactory.GetCurrentClassLogger()
|
||||
|
||||
' Test the connection first
|
||||
Dim conn = Connect(ConnectionString)
|
||||
Dim oConnectionString = GetConnectionString(Datasource, Database, User, Password)
|
||||
Dim oConnection = GetConnection(oConnectionString)
|
||||
' If initial connection was successfully, close it
|
||||
conn?.Close()
|
||||
oConnection?.Close()
|
||||
|
||||
If oConnection Is Nothing Then
|
||||
Throw New Exceptions.DatabaseException()
|
||||
End If
|
||||
|
||||
_connectionServer = Datasource
|
||||
_connectionDatabase = Database
|
||||
_connectionUsername = User
|
||||
_connectionPassword = Password
|
||||
_ConnectionString = oConnectionString
|
||||
End Sub
|
||||
|
||||
Private Function Connect(ConnectionString As String) As FbConnection
|
||||
Private Function GetConnection(ConnectionString As String) As FbConnection
|
||||
Try
|
||||
Dim conn = New FbConnection(ConnectionString)
|
||||
conn.Open()
|
||||
_connectionEstablished = True
|
||||
_connectionFailed = False
|
||||
Logger.Debug("Connection established!")
|
||||
Logger.Debug($"User: {user}")
|
||||
Logger.Debug($"DatabaseLocation: {database}")
|
||||
Logger.Debug($"DatabaseServer: {dataSource}")
|
||||
Dim oConnection = New FbConnection(ConnectionString)
|
||||
oConnection.Open()
|
||||
_ConnectionEstablished = True
|
||||
_ConnectionFailed = False
|
||||
|
||||
Return conn
|
||||
Return oConnection
|
||||
Catch ex As Exception
|
||||
_connectionFailed = True
|
||||
_connectionEstablished = False
|
||||
Logger.Error(ex)
|
||||
_ConnectionFailed = True
|
||||
_ConnectionEstablished = False
|
||||
_logger.Error(ex)
|
||||
|
||||
Return Nothing
|
||||
End Try
|
||||
@@ -58,48 +105,46 @@ Public Class Firebird
|
||||
''' <summary>
|
||||
''' Builds a connectionstring from the provided arguments.
|
||||
''' </summary>
|
||||
''' <param name="dataSource">The database server where to connect to</param>
|
||||
''' <param name="database">The datasource, eg. the path of the FDB-file</param>
|
||||
''' <param name="user">The user used to connect to the database</param>
|
||||
''' <param name="password">The password of the connecting user</param>
|
||||
''' <param name="DataSource">The database server where to connect to</param>
|
||||
''' <param name="Database">The datasource, eg. the path of the FDB-file</param>
|
||||
''' <param name="User">The user used to connect to the database</param>
|
||||
''' <param name="Password">The password of the connecting user</param>
|
||||
''' <returns>A connectionstring</returns>
|
||||
Private Function BuildConnectionString(dataSource As String, database As String, user As String, password As String) As String
|
||||
Dim connectionStringBuilder = New FbConnectionStringBuilder With {
|
||||
.DataSource = dataSource,
|
||||
.Database = database,
|
||||
.UserID = user,
|
||||
.Password = password
|
||||
}
|
||||
Return connectionStringBuilder.ConnectionString
|
||||
Private Function GetConnectionString(DataSource As String, Database As String, User As String, Password As String) As String
|
||||
Return New FbConnectionStringBuilder With {
|
||||
.DataSource = DataSource,
|
||||
.Database = Database,
|
||||
.UserID = User,
|
||||
.Password = Password
|
||||
}.ToString()
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Executes a non-query command.
|
||||
''' </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>
|
||||
Public Function NewExecuteNonQuery(sqlCommand As String) As Boolean
|
||||
Public Function ExecuteNonQuery(SqlCommand As String) As Boolean
|
||||
Try
|
||||
Dim conn As FbConnection = Connect(ConnectionString)
|
||||
Dim oConnection As FbConnection = GetConnection(ConnectionString)
|
||||
|
||||
If conn Is Nothing Then
|
||||
If oConnection Is Nothing Then
|
||||
Return False
|
||||
End If
|
||||
|
||||
Dim transaction As FbTransaction = conn.BeginTransaction()
|
||||
Dim command As New FbCommand With {
|
||||
.CommandText = sqlCommand,
|
||||
.Connection = conn,
|
||||
.Transaction = transaction
|
||||
Dim oTransaction As FbTransaction = oConnection.BeginTransaction()
|
||||
Dim oCommand As New FbCommand With {
|
||||
.CommandText = SqlCommand,
|
||||
.Connection = oConnection,
|
||||
.Transaction = oTransaction
|
||||
}
|
||||
command.ExecuteNonQuery()
|
||||
|
||||
transaction.Commit()
|
||||
conn.Close()
|
||||
oCommand.ExecuteNonQuery()
|
||||
oTransaction.Commit()
|
||||
oConnection.Close()
|
||||
|
||||
Return True
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex, $"Error in ExecuteNonQuery while executing command: '{sqlCommand}'")
|
||||
_logger.Error(ex, $"Error in ExecuteNonQuery while executing command: '{SqlCommand}'")
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
@@ -107,30 +152,30 @@ Public Class Firebird
|
||||
''' <summary>
|
||||
''' Executes a sql query resulting in a scalar value.
|
||||
''' </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>
|
||||
Public Function GetExecuteScalar(sqlQuery As String) As Object
|
||||
Public Function GetScalarValue(SqlQuery As String) As Object
|
||||
Try
|
||||
Dim conn As FbConnection = Connect(ConnectionString)
|
||||
Dim oConnection As FbConnection = GetConnection(ConnectionString)
|
||||
|
||||
If conn Is Nothing Then
|
||||
If oConnection Is Nothing Then
|
||||
Return Nothing
|
||||
End If
|
||||
|
||||
Dim transaction As FbTransaction = conn.BeginTransaction()
|
||||
Dim command As New FbCommand With {
|
||||
.CommandText = sqlQuery,
|
||||
.Connection = conn,
|
||||
.Transaction = transaction
|
||||
Dim oTransaction As FbTransaction = oConnection.BeginTransaction()
|
||||
Dim oCommand As New FbCommand With {
|
||||
.CommandText = SqlQuery,
|
||||
.Connection = oConnection,
|
||||
.Transaction = oTransaction
|
||||
}
|
||||
Dim result As Object = command.ExecuteScalar()
|
||||
Dim oResult As Object = oCommand.ExecuteScalar()
|
||||
|
||||
transaction.Commit()
|
||||
conn.Close()
|
||||
oTransaction.Commit()
|
||||
oConnection.Close()
|
||||
|
||||
Return result
|
||||
Return oResult
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex, $"Error in ReturnScalar while executing command: '{sqlQuery}'")
|
||||
_logger.Error(ex, $"Error in ReturnScalar while executing command: '{SqlQuery}'")
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
@@ -138,29 +183,29 @@ Public Class Firebird
|
||||
''' <summary>
|
||||
''' Executes a sql query resulting in a table of values.
|
||||
''' </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>
|
||||
Public Function GetDatatable(sqlQuery As String) As DataTable
|
||||
Public Function GetDatatable(SqlQuery As String) As DataTable
|
||||
Try
|
||||
Dim conn As FbConnection = Connect(ConnectionString)
|
||||
Dim oConnection As FbConnection = GetConnection(ConnectionString)
|
||||
|
||||
If conn Is Nothing Then
|
||||
If oConnection Is Nothing Then
|
||||
Return Nothing
|
||||
End If
|
||||
|
||||
Dim command As New FbCommand With {
|
||||
.CommandText = sqlQuery,
|
||||
.Connection = conn
|
||||
Dim oCommand As New FbCommand With {
|
||||
.CommandText = SqlQuery,
|
||||
.Connection = oConnection
|
||||
}
|
||||
Dim adapter As New FbDataAdapter(command)
|
||||
Dim dt As New DataTable()
|
||||
Dim oAdapter As New FbDataAdapter(oCommand)
|
||||
Dim oDatatable As New DataTable()
|
||||
|
||||
adapter.Fill(dt)
|
||||
conn.Close()
|
||||
oAdapter.Fill(oDatatable)
|
||||
oConnection.Close()
|
||||
|
||||
Return dt
|
||||
Return oDatatable
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex, $"Error in ReturnDatatable while executing command: '{sqlQuery}'")
|
||||
_logger.Error(ex, $"Error in ReturnDatatable while executing command: '{SqlQuery}'")
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Reference in New Issue
Block a user