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

Commit ad83e67

Browse files
authored
Merge pull request #18 from pacoweb/pk_
Add new option INSERT with SET IDENTITY ON and fixed timestamp column…
2 parents f787dcb + 68b7242 commit ad83e67

File tree

5 files changed

+129
-13
lines changed

5 files changed

+129
-13
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
# Azure Data Studio - 3 "Extra Sql Script As" Extension
1+
# Azure Data Studio - 5 "Extra Sql Script As" Extension
22

33
This extension adds several missing options to the context menu of the object tree:
44

55
* Script Table as INSERT
66
* Script Table as INSERT to clipboard
7+
* Script Table as INSERT SET IDENTITY ON
8+
* Script Table as INSERT SET IDENTITY ON to clipboard
79
* Script Table as UPDATE
810
* Script Table as UPDATE to clipboard
911
* Script Table as DELETE
@@ -31,10 +33,18 @@ Can be raised here: https://github.com/pacoweb/extraSqlScriptAs/issues
3133

3234
## Release Notes
3335

36+
### 0.6.0
37+
38+
- Added Script Table as INSERT with SET IDENTITY ON
39+
3440
### 0.5.0
3541

3642
- Added Script Table as SELECT
3743

3844
### 0.1.0
3945

4046
- Initial release.
47+
48+
## Star History
49+
50+
[![Star History Chart](https://api.star-history.com/svg?repos=pacoweb/extraSqlScriptAs&type=Date)](https://star-history.com/#pacoweb/extraSqlScriptAs&Date)

extension.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,56 @@ function activate(context)
5656
}
5757
);
5858

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+
59109
let updateTableCommandToClipBoard = vscode.commands.registerCommand("extraSqlScriptAs.updateTableToClipboard"
60110
, function(context)
61111
{
@@ -191,6 +241,9 @@ function activate(context)
191241
context.subscriptions.push(insertTableCommand);
192242
context.subscriptions.push(insertTableCommandToClipBoard);
193243

244+
context.subscriptions.push(insertTableCommandIdentityOn);
245+
context.subscriptions.push(insertTableCommandToClipBoardIdentityOn);
246+
194247
context.subscriptions.push(updateTableCommand);
195248
context.subscriptions.push(updateTableCommandToClipBoard);
196249

package.json

Lines changed: 21 additions & 1 deletion
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.5.0",
6+
"version": "0.6.0",
77
"license": "https://raw.githubusercontent.com/pacoweb/extraSqlScriptAs/master/LICENSE",
88
"icon": "images/default_icon.png",
99
"repository": {
@@ -19,10 +19,12 @@
1919
],
2020
"activationEvents": [
2121
"onCommand:extraSqlScriptAs.insertTable",
22+
"onCommand:extraSqlScriptAs.insertTableIdentityOn",
2223
"onCommand:extraSqlScriptAs.updateTable",
2324
"onCommand:extraSqlScriptAs.deleteTable",
2425
"onCommand:extraSqlScriptAs.selectTable",
2526
"onCommand:extraSqlScriptAs.insertTableToClipboard",
27+
"onCommand:extraSqlScriptAs.insertTableToClipboardIdentityOn",
2628
"onCommand:extraSqlScriptAs.updateTableToClipboard",
2729
"onCommand:extraSqlScriptAs.deleteTableToClipboard",
2830
"onCommand:extraSqlScriptAs.selectTableToClipboard"
@@ -34,6 +36,10 @@
3436
"command": "extraSqlScriptAs.insertTable",
3537
"title": "Script Table as INSERT"
3638
},
39+
{
40+
"command": "extraSqlScriptAs.insertTableIdentityOn",
41+
"title": "Script Table as INSERT SET IDENTITY ON"
42+
},
3743
{
3844
"command": "extraSqlScriptAs.updateTable",
3945
"title": "Script Table as UPDATE"
@@ -50,6 +56,10 @@
5056
"command": "extraSqlScriptAs.insertTableToClipboard",
5157
"title": "Script Table as INSERT to clipboard"
5258
},
59+
{
60+
"command": "extraSqlScriptAs.insertTableToClipboardIdentityOn",
61+
"title": "Script Table as INSERT SET IDENTITY ON to clipboard"
62+
},
5363
{
5464
"command": "extraSqlScriptAs.updateTableToClipboard",
5565
"title": "Script Table as UPDATE to clipboard"
@@ -70,6 +80,11 @@
7080
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
7181
"group": "1data1"
7282
},
83+
{
84+
"command": "extraSqlScriptAs.insertTableIdentityOn",
85+
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
86+
"group": "1data1"
87+
},
7388
{
7489
"command": "extraSqlScriptAs.updateTable",
7590
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
@@ -90,6 +105,11 @@
90105
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
91106
"group": "1data1"
92107
},
108+
{
109+
"command": "extraSqlScriptAs.insertTableToClipboardIdentityOn",
110+
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
111+
"group": "1data1"
112+
},
93113
{
94114
"command": "extraSqlScriptAs.updateTableToClipboard",
95115
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",

scriptInsertAs.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const colIsIdentityOrdinal = 6;
1313
const colComputedOrdinal = 7;
1414
const colDatetimePrecisionOrdinal = 8;
1515

16-
async function getSqlScriptAsInsertAsync(connectionProfile, tableCatalog, tableSchema, tableName)
16+
async function getSqlScriptAsInsertAsync(connectionProfile, tableCatalog, tableSchema, tableName, allowIdentityOn = false)
1717
{
1818
let queryText = sqlUtils.getColumnInfoQuerySql(tableCatalog, tableSchema, tableName);
1919

@@ -23,33 +23,49 @@ async function getSqlScriptAsInsertAsync(connectionProfile, tableCatalog, tableS
2323
throw "No se han obtenido resultados de la consulta";
2424
}
2525

26-
let insertSqlScript = buildFinalScript(results, tableCatalog, tableSchema, tableName);
26+
let insertSqlScript = buildFinalScript(results, tableCatalog, tableSchema, tableName, allowIdentityOn);
2727

2828
return insertSqlScript;
2929
}
3030

31-
function buildFinalScript(results, tableCatalog, tableSchema, tableName)
31+
function buildFinalScript(results, tableCatalog, tableSchema, tableName, allowIdentityOn)
3232
{
3333
let fullScript = [];
3434
let columsScriptPart = [];
3535
let valuesScriptPart = [];
3636

37-
fullScript.push(`INSERT INTO [${tableCatalog}].[${tableSchema}].[${tableName}]`);
38-
3937
columsScriptPart.push("(");
4038
valuesScriptPart.push("(");
4139

4240
let columnIndex = 0;
41+
let anyIdentityColumn = false;
4342

4443
for (let i= 0; i !== results.rowCount; i++)
4544
{
4645
let rowData = results.rows[i];
4746

48-
let isComputed = rowData[colComputedOrdinal].displayValue;
49-
let IsIdentity = rowData[colIsIdentityOrdinal].displayValue;
47+
let isComputedRaw = rowData[colComputedOrdinal].displayValue;
48+
let isIdentityRaw = rowData[colIsIdentityOrdinal].displayValue;
49+
let dataTypeRaw = rowData[colDataTypeOrdinal].displayValue;
50+
51+
let isComputedColumn = isComputedRaw === "1";
52+
let isIdentityColumn = isIdentityRaw === "1";
53+
let isTimeStampColumn = dataTypeRaw == "timestamp";
5054

51-
if(isComputed === "1" || IsIdentity === "1")
55+
if(isComputedColumn || isTimeStampColumn){
5256
continue;
57+
}
58+
59+
if(isIdentityColumn)
60+
{
61+
if(!allowIdentityOn){
62+
continue;
63+
}
64+
65+
if(!anyIdentityColumn){
66+
anyIdentityColumn = true;
67+
}
68+
}
5369

5470
const separator = (columnIndex === 0) ? " " : ",";
5571

@@ -67,9 +83,21 @@ function buildFinalScript(results, tableCatalog, tableSchema, tableName)
6783
columnIndex += 1;
6884
}
6985

86+
const printSetIdentity = allowIdentityOn && anyIdentityColumn;
87+
88+
if(printSetIdentity){
89+
fullScript.push(`SET IDENTITY_INSERT [${tableCatalog}].[${tableSchema}].[${tableName}] ON\n`);
90+
}
91+
92+
fullScript.push(`INSERT INTO [${tableCatalog}].[${tableSchema}].[${tableName}]`);
93+
7094
columsScriptPart.push(")");
7195
valuesScriptPart.push(")");
7296

97+
if(printSetIdentity){
98+
valuesScriptPart.push(`\nSET IDENTITY_INSERT [${tableCatalog}].[${tableSchema}].[${tableName}] OFF\n`);
99+
}
100+
73101
return fullScript.concat(columsScriptPart).concat(["VALUES"]).concat(valuesScriptPart).join('\n');
74102
}
75103

scriptUpdateAs.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,15 @@ function buildFinalScript(results, tableCatalog, tableSchema, tableName)
4141
{
4242
let rowData = results.rows[i];
4343

44-
let isComputed = rowData[colComputedOrdinal].displayValue;
45-
let IsIdentity = rowData[colIsIdentityOrdinal].displayValue;
44+
let isComputedRaw = rowData[colComputedOrdinal].displayValue;
45+
let isIdentityRaw = rowData[colIsIdentityOrdinal].displayValue;
46+
let dataTypeRaw = rowData[colDataTypeOrdinal].displayValue;
4647

47-
if(isComputed === "1" || IsIdentity === "1")
48+
let isComputedColumn = isComputedRaw === "1";
49+
let isIdentityColumn = isIdentityRaw === "1";
50+
let isTimeStampColumn = dataTypeRaw == "timestamp";
51+
52+
if(isComputedColumn || isIdentityColumn || isTimeStampColumn)
4853
continue;
4954

5055
const separator = (columnIndex === 0) ? " " : ",";

0 commit comments

Comments
 (0)