Skip to content

Commit

Permalink
SQL query tool
Browse files Browse the repository at this point in the history
  • Loading branch information
BjoernKW committed Dec 23, 2022
1 parent 268e89e commit fc027d6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.bjoernkw</groupId>
<artifactId>schematic</artifactId>
<version>0.0.8-SNAPSHOT</version>
<version>0.0.8</version>
<name>Schematic</name>
<description>Database management UI for Spring Boot</description>
<properties>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/bjoernkw/schematic/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

class Column {

String columnName;
private String columnName;

String dataType;
private String dataType;

public String getColumnName() {
return columnName;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/bjoernkw/schematic/IndexController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

@Controller
@RequestMapping("/")
public class IndexController {
class IndexController {

@GetMapping
public String redirect() {
String redirect() {
return "redirect:/schematic/tables";
}
}
16 changes: 13 additions & 3 deletions src/main/java/com/bjoernkw/schematic/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

class Table {

String tableName;
private String tableName;

List<Column> columns;
private List<Column> columns;

List<Map<String, Object>> rows;
private List<Map<String, Object>> rows;

private Boolean isQueryResult;

public String getTableName() {
return tableName;
Expand All @@ -34,4 +36,12 @@ public List<Map<String, Object>> getRows() {
public void setRows(List<Map<String, Object>> rows) {
this.rows = rows;
}

public Boolean getQueryResult() {
return isQueryResult;
}

public void setQueryResult(Boolean queryResult) {
isQueryResult = queryResult;
}
}
45 changes: 39 additions & 6 deletions src/main/java/com/bjoernkw/schematic/TablesController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
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")
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) {
Expand All @@ -34,6 +35,38 @@ public String listTables(Model model) {
return "index";
}

@GetMapping(params = "sqlQuery")
@HxRequest
public String queryDatabase(@RequestParam String sqlQuery, Model model) {
List<Table> tables = new ArrayList<>();
Table queryResultTable = new Table();
tables.add(queryResultTable);

queryResultTable.setTableName("queryResult");
queryResultTable.setQueryResult(true);

List<Map<String, Object>> queryResult = jdbcTemplate.queryForList(sqlQuery);
queryResultTable.setRows(queryResult);

List<Column> 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) {
Expand All @@ -47,7 +80,7 @@ public String dropTable(@PathVariable String tableName, Model model) {
getTables()
);

return "fragments/tables";
return TABLE_VIEW_FRAGMENT_NAME;
}

@DeleteMapping("/{tableName}/truncate")
Expand All @@ -63,7 +96,7 @@ public String truncateTable(@PathVariable String tableName, Model model) {
getTables()
);

return "fragments/tables";
return TABLE_VIEW_FRAGMENT_NAME;
}

private List<Table> getTables() {
Expand Down
9 changes: 5 additions & 4 deletions src/main/resources/templates/fragments/tables.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
<i class="fas fa-eye show-table-icon"></i>
<i class="fas fa-eye-slash hide-table-icon"></i>
</button>
<button class="btn btn-sm btn-outline-warning"
<button th:unless="${table.queryResult}" class="btn btn-sm btn-outline-warning"
title="Delete rows"
data-bs-toggle="modal"
th:attr="data-bs-target='#deleteRowsModal_' + ${table.tableName}">
<i class="fas fa-eraser"></i>
</button>
<button class="btn btn-sm btn-outline-danger"
<button th:unless="${table.queryResult}" class="btn btn-sm btn-outline-danger"
title="Drop table"
data-bs-toggle="modal"
th:attr="data-bs-target='#dropTableModal_' + ${table.tableName}">
Expand All @@ -31,11 +31,12 @@
<table th:id="'collapsible_' + ${table.tableName}"
class="table table-striped table-hover collapse show"
th:if="${table.columns}">
<caption>showing the first 10 rows here</caption>
<caption th:unless="${table.queryResult}">showing the first 10 rows here</caption>
<tbody>
<tr>
<td th:each="column : ${table.columns}">
<strong>[[${column.columnName}]]</strong> ([[${column.dataType}]])
<strong>[[${column.columnName}]]</strong>
<span th:if="${column.dataType}"> ([[${column.dataType}]])</span>
</td>
</tr>
<tr th:each="entry : ${table.rows}">
Expand Down
22 changes: 22 additions & 0 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,35 @@
<html lang="en"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:hx="https://github.com/wimdeblauwe/htmx-spring-boot-thymeleaf"
layout:decorate="~{layouts/layout}"
th:with="headline='Welcome to Schematic - a database management UI for Spring Boot!'">
<head>
<title>Schematic - a database management UI for Spring Boot</title>
</head>
<section class="section pt-5" layout:fragment="page-content">
<div class="container pt-5">
<h2>
Query database
<button class="btn btn-sm btn-info"
title="Hide / show database query tool"
data-bs-toggle="collapse"
data-bs-target="#collapsible_query_database">
<i class="fas fa-eye show-table-icon"></i>
<i class="fas fa-eye-slash hide-table-icon"></i>
</button>
</h2>
<div id="collapsible_query_database"
class="col-12 mb-4 p-4 border bg-success bg-opacity-10 collapse show">
<form hx:get="@{/schematic/tables}"
hx-target="#tables">
<div class="mb-3">
<label for="sqlQuery" class="form-label">SQL query</label>
<textarea class="form-control" id="sqlQuery" name="sqlQuery" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<h2>Tables</h2>
<div id="tables" class="smooth" th:insert="~{fragments/tables}"></div>
</div>
Expand Down

0 comments on commit fc027d6

Please sign in to comment.