Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CRUD operations in Results view #4458

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ database.controller('DatabaseController', function ($scope, $http, messageHub) {
let topLevelSchemaNode = node.parents.find(parentId => tree.get_node(parentId).original.kind === 'schema');
let topLevelSchemaName = tree.get_text(topLevelSchemaNode);

let sqlCommand = "SELECT * FROM \"" + topLevelSchemaName + "\"" + "." + "\"" + node.original.text + "\";\n";
messageHub.postMessage('database.sql.execute', sqlCommand);
messageHub.postMessage('result-view.database.sql.showContent', {
delchev marked this conversation as resolved.
Show resolved Hide resolved
schemaName: topLevelSchemaName,
tableName: node.original.text
});
}.bind(this)
};

Expand Down Expand Up @@ -635,16 +637,18 @@ database.controller('DatabaseController', function ($scope, $http, messageHub) {
}
};

let anonymizeMenu = function(node, data, type) {
let anonymizeMenu = function (node, data, type) {
let tree = $.jstree.reference(data.reference);
let columnNode = tree.get_node(data.reference);
let tableNode = findParentTableOfColumn(tree, node); //tree.get_node(tree.get_node(node.parent).parent);
let schemaNode = findParentSchemaOfColumn(tree, node); //tree.get_node(tree.get_node(tree.get_node(node.parent).parent).parent);

let primaryKeyName = tree.get_node(tree.get_node(node.parent).children[0]).original.name;
tree.get_node(node.parent).children.forEach(c => {if (tree.get_node(c).original.key) {
primaryKeyName = tree.get_node(c).original.name;
}});
tree.get_node(node.parent).children.forEach(c => {
if (tree.get_node(c).original.key) {
primaryKeyName = tree.get_node(c).original.name;
}
});

let parameters = {};
parameters.datasource = $scope.selectedDatasource;
Expand All @@ -657,7 +661,7 @@ database.controller('DatabaseController', function ($scope, $http, messageHub) {
messageHub.postMessage('database.data.anonymize.column', parameters);
}

let findParentTableOfColumn = function(tree, node) {
let findParentTableOfColumn = function (tree, node) {
let maybe = tree.get_node(node.parent);
if (!maybe.original.type && "Columns" === maybe.text) {
maybe = tree.get_node(maybe.parent);
Expand All @@ -668,7 +672,7 @@ database.controller('DatabaseController', function ($scope, $http, messageHub) {
return findParentTableOfColumn(tree, maybe);
}

let findParentSchemaOfColumn = function(tree, node) {
let findParentSchemaOfColumn = function (tree, node) {
let maybe = tree.get_node(node.parent);
if (!maybe.original.type && "Columns" === maybe.text) {
maybe = tree.get_node(maybe.parent);
Expand Down Expand Up @@ -749,7 +753,7 @@ database.controller('DatabaseController', function ($scope, $http, messageHub) {
});
}

let expandColumn = function(parent, column) {
let expandColumn = function (parent, column) {
let position = 'last';
let icon = "sap-icon--grid";
if (column.key) {
Expand Down Expand Up @@ -1007,12 +1011,12 @@ database.controller('DatabaseController', function ($scope, $http, messageHub) {
},
true
);

$scope.refresh = function () {
$scope.invalidateCache();
$scope.getDatabases();
}

$scope.invalidateCache = function () {
$http.get(databasesInvalidateSvcUrl);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { Controller, Get, Post, Put, Delete, response } from "sdk/http";
import { query } from "sdk/db";

@Controller
class CRUDService {

@Get("/")
async getAll(req, res) {
const { schemaName, tableName } = req.body;

const sqlQuery = `
SELECT * FROM "${schemaName}"."${tableName}";
`;

try {
const result = query.execute(sqlQuery);
res.send(result);
} catch (error) {
res.status(500).send({ error: "Failed to fetch rows", details: error });
}
}

@Post("/create")
async create(req, res) {
const { schemaName, tableName, data } = req.body;

if (!entityName || !data) {
return res.status(400).send({ error: "Entity name and data are required" });
}

const columns = Object.keys(data);
const values = Object.values(data);
const placeholders = columns.map(() => "?");

const sqlQuery = `
INSERT INTO "${schemaName}"."${tableName}"
(${columns.join(", ")})
VALUES (${placeholders.join(", ")});
`;

try {
query.execute(sqlQuery, values);
res.send({ success: true });
} catch (error) {
res.status(500).send({ error: "Failed to create row", details: error });
}
}

@Put("/update")
async update(req, res) {
const { tableName, schemaName, data, primaryKey } = req.body;
debugger
if (!entityName || !data || !primaryKey) {
return res.status(400).send({ error: "Entity name, data, and primary key are required" });
}

const setClauses = [];
const parameters = [];
const whereClauses = [];

Object.keys(data).forEach((key) => {
if (!primaryKey.includes(key)) {
setClauses.push(`"${key}" = ?`);
parameters.push(data[key]);
} else {
whereClauses.push(`"${key}" = ?`);
parameters.push(data[key]);
}
});

const sqlQuery = `
UPDATE "${schemaName}"."${tableName}"
SET ${setClauses.join(", ")}
WHERE ${whereClauses.join(" AND ")};
`;

try {
query.execute(sqlQuery, parameters);
res.send({ success: true });
} catch (error) {
res.status(500).send({ error: "Failed to update row", details: error });
}
}

@Delete("/delete")
async delete(req, res) {
const { schemaName, tableName, primaryKey } = req.body;

if (!entityName || !primaryKey) {
return res.status(400).send({ error: "Entity name and primary key are required" });
}

const whereClauses = [];
const parameters = [];

primaryKey.forEach((key) => {
whereClauses.push(`"${key}" = ?`);
parameters.push(req.body[key]);
});

const sqlQuery = `
DELETE FROM "${schemaName}"."${tableName}"
WHERE ${whereClauses.join(" AND ")};
`;

try {
query.execute(sqlQuery, parameters);
res.send({ success: true });
} catch (error) {
res.status(500).send({ error: "Failed to delete row", details: error });
}
}
}
Loading
Loading