Common: Apply distinct to sql placeholders

This commit is contained in:
Jonathan Jenne 2022-05-18 12:59:03 +02:00
parent 48830e3dd9
commit f0c27890a8
6 changed files with 50 additions and 41 deletions

View File

@ -38,11 +38,6 @@ Namespace SQLEditor
Name = pValue Name = pValue
[Module] = pModule [Module] = pModule
End Sub End Sub
Public Overrides Function Equals(obj As Object) As Boolean
Return DirectCast(obj, Placeholder).Module = [Module] And
DirectCast(obj, Placeholder).Name = Name
End Function
End Class End Class
End Namespace End Namespace

View File

@ -57,45 +57,45 @@ Namespace SQLEditor
End Sub End Sub
Private Function ParseTokens() As List(Of SyntaxHighlightToken) Private Function ParseTokens() As List(Of SyntaxHighlightToken)
Dim tokens As New List(Of SyntaxHighlightToken)() Dim oTokens As New List(Of SyntaxHighlightToken)()
Dim ranges() As DocumentRange = Nothing Dim oRanges As IEnumerable(Of DocumentRange) = Nothing
' search for quoted strings ' search for quoted strings
ranges = TryCast(document.FindAll(_quotedString).GetAsFrozen(), DocumentRange()) oRanges = document.FindAll(_quotedString).GetAsFrozen()
For i As Integer = 0 To ranges.Length - 1 For Each oRange In oRanges
tokens.Add(CreateToken(ranges(i).Start.ToInt(), ranges(i).End.ToInt(), Color.Red)) oTokens.Add(CreateToken(oRange.Start.ToInt, oRange.End.ToInt, Color.Red))
Next i Next
'Extract all keywords 'Extract all keywords
ranges = TryCast(document.FindAll(_keywords).GetAsFrozen(), DocumentRange()) oRanges = TryCast(document.FindAll(_keywords).GetAsFrozen(), DocumentRange())
For j As Integer = 0 To ranges.Length - 1 For Each oRange In oRanges
If Not IsRangeInTokens(ranges(j), tokens) Then If Not IsRangeInTokens(oRange, oTokens) Then
tokens.Add(CreateToken(ranges(j).Start.ToInt(), ranges(j).End.ToInt(), Color.Blue)) oTokens.Add(CreateToken(oRange.Start.ToInt(), oRange.End.ToInt(), Color.Blue))
End If End If
Next j Next
'Find all placeholders 'Find all placeholders
ranges = TryCast(document.FindAll(_placeholderString).GetAsFrozen(), DocumentRange()) oRanges = TryCast(document.FindAll(_placeholderString).GetAsFrozen(), DocumentRange())
For j As Integer = 0 To ranges.Length - 1 For Each oRange In oRanges
If Not IsRangeInTokens(ranges(j), tokens) Then If Not IsRangeInTokens(oRange, oTokens) Then
tokens.Add(CreateToken(ranges(j).Start.ToInt(), ranges(j).End.ToInt(), Color.DarkTurquoise)) oTokens.Add(CreateToken(oRange.Start.ToInt(), oRange.End.ToInt(), Color.DarkTurquoise))
End If End If
Next j Next
'Find all comments 'Find all comments
ranges = TryCast(document.FindAll(_commentedString).GetAsFrozen(), DocumentRange()) oRanges = TryCast(document.FindAll(_commentedString).GetAsFrozen(), DocumentRange())
For j As Integer = 0 To ranges.Length - 1 For Each oRange In oRanges
If Not IsRangeInTokens(ranges(j), tokens) Then If Not IsRangeInTokens(oRange, oTokens) Then
tokens.Add(CreateToken(ranges(j).Start.ToInt(), ranges(j).End.ToInt(), Color.Green)) oTokens.Add(CreateToken(oRange.Start.ToInt(), oRange.End.ToInt(), Color.Green))
End If End If
Next j Next
' order tokens by their start position ' order tokens by their start position
tokens.Sort(New SyntaxHighlightTokenComparer()) oTokens.Sort(New SyntaxHighlightTokenComparer())
' fill in gaps in document coverage ' fill in gaps in document coverage
tokens = CombineWithPlainTextTokens(tokens) oTokens = CombineWithPlainTextTokens(oTokens)
Return tokens Return oTokens
End Function End Function
'Parse the remaining text into tokens: 'Parse the remaining text into tokens:
@ -129,8 +129,9 @@ Namespace SQLEditor
'Create a token from the retrieved range and specify its forecolor 'Create a token from the retrieved range and specify its forecolor
Private Function CreateToken(ByVal start As Integer, ByVal [end] As Integer, ByVal foreColor As Color) As SyntaxHighlightToken Private Function CreateToken(ByVal start As Integer, ByVal [end] As Integer, ByVal foreColor As Color) As SyntaxHighlightToken
Dim properties As New SyntaxHighlightProperties() Dim properties As New SyntaxHighlightProperties With {
properties.ForeColor = foreColor .ForeColor = foreColor
}
Return New SyntaxHighlightToken(start, [end] - start, properties) Return New SyntaxHighlightToken(start, [end] - start, properties)
End Function End Function

