2020-08-25 10:43:38 +02:00

106 lines
3.9 KiB
VB.net

Imports System.Data.SqlClient
Public Class Database
Private _Config As ConfigManager(Of Config)
Private _LogConfig As LogConfig
Private _Logger As Logger
Private Const QUERY_TIMEOUT = 120000
Public Sub New(LogConfig As LogConfig, ConfigManager As ConfigManager(Of Config))
_LogConfig = LogConfig
_Logger = LogConfig.GetLogger()
_Config = ConfigManager
End Sub
Public Function LoadVendors() As DataTable
Return GetDatatable("SELECT C229, C003 FROM V050 WHERE C229 IS NOT NULL")
End Function
Public Function LoadVendorIdByCode(VendorCode As String) As String
Return GetScalarValue($"SELECT SUBSTRING(C000,0,6) C999 FROM T309 WHERE C001 = '{VendorCode}' AND C002 = 1")
End Function
Public Function LoadGroupsByVendor(VendorId As String) As DataTable
Return GetDatatable($"SELECT C001, SUBSTRING(C000,7,5) C999 FROM T309 WHERE C000 LIKE '{VendorId}%' AND C002 = 2 AND C001 LIKE '__ | %'")
End Function
Public Function LoadVersionsByVendorAndGroup(VendorId As String, GroupId As String) As DataTable
Return GetDatatable($"SELECT C001 FROM T309 WHERE C000 LIKE '{VendorId}-{GroupId}-%' AND C002 = 3 AND C001 LIKE '__ | %'")
End Function
#Region "Database-Access"
Private Function GetSQLConnection() As SqlConnection
Return GetSQLConnection(_Config.Config.ConnectionString)
End Function
Private Function GetSQLConnection(ConnectionString As String) As SqlConnection
Try
Dim oConnection As New SqlConnection(ConnectionString)
oConnection.Open()
Dim oMaskedConnectionString = MaskConnectionString(ConnectionString)
_Logger.Debug("The Following Connection is open: {0}", oMaskedConnectionString)
Return oConnection
Catch ex As Exception
_Logger.Error(ex)
Return Nothing
End Try
End Function
Private Function MaskConnectionString(ConnectionString As String) As String
Try
Dim oBuilder As New SqlConnectionStringBuilder() With {.ConnectionString = ConnectionString}
Dim oConnectionString = ConnectionString.Replace(oBuilder.Password, "XXXXX")
Return oConnectionString
Catch ex As Exception
_Logger.Error(ex)
Return "Invalid ConnectionString"
End Try
End Function
Public Function GetDatatable(SqlCommand As String) As DataTable
Try
_Logger.Debug("GetDatatable: Running Query [{0}]", SqlCommand)
Using oConnection = GetSQLConnection()
Using oSQLCOmmand = oConnection.CreateCommand()
oSQLCOmmand.CommandText = SqlCommand
oSQLCOmmand.CommandTimeout = QUERY_TIMEOUT
Dim dt As DataTable = New DataTable()
Dim oAdapter As SqlDataAdapter = New SqlDataAdapter(oSQLCOmmand)
oAdapter.Fill(dt)
Return dt
End Using
End Using
Catch ex As Exception
_Logger.Warn($"GetDatatable failed SQLCommand [{SqlCommand}] - ERROR: {ex.Message}")
Return Nothing
End Try
End Function
Public Function GetScalarValue(SQLCommand As String) As Object
Try
_Logger.Debug("GetScalarValue: Running Query [{0}]", SQLCommand)
Using oConnection As SqlConnection = GetSQLConnection()
Using oSQLCOmmand = oConnection.CreateCommand()
oSQLCOmmand.CommandText = SQLCommand
oSQLCOmmand.CommandTimeout = QUERY_TIMEOUT
Dim oResult As Object = oSQLCOmmand.ExecuteScalar()
Return oResult
End Using
End Using
Catch ex As Exception
_Logger.Warn($"GetScalarValue failed SQLCommand [{SQLCommand}] - ERROR: {ex.Message}")
Return Nothing
End Try
End Function
#End Region
End Class