1
1
using EnvDTE ;
2
+ using EnvDTE80 ;
2
3
using Microsoft . SqlServer . TransactSql . ScriptDom ;
3
- using Microsoft . VisualStudio ;
4
- using Microsoft . VisualStudio . OLE . Interop ;
5
4
using Microsoft . VisualStudio . Shell ;
6
- using Microsoft . VisualStudio . Shell . Interop ;
7
5
using SQLScriptsExplorer . Addin . Repository ;
8
6
using SQLScriptsExplorer . Addin . Repository . Interfaces ;
9
7
using System ;
@@ -15,41 +13,40 @@ namespace SQLScriptsExplorer.Addin.Infrastructure
15
13
{
16
14
public static class DocumentManager
17
15
{
16
+ private static DTE2 DTE
17
+ {
18
+ get
19
+ {
20
+ ThreadHelper . ThrowIfNotOnUIThread ( ) ;
21
+ return Package . GetGlobalService ( typeof ( DTE ) ) as DTE2 ;
22
+ }
23
+ }
24
+
18
25
public static void OpenTemplate ( string fileName , string fileFullPath )
19
26
{
20
27
try
21
28
{
22
- // TODO: This needs to open in a new window, not edit the current one.
23
29
ThreadHelper . ThrowIfNotOnUIThread ( ) ;
24
30
25
- if ( ! File . Exists ( fileFullPath ) )
31
+ if ( File . Exists ( fileFullPath ) )
26
32
{
27
- throw new FileNotFoundException ( $ "File { fileFullPath } doesn't exist!") ;
28
- }
33
+ string fileContent = File . ReadAllText ( fileFullPath ) ;
34
+ var docName = Path . GetFileNameWithoutExtension ( fileName ) ;
35
+ var docExtension = Path . GetExtension ( fileName ) ;
29
36
30
- var openDoc = Package . GetGlobalService ( typeof ( SVsUIShellOpenDocument ) ) as IVsUIShellOpenDocument ;
31
- if ( openDoc == null )
32
- {
33
- throw new InvalidOperationException ( "Could not get OpenDocument service." ) ;
34
- }
35
-
36
- Guid logicalView = VSConstants . LOGVIEWID_Primary ;
37
- IVsUIHierarchy hierarchy ;
38
- uint itemId ;
39
- IVsWindowFrame windowFrame ;
40
- Microsoft . VisualStudio . OLE . Interop . IServiceProvider docServiceProvider ;
37
+ var fileDocument = DTE . ItemOperations . NewFile ( @"General\Text File" , $ "{ docName } _Copy.{ docExtension } ") . Document ;
41
38
42
- int hr = openDoc . OpenDocumentViaProject (
43
- fileFullPath ,
44
- ref logicalView ,
45
- out docServiceProvider ,
46
- out hierarchy ,
47
- out itemId ,
48
- out windowFrame ) ;
39
+ TextSelection textSelection = fileDocument . Selection as TextSelection ;
40
+ textSelection . SelectAll ( ) ;
41
+ textSelection . Text = string . Empty ;
42
+ textSelection . Insert ( fileContent ) ;
43
+ textSelection . StartOfDocument ( ) ;
49
44
50
- if ( ErrorHandler . Succeeded ( hr ) && windowFrame != null )
45
+ fileDocument . Save ( ) ;
46
+ }
47
+ else
51
48
{
52
- windowFrame . Show ( ) ;
49
+ throw new Exception ( $ "File { fileFullPath } doesn't exist!" ) ;
53
50
}
54
51
}
55
52
catch ( Exception ex )
@@ -62,36 +59,13 @@ public static void EditTemplate(string fileName, string fileFullPath)
62
59
{
63
60
try
64
61
{
65
- ThreadHelper . ThrowIfNotOnUIThread ( ) ;
66
-
67
- if ( ! File . Exists ( fileFullPath ) )
68
- {
69
- throw new FileNotFoundException ( $ "File { fileFullPath } doesn't exist!") ;
70
- }
71
-
72
- var openDoc = Package . GetGlobalService ( typeof ( SVsUIShellOpenDocument ) ) as IVsUIShellOpenDocument ;
73
- if ( openDoc == null )
62
+ if ( File . Exists ( fileFullPath ) )
74
63
{
75
- throw new InvalidOperationException ( "Could not get OpenDocument service." ) ;
64
+ DTE . ItemOperations . OpenFile ( fileFullPath ) ;
76
65
}
77
-
78
- Guid logicalView = VSConstants . LOGVIEWID_Primary ;
79
- IVsUIHierarchy hierarchy ;
80
- uint itemId ;
81
- IVsWindowFrame windowFrame ;
82
- Microsoft . VisualStudio . OLE . Interop . IServiceProvider docServiceProvider ;
83
-
84
- int hr = openDoc . OpenDocumentViaProject (
85
- fileFullPath ,
86
- ref logicalView ,
87
- out docServiceProvider ,
88
- out hierarchy ,
89
- out itemId ,
90
- out windowFrame ) ;
91
-
92
- if ( ErrorHandler . Succeeded ( hr ) && windowFrame != null )
66
+ else
93
67
{
94
- windowFrame . Show ( ) ;
68
+ throw new Exception ( $ "File { fileFullPath } doesn't exist!" ) ;
95
69
}
96
70
}
97
71
catch ( Exception ex )
@@ -102,64 +76,14 @@ public static void EditTemplate(string fileName, string fileFullPath)
102
76
103
77
public static void ExecuteTemplate ( string fileName , string fileFullPath )
104
78
{
105
- // TODO: This is opening the document, however it's not executing the query (automatically removing the first line instead).
79
+ string CMD_QUERY_EXECUTE = "Query.Execute" ;
106
80
107
81
try
108
82
{
109
- ThreadHelper . ThrowIfNotOnUIThread ( ) ;
110
-
111
- if ( ! File . Exists ( fileFullPath ) )
83
+ // Ensure the document we are executing is the document we have opened by checking its name
84
+ if ( DTE . ActiveDocument != null && DTE . ActiveDocument . ProjectItem . Name . Equals ( fileName ) )
112
85
{
113
- throw new FileNotFoundException ( $ "File { fileFullPath } doesn't exist!") ;
114
- }
115
-
116
- var openDoc = Package . GetGlobalService ( typeof ( SVsUIShellOpenDocument ) ) as IVsUIShellOpenDocument ;
117
- if ( openDoc == null )
118
- {
119
- throw new InvalidOperationException ( "Could not get OpenDocument service." ) ;
120
- }
121
-
122
- Guid logicalView = VSConstants . LOGVIEWID_Primary ;
123
- IVsUIHierarchy hierarchy ;
124
- uint itemId ;
125
- IVsWindowFrame windowFrame ;
126
- Microsoft . VisualStudio . OLE . Interop . IServiceProvider docServiceProvider ;
127
-
128
- int hr = openDoc . OpenDocumentViaProject (
129
- fileFullPath ,
130
- ref logicalView ,
131
- out docServiceProvider ,
132
- out hierarchy ,
133
- out itemId ,
134
- out windowFrame ) ;
135
-
136
- if ( ErrorHandler . Succeeded ( hr ) && windowFrame != null )
137
- {
138
- windowFrame . Show ( ) ;
139
-
140
- // Now, get the command interface from the window frame
141
- var commandTarget = GetCommandTarget ( windowFrame ) ;
142
- if ( commandTarget != null )
143
- {
144
- // Execute the Query.Execute command
145
- Guid cmdGroup = new Guid ( "5EFC7975-14BC-11CF-9B2B-00AA00573819" ) ;
146
- uint cmdId = 16 ; // Command ID for Query.Execute in SSMS
147
-
148
- commandTarget . Exec (
149
- ref cmdGroup ,
150
- cmdId ,
151
- ( uint ) OLECMDEXECOPT . OLECMDEXECOPT_DONTPROMPTUSER ,
152
- IntPtr . Zero ,
153
- IntPtr . Zero ) ;
154
- }
155
- else
156
- {
157
- throw new InvalidOperationException ( "Could not get command target to execute the query." ) ;
158
- }
159
- }
160
- else
161
- {
162
- throw new InvalidOperationException ( $ "Failed to open document: { fileFullPath } ") ;
86
+ DTE . ExecuteCommand ( CMD_QUERY_EXECUTE ) ;
163
87
}
164
88
}
165
89
catch ( Exception ex )
@@ -168,29 +92,13 @@ public static void ExecuteTemplate(string fileName, string fileFullPath)
168
92
}
169
93
}
170
94
171
- private static IOleCommandTarget GetCommandTarget ( IVsWindowFrame windowFrame )
172
- {
173
- ThreadHelper . ThrowIfNotOnUIThread ( ) ;
174
-
175
- object docView ;
176
- if ( ErrorHandler . Succeeded ( windowFrame . GetProperty ( ( int ) __VSFPROPID . VSFPROPID_DocView , out docView ) ) && docView != null )
177
- {
178
- return docView as IOleCommandTarget ;
179
- }
180
- return null ;
181
- }
182
-
183
95
public static void FormatSelection ( )
184
96
{
185
97
try
186
98
{
187
- ThreadHelper . ThrowIfNotOnUIThread ( ) ;
188
-
189
- DTE dte = Package . GetGlobalService ( typeof ( DTE ) ) as DTE ;
190
-
191
- if ( dte . ActiveDocument != null )
99
+ if ( DTE . ActiveDocument != null )
192
100
{
193
- TextSelection selection = ( TextSelection ) dte . ActiveDocument . Selection ;
101
+ TextSelection selection = ( TextSelection ) DTE . ActiveDocument . Selection ;
194
102
195
103
// Format whole text: selection.SelectAll();
196
104
string selectedText = selection . Text ;
@@ -242,7 +150,15 @@ private static string FormatSelectionUsingSQLServer(string code)
242
150
243
151
private static Tuple < TSqlParser , SqlScriptGenerator > GetSQLParser ( string targetVersion )
244
152
{
245
- if ( targetVersion == "SQL Server 2019" )
153
+ if ( targetVersion == "SQL Server 2025" )
154
+ {
155
+ return new Tuple < TSqlParser , SqlScriptGenerator > ( new TSql170Parser ( false ) , new Sql170ScriptGenerator ( ) ) ;
156
+ }
157
+ else if ( targetVersion == "SQL Server 2022" )
158
+ {
159
+ return new Tuple < TSqlParser , SqlScriptGenerator > ( new TSql160Parser ( false ) , new Sql160ScriptGenerator ( ) ) ;
160
+ }
161
+ else if ( targetVersion == "SQL Server 2019" )
246
162
{
247
163
return new Tuple < TSqlParser , SqlScriptGenerator > ( new TSql150Parser ( false ) , new Sql150ScriptGenerator ( ) ) ;
248
164
}
0 commit comments