Skip to content

Commit

Permalink
Ability to generate SQL from JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
worksofliam committed May 30, 2022
1 parent 9909b83 commit 1cf2b3d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,20 @@
"title": "Generate SQL",
"category": "Db2 for i",
"icon": "$(add)"
}
},
{
"command": "vscode-db2i.pasteGenerator",
"title": "Paste JSON as SQL"
}
],
"menus": {
"editor/context": [
{
"command": "vscode-db2i.pasteGenerator",
"group": "1_sql",
"when": "editorLangId == sql"
}
],
"view/title": [
{
"command": "vscode-db2i.addSchemaToSchemaBrowser",
Expand Down
4 changes: 2 additions & 2 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
const vscode = require(`vscode`);
const schemaBrowser = require(`./views/schemaBrowser`);

const Configuration = require(`./configuration`);

const JSONServices = require(`./language/json`);
const languageProvider = require(`./language/provider`);

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

JSONServices.initialise(context);
languageProvider.initialise(context);
}

Expand Down
65 changes: 65 additions & 0 deletions src/language/json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const vscode = require(`vscode`);

const Statement = require(`../database/statement`);

/**
*
* @param {vscode.ExtensionContext} context
*/
exports.initialise = (context) => {
context.subscriptions.push(
vscode.commands.registerCommand(`vscode-db2i.pasteGenerator`, async () => {
try {
const clipboard_content = await vscode.env.clipboard.readText();
const parsedData = JSON.parse(clipboard_content);

const sql = exports.generateSQL(parsedData);
const formatted = Statement.format(sql);

if (vscode.window.activeTextEditor) {
vscode.window.activeTextEditor.edit((edit) => {
edit.insert(vscode.window.activeTextEditor.selection.active, formatted);
});
}
} catch (e) {
vscode.window.showErrorMessage(`Error: ${e.message}`);
}
}),
)
}

exports.generateSQL = (jsonIn) => {
if (Array.isArray(jsonIn)) {
return generateArray(jsonIn);
} else {
return generateObject(jsonIn);
}
}

const generateArray = (elementIn) => {
const firstValue = Array.isArray(elementIn) ? elementIn[0] : elementIn;
if (typeof firstValue === `object`) {
return `(SELECT json_arrayagg(${generateObject(firstValue)}) from SYSIBM.SYSDUMMY1)`
} else {
return `(SELECT json_arrayagg(${typeof firstValue === `string` ? `'${firstValue}'` : firstValue}) from SYSIBM.SYSDUMMY1)`
}
}

const generateObject = (objIn) => {
const items = [];

Object.keys(objIn).forEach((key) => {
const value = objIn[key];
if (typeof value === `object`) {
if (Array.isArray(value)) {
items.push(`'${key}': ${generateArray(value[0])} format json`);
} else {
items.push(`'${key}': ${generateObject(value)}`);
}
} else {
items.push(`'${key}': ${typeof value === `string` ? `'${value}'` : value}`);
}
});

return `json_object(${items.join(`, `)})`;
}

0 comments on commit 1cf2b3d

Please sign in to comment.