From ee58fa0d77206fb78eaaab78c6c6ad7c1e01cb2b Mon Sep 17 00:00:00 2001 From: Francesco Date: Sun, 22 Oct 2023 16:13:58 +0200 Subject: [PATCH] WIP SQL console: enable flag; better table output --- .../dbadmin/external/DbAdminProperties.java | 33 +++++++++++------ .../controller/DefaultDbAdminController.java | 16 +++++++++ .../external/controller/GlobalController.java | 8 +++++ .../dbmapping/query/DbQueryOutputField.java | 36 +++++++++++++++++-- .../templates/fragments/generic_data_row.html | 9 ++++- .../templates/fragments/resources.html | 3 +- 6 files changed, 90 insertions(+), 15 deletions(-) diff --git a/src/main/java/tech/ailef/dbadmin/external/DbAdminProperties.java b/src/main/java/tech/ailef/dbadmin/external/DbAdminProperties.java index 0ac314c..5408d54 100644 --- a/src/main/java/tech/ailef/dbadmin/external/DbAdminProperties.java +++ b/src/main/java/tech/ailef/dbadmin/external/DbAdminProperties.java @@ -19,9 +19,6 @@ package tech.ailef.dbadmin.external; -import java.util.HashMap; -import java.util.Map; - import org.springframework.boot.context.properties.ConfigurationProperties; /** @@ -50,6 +47,11 @@ public class DbAdminProperties { */ private boolean testMode = false; + /** + * Whether the SQL console feature is enabled + */ + private boolean sqlConsoleEnabled = true; + /** * Whether Spring Boot Database Admin is enabled * @return @@ -61,6 +63,14 @@ public class DbAdminProperties { public void setEnabled(boolean enabled) { this.enabled = enabled; } + + public boolean isSqlConsoleEnabled() { + return sqlConsoleEnabled; + } + + public void setSqlConsoleEnabled(boolean sqlConsoleEnabled) { + this.sqlConsoleEnabled = sqlConsoleEnabled; + } /** * Returns the prefix that is prepended to all routes registered by Spring Boot Database Admin. @@ -94,14 +104,15 @@ public class DbAdminProperties { this.testMode = testMode; } - public Map toMap() { - Map conf = new HashMap<>(); - conf.put("enabled", enabled + ""); - conf.put("baseUrl", baseUrl); - conf.put("modelsPackage", modelsPackage); - conf.put("testMode", testMode + ""); - return conf; - } +// public Map toMap() { +// Map conf = new HashMap<>(); +// conf.put("enabled", enabled + ""); +// conf.put("baseUrl", baseUrl); +// conf.put("modelsPackage", modelsPackage); +// conf.put("testMode", testMode + ""); +// conf.put("sqlConsoleEnabled", sqlConsoleEnabled + ""); +// return conf; +// } } diff --git a/src/main/java/tech/ailef/dbadmin/external/controller/DefaultDbAdminController.java b/src/main/java/tech/ailef/dbadmin/external/controller/DefaultDbAdminController.java index 5f5657f..6b7ff0a 100644 --- a/src/main/java/tech/ailef/dbadmin/external/controller/DefaultDbAdminController.java +++ b/src/main/java/tech/ailef/dbadmin/external/controller/DefaultDbAdminController.java @@ -551,6 +551,10 @@ public class DefaultDbAdminController { @GetMapping("/console/new") public String consoleNew(Model model) { + if (!properties.isSqlConsoleEnabled()) { + throw new DbAdminException("SQL console not enabled"); + } + model.addAttribute("activePage", "console"); ConsoleQuery q = new ConsoleQuery(); @@ -560,6 +564,10 @@ public class DefaultDbAdminController { @GetMapping("/console") public String console(Model model) { + if (!properties.isSqlConsoleEnabled()) { + throw new DbAdminException("SQL console not enabled"); + } + List tabs = consoleQueryRepository.findAll(); if (tabs.isEmpty()) { @@ -574,6 +582,10 @@ public class DefaultDbAdminController { @PostMapping("/console/delete/{queryId}") public String consoleDelete(@PathVariable String queryId, Model model) { + if (!properties.isSqlConsoleEnabled()) { + throw new DbAdminException("SQL console not enabled"); + } + consoleQueryRepository.deleteById(queryId); return "redirect:/" + properties.getBaseUrl() + "/console"; } @@ -584,6 +596,10 @@ public class DefaultDbAdminController { public String consoleRun(Model model, @RequestParam(required = false) String query, @RequestParam(required = false) String queryTitle, @PathVariable String queryId) { + if (!properties.isSqlConsoleEnabled()) { + throw new DbAdminException("SQL console not enabled"); + } + ConsoleQuery activeQuery = consoleQueryRepository.findById(queryId).orElseThrow(() -> { return new DbAdminNotFoundException("Query with ID " + queryId + " not found."); }); diff --git a/src/main/java/tech/ailef/dbadmin/external/controller/GlobalController.java b/src/main/java/tech/ailef/dbadmin/external/controller/GlobalController.java index 33fcb6c..fa2813f 100644 --- a/src/main/java/tech/ailef/dbadmin/external/controller/GlobalController.java +++ b/src/main/java/tech/ailef/dbadmin/external/controller/GlobalController.java @@ -59,6 +59,7 @@ public class GlobalController { model.addAttribute("dbadmin_userConf", userConf); model.addAttribute("dbadmin_baseUrl", getBaseUrl()); model.addAttribute("dbadmin_version", dbAdmin.getVersion()); + model.addAttribute("dbadmin_properties", props); return "other/error"; } @@ -70,6 +71,7 @@ public class GlobalController { model.addAttribute("dbadmin_userConf", userConf); model.addAttribute("dbadmin_baseUrl", getBaseUrl()); model.addAttribute("dbadmin_version", dbAdmin.getVersion()); + model.addAttribute("dbadmin_properties", props); response.setStatus(404); return "other/error"; } @@ -118,5 +120,11 @@ public class GlobalController { public UserConfiguration getUserConf() { return userConf; } + + @ModelAttribute("dbadmin_properties") + public DbAdminProperties getProps() { + return props; + } + } diff --git a/src/main/java/tech/ailef/dbadmin/external/dbmapping/query/DbQueryOutputField.java b/src/main/java/tech/ailef/dbadmin/external/dbmapping/query/DbQueryOutputField.java index 16f1ab5..c089083 100644 --- a/src/main/java/tech/ailef/dbadmin/external/dbmapping/query/DbQueryOutputField.java +++ b/src/main/java/tech/ailef/dbadmin/external/dbmapping/query/DbQueryOutputField.java @@ -40,21 +40,53 @@ public class DbQueryOutputField { public boolean isPrimaryKey() { return dbField != null && dbField.isPrimaryKey(); } - + + /** + * Returns true if this field is a foreign key, only in the case + * the field has been mapped to a table + */ public boolean isForeignKey() { return dbField != null && dbField.isForeignKey(); } - + + /** + * Returns true if this field is a binary field (BLOB, etc.), only in the case + * the field has been mapped to a table + * @return + */ public boolean isBinary() { return dbField != null && dbField.isBinary(); } + /** + * Returns true if this field is mapped to a table + * @return + */ + public boolean isMapped() { + return dbField != null; + } + + /** + * Returns the type of the field, only in the case the field + * has been mapped to a table + * @return + */ public String getType() { if (dbField != null) return dbField.getType().toString(); return "-"; } + public String getJavaName() { + if (dbField == null) return null; + return dbField.getJavaName(); + } + + public String getEntityClassName() { + if (dbField == null) return null; + return dbField.getSchema().getClassName(); + } + @Override public int hashCode() { return Objects.hash(name, table); diff --git a/src/main/resources/templates/fragments/generic_data_row.html b/src/main/resources/templates/fragments/generic_data_row.html index dd27ec7..a1afdd8 100644 --- a/src/main/resources/templates/fragments/generic_data_row.html +++ b/src/main/resources/templates/fragments/generic_data_row.html @@ -12,7 +12,14 @@ - + + + BINARY + + + + + diff --git a/src/main/resources/templates/fragments/resources.html b/src/main/resources/templates/fragments/resources.html index a3fa93a..a637f4e 100644 --- a/src/main/resources/templates/fragments/resources.html +++ b/src/main/resources/templates/fragments/resources.html @@ -83,7 +83,8 @@ -
  • +