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

Namespace Methods.Database.GetScalarValue
    ''' <summary>
    ''' Database method for retrieving a Single Value from an SQL Command and a Connection Id.
    ''' </summary>
    Public Class GetScalarValueMethod
        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 Single Value. If ConnectionId is empty, uses the configured DD_ECM Database.
        ''' </summary>
        Public Function Run(pData As GetScalarValueRequest) As GetScalarValueResponse
            Try
                Dim oResult As Object = Nothing

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

                        ElseIf pData.NamedDatabase = DatabaseName.ECM Then
                            oResult = DatabaseECM.GetScalarValue(pData.SqlCommand)

                        ElseIf pData.NamedDatabase = DatabaseName.IDB Then
                            oResult = DatabaseIDB.GetScalarValue(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

                ' Remove this in the future, empty return values for scalar should be allowed
                'If oResult Is Nothing Then
                '    LogAndThrow($"SQL Command did not return any results: [{pData.SqlCommand}]!")
                'End If

                Return New GetScalarValueResponse(oResult)
            Catch ex As Exception
                Logger.Warn("Error occurred while getting database scalar value!")
                Logger.Error(ex)
                Return New GetScalarValueResponse(ex)
            End Try
        End Function

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

                    Return DatabaseIDB.GetScalarValueWithConnection(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