Skip to content
This repository was archived by the owner on Feb 8, 2025. It is now read-only.

Commit d822882

Browse files
committed
Added new Select option
1 parent 287bdbb commit d822882

File tree

4 files changed

+122
-4
lines changed

4 files changed

+122
-4
lines changed

extension.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const vscode = require('vscode');
2-
const azdata = require('azdata');
2+
33
const {getSqlScriptAsInsertAsync} = require('./scriptInsertAs.js');
44
const {getSqlScriptAsUpdateAsync} = require('./scriptUpdateAs.js');
5+
const {getSqlScriptAsSelectAsync} = require('./scriptSelectAs.js');
56
const sqlUtils = require('./scriptSqlUtils.js');
67

78
function activate(context)
@@ -137,6 +138,56 @@ function activate(context)
137138
}
138139
);
139140

141+
let selectTableCommandToClipBoard = vscode.commands.registerCommand("extraSqlScriptAs.selectTableToClipboard"
142+
, function(context)
143+
{
144+
let databaseName = context.connectionProfile.databaseName;
145+
let schemaName = context.nodeInfo.metadata.schema;
146+
let tableName = context.nodeInfo.metadata.name;
147+
148+
getSqlScriptAsSelectAsync(context.connectionProfile, databaseName, schemaName, tableName)
149+
.then(scriptText =>
150+
{
151+
vscode.env.clipboard.writeText(scriptText).then((text)=>{
152+
vscode.window.showInformationMessage('Script copied to clipboard.');
153+
});
154+
})
155+
.catch(reason =>
156+
{
157+
vscode.window.showErrorMessage(reason);
158+
}
159+
);
160+
}
161+
);
162+
163+
let selectTableCommand = vscode.commands.registerCommand("extraSqlScriptAs.selectTable"
164+
, function(context)
165+
{
166+
let databaseName = context.connectionProfile.databaseName;
167+
let schemaName = context.nodeInfo.metadata.schema;
168+
let tableName = context.nodeInfo.metadata.name;
169+
170+
//Test
171+
getSqlScriptAsSelectAsync(context.connectionProfile, databaseName, schemaName, tableName)
172+
.then(scriptText =>
173+
{
174+
vscode.commands.executeCommand('newQuery').then(s => {
175+
176+
let editor = vscode.window.activeTextEditor;
177+
178+
editor.edit(edit => {
179+
edit.insert(new vscode.Position(0, 0), scriptText);
180+
});
181+
});
182+
})
183+
.catch(reason =>
184+
{
185+
vscode.window.showErrorMessage(reason);
186+
}
187+
);
188+
}
189+
);
190+
140191
context.subscriptions.push(insertTableCommand);
141192
context.subscriptions.push(insertTableCommandToClipBoard);
142193

@@ -145,6 +196,9 @@ function activate(context)
145196

146197
context.subscriptions.push(deleteTableCommand);
147198
context.subscriptions.push(deleteTableCommandToClipBoard);
199+
200+
context.subscriptions.push(selectTableCommand);
201+
context.subscriptions.push(selectTableCommandToClipBoard);
148202
};
149203

