Imports DigitalData.Modules.Logging
Imports Oracle.ManagedDataAccess.Client

Public Class Oracle
    Public DBInitialized As Boolean = False
    Public CurrentOracleConnectionString As String = ""

    Private _Timeout As Integer
    Private _Logger As Logger

    Public Sub New(LogConfig As LogConfig, ConnectionString As String, Optional Timeout As Integer = 120)
        _Timeout = Timeout
        _Logger = LogConfig.GetLogger()

        CurrentOracleConnectionString = ConnectionString
        DBInitialized = TestCanConnect(CurrentOracleConnectionString)
    End Sub

    Public Sub New(LogConfig As LogConfig, Server As String, Database As String, UserId As String, Password As String, Optional Timeout As Integer = 120)
        _Timeout = Timeout
        _Logger = LogConfig.GetLogger()

        CurrentOracleConnectionString = GetConnectionString(Server, Database, UserId, Password)
        DBInitialized = TestCanConnect(CurrentOracleConnectionString)
    End Sub

    Private Function TestCanConnect(ConnectionString As String) As Boolean
        Try
            Dim oSQLconnect As New OracleConnection
            oSQLconnect.ConnectionString = ConnectionString
            oSQLconnect.Open()
            oSQLconnect.Close()
            Return True
        Catch ex As Exception
            _Logger.Error(ex)
            Return False
        End Try
    End Function

    Public Shared Function GetConnectionString(Server As String, Database As String, UserId As String, Password As String) As String
        Dim oConnectionString = $"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={Server})(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={Database})));User Id={UserId};Password={Password};"
        Return oConnectionString
    End Function

    ''' <summary>
    ''' Returns a datatable for a sql-statement 
    ''' </summary>
    ''' <param name="sqlcommand">sqlcommand for datatable (select XYZ from TableORView)</param>
    ''' <returns>Returns a datatable</returns>
    Public Function GetDatatable(sqlcommand As String) As DataTable
        Try
            Dim oSQLconnect As New OracleConnection
            Dim oSQLCOmmand As OracleCommand
            oSQLconnect.ConnectionString = CurrentOracleConnectionString
            oSQLconnect.Open()
            oSQLCOmmand = oSQLconnect.CreateCommand()
            oSQLCOmmand.CommandText = sqlcommand
            oSQLCOmmand.CommandTimeout = _Timeout
            Dim adapter1 As OracleDataAdapter = New OracleDataAdapter(oSQLCOmmand)
            Dim dt As DataTable = New DataTable()
            adapter1.Fill(dt)
            oSQLconnect.Close()
            Return dt
        Catch ex As Exception
            _Logger.Error(ex)
            _Logger.Debug("sqlcommand: " & sqlcommand)
            Return Nothing
        End Try
    End Function
    ''' <summary>
    ''' Executes the passed sql-statement 
    ''' </summary>
    ''' <param name="executeStatement">the sql statement</param>
    ''' <returns>Returns true if properly executed, else false</returns>
    Public Function NewExecutenonQuery(executeStatement As String) As Boolean
        Try
            Dim oSQLconnect As New OracleConnection
            Dim oSQLCOmmand As OracleCommand
            oSQLconnect.ConnectionString = CurrentOracleConnectionString
            oSQLconnect.Open()
            oSQLCOmmand = oSQLconnect.CreateCommand()
            oSQLCOmmand.CommandText = executeStatement
            oSQLCOmmand.CommandTimeout = _Timeout
            oSQLCOmmand.ExecuteNonQuery()
            oSQLCOmmand.Dispose()
            oSQLconnect.Close()
            Return True
        Catch ex As Exception
            _Logger.Error(ex)
            _Logger.Debug("executeStatement: " & executeStatement)
            Return False
        End Try
    End Function

    ''' <summary>
    ''' Executes the passed sql-statement as Scalar
    ''' </summary>
    ''' <param name="executeStatement">the sql statement</param>
    ''' <returns>Returns the scalarvalue</returns> 
    Public Function NewExecuteScalar(executeStatement As String)
        Dim result
        Try
            Dim oSQLconnect As New OracleConnection
            Dim oSQLCOmmand As OracleCommand
            oSQLconnect.ConnectionString = CurrentOracleConnectionString
            oSQLconnect.Open()
            oSQLCOmmand = oSQLconnect.CreateCommand()
            oSQLCOmmand.CommandText = executeStatement
            oSQLCOmmand.CommandTimeout = _Timeout
            result = oSQLCOmmand.ExecuteScalar()
            oSQLCOmmand.Dispose()
            oSQLconnect.Close()
            Return result
        Catch ex As Exception
            _Logger.Error(ex)
            _Logger.Debug("executeStatement: " & executeStatement)
            Throw ex
        End Try
    End Function
End Class
