mirror of
https://github.com/dalbodeule/snap-admin.git
synced 2025-06-08 21:38:21 +00:00
WIP SQL console: tabs
This commit is contained in:
parent
0691d17867
commit
471c5ac18a
@ -30,6 +30,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
import org.hibernate.id.IdentifierGenerationException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.jdbc.UncategorizedSQLException;
|
||||
@ -69,8 +70,10 @@ import tech.ailef.dbadmin.external.exceptions.DbAdminException;
|
||||
import tech.ailef.dbadmin.external.exceptions.DbAdminNotFoundException;
|
||||
import tech.ailef.dbadmin.external.exceptions.InvalidPageException;
|
||||
import tech.ailef.dbadmin.external.misc.Utils;
|
||||
import tech.ailef.dbadmin.internal.model.ConsoleQuery;
|
||||
import tech.ailef.dbadmin.internal.model.UserAction;
|
||||
import tech.ailef.dbadmin.internal.model.UserSetting;
|
||||
import tech.ailef.dbadmin.internal.repository.ConsoleQueryRepository;
|
||||
import tech.ailef.dbadmin.internal.repository.UserSettingsRepository;
|
||||
import tech.ailef.dbadmin.internal.service.UserActionService;
|
||||
|
||||
@ -92,6 +95,9 @@ public class DefaultDbAdminController {
|
||||
@Autowired
|
||||
private UserActionService userActionService;
|
||||
|
||||
@Autowired
|
||||
private ConsoleQueryRepository consoleQueryRepository;
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbTemplate;
|
||||
|
||||
@ -546,25 +552,37 @@ public class DefaultDbAdminController {
|
||||
public String console(Model model, @RequestParam(required=false) String query) {
|
||||
model.addAttribute("activePage", "console");
|
||||
model.addAttribute("query", query == null ? "" : query);
|
||||
List<ConsoleQuery> tabs = consoleQueryRepository.findAll();
|
||||
|
||||
if (tabs.isEmpty()) {
|
||||
ConsoleQuery q = new ConsoleQuery();
|
||||
consoleQueryRepository.save(q);
|
||||
tabs.add(q);
|
||||
}
|
||||
model.addAttribute("tabs", tabs);
|
||||
if (query != null) {
|
||||
List<DbQueryResultRow> results = jdbTemplate.query(query, (rs, rowNum) -> {
|
||||
Map<DbQueryOutputField, Object> result = new HashMap<>();
|
||||
try {
|
||||
List<DbQueryResultRow> results = jdbTemplate.query(query, (rs, rowNum) -> {
|
||||
Map<DbQueryOutputField, Object> result = new HashMap<>();
|
||||
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
int cols = metaData.getColumnCount();
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
int cols = metaData.getColumnCount();
|
||||
|
||||
for (int i = 0; i < cols; i++) {
|
||||
Object o = rs.getObject(i + 1);
|
||||
String columnName = metaData.getColumnName(i + 1);
|
||||
String tableName = metaData.getTableName(i + 1);
|
||||
DbQueryOutputField field = new DbQueryOutputField(columnName, tableName, dbAdmin);
|
||||
for (int i = 0; i < cols; i++) {
|
||||
Object o = rs.getObject(i + 1);
|
||||
String columnName = metaData.getColumnName(i + 1);
|
||||
String tableName = metaData.getTableName(i + 1);
|
||||
DbQueryOutputField field = new DbQueryOutputField(columnName, tableName, dbAdmin);
|
||||
|
||||
result.put(field, o);
|
||||
}
|
||||
result.put(field, o);
|
||||
}
|
||||
|
||||
return new DbQueryResultRow(result, query);
|
||||
});
|
||||
model.addAttribute("results", new DbQueryResult(results));
|
||||
return new DbQueryResultRow(result, query);
|
||||
});
|
||||
model.addAttribute("results", new DbQueryResult(results));
|
||||
} catch (DataAccessException e) {
|
||||
model.addAttribute("error", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -50,7 +50,9 @@ public class DbQueryOutputField {
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return "TODO TYPE";
|
||||
if (dbField != null)
|
||||
return dbField.getType().toString();
|
||||
return "-";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,48 @@
|
||||
package tech.ailef.dbadmin.internal.model;
|
||||
|
||||
import org.hibernate.annotations.UuidGenerator;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Lob;
|
||||
|
||||
@Entity
|
||||
public class ConsoleQuery {
|
||||
@Id
|
||||
@UuidGenerator
|
||||
private String id;
|
||||
|
||||
@Lob
|
||||
private String sql;
|
||||
|
||||
private String title;
|
||||
|
||||
public ConsoleQuery() {
|
||||
this.title = "Untitled Query";
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getSql() {
|
||||
return sql;
|
||||
}
|
||||
|
||||
public void setSql(String sql) {
|
||||
this.sql = sql;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Spring Boot Database Admin - An automatically generated CRUD admin UI for Spring Boot apps
|
||||
* Copyright (C) 2023 Ailef (http://ailef.tech)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
package tech.ailef.dbadmin.internal.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import tech.ailef.dbadmin.internal.model.ConsoleQuery;
|
||||
|
||||
@Repository
|
||||
public interface ConsoleQueryRepository extends JpaRepository<ConsoleQuery, String>, CustomActionRepository {
|
||||
|
||||
}
|
@ -11,16 +11,27 @@
|
||||
<h1 class="fw-bold mb-4"><i class="align-middle bi bi-terminal"></i><span class="align-middle"> SQL console</span></h1>
|
||||
<div class="row mt-4">
|
||||
<div class="col">
|
||||
<div class="box">
|
||||
<div class="w-100 d-flex inner-navigation">
|
||||
<a th:each="query : ${tabs}" th:href="|/${dbadmin_baseUrl}/console/${query.getId()}|">
|
||||
<div class="ui-tab ps-5 pe-5 p-3">
|
||||
<i class="bi bi-filetype-sql pe-2"></i> Untitled Query
|
||||
</div>
|
||||
</a>
|
||||
<div class="inner-navigation-border flex-grow-1">
|
||||
</div>
|
||||
</div>
|
||||
<div class="box with-navigation">
|
||||
<form th:action="|/${dbadmin_baseUrl}/console|" method="GET">
|
||||
<textarea class="form-control" rows="6" name="query"
|
||||
th:text="${query}"></textarea>
|
||||
<input class="ui-btn btn btn-primary mt-3" type="submit" value="Run">
|
||||
</form>
|
||||
<div class="separator mt-3 mb-3"></div>
|
||||
<div th:replace="~{fragments/generic_table :: table(results=${results})}"></div>
|
||||
|
||||
|
||||
<div th:if="${query != null && !query.isBlank() && error == null}">
|
||||
<div class="separator mt-3 mb-3"></div>
|
||||
<div th:replace="~{fragments/generic_table :: table(results=${results})}"></div>
|
||||
</div>
|
||||
<th:block th:replace="~{fragments/resources :: alerts}"></th:block>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user