Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text.RegularExpressions
Imports System.Windows.Forms
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Imports System.Runtime.InteropServices
Imports System.IO
Imports System.Text
Namespace SecureSQLtool
Partial Public Class Navigator
Inherits Form
Private applicationObject As DTE2
Private multiple_finder_replacer As MultipleFinderAndReplacer
Private texter As String
''' <summary>
''' Initializes a new instance of the FindAndReplaceForm class.
''' </summary>
Private Sub New()
InitializeComponent()
'Me.captureInfoList = New List(Of CaptureInfo)()
'Me.captureInfoHash = New Dictionary(Of String, CaptureInfo)()
'Me.settingsCache = New SettingsCache()
End Sub
''' <summary>
''' Initializes a new instance of the FindAndReplaceForm class.
''' </summary>
''' <param name="applicationObject">The application object.</param>
Public Sub New(ByVal applicationObject As DTE2)
Me.New()
Me.applicationObject = applicationObject
Me.multiple_finder_replacer = New MultipleFinderAndReplacer(Me.applicationObject)
End Sub
Private Sub Navigator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.multiple_finder_replacer.Pattern = "SQL\s=\s.+?\n.*?(objcommand.\.CommandText\s=\sSQL|Me\.SqlDataSource.\.SelectCommand\s=\sSQL|objcommand\.CommandText\s=\sSQL|Else|End\sIf|s\.SelectCommand\s=\sSQL)"
Me.multiple_finder_replacer.RegexOptions = RegexOptions.Singleline Or RegexOptions.IgnoreCase
Me.multiple_finder_replacer.FileTypes = "*.vb"
Me.multiple_finder_replacer.DisplayOnlyFileNames = True
Me.multiple_finder_replacer.IncludeSubDirectories = True
Me.multiple_finder_replacer.MatchContextBeforeLineCount = 0
Me.multiple_finder_replacer.MatchContextAfterLineCount = 0
Me.multiple_finder_replacer.KeepModifedFilesOpen = True
Me.multiple_finder_replacer.ContinueProcessing = True
End Sub
Private Sub findNextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles findNextButton.Click
Dim tmp As String = ""
Dim selection As TextSelection
Dim i As Integer
Dim SQL As String
Try
UpdateItemsToProcess()
Me.multiple_finder_replacer.FindNext()
selection = multiple_finder_replacer.finderAndReplacer.GetTextSelection
tmp = Regex.Replace(selection.Text, "objcommand.\.CommandText\s=\sSQL|Me\.SqlDataSource.\.SelectCommand\s=\sSQL|Session\(""sql""\)\s=\sSQL|Else|End\sIf|SQL\s=\s|SQL\s|Dim\s|objcommand\.CommandText\s=\sSQL|\t|\n|\r", "", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
SQL = tmp
tmp = Regex.Replace(tmp, "\s{2}", " ", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
tmp = Regex.Replace(tmp, "\x22[\s]*&[\s]*?\x22", "", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
tmp = tmp.Replace("s As SqlDataSource = CType(e.Row.FindControl(""SqlDataSource4""), SqlDataSource) s.SelectCommand = SQL", "")
tmp = tmp.Replace("s As SqlDataSource = CType(e.Row.FindControl(""SqlDataSource2""), SqlDataSource) s.SelectCommand = SQL", "")
tmp = tmp.Trim()
Me.RichTextBox1.Text = tmp
Catch ex As Exception
MessageBox.Show(ex.Message, "RegexFindAndReplace Error!")
Finally
End Try
Dim Matches As MatchCollection
Dim Reg As New Regex("\x22[\s]*?&.*?&[\s]*?\x22|'[\s]*?\x22[\s]*?&.*?&[\s]*?\x22[\s]*?'", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
Matches = Reg.Matches(tmp)
Dim ntmp As New StringBuilder
ntmp.AppendLine()
If Matcher(selection.Text, "objcommand.\.CommandText\s=\sSQL") Then
i = 1
Dim tp As String = Matchfound(selection.Text, "objcommand.")
For Each Match In Matches
Dim r As New Regex("\x22[\s]*?&|&[\s]*?\x22|'[\s]*?\x22[\s]*?&|&[\s]*?\x22[\s]*?'", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
tmp = tmp.Replace(Match.ToString, ":parameter" & i)
Dim law As String = r.Replace(Match.ToString, "")
law = law.Replace("'", "").Trim
ntmp.Append(" " & tp & ".Parameters.AddWithValue("":parameter" & i & """, " & law & ")")
ntmp.AppendLine()
i = i + 1
Next
If selection.Text.Contains("Session(""sql"") = SQL") Then
ntmp.AppendLine(" " & "Session(""sql"") = SQL")
End If
ntmp.Append(" " & Matchfound(selection.Text, "objcommand.\.CommandText\s=\sSQL"))
ElseIf Matcher(selection.Text, "Me\.SqlDataSource.\.SelectCommand\s=\sSQL") Then
i = 1
Dim tp As String = Matchfound(selection.Text, "Me\.SqlDataSource.")
For Each Match In Matches
Dim r As New Regex("\x22[\s]*?&|&[\s]*?\x22|'[\s]*?\x22[\s]*?&|&[\s]*?\x22[\s]*?'", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
tmp = tmp.Replace(Match.ToString, ":parameter" & i)
Dim law As String = r.Replace(Match.ToString, "")
law = law.Replace("'", "").Trim
ntmp.Append(" " & tp & ".SelectParameters.Add("":parameter" & i & """, " & law & ")")
ntmp.AppendLine()
i = i + 1
Next
If selection.Text.Contains("Session(""sql"") = SQL") Then
ntmp.AppendLine(" " & "Session(""sql"") = SQL")
End If
ntmp.Append(" " & Matchfound(selection.Text, "Me\.SqlDataSource.\.SelectCommand\s=\sSQL"))
ElseIf Matcher(selection.Text, "objcommand\.CommandText\s=\sSQL") Then
i = 1
For Each Match In Matches
Dim r As New Regex("\x22[\s]*?&|&[\s]*?\x22|'[\s]*?\x22[\s]*?&|&[\s]*?\x22[\s]*?'", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
tmp = tmp.Replace(Match.ToString, ":parameter" & i)
Dim law As String = r.Replace(Match.ToString, "")
law = law.Replace("'", "").Trim
ntmp.Append(" " & "objcommand.Parameters.AddWithValue("":parameter" & i & """, " & law & ")")
ntmp.AppendLine()
i = i + 1
Next
If selection.Text.Contains("Session(""sql"") = SQL") Then
ntmp.AppendLine(" " & "Session(""sql"") = SQL")
End If
ntmp.Append(" " & Matchfound(selection.Text, "objcommand\.CommandText\s=\sSQL"))
ElseIf Matcher(selection.Text, "s\.SelectCommand\s=\sSQL") Then
Dim tp As String = Matchfound(selection.Text, "\x22SqlDataSource.\x22")
ntmp.AppendLine(" " & "Dim s As SqlDataSource = CType(e.Row.FindControl(" & tp & "), SqlDataSource)")
i = 1
For Each Match In Matches
Dim r As New Regex("\x22[\s]*?&|&[\s]*?\x22|'[\s]*?\x22[\s]*?&|&[\s]*?\x22[\s]*?'", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
tmp = tmp.Replace(Match.ToString, ":parameter" & i)
Dim law As String = r.Replace(Match.ToString, "")
law = law.Replace("'", "").Trim
ntmp.Append(" " & "s.SelectParameters.Add("":parameter" & i & """, " & law & ")")
ntmp.AppendLine()
i = i + 1
Next
If selection.Text.Contains("Session(""sql"") = SQL") Then
ntmp.AppendLine(" " & "Session(""sql"") = SQL")
End If
ntmp.Append(" " & Matchfound(selection.Text, "s\.SelectCommand\s=\sSQL"))
ElseIf Matcher(selection.Text, "Else") Then
i = 1
For Each Match In Matches
Dim r As New Regex("\x22[\s]*?&|&[\s]*?\x22|'[\s]*?\x22[\s]*?&|&[\s]*?\x22[\s]*?'", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
tmp = tmp.Replace(Match.ToString, ":parameter" & i)
Dim law As String = r.Replace(Match.ToString, "")
law = law.Replace("'", "").Trim
ntmp.Append(" " & "Me.SqlDataSource1.SelectParameters.Add("":parameter" & i & """, " & law & ")")
ntmp.AppendLine()
i = i + 1
Next
If selection.Text.Contains("Session(""sql"") = SQL") Then
ntmp.AppendLine(" " & "Session(""sql"") = SQL")
End If
ntmp.Append("Else")
ElseIf Matcher(selection.Text, "End\sIf") Then
i = 1
For Each Match In Matches
Dim r As New Regex("\x22[\s]*?&|&[\s]*?\x22|'[\s]*?\x22[\s]*?&|&[\s]*?\x22[\s]*?'", RegexOptions.IgnoreCase Or RegexOptions.Singleline)
tmp = tmp.Replace(Match.ToString, ":parameter" & i)
Dim law As String = r.Replace(Match.ToString, "")
law = law.Replace("'", "").Trim
ntmp.Append(" " & "Me.SqlDataSource1.SelectParameters.Add("":parameter" & i & """, " & law & ")")
ntmp.AppendLine()
i = i + 1
Next
If selection.Text.Contains("Session(""sql"") = SQL") Then
ntmp.AppendLine(" " & "Session(""sql"") = SQL")
End If
ntmp.Append("End If")
End If
If Matcher(selection.Text, "SQL\s=\s") Then
tmp = "SQL = " & tmp
End If
texter = tmp & " " & ntmp.ToString
Me.RichTextBox2.Text = texter.Replace(" ", "")
Label1.Text = i & " parameters found in the query"
' try to get the text editor's font
Dim textEditorProperties As Properties = Me.applicationObject.Properties("FontsAndColors", "TextEditor")
Dim textEditorFontFamily As [Property] = textEditorProperties.Item("FontFamily")
Dim textEditorFontSize As [Property] = textEditorProperties.Item("FontSize")
' set the font of the pattern editors
Me.RichTextBox1.Font = New Font(textEditorFontFamily.Value.ToString(), Single.Parse(textEditorFontSize.Value.ToString()))
Me.RichTextBox2.Font = New Font(textEditorFontFamily.Value.ToString(), Single.Parse(textEditorFontSize.Value.ToString()))
End Sub
Private Sub UpdateItemsToProcess()
If Me.multiple_finder_replacer.ItemsToProcess.Count = 0 Then
Dim filenames As New List(Of String)()
' the MultipleFinderAndReplacer.ItemsToProcess property is a stack where the top always contains
' the current item to process. If that item is a directory, the MultipleFinderAndReplacer removes
' it from the top of the stack and adds the files in that directory to the stack. If the item is
' a EnvDTE.Project, the text files in that project will be added to the stack.
Try
If True Then
Dim project As Project = DirectCast(DirectCast(Me.applicationObject.ActiveSolutionProjects, Array).GetValue(0), Project)
Me.multiple_finder_replacer.ItemsToProcess.Push(project)
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error!")
End Try
End If
End Sub
Private Sub replaceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles replaceButton.Click
UpdateItemsToProcess()
Me.multiple_finder_replacer.ReplaceNext(texter)
Label1.Text = "Refracted successfully"
End Sub
Private Sub skipFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles skipFileButton.Click
If Me.multiple_finder_replacer.ItemsToProcess.Count > 0 Then
Me.multiple_finder_replacer.ItemsToProcess.Pop()
Else
UpdateItemsToProcess()
End If
Me.multiple_finder_replacer.FindNext()
End Sub
Private Sub stopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopButton.Click
multiple_finder_replacer.ContinueProcessing = False
End Sub
Private Sub closeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closeButton.Click
Me.Close()
End Sub
Private Function Matcher(ByVal text As String, ByVal pattern As String) As Boolean
Dim Matches As MatchCollection
Dim Reg As New Regex(pattern, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
Matches = Reg.Matches(text)
If Matches.Count > 0 Then
Return True
Else
Return False
End If
End Function
Private Function Matchfound(ByVal text As String, ByVal pattern As String) As String
Dim Matches As MatchCollection
Dim Reg As New Regex(pattern, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
Matches = Reg.Matches(text)
If Matches.Count > 0 Then
Return Matches.Item(0).ToString
Else
Return ""
End If
End Function
End Class
End Namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.