2022-01-18 13:26:12 +01:00

67 lines
2.8 KiB
VB.net

Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Namespace Methods.Database.ExecuteNonQuery
''' <summary>
''' Database method for executing a query from the supplied SQL Command and a Connection Id.
''' </summary>
Public Class ExecuteNonQueryMethod
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>
''' Executes a Query. If ConnectionId is empty, uses the configured DD_ECM Database.
''' </summary>
Public Function Run(pData As ExecuteNonQueryRequest) As ExecuteNonQueryResponse
Try
Dim oResult As Boolean = False
Select Case pData.DatabaseType
Case DatabaseType.MSSQL
If pData.NamedDatabase = DatabaseName.None Then
oResult = ExecuteNonQueryByConnectionId(pData.SqlCommand, pData.ConnectionId)
ElseIf pData.NamedDatabase = DatabaseName.ECM Then
oResult = DatabaseECM.ExecuteNonQuery(pData.SqlCommand)
ElseIf pData.NamedDatabase = DatabaseName.IDB Then
oResult = DatabaseIDB.ExecuteNonQuery(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
Return New ExecuteNonQueryResponse(oResult)
Catch ex As Exception
Logger.Warn("Error occurred while getting database scalar value!")
Logger.Error(ex)
Return New ExecuteNonQueryResponse(ex)
End Try
End Function
Private Function ExecuteNonQueryByConnectionId(pSQLCommand As String, pConnectionId As Integer) As Boolean
If pConnectionId = 0 Then
Return DatabaseECM.ExecuteNonQuery(pSQLCommand)
Else
Dim oConnectionString = DatabaseECM.Get_ConnectionStringforID(pConnectionId)
If oConnectionString <> "" Then
Return DatabaseIDB.ExecuteNonQueryWithConnection(pSQLCommand, oConnectionString)
Else
Logger.Warn("ConnectionId [{0}] could not be translated into ConnectionString. Exiting.")
Return False
End If
End If
End Function
End Class
End Namespace