Monorepo/Controls.SQLEditor/frmSQLEditor.vb
2022-05-05 16:34:56 +02:00

193 lines
7.1 KiB
VB.net

Imports DevExpress.XtraBars.Ribbon
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.XtraRichEdit.Services
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Patterns
Public Class frmSQLEditor
Private ReadOnly Patterns As ClassPatterns
Private ReadOnly LogConfig As LogConfig
Private ReadOnly Database As MSSQLServer
Private ReadOnly Placeholders As Placeholders
Private ClearPlaceholdersAfterSuccessfulExecute As Boolean = False
Private FormLoading As Boolean = False
Public Enum PlaceholderCollection
Globix
Zooflow
End Enum
Public Sub SetPlaceholders(pCollection As PlaceholderCollection)
End Sub
Public Sub New(pLogConfig As LogConfig, pDatabase As MSSQLServer)
' Dieser Aufruf ist für den Designer erforderlich.
InitializeComponent()
' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
LogConfig = pLogConfig
Database = pDatabase
Patterns = New ClassPatterns(LogConfig)
Placeholders = New Placeholders()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FormLoading = True
Try
Dim oConnectionGroups = LoadConnections()
RibbonGalleryBarItem2.Gallery.Groups.AddRange(oConnectionGroups.ToArray)
Dim oPlaceholderGroups = LoadPlaceholders()
RibbonGalleryBarItem1.Gallery.Groups.AddRange(oPlaceholderGroups.ToArray)
ConfigureRichEditControl()
chkClearPlaceholders.Checked = ClearPlaceholdersAfterSuccessfulExecute
Catch ex As Exception
Finally
FormLoading = False
End Try
End Sub
Private Sub RibbonGalleryBarItem1_GalleryItemClick(sender As Object, e As GalleryItemClickEventArgs) Handles RibbonGalleryBarItem1.GalleryItemClick
Dim oPlaceholder As Placeholder = e.Item.Tag
Dim pPosition = RichEditControl1.Document.CaretPosition
RichEditControl1.Document.InsertSingleLineText(pPosition, Patterns.WrapPatternValue(oPlaceholder.Module, oPlaceholder.Name))
End Sub
Private Function LoadPlaceholders() As List(Of GalleryItemGroup)
Dim oInternalPlaceholders = Placeholders.GetInternalGroup()
Dim oUserPlaceholder = Placeholders.GetUserGroup()
Return New List(Of GalleryItemGroup)() From {
oInternalPlaceholders,
oUserPlaceholder
}
End Function
Private Function LoadConnections() As List(Of GalleryItemGroup)
Try
Dim oSql = "SELECT GUID, Bezeichnung FROM [DD_ECM].[dbo].[TBDD_CONNECTION] WHERE AKTIV = 1"
Dim oTable = Database.GetDatatable(oSql)
Dim oConnections = New List(Of Connection)
For Each oRow As DataRow In oTable.Rows
oConnections.Add(New Connection() With {
.Id = oRow.Item("GUID"),
.Name = oRow.Item("Bezeichnung")})
Next
Dim oGroup1 = New GalleryItemGroup() With {.Caption = "Verbindungen"}
Dim oItems As New List(Of GalleryItem)
For Each oConnection In oConnections
oItems.Add(GetGalleryItem(oConnection))
Next
oGroup1.Items.AddRange(oItems.ToArray)
Dim oConnectionGroups = New List(Of GalleryItemGroup)() From {oGroup1}
Return oConnectionGroups
Catch ex As Exception
Return Nothing
End Try
End Function
Private Sub ConfigureRichEditControl()
RichEditControl1.Options.Search.RegExResultMaxGuaranteedLength = 500
RichEditControl1.ReplaceService(Of ISyntaxHighlightService)(New SQLSyntaxHighlightService(RichEditControl1.Document))
RichEditControl1.ActiveViewType = DevExpress.XtraRichEdit.RichEditViewType.Simple
RichEditControl1.Document.Sections(0).Page.Width = DevExpress.Office.Utils.Units.InchesToDocumentsF(80.0F)
RichEditControl1.Document.DefaultCharacterProperties.FontName = "Courier New"
RichEditControl1.Document.DefaultCharacterProperties.FontSize = 12
End Sub
Private Sub ClearPlaceholders()
GridPlaceholders.DataSource = New List(Of Placeholder)
End Sub
Private Function GetGalleryItem(pConnection As Connection) As GalleryItem
Dim oItem = New GalleryItem(Nothing, pConnection.Name, Nothing) With {
.Tag = pConnection
}
oItem.ImageOptions.SvgImage = My.Resources.actions_database
Return oItem
End Function
Private Property LastPatterns As New List(Of Pattern)
Private Sub RichEditControl1_ContentChanged(sender As Object, e As EventArgs) Handles RichEditControl1.ContentChanged
Dim oSqlText = RichEditControl1.Document.Text
Dim oPatterns = Patterns.GetAllPatterns(oSqlText)
If oPatterns.Count = 0 Then
Exit Sub
End If
If oPatterns.SequenceEqual(LastPatterns) Then
Exit Sub
End If
GridPlaceholders.DataSource = oPatterns.Select(Function(pattern)
Return New Placeholder(pattern.Value, pattern.Value, pattern.Type, pattern.Value) With {.Pattern = pattern}
End Function).ToList()
LastPatterns = oPatterns
End Sub
Private Async Sub BarButtonItem3_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnExecuteSQL.ItemClick
Try
Dim oSql = RichEditControl1.Document.Text
Dim oPlaceholders As List(Of Placeholder) = GridPlaceholders.DataSource
If oPlaceholders IsNot Nothing Then
For Each oPlaceholder In oPlaceholders
Dim oWrapped = Patterns.WrapPatternValue(oPlaceholder.Module, oPlaceholder.Name)
If oPlaceholder.Value Is Nothing Then
Throw New ApplicationException($"Der Platzhalter '{oWrapped}' wurde nicht ausgefüllt!")
End If
oSql = oSql.Replace(oWrapped, oPlaceholder.Value)
Next
End If
Dim oDatatable = Await Database.GetDatatableAsync(oSql)
Dim oForm As New frmSQLResult(oDatatable)
oForm.Show()
Catch ex As ApplicationException
MsgBox(ex.Message, MsgBoxStyle.Exclamation, Text)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
End Try
End Sub
Private Class Connection
Public Property Id As Integer
Public Property Name As String
End Class
Private Sub BarCheckItem1_CheckedChanged(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles chkClearPlaceholders.CheckedChanged
If FormLoading = False Then
ClearPlaceholdersAfterSuccessfulExecute = chkClearPlaceholders.Checked
End If
End Sub
Private Sub btnClearPlaceholders_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnClearPlaceholders.ItemClick
ClearPlaceholders()
End Sub
End Class