diff --git a/pom.xml b/pom.xml index ab031f7..93fd0b8 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.bjoernkw schematic - 0.0.8-SNAPSHOT + 0.0.8 Schematic Database management UI for Spring Boot diff --git a/src/main/java/com/bjoernkw/schematic/Column.java b/src/main/java/com/bjoernkw/schematic/Column.java index 5fd2360..8c81bcc 100644 --- a/src/main/java/com/bjoernkw/schematic/Column.java +++ b/src/main/java/com/bjoernkw/schematic/Column.java @@ -2,9 +2,9 @@ class Column { - String columnName; + private String columnName; - String dataType; + private String dataType; public String getColumnName() { return columnName; diff --git a/src/main/java/com/bjoernkw/schematic/IndexController.java b/src/main/java/com/bjoernkw/schematic/IndexController.java index 4f8d45e..7ca8259 100644 --- a/src/main/java/com/bjoernkw/schematic/IndexController.java +++ b/src/main/java/com/bjoernkw/schematic/IndexController.java @@ -6,10 +6,10 @@ @Controller @RequestMapping("/") -public class IndexController { +class IndexController { @GetMapping - public String redirect() { + String redirect() { return "redirect:/schematic/tables"; } } diff --git a/src/main/java/com/bjoernkw/schematic/Table.java b/src/main/java/com/bjoernkw/schematic/Table.java index 1893c01..402eac0 100644 --- a/src/main/java/com/bjoernkw/schematic/Table.java +++ b/src/main/java/com/bjoernkw/schematic/Table.java @@ -5,11 +5,13 @@ class Table { - String tableName; + private String tableName; - List columns; + private List columns; - List> rows; + private List> rows; + + private Boolean isQueryResult; public String getTableName() { return tableName; @@ -34,4 +36,12 @@ public List> getRows() { public void setRows(List> rows) { this.rows = rows; } + + public Boolean getQueryResult() { + return isQueryResult; + } + + public void setQueryResult(Boolean queryResult) { + isQueryResult = queryResult; + } } diff --git a/src/main/java/com/bjoernkw/schematic/TablesController.java b/src/main/java/com/bjoernkw/schematic/TablesController.java index ce3e602..ac77068 100644 --- a/src/main/java/com/bjoernkw/schematic/TablesController.java +++ b/src/main/java/com/bjoernkw/schematic/TablesController.java @@ -5,12 +5,11 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.*; +import java.util.ArrayList; import java.util.List; +import java.util.Map; @Controller @RequestMapping("/schematic/tables") @@ -18,6 +17,8 @@ public class TablesController { private static final String VIEW_MODEL_NAME = "tables"; + private static final String TABLE_VIEW_FRAGMENT_NAME = "fragments/tables"; + private final JdbcTemplate jdbcTemplate; public TablesController(JdbcTemplate jdbcTemplate) { @@ -34,6 +35,38 @@ public String listTables(Model model) { return "index"; } + @GetMapping(params = "sqlQuery") + @HxRequest + public String queryDatabase(@RequestParam String sqlQuery, Model model) { + List tables = new ArrayList<>(); + Table queryResultTable = new Table(); + tables.add(queryResultTable); + + queryResultTable.setTableName("queryResult"); + queryResultTable.setQueryResult(true); + + List> queryResult = jdbcTemplate.queryForList(sqlQuery); + queryResultTable.setRows(queryResult); + + List columns = new ArrayList<>(); + queryResult.forEach(row -> row.forEach((key, value) -> { + Column column = new Column(); + column.setColumnName(key); + + columns.add(column); + })); + queryResultTable.setColumns(columns); + + tables.addAll(getTables()); + + model.addAttribute( + VIEW_MODEL_NAME, + tables + ); + + return TABLE_VIEW_FRAGMENT_NAME; + } + @DeleteMapping("/{tableName}") @HxRequest public String dropTable(@PathVariable String tableName, Model model) { @@ -47,7 +80,7 @@ public String dropTable(@PathVariable String tableName, Model model) { getTables() ); - return "fragments/tables"; + return TABLE_VIEW_FRAGMENT_NAME; } @DeleteMapping("/{tableName}/truncate") @@ -63,7 +96,7 @@ public String truncateTable(@PathVariable String tableName, Model model) { getTables() ); - return "fragments/tables"; + return TABLE_VIEW_FRAGMENT_NAME; } private List
getTables() { diff --git a/src/main/resources/templates/fragments/tables.html b/src/main/resources/templates/fragments/tables.html index 63b1ddf..22e2012 100644 --- a/src/main/resources/templates/fragments/tables.html +++ b/src/main/resources/templates/fragments/tables.html @@ -14,13 +14,13 @@ - -
- + diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 5e746bf..174928d 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -2,6 +2,7 @@ @@ -9,6 +10,27 @@
+

+ Query database + +

+
+
+
+ + +
+ + +

Tables

showing the first 10 rows hereshowing the first 10 rows here
- [[${column.columnName}]] ([[${column.dataType}]]) + [[${column.columnName}]] + ([[${column.dataType}]])