View File

@ -39,6 +39,7 @@ Partial Class frmSQLEditor
Me.BarButtonItem3 = New DevExpress.XtraBars.BarButtonItem() Me.BarButtonItem3 = New DevExpress.XtraBars.BarButtonItem()
Me.BarButtonItem4 = New DevExpress.XtraBars.BarButtonItem() Me.BarButtonItem4 = New DevExpress.XtraBars.BarButtonItem()
Me.BarButtonItem5 = New DevExpress.XtraBars.BarButtonItem() Me.BarButtonItem5 = New DevExpress.XtraBars.BarButtonItem()
Me.BarButtonItem6 = New DevExpress.XtraBars.BarButtonItem()
Me.RibbonPage1 = New DevExpress.XtraBars.Ribbon.RibbonPage() Me.RibbonPage1 = New DevExpress.XtraBars.Ribbon.RibbonPage()
Me.RibbonPageGroup3 = New DevExpress.XtraBars.Ribbon.RibbonPageGroup() Me.RibbonPageGroup3 = New DevExpress.XtraBars.Ribbon.RibbonPageGroup()
Me.RibbonPageGroup1 = New DevExpress.XtraBars.Ribbon.RibbonPageGroup() Me.RibbonPageGroup1 = New DevExpress.XtraBars.Ribbon.RibbonPageGroup()
@ -50,7 +51,6 @@ Partial Class frmSQLEditor
Me.ViewPlaceholders = New DevExpress.XtraGrid.Views.Grid.GridView() Me.ViewPlaceholders = New DevExpress.XtraGrid.Views.Grid.GridView()
Me.colPattern = New DevExpress.XtraGrid.Columns.GridColumn() Me.colPattern = New DevExpress.XtraGrid.Columns.GridColumn()
Me.colValue = New DevExpress.XtraGrid.Columns.GridColumn() Me.colValue = New DevExpress.XtraGrid.Columns.GridColumn()
Me.BarButtonItem6 = New DevExpress.XtraBars.BarButtonItem()
CType(Me.RibbonControl1, System.ComponentModel.ISupportInitialize).BeginInit() CType(Me.RibbonControl1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.SvgImageCollection1, System.ComponentModel.ISupportInitialize).BeginInit() CType(Me.SvgImageCollection1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.SplitContainerControl1, System.ComponentModel.ISupportInitialize).BeginInit() CType(Me.SplitContainerControl1, System.ComponentModel.ISupportInitialize).BeginInit()
@ -65,6 +65,7 @@ Partial Class frmSQLEditor
' '
'RibbonControl1 'RibbonControl1
' '
Me.RibbonControl1.ColorScheme = DevExpress.XtraBars.Ribbon.RibbonControlColorScheme.Green
Me.RibbonControl1.ExpandCollapseItem.Id = 0 Me.RibbonControl1.ExpandCollapseItem.Id = 0
Me.RibbonControl1.Items.AddRange(New DevExpress.XtraBars.BarItem() {Me.RibbonControl1.ExpandCollapseItem, Me.RibbonControl1.SearchEditItem, Me.GalleryPlaceholders, Me.btnEditConnections, Me.btnSave, Me.btnExecuteSQL, Me.GalleryConnection, Me.chkClearPlaceholders, Me.btnClearPlaceholders, Me.chkShowPlaceholders, Me.BarButtonItem1, Me.BarButtonItem2, Me.BarButtonItem3, Me.BarButtonItem4, Me.BarButtonItem5, Me.BarButtonItem6}) Me.RibbonControl1.Items.AddRange(New DevExpress.XtraBars.BarItem() {Me.RibbonControl1.ExpandCollapseItem, Me.RibbonControl1.SearchEditItem, Me.GalleryPlaceholders, Me.btnEditConnections, Me.btnSave, Me.btnExecuteSQL, Me.GalleryConnection, Me.chkClearPlaceholders, Me.btnClearPlaceholders, Me.chkShowPlaceholders, Me.BarButtonItem1, Me.BarButtonItem2, Me.BarButtonItem3, Me.BarButtonItem4, Me.BarButtonItem5, Me.BarButtonItem6})
Me.RibbonControl1.Location = New System.Drawing.Point(0, 0) Me.RibbonControl1.Location = New System.Drawing.Point(0, 0)
@ -183,6 +184,13 @@ Partial Class frmSQLEditor
Me.BarButtonItem5.ImageOptions.SvgImage = Global.DigitalData.GUIs.Common.My.Resources.Resources.actions_database Me.BarButtonItem5.ImageOptions.SvgImage = Global.DigitalData.GUIs.Common.My.Resources.Resources.actions_database
Me.BarButtonItem5.Name = "BarButtonItem5" Me.BarButtonItem5.Name = "BarButtonItem5"
' '
'BarButtonItem6
'
Me.BarButtonItem6.Caption = "Abbrechen"
Me.BarButtonItem6.Id = 19
Me.BarButtonItem6.ImageOptions.SvgImage = CType(resources.GetObject("BarButtonItem6.ImageOptions.SvgImage"), DevExpress.Utils.Svg.SvgImage)
Me.BarButtonItem6.Name = "BarButtonItem6"
'
'RibbonPage1 'RibbonPage1
' '
Me.RibbonPage1.Groups.AddRange(New DevExpress.XtraBars.Ribbon.RibbonPageGroup() {Me.RibbonPageGroup3, Me.RibbonPageGroup1}) Me.RibbonPage1.Groups.AddRange(New DevExpress.XtraBars.Ribbon.RibbonPageGroup() {Me.RibbonPageGroup3, Me.RibbonPageGroup1})
@ -225,6 +233,7 @@ Partial Class frmSQLEditor
Me.txtSQLCommand.Location = New System.Drawing.Point(0, 0) Me.txtSQLCommand.Location = New System.Drawing.Point(0, 0)
Me.txtSQLCommand.MenuManager = Me.RibbonControl1 Me.txtSQLCommand.MenuManager = Me.RibbonControl1
Me.txtSQLCommand.Name = "txtSQLCommand" Me.txtSQLCommand.Name = "txtSQLCommand"
Me.txtSQLCommand.Options.CopyPaste.InsertOptions = DevExpress.XtraRichEdit.API.Native.InsertOptions.KeepTextOnly
Me.txtSQLCommand.Size = New System.Drawing.Size(802, 508) Me.txtSQLCommand.Size = New System.Drawing.Size(802, 508)
Me.txtSQLCommand.TabIndex = 2 Me.txtSQLCommand.TabIndex = 2
' '
@ -250,7 +259,7 @@ Partial Class frmSQLEditor
' '
'GridPlaceholders 'GridPlaceholders
' '
Me.GridPlaceholders.Dock = System.Windows.Forms.DockStyle.Fill Me.GridPlaceholders.Anchor = System.Windows.Forms.AnchorStyles.Right
Me.GridPlaceholders.Location = New System.Drawing.Point(0, 0) Me.GridPlaceholders.Location = New System.Drawing.Point(0, 0)
Me.GridPlaceholders.MainView = Me.ViewPlaceholders Me.GridPlaceholders.MainView = Me.ViewPlaceholders
Me.GridPlaceholders.MenuManager = Me.RibbonControl1 Me.GridPlaceholders.MenuManager = Me.RibbonControl1
@ -283,13 +292,6 @@ Partial Class frmSQLEditor
Me.colValue.Visible = True Me.colValue.Visible = True
Me.colValue.VisibleIndex = 1 Me.colValue.VisibleIndex = 1
' '
'BarButtonItem6
'
Me.BarButtonItem6.Caption = "Abbrechen"
Me.BarButtonItem6.Id = 19
Me.BarButtonItem6.ImageOptions.SvgImage = CType(resources.GetObject("BarButtonItem6.ImageOptions.SvgImage"), DevExpress.Utils.Svg.SvgImage)
Me.BarButtonItem6.Name = "BarButtonItem6"
'
'frmSQLEditor 'frmSQLEditor
' '
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)

