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

Commit 4891cfa

Browse files
authored
Merge pull request #23 from olegrumiancev/master
Added support for MySQL provider
2 parents 2536dda + 2c76134 commit 4891cfa

File tree

7 files changed

+255
-36
lines changed

7 files changed

+255
-36
lines changed

extension.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const vscode = require('vscode');
22
const {getSqlScriptAsInsertAsync} = require('./scriptInsertAs.js');
33
const {getSqlScriptAsUpdateAsync} = require('./scriptUpdateAs.js');
44
const {getSqlScriptAsSelectAsync} = require('./scriptSelectAs.js');
5+
const {getDeleteSqlScript} = require('./scriptDeleteAs.js');
56
const sqlUtils = require('./scriptSqlUtils.js');
67

78
const databaseNotFoundMessage = 'Database name not found.';
@@ -61,8 +62,8 @@ function activate(context) {
6162
{ name: "extraSqlScriptAs.insertTableIdentityOn", func: getSqlScriptAsInsertAsync, identityOn: true },
6263
{ name: "extraSqlScriptAs.updateTableToClipboard", func: getSqlScriptAsUpdateAsync, clipboard: true },
6364
{ 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)) },
65+
{ name: "extraSqlScriptAs.deleteTableToClipboard", func: (profile, db, schema, table) => Promise.resolve(getDeleteSqlScript(profile, db, schema, table)), clipboard: true },
66+
{ name: "extraSqlScriptAs.deleteTable", func: (profile, db, schema, table) => Promise.resolve(getDeleteSqlScript(profile, db, schema, table)) },
6667
{ name: "extraSqlScriptAs.selectTableToClipboard", func: getSqlScriptAsSelectAsync, clipboard: true },
6768
{ name: "extraSqlScriptAs.selectTable", func: getSqlScriptAsSelectAsync },
6869
];

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "extra-sql-script-as",
3-
"displayName": "Extra Sql Script As",
3+
"displayName": "Extra Sql Script As (MSSQL + MySQL)",
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.6.3",
6+
"version": "0.7.0",
77
"license": "https://raw.githubusercontent.com/pacoweb/extraSqlScriptAs/master/LICENSE",
88
"icon": "images/default_icon.png",
99
"repository": {
@@ -83,14 +83,14 @@
8383
"menus": {
8484
"objectExplorer/item/context": [
8585
{
86-
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
86+
"when": "(connectionProvider == MSSQL || connectionProvider == MySQL) && nodeType && nodeType == Table",
8787
"group":"extraSqlScriptAs",
8888
"submenu":"extraSqlScriptAs.submenu"
8989
}],
9090
"extraSqlScriptAs.submenu":[
9191
{
9292
"command": "extraSqlScriptAs.insertTable",
93-
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
93+
"when": "(connectionProvider == MSSQL || connectionProvider == MySQL) && nodeType && nodeType == Table",
9494
"group":"extraSqlScriptAs"
9595
},
9696
{
@@ -100,22 +100,22 @@
100100
},
101101
{
102102
"command": "extraSqlScriptAs.updateTable",
103-
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
103+
"when": "(connectionProvider == MSSQL || connectionProvider == MySQL) && nodeType && nodeType == Table",
104104
"group":"extraSqlScriptAs"
105105
},
106106
{
107107
"command": "extraSqlScriptAs.deleteTable",
108-
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
108+
"when": "(connectionProvider == MSSQL || connectionProvider == MySQL) && nodeType && nodeType == Table",
109109
"group":"extraSqlScriptAs"
110110
},
111111
{
112112
"command": "extraSqlScriptAs.selectTable",
113-
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
113+
"when": "(connectionProvider == MSSQL || connectionProvider == MySQL) && nodeType && nodeType == Table",
114114
"group":"extraSqlScriptAs"
115115
},
116116
{
117117
"command": "extraSqlScriptAs.insertTableToClipboard",
118-
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
118+
"when": "(connectionProvider == MSSQL || connectionProvider == MySQL) && nodeType && nodeType == Table",
119119
"group":"extraSqlScriptAs"
120120
},
121121
{
@@ -125,17 +125,17 @@
125125
},
126126
{
127127
"command": "extraSqlScriptAs.updateTableToClipboard",
128-
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
128+
"when": "(connectionProvider == MSSQL || connectionProvider == MySQL) && nodeType && nodeType == Table",
129129
"group":"extraSqlScriptAs"
130130
},
131131
{
132132
"command": "extraSqlScriptAs.deleteTableToClipboard",
133-
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
133+
"when": "(connectionProvider == MSSQL || connectionProvider == MySQL) && nodeType && nodeType == Table",
134134
"group":"extraSqlScriptAs"
135135
},
136136
{
137137
"command": "extraSqlScriptAs.selectTableToClipboard",
138-
"when": "connectionProvider == MSSQL && nodeType && nodeType == Table",
138+
"when": "(connectionProvider == MSSQL || connectionProvider == MySQL) && nodeType && nodeType == Table",
139139
"group":"extraSqlScriptAs"
140140
}
141141
]

scriptDeleteAs.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const vscode = require('vscode');
4+
const sqlUtils = require('./scriptSqlUtils.js');
5+
6+
function getDeleteSqlScript(connectionProfile, tableCatalog, tableSchema, tableName)
7+
{
8+
let provider = connectionProfile.providerName;
9+
let queryText = "[FAILED TO RESOLVE QUERY TEXT]";
10+
if (provider === "MSSQL") {
11+
queryText = `DELETE FROM [${tableCatalog}].[${tableSchema}].[${tableName}]
12+
WHERE <Search Conditions,,>`;
13+
}
14+
else if (provider === "MySQL") {
15+
queryText = `DELETE FROM \`${tableSchema}\`.\`${tableName}\`
16+
WHERE <Search Conditions,,>`;
17+
}
18+
return queryText;
19+
}
20+
21+
module.exports.getDeleteSqlScript = getDeleteSqlScript;

scriptInsertAs.js

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,32 @@ const colDatetimePrecisionOrdinal = 8;
1515

1616
async function getSqlScriptAsInsertAsync(connectionProfile, tableCatalog, tableSchema, tableName, allowIdentityOn = false)
1717
{
18-
let queryText = sqlUtils.getColumnInfoQuerySql(tableCatalog, tableSchema, tableName);
19-
20-
let results = await sqlUtils.getResultsFromQuerySql(connectionProfile, "MSSQL", queryText, tableCatalog);
18+
let provider = connectionProfile.providerName;
19+
let queryText = "[FAILED TO RESOLVE QUERY TEXT]";
20+
if (provider === "MSSQL") {
21+
queryText = sqlUtils.getColumnInfoQuerySql(tableCatalog, tableSchema, tableName);
22+
}
23+
else if (provider === "MySQL") {
24+
queryText = sqlUtils.getColumnInfoQueryMySql(tableCatalog, tableSchema, tableName);
25+
}
2126

27+
let results = await sqlUtils.getResultsFromQuerySql(connectionProfile, provider, queryText, tableCatalog);
2228
if (!results || results.rowCount === 0) {
23-
throw "No se han obtenido resultados de la consulta";
29+
throw "No results";
2430
}
2531

26-
let insertSqlScript = buildFinalScript(results, tableCatalog, tableSchema, tableName, allowIdentityOn);
32+
let insertSqlScript = "...";
33+
if (provider === "MSSQL") {
34+
insertSqlScript = buildFinalScriptMSSQL(results, tableCatalog, tableSchema, tableName, allowIdentityOn);
35+
}
36+
else if (provider === "MySQL") {
37+
insertSqlScript = buildFinalScriptMySQL(results, tableCatalog, tableSchema, tableName, allowIdentityOn);
38+
}
2739

2840
return insertSqlScript;
2941
}
3042

31-
function buildFinalScript(results, tableCatalog, tableSchema, tableName, allowIdentityOn)
43+
function buildFinalScriptMSSQL(results, tableCatalog, tableSchema, tableName, allowIdentityOn)
3244
{
3345
let fullScript = [];
3446
let columsScriptPart = [];
@@ -101,4 +113,79 @@ function buildFinalScript(results, tableCatalog, tableSchema, tableName, allowId
101113
return fullScript.concat(columsScriptPart).concat(["VALUES"]).concat(valuesScriptPart).join('\n');
102114
}
103115

116+
function buildFinalScriptMySQL(results, tableCatalog, tableSchema, tableName, allowIdentityOn)
117+
{
118+
let fullScript = [];
119+
let columsScriptPart = [];
120+
let valuesScriptPart = [];
121+
122+
columsScriptPart.push("(");
123+
valuesScriptPart.push("(");
124+
125+
let columnIndex = 0;
126+
let anyIdentityColumn = false;
127+
128+
for (let i= 0; i !== results.rowCount; i++)
129+
{
130+
let rowData = results.rows[i];
131+
132+
let isComputedRaw = rowData[colComputedOrdinal].displayValue;
133+
let isIdentityRaw = rowData[colIsIdentityOrdinal].displayValue;
134+
let dataTypeRaw = rowData[colDataTypeOrdinal].displayValue;
135+
136+
let isComputedColumn = isComputedRaw === "1";
137+
let isIdentityColumn = isIdentityRaw === "1";
138+
let isTimeStampColumn = dataTypeRaw == "timestamp";
139+
140+
if(isComputedColumn || isTimeStampColumn){
141+
continue;
142+
}
143+
144+
if(isIdentityColumn)
145+
{
146+
if(!allowIdentityOn){
147+
continue;
148+
}
149+
150+
if(!anyIdentityColumn){
151+
anyIdentityColumn = true;
152+
}
153+
}
154+
155+
const separator = (columnIndex === 0) ? " " : ",";
156+
157+
columsScriptPart.push("\t\t" + separator + "`" + rowData[colNameOrdinal].displayValue + "`");
158+
159+
valuesScriptPart.push("\t\t" + separator + sqlUtils.getColTypeString(
160+
rowData[colDataTypeOrdinal].displayValue,
161+
rowData[colCharsMaxLenOrdinal].displayValue,
162+
rowData[colNumericPrecisionOrdinal].displayValue,
163+
rowData[colNumerocScaleOrdinal].displayValue,
164+
rowData[colIsNullableOrdinal].displayValue,
165+
rowData[colDatetimePrecisionOrdinal].displayValue
166+
));
167+
168+
columnIndex += 1;
169+
}
170+
171+
const printSetIdentity = allowIdentityOn && anyIdentityColumn;
172+
173+
if(printSetIdentity){
174+
// no need in MySQL
175+
//fullScript.push(`SET IDENTITY_INSERT \`${tableSchema}\`.\`${tableName}\` ON;\n`);
176+
}
177+
178+
fullScript.push(`INSERT INTO \`${tableSchema}\`.\`${tableName}\``);
179+
180+
columsScriptPart.push(")");
181+
valuesScriptPart.push(");");
182+
183+
if(printSetIdentity){
184+
// no need in MySQL
185+
//valuesScriptPart.push(`\nSET IDENTITY_INSERT \`${tableSchema}\`.\`${tableName}\` OFF;\n`);
186+
}
187+
188+
return fullScript.concat(columsScriptPart).concat(["VALUES"]).concat(valuesScriptPart).join('\n');
189+
}
190+
104191
module.exports.getSqlScriptAsInsertAsync = getSqlScriptAsInsertAsync;

scriptSelectAs.js

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,33 @@ const colNameOrdinal = 0;
66

77
async function getSqlScriptAsSelectAsync(connectionProfile, tableCatalog, tableSchema, tableName)
88
{
9-
let queryText = sqlUtils.getColumnInfoQuerySql(tableCatalog, tableSchema, tableName);
9+
let provider = connectionProfile.providerName;
10+
let queryText = "[FAILED TO RESOLVE QUERY TEXT]";
11+
if (provider === "MSSQL") {
12+
queryText = sqlUtils.getColumnInfoQuerySql(tableCatalog, tableSchema, tableName);
13+
}
14+
else if (provider === "MySQL") {
15+
queryText = sqlUtils.getColumnInfoQueryMySql(tableCatalog, tableSchema, tableName);
16+
}
1017

11-
let results = await sqlUtils.getResultsFromQuerySql(connectionProfile, "MSSQL", queryText, tableCatalog);
18+
let results = await sqlUtils.getResultsFromQuerySql(connectionProfile, provider, queryText, tableCatalog);
1219

1320
if (!results || results.rowCount === 0) {
14-
throw "No se han obtenido resultados de la consulta";
21+
throw "No results";
1522
}
1623

17-
let updateSqlScript = buildFinalScript(results, tableCatalog, tableSchema, tableName);
24+
let selectSqlScript = "...";
25+
if (provider === "MSSQL") {
26+
selectSqlScript = buildFinalScriptMSSQL(results, tableCatalog, tableSchema, tableName);
27+
}
28+
else if (provider === "MySQL") {
29+
selectSqlScript = buildFinalScriptMySQL(results, tableCatalog, tableSchema, tableName);
30+
}
1831

19-
return updateSqlScript;
32+
return selectSqlScript;
2033
}
2134

22-
function buildFinalScript(results, tableCatalog, tableSchema, tableName)
35+
function buildFinalScriptMSSQL(results, tableCatalog, tableSchema, tableName)
2336
{
2437
let fullScript = [];
2538
let columsScriptPart = [];
@@ -42,4 +55,27 @@ function buildFinalScript(results, tableCatalog, tableSchema, tableName)
4255
return fullScript.concat(columsScriptPart).concat([`FROM [${tableCatalog}].[${tableSchema}].[${tableName}] `]).join('\n');
4356
}
4457

58+
function buildFinalScriptMySQL(results, tableCatalog, tableSchema, tableName)
59+
{
60+
let fullScript = [];
61+
let columsScriptPart = [];
62+
63+
fullScript.push("SELECT ");
64+
65+
let columnIndex = 0;
66+
67+
for (let i= 0; i !== results.rowCount; i++)
68+
{
69+
let rowData = results.rows[i];
70+
71+
const separator = (columnIndex === 0) ? " " : ",";
72+
73+
columsScriptPart.push("\t\t" + separator + "`" + rowData[colNameOrdinal].displayValue + "`");
74+
75+
columnIndex += 1;
76+
}
77+
78+
return fullScript.concat(columsScriptPart).concat([`FROM \`${tableSchema}\`.\`${tableName}\` `, `LIMIT 1000`, `;`]).join('\n');
79+
}
80+
4581
module.exports.getSqlScriptAsSelectAsync = getSqlScriptAsSelectAsync;

scriptSqlUtils.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11

22
const azdata = require('azdata');
33

4-
function getDeleteSqlScript(tableCatalog, tableSchema, tableName)
5-
{
6-
return `DELETE FROM [${tableCatalog}].[${tableSchema}].[${tableName}]
7-
WHERE <Search Conditions,,>`;
4+
function getColumnInfoQueryMySql(tableCatalog, tableSchema, tableName) {
5+
return `SELECT
6+
C.COLUMN_NAME,
7+
C.DATA_TYPE,
8+
C.CHARACTER_MAXIMUM_LENGTH,
9+
C.NUMERIC_PRECISION,
10+
C.NUMERIC_SCALE,
11+
C.IS_NULLABLE,
12+
(CASE WHEN EXTRA LIKE '%auto_increment%' THEN 'YES' ELSE 'NO' END) AS IS_IDENTITY,
13+
(CASE WHEN EXTRA LIKE '%VIRTUAL GENERATED%' OR EXTRA LIKE '%STORED GENERATED%' THEN 'YES' ELSE 'NO' END) AS IS_COMPUTED,
14+
C.DATETIME_PRECISION
15+
FROM
16+
INFORMATION_SCHEMA.TABLES AS T
17+
INNER JOIN
18+
INFORMATION_SCHEMA.COLUMNS AS C ON C.TABLE_NAME = T.TABLE_NAME AND C.TABLE_SCHEMA = T.TABLE_SCHEMA
19+
WHERE
20+
T.TABLE_TYPE = 'BASE TABLE'
21+
AND T.TABLE_SCHEMA = '${tableSchema}' -- Ensure this is the correct catalog name
22+
AND T.TABLE_NAME = '${tableName}'
23+
ORDER BY
24+
C.ORDINAL_POSITION;`;
825
}
926

1027
function getColumnInfoQuerySql(tableCatalog, tableSchema, tableName)
@@ -111,6 +128,6 @@ function getColTypeString (dataType, charMaxLen, numericPrecision, numericScale,
111128
}
112129

113130
module.exports.getResultsFromQuerySql = getResultsFromQuerySql;
131+
module.exports.getColumnInfoQueryMySql = getColumnInfoQueryMySql;
114132
module.exports.getColTypeString = getColTypeString;
115-
module.exports.getColumnInfoQuerySql = getColumnInfoQuerySql;
116-
module.exports.getDeleteSqlScript = getDeleteSqlScript;
133+
module.exports.getColumnInfoQuerySql = getColumnInfoQuerySql;

0 commit comments

Comments
 (0)