72 lines
2.3 KiB
VB.net
72 lines
2.3 KiB
VB.net
Public Class Querybuilder
|
|
|
|
Public Shared Function Build(config As ConfigValues, db As DB)
|
|
Dim mainQuery As String
|
|
Dim companies As List(Of String)
|
|
Dim stringsToIgnore As New List(Of String)
|
|
|
|
' searchvalues ist entweder eine SQL Abfrage oder eine Semikolongetrennte Liste
|
|
If config.query.ToUpper.StartsWith("SELECT") Then
|
|
companies = GetLiveQueryResult(config.query, db)
|
|
Else
|
|
companies = New List(Of String)(config.query.Split(";"))
|
|
End If
|
|
|
|
If config.queryIgnore.Count > 0 And config.queryIgnore.Contains(";") Then
|
|
stringsToIgnore = New List(Of String)(config.queryIgnore.Split(";"))
|
|
End If
|
|
|
|
mainQuery = BuildMainQuery(companies)
|
|
mainQuery &= BuildIgnoreQuery(stringsToIgnore)
|
|
|
|
Return mainQuery
|
|
End Function
|
|
|
|
Public Shared Function BuildMainQuery(companies As List(Of String)) As String
|
|
Dim query As String = String.Empty
|
|
|
|
For Each company As String In companies
|
|
Dim querypart As String = String.Format("(INH=""{0}"" OR ANM=""{0}"")", company.Trim())
|
|
|
|
query &= querypart
|
|
|
|
' Bei allen bis auf das letzte Unternehmen einen OR Operator einfügen
|
|
If (companies.IndexOf(company) + 1) < companies.Count Then
|
|
query &= " OR "
|
|
End If
|
|
Next
|
|
|
|
Return query
|
|
End Function
|
|
|
|
Public Shared Function BuildIgnoreQuery(stringsToIgnore As List(Of String))
|
|
Dim query As String = String.Empty
|
|
|
|
For Each stringToIgnore In stringsToIgnore
|
|
Dim querypart As String = String.Format(" NOT (INH=""{0}"" OR ANM=""{0}"")", stringToIgnore)
|
|
|
|
query &= querypart
|
|
Next
|
|
|
|
Return query
|
|
End Function
|
|
|
|
Public Shared Function GetLiveQueryResult(query As String, db As DB) As List(Of String)
|
|
Dim result As New List(Of String)
|
|
Dim SQL As String = query
|
|
Dim DT As DataTable = db.QueryTable(SQL)
|
|
|
|
If DT.Rows.Count = 0 Then
|
|
Throw New Exception("SQL Abfrage lieferte ein leeres Ergebnis zurück: " & query)
|
|
End If
|
|
|
|
For Each row As DataRow In DT.Rows
|
|
Dim item As String = CStr(row.Item(0)).Trim()
|
|
result.Add(row.Item(0))
|
|
Next
|
|
|
|
Return result
|
|
End Function
|
|
|
|
End Class
|