2019-11-27 17:54:54 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
Imports System
|
|
|
|
|
Imports System.Collections.Generic
|
2019-11-27 19:25:00 -08:00
|
|
|
Imports System.Drawing
|
2019-12-01 14:23:48 -08:00
|
|
|
Imports System.Drawing.Imaging
|
2019-11-27 17:54:54 -08:00
|
|
|
Imports System.IO
|
2019-11-27 19:25:00 -08:00
|
|
|
Imports System.Windows.Controls
|
2019-12-01 12:19:10 -08:00
|
|
|
Imports System.Windows.Media.Imaging
|
2019-11-27 17:54:54 -08:00
|
|
|
Imports EnvDTE
|
|
|
|
|
Imports Microsoft.SqlServer.TransactSql.ScriptDom
|
|
|
|
|
Imports Microsoft.VisualStudio.Shell
|
|
|
|
|
'''<summary>
|
|
|
|
|
''' Interaction logic for ToolWindow1Control.xaml
|
|
|
|
|
'''</summary>
|
|
|
|
|
Partial Public Class ToolWindow1Control
|
|
|
|
|
Inherits System.Windows.Controls.UserControl
|
|
|
|
|
|
|
|
|
|
Public Sub New()
|
|
|
|
|
|
|
|
|
|
' This call is required by the designer.
|
|
|
|
|
InitializeComponent()
|
|
|
|
|
|
2019-12-01 12:19:10 -08:00
|
|
|
AddFiles(False)
|
2019-12-01 14:23:48 -08:00
|
|
|
|
|
|
|
|
MenuSettings.Icon = GetImage("settings")
|
|
|
|
|
'MenuFormat.Icon = GetImage("magic_wand")
|
|
|
|
|
|
2019-11-27 17:54:54 -08:00
|
|
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
|
|
|
|
2019-12-01 12:19:10 -08:00
|
|
|
Function AddFiles(Optional Interactive As Boolean = True)
|
|
|
|
|
|
|
|
|
|
FileMenuTemplates.Items.Clear()
|
2019-11-27 17:54:54 -08:00
|
|
|
|
2019-12-01 12:19:10 -08:00
|
|
|
Dim Folder = SettingManager.GetTemplatesFolder()
|
2019-11-27 17:54:54 -08:00
|
|
|
|
2019-11-27 19:25:00 -08:00
|
|
|
If My.Computer.FileSystem.DirectoryExists(Folder) Then
|
2019-11-27 17:54:54 -08:00
|
|
|
|
2019-11-27 19:25:00 -08:00
|
|
|
Dim i As Integer = 1
|
2019-11-27 17:54:54 -08:00
|
|
|
|
2019-11-27 19:25:00 -08:00
|
|
|
CreateCommands(FileMenuTemplates, Folder, i)
|
|
|
|
|
|
2019-12-01 12:19:10 -08:00
|
|
|
Else
|
|
|
|
|
If Interactive Then
|
|
|
|
|
System.Windows.MessageBox.Show("Folder '" + Folder + "' doesn't exist!")
|
|
|
|
|
End If
|
2019-11-27 19:25:00 -08:00
|
|
|
End If
|
2019-11-27 17:54:54 -08:00
|
|
|
|
2019-12-01 12:19:10 -08:00
|
|
|
Dim mi = New MenuItem
|
|
|
|
|
mi.Header = "Refresh This List"
|
2019-12-01 14:23:48 -08:00
|
|
|
mi.Icon = GetImage("refresh")
|
2019-12-01 12:19:10 -08:00
|
|
|
|
|
|
|
|
AddHandler mi.Click, AddressOf buttonRefresh_Click
|
|
|
|
|
|
|
|
|
|
FileMenuTemplates.Items.Add(mi)
|
|
|
|
|
|
2019-11-27 17:54:54 -08:00
|
|
|
End Function
|
|
|
|
|
|
2019-11-27 19:25:00 -08:00
|
|
|
Function CreateCommands(MenuItem As MenuItem, Folder As String, ByRef i As Integer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Dim Dirs = My.Computer.FileSystem.GetDirectories(Folder)
|
|
|
|
|
For Each DirStr In Dirs
|
|
|
|
|
|
|
|
|
|
Dim DI = My.Computer.FileSystem.GetFileInfo(DirStr)
|
|
|
|
|
|
|
|
|
|
Dim mi = New MenuItem
|
|
|
|
|
mi.Header = DI.Name
|
2019-12-01 14:23:48 -08:00
|
|
|
mi.Icon = GetImage("folder")
|
2019-11-27 19:25:00 -08:00
|
|
|
|
|
|
|
|
MenuItem.Items.Add(mi)
|
|
|
|
|
|
|
|
|
|
CreateCommands(mi, Path.Combine(Folder, DirStr), i)
|
|
|
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
Next
|
|
|
|
|
|
|
|
|
|
Dim Files = My.Computer.FileSystem.GetFiles(Folder)
|
|
|
|
|
|
|
|
|
|
For Each File In Files
|
|
|
|
|
|
|
|
|
|
Dim FI = My.Computer.FileSystem.GetFileInfo(File)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Dim mi = New MenuItem
|
|
|
|
|
mi.Header = FI.Name
|
|
|
|
|
mi.ToolTip = File
|
2019-12-01 14:23:48 -08:00
|
|
|
mi.Icon = GetImage("sql_script")
|
2019-11-27 19:25:00 -08:00
|
|
|
|
|
|
|
|
AddHandler mi.Click, AddressOf insert_template
|
|
|
|
|
|
|
|
|
|
MenuItem.Items.Add(mi)
|
|
|
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
Next
|
|
|
|
|
|
|
|
|
|
End Function
|
|
|
|
|
|
2019-12-01 14:23:48 -08:00
|
|
|
|
|
|
|
|
Function GetImage(Name As String) As System.Windows.Controls.Image
|
|
|
|
|
|
|
|
|
|
'' This is the only way I was able to add image into the menu
|
|
|
|
|
Try
|
|
|
|
|
|
|
|
|
|
Dim memory As MemoryStream = New MemoryStream()
|
|
|
|
|
My.Resources.ResourceManager.GetObject(Name).Save(memory, ImageFormat.Png)
|
|
|
|
|
memory.Position = 0
|
|
|
|
|
Dim BitmapImage = New BitmapImage()
|
|
|
|
|
BitmapImage.BeginInit()
|
|
|
|
|
BitmapImage.StreamSource = memory
|
|
|
|
|
BitmapImage.CacheOption = BitmapCacheOption.OnLoad
|
|
|
|
|
BitmapImage.EndInit()
|
|
|
|
|
|
|
|
|
|
Dim ObjImage = New System.Windows.Controls.Image
|
|
|
|
|
ObjImage.Source = BitmapImage
|
|
|
|
|
|
|
|
|
|
Return ObjImage
|
|
|
|
|
|
|
|
|
|
Catch ex As Exception
|
|
|
|
|
|
|
|
|
|
End Try
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
End Function
|
|
|
|
|
|
2019-11-27 19:25:00 -08:00
|
|
|
Private Sub buttonRefresh_Click(ByVal sender As Object, ByVal e As System.EventArgs)
|
|
|
|
|
|
|
|
|
|
AddFiles()
|
|
|
|
|
|
2019-12-01 13:06:34 -08:00
|
|
|
System.Windows.MessageBox.Show("The template list has been refreshed!")
|
2019-11-27 19:25:00 -08:00
|
|
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
|
|
|
Private Sub insert_template(ByVal sender As Object, ByVal e As System.EventArgs)
|
|
|
|
|
|
|
|
|
|
Try
|
|
|
|
|
|
|
|
|
|
Dim FileName As String = sender.ToolTip
|
|
|
|
|
|
2019-12-01 13:06:34 -08:00
|
|
|
If My.Computer.FileSystem.FileExists(FileName) Then
|
2019-11-27 19:25:00 -08:00
|
|
|
|
2019-12-01 13:06:34 -08:00
|
|
|
Dim FileContent = My.Computer.FileSystem.ReadAllText(FileName)
|
2019-11-27 19:25:00 -08:00
|
|
|
|
2019-12-01 13:06:34 -08:00
|
|
|
Dim dte As DTE = TryCast(Package.GetGlobalService(GetType(DTE)), DTE)
|
2019-11-27 19:25:00 -08:00
|
|
|
|
2019-12-01 13:06:34 -08:00
|
|
|
If Not dte.ActiveDocument Is Nothing Then
|
2019-11-27 19:25:00 -08:00
|
|
|
|
2019-12-01 13:06:34 -08:00
|
|
|
Dim selection As TextSelection = DirectCast(dte.ActiveDocument.Selection, TextSelection)
|
|
|
|
|
|
|
|
|
|
selection.Delete()
|
|
|
|
|
|
|
|
|
|
selection.Insert(FileContent.Trim)
|
|
|
|
|
|
|
|
|
|
End If
|
|
|
|
|
Else
|
|
|
|
|
System.Windows.MessageBox.Show("File " + FileName + " doesn't exist!")
|
|
|
|
|
End If
|
2019-11-27 19:25:00 -08:00
|
|
|
|
|
|
|
|
Catch ex As Exception
|
|
|
|
|
System.Windows.MessageBox.Show(ex.Message)
|
|
|
|
|
End Try
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
|
|
|
|
2019-11-27 17:54:54 -08:00
|
|
|
''' <summary>
|
|
|
|
|
''' Handles click on the button by displaying a message box.
|
|
|
|
|
''' </summary>
|
|
|
|
|
''' <param name="sender">The event sender.</param>
|
|
|
|
|
''' <param name="e">The event args.</param>
|
|
|
|
|
<System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")>
|
|
|
|
|
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FormatSelection()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
2019-12-01 14:23:48 -08:00
|
|
|
Function ParseCodeViaTSQLParser(OldCode As String) As String
|
|
|
|
|
|
|
|
|
|
Dim ResultCode As String = ""
|
|
|
|
|
|
|
|
|
|
Dim SqlParser As TSqlParser = Nothing
|
|
|
|
|
|
|
|
|
|
Dim TargetVersion As String = SettingManager.GetSQLParserVersion()
|
|
|
|
|
If TargetVersion = "SQL Server 2008" Then
|
|
|
|
|
SqlParser = New TSql100Parser(False)
|
|
|
|
|
ElseIf TargetVersion = "SQL Server 2012" Then
|
|
|
|
|
SqlParser = New TSql110Parser(False)
|
|
|
|
|
ElseIf TargetVersion = "SQL Server 2014" Then
|
|
|
|
|
SqlParser = New TSql120Parser(False)
|
|
|
|
|
ElseIf TargetVersion = "SQL Server 2016" Then
|
|
|
|
|
SqlParser = New TSql130Parser(False)
|
|
|
|
|
ElseIf TargetVersion = "SQL Server 2017" Then
|
|
|
|
|
SqlParser = New TSql140Parser(False)
|
|
|
|
|
ElseIf TargetVersion = "SQL Server 2019" Then
|
|
|
|
|
SqlParser = New TSql150Parser(False)
|
2023-02-14 09:23:51 -07:00
|
|
|
ElseIf TargetVersion = "SQL Server 2022" Then
|
|
|
|
|
SqlParser = New TSql160Parser(False)
|
2019-12-01 14:23:48 -08:00
|
|
|
Else
|
2023-02-14 09:23:51 -07:00
|
|
|
SqlParser = New TSql150Parser(False)
|
2019-12-01 14:23:48 -08:00
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
Dim parseErrors As IList(Of ParseError) = New List(Of ParseError)
|
|
|
|
|
Dim result As TSqlFragment = SqlParser.Parse(New StringReader(OldCode), parseErrors)
|
|
|
|
|
|
|
|
|
|
If parseErrors.Count > 0 Then
|
|
|
|
|
|
|
|
|
|
Dim ErrorStr = ""
|
|
|
|
|
For Each StrError In parseErrors
|
|
|
|
|
ErrorStr = ErrorStr + Environment.NewLine + StrError.Message
|
|
|
|
|
Next
|
|
|
|
|
|
|
|
|
|
Throw New System.Exception("TSqlParser unable format selected T-SQL due to a syntax error." + Environment.NewLine + ErrorStr)
|
|
|
|
|
|
|
|
|
|
End If
|
|
|
|
|
|
2023-02-14 09:49:35 -07:00
|
|
|
'---------------------------------------------------------------
|
|
|
|
|
' This is how you can strip off all comments, but keep the format on
|
|
|
|
|
'Dim resultScript = ""
|
|
|
|
|
'For Each Item In result.ScriptTokenStream
|
|
|
|
|
' If Item.TokenType = TSqlTokenType.MultilineComment _
|
|
|
|
|
' Or Item.TokenType = TSqlTokenType.SingleLineComment Then
|
|
|
|
|
' Continue For
|
|
|
|
|
' End If
|
|
|
|
|
' resultScript = resultScript + Item.Text
|
|
|
|
|
'Next
|
|
|
|
|
'---------------------------------------------------------------
|
|
|
|
|
' Still need to find how to format the script and keep comments in the text
|
|
|
|
|
'---------------------------------------------------------------
|
2019-12-01 14:23:48 -08:00
|
|
|
|
|
|
|
|
Dim Gen As SqlScriptGenerator = Nothing
|
|
|
|
|
|
|
|
|
|
If TargetVersion = "SQL Server 2008" Then
|
|
|
|
|
Gen = New Sql100ScriptGenerator
|
2023-02-14 09:23:51 -07:00
|
|
|
Gen.Options.SqlVersion = SqlVersion.Sql100
|
2019-12-01 14:23:48 -08:00
|
|
|
ElseIf TargetVersion = "SQL Server 2012" Then
|
|
|
|
|
Gen = New Sql110ScriptGenerator
|
2023-02-14 09:23:51 -07:00
|
|
|
Gen.Options.SqlVersion = SqlVersion.Sql110
|
2019-12-01 14:23:48 -08:00
|
|
|
ElseIf TargetVersion = "SQL Server 2014" Then
|
|
|
|
|
Gen = New Sql120ScriptGenerator
|
2023-02-14 09:23:51 -07:00
|
|
|
Gen.Options.SqlVersion = SqlVersion.Sql120
|
2019-12-01 14:23:48 -08:00
|
|
|
ElseIf TargetVersion = "SQL Server 2016" Then
|
|
|
|
|
Gen = New Sql130ScriptGenerator
|
2023-02-14 09:23:51 -07:00
|
|
|
Gen.Options.SqlVersion = SqlVersion.Sql130
|
2019-12-01 14:23:48 -08:00
|
|
|
ElseIf TargetVersion = "SQL Server 2017" Then
|
|
|
|
|
Gen = New Sql140ScriptGenerator
|
2023-02-14 09:23:51 -07:00
|
|
|
Gen.Options.SqlVersion = SqlVersion.Sql140
|
2019-12-01 14:23:48 -08:00
|
|
|
ElseIf TargetVersion = "SQL Server 2019" Then
|
|
|
|
|
Gen = New Sql150ScriptGenerator
|
2023-02-14 09:23:51 -07:00
|
|
|
Gen.Options.SqlVersion = SqlVersion.Sql150
|
|
|
|
|
ElseIf TargetVersion = "SQL Server 2022" Then
|
|
|
|
|
Gen = New Sql160ScriptGenerator
|
|
|
|
|
Gen.Options.SqlVersion = SqlVersion.Sql160
|
2019-12-01 14:23:48 -08:00
|
|
|
Else
|
2023-02-14 09:23:51 -07:00
|
|
|
Gen = New Sql150ScriptGenerator
|
2019-12-01 14:23:48 -08:00
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
Gen.Options.AlignClauseBodies = False
|
|
|
|
|
Gen.GenerateScript(result, ResultCode)
|
|
|
|
|
|
|
|
|
|
Return ResultCode
|
|
|
|
|
|
|
|
|
|
End Function
|
2019-11-27 17:54:54 -08:00
|
|
|
|
|
|
|
|
Private Sub FormatSelection()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Dim dte As DTE = TryCast(Package.GetGlobalService(GetType(DTE)), DTE)
|
|
|
|
|
|
|
|
|
|
If Not dte.ActiveDocument Is Nothing Then
|
|
|
|
|
|
|
|
|
|
Dim title = "Format TSQL"
|
|
|
|
|
|
|
|
|
|
Try
|
|
|
|
|
|
|
|
|
|
Dim selection As TextSelection = DirectCast(dte.ActiveDocument.Selection, TextSelection)
|
|
|
|
|
|
|
|
|
|
Dim OldStr = selection.Text
|
|
|
|
|
|
2019-12-01 14:23:48 -08:00
|
|
|
'nothing is selected
|
|
|
|
|
If String.IsNullOrEmpty(OldStr) Then
|
|
|
|
|
Return
|
2019-11-27 17:54:54 -08:00
|
|
|
End If
|
|
|
|
|
|
2019-12-01 14:23:48 -08:00
|
|
|
Dim Result As String = ""
|
2019-11-27 17:54:54 -08:00
|
|
|
|
2019-12-01 14:23:48 -08:00
|
|
|
If SettingManager.GetSQLParserType() = False Then
|
|
|
|
|
Result = ParseCodeViaTSQLParser(OldStr)
|
2019-11-27 17:54:54 -08:00
|
|
|
Else
|
2019-12-01 14:23:48 -08:00
|
|
|
Dim FO = New PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatterOptions With {
|
|
|
|
|
.IndentString = "\t",
|
|
|
|
|
.SpacesPerTab = 4,
|
|
|
|
|
.MaxLineWidth = 999,
|
|
|
|
|
.ExpandCommaLists = True,
|
|
|
|
|
.TrailingCommas = True,
|
|
|
|
|
.SpaceAfterExpandedComma = False,
|
|
|
|
|
.ExpandBooleanExpressions = True,
|
|
|
|
|
.ExpandCaseStatements = True,
|
|
|
|
|
.ExpandBetweenConditions = False,
|
|
|
|
|
.BreakJoinOnSections = False,
|
|
|
|
|
.UppercaseKeywords = True,
|
|
|
|
|
.HTMLColoring = False,
|
|
|
|
|
.KeywordStandardization = True}
|
|
|
|
|
|
|
|
|
|
Dim formatter = New PoorMansTSqlFormatterLib.Formatters.TSqlStandardFormatter(FO)
|
|
|
|
|
Dim formatMgr = New PoorMansTSqlFormatterLib.SqlFormattingManager(formatter)
|
|
|
|
|
|
|
|
|
|
Result = formatMgr.Format(OldStr)
|
2019-11-27 17:54:54 -08:00
|
|
|
End If
|
|
|
|
|
|
2019-12-01 14:23:48 -08:00
|
|
|
selection.Delete()
|
2019-11-27 17:54:54 -08:00
|
|
|
|
2019-12-01 14:23:48 -08:00
|
|
|
selection.Insert(Result)
|
2019-11-27 17:54:54 -08:00
|
|
|
|
|
|
|
|
Catch ex As Exception
|
|
|
|
|
System.Windows.MessageBox.Show(ex.Message)
|
|
|
|
|
End Try
|
|
|
|
|
|
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
2019-12-01 12:19:10 -08:00
|
|
|
Private Sub buttonHelp_Click(sender As Object, e As System.Windows.RoutedEventArgs)
|
|
|
|
|
|
|
|
|
|
Dim AboutBoxForm = New AboutBox
|
|
|
|
|
|
|
|
|
|
AboutBoxForm.ShowDialog()
|
|
|
|
|
|
|
|
|
|
AboutBoxForm.Dispose()
|
|
|
|
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
|
|
|
Private Sub buttonSetting_Click(sender As Object, e As System.Windows.RoutedEventArgs)
|
|
|
|
|
|
|
|
|
|
Dim SettingFormForm = New SettingForm
|
|
|
|
|
|
|
|
|
|
SettingFormForm.ShowDialog()
|
|
|
|
|
|
|
|
|
|
SettingFormForm.Dispose()
|
|
|
|
|
|
|
|
|
|
End Sub
|
2019-11-27 17:54:54 -08:00
|
|
|
|
|
|
|
|
End Class
|