11const vscode = require ( 'vscode' ) ;
2-
32const { getSqlScriptAsInsertAsync} = require ( './scriptInsertAs.js' ) ;
43const { getSqlScriptAsUpdateAsync} = require ( './scriptUpdateAs.js' ) ;
54const { getSqlScriptAsSelectAsync} = require ( './scriptSelectAs.js' ) ;
65const sqlUtils = require ( './scriptSqlUtils.js' ) ;
76
8- function activate ( context )
9- {
10- let insertTableCommandToClipBoard = vscode . commands . registerCommand ( "extraSqlScriptAs.insertTableToClipboard"
11- , function ( context )
12- {
13- let databaseName = context . connectionProfile . databaseName ;
14- let schemaName = context . nodeInfo . metadata . schema ;
15- let tableName = context . nodeInfo . metadata . name ;
16-
17- getSqlScriptAsInsertAsync ( context . connectionProfile , databaseName , schemaName , tableName )
18- . then ( scriptText =>
19- {
20- vscode . env . clipboard . writeText ( scriptText ) . then ( ( text ) => {
21- vscode . window . showInformationMessage ( 'Script copied to clipboard.' ) ;
22- } ) ;
23- } )
24- . catch ( reason =>
25- {
26- vscode . window . showErrorMessage ( reason ) ;
27- }
28- ) ;
29- }
30- ) ;
31-
32- let insertTableCommand = vscode . commands . registerCommand ( "extraSqlScriptAs.insertTable"
33- , function ( context )
34- {
35- let databaseName = context . connectionProfile . databaseName ;
36- let schemaName = context . nodeInfo . metadata . schema ;
37- let tableName = context . nodeInfo . metadata . name ;
38-
39- getSqlScriptAsInsertAsync ( context . connectionProfile , databaseName , schemaName , tableName )
40- . then ( scriptText =>
41- {
42- vscode . commands . executeCommand ( 'newQuery' ) . then ( s => {
43-
44- let editor = vscode . window . activeTextEditor ;
45-
46- editor . edit ( edit => {
47- edit . insert ( new vscode . Position ( 0 , 0 ) , scriptText ) ;
48- } ) ;
49- } ) ;
50- } )
51- . catch ( reason =>
52- {
53- vscode . window . showErrorMessage ( reason ) ;
54- }
55- ) ;
56- }
57- ) ;
58-
59- let insertTableCommandToClipBoardIdentityOn = vscode . commands . registerCommand ( "extraSqlScriptAs.insertTableToClipboardIdentityOn"
60- , function ( context )
61- {
62- let databaseName = context . connectionProfile . databaseName ;
63- let schemaName = context . nodeInfo . metadata . schema ;
64- let tableName = context . nodeInfo . metadata . name ;
65-
66- getSqlScriptAsInsertAsync ( context . connectionProfile , databaseName , schemaName , tableName , true )
67- . then ( scriptText =>
68- {
69- vscode . env . clipboard . writeText ( scriptText ) . then ( ( text ) => {
70- vscode . window . showInformationMessage ( 'Script copied to clipboard.' ) ;
71- } ) ;
72- } )
73- . catch ( reason =>
74- {
75- vscode . window . showErrorMessage ( reason ) ;
76- }
77- ) ;
78- }
79- ) ;
80-
81- let insertTableCommandIdentityOn = vscode . commands . registerCommand ( "extraSqlScriptAs.insertTableIdentityOn"
82- , function ( context )
83- {
84- let databaseName = context . connectionProfile . databaseName ;
85- let schemaName = context . nodeInfo . metadata . schema ;
86- let tableName = context . nodeInfo . metadata . name ;
87-
88- getSqlScriptAsInsertAsync ( context . connectionProfile , databaseName , schemaName , tableName , true )
89- . then ( scriptText =>
90- {
91- vscode . commands . executeCommand ( 'newQuery' ) . then ( s => {
92-
93- let editor = vscode . window . activeTextEditor ;
94-
95- editor . edit ( edit => {
96- edit . insert ( new vscode . Position ( 0 , 0 ) , scriptText ) ;
97- } ) ;
98- } ) ;
99- } )
100- . catch ( reason =>
101- {
102- vscode . window . showErrorMessage ( reason ) ;
103- }
104- ) ;
105- }
106- ) ;
107-
108-
109- let updateTableCommandToClipBoard = vscode . commands . registerCommand ( "extraSqlScriptAs.updateTableToClipboard"
110- , function ( context )
111- {
112- let databaseName = context . connectionProfile . databaseName ;
113- let schemaName = context . nodeInfo . metadata . schema ;
114- let tableName = context . nodeInfo . metadata . name ;
115-
116- getSqlScriptAsUpdateAsync ( context . connectionProfile , databaseName , schemaName , tableName )
117- . then ( scriptText =>
118- {
119- vscode . env . clipboard . writeText ( scriptText ) . then ( ( text ) => {
120- vscode . window . showInformationMessage ( 'Script copied to clipboard.' ) ;
121- } ) ;
122- } )
123- . catch ( reason =>
124- {
125- vscode . window . showErrorMessage ( reason ) ;
126- }
127- ) ;
128- }
129- ) ;
130-
131- let updateTableCommand = vscode . commands . registerCommand ( "extraSqlScriptAs.updateTable"
132- , function ( context )
133- {
134- let databaseName = context . connectionProfile . databaseName ;
135- let schemaName = context . nodeInfo . metadata . schema ;
136- let tableName = context . nodeInfo . metadata . name ;
137-
138- //Test
139- getSqlScriptAsUpdateAsync ( context . connectionProfile , databaseName , schemaName , tableName )
140- . then ( scriptText =>
141- {
142- vscode . commands . executeCommand ( 'newQuery' ) . then ( s => {
143-
144- let editor = vscode . window . activeTextEditor ;
145-
146- editor . edit ( edit => {
147- edit . insert ( new vscode . Position ( 0 , 0 ) , scriptText ) ;
148- } ) ;
149- } ) ;
150- } )
151- . catch ( reason =>
152- {
153- vscode . window . showErrorMessage ( reason ) ;
154- }
155- ) ;
156- }
157- ) ;
158-
159- let deleteTableCommandToClipBoard = vscode . commands . registerCommand ( "extraSqlScriptAs.deleteTableToClipboard"
160- , function ( context )
161- {
162- let databaseName = context . connectionProfile . databaseName ;
163- let schemaName = context . nodeInfo . metadata . schema ;
164- let tableName = context . nodeInfo . metadata . name ;
165-
166- vscode . env . clipboard . writeText ( sqlUtils . getDeleteSqlScript ( databaseName , schemaName , tableName ) ) . then ( ( text ) => {
167- vscode . window . showInformationMessage ( 'Script copied to clipboard.' ) ;
168- } ) ;
169- }
170- ) ;
171-
172- let deleteTableCommand = vscode . commands . registerCommand ( "extraSqlScriptAs.deleteTable"
173- , function ( context )
174- {
175- let databaseName = context . connectionProfile . databaseName ;
176- let schemaName = context . nodeInfo . metadata . schema ;
177- let tableName = context . nodeInfo . metadata . name ;
178-
179- vscode . commands . executeCommand ( 'newQuery' ) . then ( s => {
180-
181- let editor = vscode . window . activeTextEditor ;
182-
183- editor . edit ( edit => {
184- edit . insert ( new vscode . Position ( 0 , 0 )
185- , sqlUtils . getDeleteSqlScript ( databaseName , schemaName , tableName ) )
186- } ) ;
187- } ) ;
188- }
189- ) ;
190-
191- let selectTableCommandToClipBoard = vscode . commands . registerCommand ( "extraSqlScriptAs.selectTableToClipboard"
192- , function ( context )
193- {
194- let databaseName = context . connectionProfile . databaseName ;
195- let schemaName = context . nodeInfo . metadata . schema ;
196- let tableName = context . nodeInfo . metadata . name ;
197-
198- getSqlScriptAsSelectAsync ( context . connectionProfile , databaseName , schemaName , tableName )
199- . then ( scriptText =>
200- {
201- vscode . env . clipboard . writeText ( scriptText ) . then ( ( text ) => {
202- vscode . window . showInformationMessage ( 'Script copied to clipboard.' ) ;
203- } ) ;
204- } )
205- . catch ( reason =>
206- {
207- vscode . window . showErrorMessage ( reason ) ;
208- }
209- ) ;
210- }
211- ) ;
212-
213- let selectTableCommand = vscode . commands . registerCommand ( "extraSqlScriptAs.selectTable"
214- , function ( context )
215- {
216- let databaseName = context . connectionProfile . databaseName ;
217- let schemaName = context . nodeInfo . metadata . schema ;
218- let tableName = context . nodeInfo . metadata . name ;
219-
220- //Test
221- getSqlScriptAsSelectAsync ( context . connectionProfile , databaseName , schemaName , tableName )
222- . then ( scriptText =>
223- {
224- vscode . commands . executeCommand ( 'newQuery' ) . then ( s => {
225-
226- let editor = vscode . window . activeTextEditor ;
7+ const databaseNotFoundMessage = 'Database name not found.' ;
2278
228- editor . edit ( edit => {
229- edit . insert ( new vscode . Position ( 0 , 0 ) , scriptText ) ;
230- } ) ;
231- } ) ;
232- } )
233- . catch ( reason =>
234- {
235- vscode . window . showErrorMessage ( reason ) ;
236- }
237- ) ;
9+ function tryGetBestDatabaseName ( context )
10+ {
11+ let databaseName = context . connectionProfile . databaseName ;
12+
13+ if ( databaseName )
14+ return databaseName ;
15+
16+ //In Macos, in some cases, the database name is not available in the connection profile.
17+ //In this case, we try to get the database name from metadata.urn
18+ const metadataUrn = context . nodeInfo . metadata . urn ;
19+
20+ if ( metadataUrn ) {
21+ const regex = / \/ D a t a b a s e \[ @ N a m e = ' ( [ ^ ' ] * ) ' \] / ;
22+ const match = metadataUrn . match ( regex ) ;
23+ if ( match ) return match [ 1 ] ;
24+ }
25+
26+ return null ;
27+ }
28+
29+ async function handleCommand ( context , getScriptFunc , clipboard = false , identityOn = false ) {
30+ let databaseName = tryGetBestDatabaseName ( context ) ;
31+ if ( ! databaseName ) {
32+ vscode . window . showErrorMessage ( databaseNotFoundMessage ) ;
33+ return ;
34+ }
35+
36+ let schemaName = context . nodeInfo . metadata . schema ;
37+ let tableName = context . nodeInfo . metadata . name ;
38+
39+ try {
40+ let scriptText = await getScriptFunc ( context . connectionProfile , databaseName , schemaName , tableName , identityOn ) ;
41+ if ( clipboard ) {
42+ await vscode . env . clipboard . writeText ( scriptText ) ;
43+ vscode . window . showInformationMessage ( 'Script copied to clipboard.' ) ;
44+ } else {
45+ await vscode . commands . executeCommand ( 'newQuery' ) ;
46+ let editor = vscode . window . activeTextEditor ;
47+ editor . edit ( edit => {
48+ edit . insert ( new vscode . Position ( 0 , 0 ) , scriptText ) ;
49+ } ) ;
23850 }
239- ) ;
240-
241- context . subscriptions . push ( insertTableCommand ) ;
242- context . subscriptions . push ( insertTableCommandToClipBoard ) ;
243-
244- context . subscriptions . push ( insertTableCommandIdentityOn ) ;
245- context . subscriptions . push ( insertTableCommandToClipBoardIdentityOn ) ;
246-
247- context . subscriptions . push ( updateTableCommand ) ;
248- context . subscriptions . push ( updateTableCommandToClipBoard ) ;
249-
250- context . subscriptions . push ( deleteTableCommand ) ;
251- context . subscriptions . push ( deleteTableCommandToClipBoard ) ;
252-
253- context . subscriptions . push ( selectTableCommand ) ;
254- context . subscriptions . push ( selectTableCommandToClipBoard ) ;
255- } ;
256-
257- function deactivate ( ) {
258-
259- } ;
51+ } catch ( reason ) {
52+ vscode . window . showErrorMessage ( reason ) ;
53+ }
54+ }
55+
56+ function activate ( context ) {
57+ const commands = [
58+ { name : "extraSqlScriptAs.insertTableToClipboard" , func : getSqlScriptAsInsertAsync , clipboard : true } ,
59+ { name : "extraSqlScriptAs.insertTable" , func : getSqlScriptAsInsertAsync } ,
60+ { name : "extraSqlScriptAs.insertTableToClipboardIdentityOn" , func : getSqlScriptAsInsertAsync , clipboard : true , identityOn : true } ,
61+ { name : "extraSqlScriptAs.insertTableIdentityOn" , func : getSqlScriptAsInsertAsync , identityOn : true } ,
62+ { name : "extraSqlScriptAs.updateTableToClipboard" , func : getSqlScriptAsUpdateAsync , clipboard : true } ,
63+ { name : "extraSqlScriptAs.updateTable" , func : getSqlScriptAsUpdateAsync } ,
64+ { name : "extraSqlScriptAs.deleteTableToClipboard" , func : ( profile , db , schema , table ) => Promise . resolve ( sqlUtils . getDeleteSqlScript ( db , schema , table ) ) , clipboard : true } ,
65+ { name : "extraSqlScriptAs.deleteTable" , func : ( profile , db , schema , table ) => Promise . resolve ( sqlUtils . getDeleteSqlScript ( db , schema , table ) ) } ,
66+ { name : "extraSqlScriptAs.selectTableToClipboard" , func : getSqlScriptAsSelectAsync , clipboard : true } ,
67+ { name : "extraSqlScriptAs.selectTable" , func : getSqlScriptAsSelectAsync } ,
68+ ] ;
69+
70+ for ( const { name, func, clipboard, identityOn } of commands ) {
71+ let command = vscode . commands . registerCommand ( name , context => handleCommand ( context , func , clipboard , identityOn ) ) ;
72+ context . subscriptions . push ( command ) ;
73+ }
74+ }
75+
76+ function deactivate ( ) { }
26077
26178exports . activate = activate ;
26279exports . deactivate = deactivate ;
0 commit comments