mirror of
https://github.com/dalbodeule/snap-admin.git
synced 2025-12-16 05:12:00 +09:00
WIP SQL console: tabs
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
package tech.ailef.dbadmin.external.controller;
|
||||
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -548,21 +549,52 @@ public class DefaultDbAdminController {
|
||||
return "help";
|
||||
}
|
||||
|
||||
@GetMapping("/console")
|
||||
public String console(Model model, @RequestParam(required=false) String query) {
|
||||
@GetMapping("/console/new")
|
||||
public String consoleNew(Model model) {
|
||||
model.addAttribute("activePage", "console");
|
||||
model.addAttribute("query", query == null ? "" : query);
|
||||
|
||||
ConsoleQuery q = new ConsoleQuery();
|
||||
consoleQueryRepository.save(q);
|
||||
return "redirect:/" + properties.getBaseUrl() + "/console/run/" + q.getId();
|
||||
}
|
||||
|
||||
@GetMapping("/console")
|
||||
public String console(Model model) {
|
||||
List<ConsoleQuery> tabs = consoleQueryRepository.findAll();
|
||||
|
||||
if (tabs.isEmpty()) {
|
||||
ConsoleQuery q = new ConsoleQuery();
|
||||
consoleQueryRepository.save(q);
|
||||
tabs.add(q);
|
||||
return "redirect:/" + properties.getBaseUrl() + "/console/run/" + q.getId();
|
||||
} else {
|
||||
return "redirect:/" + properties.getBaseUrl() + "/console/run/" + tabs.get(0).getId();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@GetMapping("/console/run/{queryId}")
|
||||
public String consoleRun(Model model, @RequestParam(required = false) String query, @PathVariable String queryId) {
|
||||
ConsoleQuery activeQuery = consoleQueryRepository.findById(queryId).orElseThrow(() -> {
|
||||
return new DbAdminNotFoundException("Query with ID " + queryId + " not found.");
|
||||
});
|
||||
|
||||
if (query != null && !query.isBlank()) {
|
||||
activeQuery.setSql(query);
|
||||
}
|
||||
|
||||
activeQuery.setUpdatedAt(LocalDateTime.now());
|
||||
consoleQueryRepository.save(activeQuery);
|
||||
|
||||
model.addAttribute("activePage", "console");
|
||||
model.addAttribute("activeQuery", activeQuery);
|
||||
|
||||
List<ConsoleQuery> tabs = consoleQueryRepository.findAll();
|
||||
model.addAttribute("tabs", tabs);
|
||||
if (query != null) {
|
||||
|
||||
if (activeQuery.getSql() != null && !activeQuery.getSql().isBlank()) {
|
||||
try {
|
||||
List<DbQueryResultRow> results = jdbTemplate.query(query, (rs, rowNum) -> {
|
||||
List<DbQueryResultRow> results = jdbTemplate.query(activeQuery.getSql(), (rs, rowNum) -> {
|
||||
Map<DbQueryOutputField, Object> result = new HashMap<>();
|
||||
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
@@ -585,8 +617,6 @@ public class DefaultDbAdminController {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return "console";
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package tech.ailef.dbadmin.internal.model;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import org.hibernate.annotations.UuidGenerator;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -16,9 +18,15 @@ public class ConsoleQuery {
|
||||
private String sql;
|
||||
|
||||
private String title;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
public ConsoleQuery() {
|
||||
this.title = "Untitled Query";
|
||||
this.createdAt = LocalDateTime.now();
|
||||
this.updatedAt = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
@@ -44,5 +52,28 @@ public class ConsoleQuery {
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public void setCreatedAt(LocalDateTime createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
public LocalDateTime getUpdatedAt() {
|
||||
return updatedAt;
|
||||
}
|
||||
|
||||
public void setUpdatedAt(LocalDateTime updatedAt) {
|
||||
this.updatedAt = updatedAt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ConsoleQuery [id=" + id + ", sql=" + sql + ", title=" + title + ", createdAt=" + createdAt
|
||||
+ ", updatedAt=" + updatedAt + "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user