Skip to content

Commit 1cf2b3d

Browse files
committed
Ability to generate SQL from JSON
1 parent 9909b83 commit 1cf2b3d

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,20 @@
105105
"title": "Generate SQL",
106106
"category": "Db2 for i",
107107
"icon": "$(add)"
108-
}
108+
},
109+
{
110+
"command": "vscode-db2i.pasteGenerator",
111+
"title": "Paste JSON as SQL"
112+
}
109113
],
110114
"menus": {
115+
"editor/context": [
116+
{
117+
"command": "vscode-db2i.pasteGenerator",
118+
"group": "1_sql",
119+
"when": "editorLangId == sql"
120+
}
121+
],
111122
"view/title": [
112123
{
113124
"command": "vscode-db2i.addSchemaToSchemaBrowser",

src/extension.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
const vscode = require(`vscode`);
44
const schemaBrowser = require(`./views/schemaBrowser`);
55

6-
const Configuration = require(`./configuration`);
7-
6+
const JSONServices = require(`./language/json`);
87
const languageProvider = require(`./language/provider`);
98

109
// this method is called when your extension is activated
@@ -26,6 +25,7 @@ function activate(context) {
2625
),
2726
);
2827

28+
JSONServices.initialise(context);
2929
languageProvider.initialise(context);
3030
}
3131

src/language/json.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const vscode = require(`vscode`);
2+
3+
const Statement = require(`../database/statement`);
4+
5+
/**
6+
*
7+
* @param {vscode.ExtensionContext} context
8+
*/
9+
exports.initialise = (context) => {
10+
context.subscriptions.push(
11+
vscode.commands.registerCommand(`vscode-db2i.pasteGenerator`, async () => {
12+
try {
13+
const clipboard_content = await vscode.env.clipboard.readText();
14+
const parsedData = JSON.parse(clipboard_content);
15+
16+
const sql = exports.generateSQL(parsedData);
17+
const formatted = Statement.format(sql);
18+
19+
if (vscode.window.activeTextEditor) {
20+
vscode.window.activeTextEditor.edit((edit) => {
21+
edit.insert(vscode.window.activeTextEditor.selection.active, formatted);
22+
});
23+
}
24+
} catch (e) {
25+
vscode.window.showErrorMessage(`Error: ${e.message}`);
26+
}
27+
}),
28+
)
29+
}
30+
31+
exports.generateSQL = (jsonIn) => {
32+
if (Array.isArray(jsonIn)) {
33+
return generateArray(jsonIn);
34+
} else {
35+
return generateObject(jsonIn);
36+
}
37+
}
38+
39+
const generateArray = (elementIn) => {
40+
const firstValue = Array.isArray(elementIn) ? elementIn[0] : elementIn;
41+
if (typeof firstValue === `object`) {
42+
return `(SELECT json_arrayagg(${generateObject(firstValue)}) from SYSIBM.SYSDUMMY1)`
43+
} else {
44+
return `(SELECT json_arrayagg(${typeof firstValue === `string` ? `'${firstValue}'` : firstValue}) from SYSIBM.SYSDUMMY1)`
45+
}
46+
}
47+
48+
const generateObject = (objIn) => {
49+
const items = [];
50+
51+
Object.keys(objIn).forEach((key) => {
52+
const value = objIn[key];
53+
if (typeof value === `object`) {
54+
if (Array.isArray(value)) {
55+
items.push(`'${key}': ${generateArray(value[0])} format json`);
56+
} else {
57+
items.push(`'${key}': ${generateObject(value)}`);
58+
}
59+
} else {
60+
items.push(`'${key}': ${typeof value === `string` ? `'${value}'` : value}`);
61+
}
62+
});
63+
64+
return `json_object(${items.join(`, `)})`;
65+
}

0 commit comments

Comments
 (0)