View File

@ -136,4 +136,7 @@
LDQgIGw0LTRsMywzbC00LDRMMjMsMjB6IiBjbGFzcz0iUmVkIiAvPg0KPC9zdmc+Cw== LDQgIGw0LTRsMywzbC00LDRMMjMsMjB6IiBjbGFzcz0iUmVkIiAvPg0KPC9zdmc+Cw==
</value> </value>
</data> </data>
<metadata name="SvgImageCollection1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root> </root>

View File

@ -209,6 +209,7 @@ Public Class frmSQLEditor
' noop ' noop
Else Else
Dim oPlaceholders = oPatterns. Dim oPlaceholders = oPatterns.
Distinct().
Select(Function(pattern) New SQLEditor.Placeholder(pattern.Value, pattern.Value, pattern.Type, pattern.Value) With {.Pattern = pattern}). Select(Function(pattern) New SQLEditor.Placeholder(pattern.Value, pattern.Value, pattern.Type, pattern.Value) With {.Pattern = pattern}).
ToList() ToList()
GridPlaceholders.DataSource = oPlaceholders GridPlaceholders.DataSource = oPlaceholders
@ -232,7 +233,6 @@ Public Class frmSQLEditor
Throw New ApplicationException($"Der Platzhalter '{oWrapped}' wurde nicht ausgefüllt!") Throw New ApplicationException($"Der Platzhalter '{oWrapped}' wurde nicht ausgefüllt!")
End If End If
oSql = oSql.Replace(oWrapped, oPlaceholder.Value) oSql = oSql.Replace(oWrapped, oPlaceholder.Value)
Next Next
End If End If

View File

@ -10,4 +10,12 @@
Public Overrides Function ToString() As String Public Overrides Function ToString() As String
Return $"{{#{Type}#{Value}}}" Return $"{{#{Type}#{Value}}}"
End Function End Function
Public Overrides Function GetHashCode() As Integer
Return (Value.GetHashCode & Type.GetHashCode).GetHashCode
End Function
Public Overrides Function Equals(obj As Object) As Boolean
Return Me.GetHashCode = DirectCast(obj, Pattern).GetHashCode
End Function
End Class End Class