Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging

Namespace Methods.Database.GetDatatable
    ''' <summary>
    ''' Database method for retrieving a Datatable from an SQL Command and a Connection Id.
    ''' </summary>
    Public Class GetDatatableMethod
        Inherits BaseMethod

        Public Sub New(pLogConfig As LogConfig, pDatabaseIDB As MSSQLServer, pDatabaseECM As MSSQLServer, pGlobalState As GlobalState)
            MyBase.New(pLogConfig, pDatabaseIDB, pDatabaseECM, pGlobalState)
        End Sub

        ''' <summary>
        ''' Returns a Datatable. If ConnectionId is empty, uses the configured DD_ECM Database.
        ''' </summary>
        Public Function Run(pData As GetDatatableRequest) As GetDatatableResponse
            Try
                Dim oDatatable As DataTable = Nothing

                Select Case pData.DatabaseType
                    Case DatabaseType.MSSQL
                        If pData.NamedDatabase = DatabaseName.None Then
                            oDatatable = GetDatatableByConnectionId(pData.SqlCommand, pData.ConnectionId)

                        ElseIf pData.NamedDatabase = DatabaseName.ECM Then
                            oDatatable = DatabaseECM.GetDatatable(pData.SqlCommand)

                        ElseIf pData.NamedDatabase = DatabaseName.IDB Then
                            oDatatable = DatabaseIDB.GetDatatable(pData.SqlCommand)

                        Else
                            LogAndThrow($"Unsupported Named Database supplied. SQL Command [{pData.SqlCommand}] was not executed!")

                        End If

                    Case Else
                        LogAndThrow($"Only MSSQL Server is supported at this time. SQL Command [{pData.SqlCommand}] was not executed!")

                End Select

                If oDatatable Is Nothing Then
                    LogAndThrow($"SQL Command did not return any results: [{pData.SqlCommand}]!")
                End If

                Return New GetDatatableResponse(oDatatable)
            Catch ex As Exception
                Logger.Warn("Error occurred while getting database table!")
                Logger.Error(ex)
                Return New GetDatatableResponse(ex)
            End Try
        End Function

        Private Function GetDatatableByConnectionId(pSQLCommand As String, pConnectionId As Integer) As DataTable
            If pConnectionId = 0 Then
                Return DatabaseECM.GetDatatable(pSQLCommand)
            Else
                Dim oConnectionString = DatabaseECM.Get_ConnectionStringforID(pConnectionId)
                If oConnectionString <> "" Then

                    Return DatabaseIDB.GetDatatableWithConnection(pSQLCommand, oConnectionString)
                Else
                    Logger.Warn("ConnectionId [{0}] could not be translated into ConnectionString. Exiting.")
                    Return Nothing
                End If
            End If
        End Function
    End Class

End Namespace