150204
function deactivate() {

package.json

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "Extra Sql Script As",
44
"description": "This extension adds several missing options to the context menu of the object tree: Script Table as INSERT, Script Table as UPDATE...",
55
"publisher": "pacoweb",
6-
"version": "0.4.1",
6+
"version": "0.5.0",
77
"license": "https://raw.githubusercontent.com/pacoweb/extraSqlScriptAs/master/LICENSE",
88
"icon": "images/default_icon.png",
99
"repository": {
@@ -21,9 +21,11 @@
2121
"onCommand:extraSqlScriptAs.insertTable",
2222
"onCommand:extraSqlScriptAs.updateTable",
2323
"onCommand:extraSqlScriptAs.deleteTable",
24+
"onCommand:extraSqlScriptAs.selectTable",
2425
"onCommand:extraSqlScriptAs.insertTableToClipboard",
2526
"onCommand:extraSqlScriptAs.updateTableToClipboard",
26-
"onCommand:extraSqlScriptAs.deleteTableToClipboard"
27+
"onCommand:extraSqlScriptAs.deleteTableToClipboard",
28+
"onCommand:extraSqlScriptAs.selectTableToClipboard"
2729
],
2830
"main": "./extension.js",
2931
"contributes": {
@@ -40,6 +42,10 @@
4042
"command": "extraSqlScriptAs.deleteTable",
4143
"title": "Script Table as DELETE"
4244
},
45+
{
46+
"command": "extraSqlScriptAs.selectTable",
47+
"title": "Script Table as SELECT"
48+
},
4349
{
4450
"command": "extraSqlScriptAs.insertTableToClipboard",
4551
"title": "Script Table as INSERT to clipboard"
@@ -51,6 +57,10 @@
5157
{
5258
"command": "extraSqlScriptAs.deleteTableToClipboard",
5359
"title": "Script Table as DELETE to clipboard"
60+
},
61+
{
62+
"command": "extraSqlScriptAs.selectTableToClipboard",
63+
"title": "Script Table as SELECT to clipboard"
5464
}
5565
],
5666
"menus": {
@@ -70,6 +80,11 @@
7080
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
7181
"group": "1data1"
7282
},
83+
{
84+
"command": "extraSqlScriptAs.selectTable",
85+
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
86+
"group": "1data1"
87+
},
7388
{
7489
"command": "extraSqlScriptAs.insertTableToClipboard",
7590
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
@@ -84,6 +99,11 @@
8499
"command": "extraSqlScriptAs.deleteTableToClipboard",
85100
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
86101
"group": "1data1"
102+
},
103+
{
104+
"command": "extraSqlScriptAs.selectTableToClipboard",
105+
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
106+
"group": "1data1"
87107
}
88108
]
89109
}

scriptSelectAs.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const sqlUtils = require('./scriptSqlUtils.js');
4+
5+
const colNameOrdinal = 0;
6+
7+
async function getSqlScriptAsSelectAsync(connectionProfile, tableCatalog, tableSchema, tableName)
8+
{
9+
let queryText = sqlUtils.getColumnInfoQuerySql(tableCatalog, tableSchema, tableName);
10+
11+
let results = await sqlUtils.getResultsFromQuerySql(connectionProfile, "MSSQL", queryText);
12+
13+
if (!results || results.rowCount === 0) {
14+
throw "No se han obtenido resultados de la consulta";
15+
}
16+
17+
let updateSqlScript = buildFinalScript(results, tableCatalog, tableSchema, tableName);
18+
19+
return updateSqlScript;
20+
}
21+
22+
function buildFinalScript(results, tableCatalog, tableSchema, tableName)
23+
{
24+
let fullScript = [];
25+
let columsScriptPart = [];
26+
27+
fullScript.push("SELECT ");
28+
29+
let columnIndex = 0;
30+
31+
for (let i= 0; i !== results.rowCount; i++)
32+
{
33+
let rowData = results.rows[i];
34+
35+
const separator = (columnIndex === 0) ? " " : ",";
36+
37+
columsScriptPart.push("\t\t" + separator + "[" + rowData[colNameOrdinal].displayValue + "]");
38+
39+
columnIndex += 1;
40+
}
41+
42+
return fullScript.concat(columsScriptPart).concat([`FROM [${tableCatalog}].[${tableSchema}].[${tableName}] `]).join('\n');
43+
}
44+
45+
module.exports.getSqlScriptAsSelectAsync = getSqlScriptAsSelectAsync;

scriptUpdateAs.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const vscode = require('vscode');
43
const sqlUtils = require('./scriptSqlUtils.js');
54

65
const colNameOrdinal = 0;

0 commit comments

Comments
 (0)