Fixed lower page size change

This commit is contained in:
Francesco 2023-11-11 11:36:14 +01:00
parent 32751552d4
commit 1f6d79cf6c
4 changed files with 67 additions and 30 deletions

View File

@ -28,6 +28,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;
@ -581,6 +582,16 @@ public class SnapAdminController {
if (tabs.isEmpty()) {
ConsoleQuery q = new ConsoleQuery();
int randomIndex = new Random().nextInt(0, snapAdmin.getSchemas().size());
String randomTable = snapAdmin.getSchemas().get(randomIndex).getTableName();
q.setSql(
"-- It's recommended to always include a LIMIT clause in your query\n"
+ "-- Although the SQL Console supports pagination, it retrieves the entire ResultSet\n\n"
+ "-- SELECT * FROM " + randomTable + " LIMIT 1000;\n"
);
consoleService.save(q);
return "redirect:/" + properties.getBaseUrl() + "/console/run/" + q.getId();
} else {
@ -645,6 +656,9 @@ public class SnapAdminController {
results.crop(startOffset, endOffset);
model.addAttribute("pagination", pagination);
model.addAttribute("results", results);
} else {
PaginationInfo pagination = new PaginationInfo(page, 0, pageSize, results.size(), null, null);
model.addAttribute("pagination", pagination);
}
model.addAttribute("title", "SQL Console | " + activeQuery.getTitle());

View File

@ -31,6 +31,7 @@ import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
@ -294,6 +295,7 @@ public class SnapAdminRepository {
public DbQueryResult executeQuery(String sql) {
List<DbQueryResultRow> results = new ArrayList<>();
if (sql != null && !sql.isBlank()) {
try {
results = jdbcTemplate.query(sql, (rs, rowNum) -> {
Map<DbQueryOutputField, Object> result = new HashMap<>();
@ -317,6 +319,9 @@ public class SnapAdminRepository {
return row;
});
} catch (TransientDataAccessResourceException e) {
// If there's an exception we leave the results as empty
}
}
return new DbQueryResult(results);
}

View File

@ -6,11 +6,11 @@ document.addEventListener("DOMContentLoaded", () => {
});
if (document.querySelector("nav select.page-size") != null) {
document.querySelector("nav select.page-size").addEventListener('change', function(e) {
document.querySelectorAll("nav select.page-size").forEach(e => {
e.addEventListener('change', function(e) {
console.log(e.target.parentElement);
e.target.parentElement.submit();
// this.parentElement.querySelector("input[name=\"pageSize\"]").value = e.target.value;
// this.parentElement.submit();
});
});
}

View File

@ -106,7 +106,7 @@
<!-- Pagination -->
<nav aria-label="Results pagination">
<div class="d-flex justify-content-between">
<div th:if="${pagination != null && pagination.getMaxPage() != 1}" class="d-flex w-100">
<div th:if="${pagination != null && pagination.getMaxPage() > 1}" class="d-flex w-100">
<ul class="pagination me-3">
<li class="page-item" th:if="${pagination.getCurrentPage() != 1}">
<a class="page-link"
@ -193,7 +193,7 @@
<!-- Pagination -->
<nav aria-label="Results pagination">
<div class="d-flex justify-content-between">
<div th:if="${pagination != null && pagination.getMaxPage() != 1}" class="d-flex">
<div th:if="${pagination != null && pagination.getMaxPage() > 1}" class="d-flex">
<ul class="pagination me-3">
<li class="page-item" th:if="${pagination.getCurrentPage() != 1}">
<a class="page-link"
@ -229,18 +229,36 @@
</li>
</ul>
<div class="d-flex align-items-center" th:if="${pagination.getMaxPage() > 1}">
<p class="m-0 p-0">
<p class="m-0 p-0 me-2">
<i>Showing [[ ${results.size()} ]] of [[ ${pagination.getMaxElement()} ]]
results in [[ ${elapsedTime} ]] seconds</i>
</p>
<form method="GET" th:action="|/${snapadmin_baseUrl}/console/run/${activeQuery.getId()}|">
<select name="pageSize" class="form-select page-size" style="width: 200px">
<option disabled>Page size</option>
<option th:selected="${pagination.getPageSize() == 50}">50</option>
<option th:selected="${pagination.getPageSize() == 100}">100</option>
<option th:selected="${pagination.getPageSize() == 150}">150</option>
<option th:selected="${pagination.getPageSize() == 200}">200</option>
</select>
</form>
</div>
</div>
<div class="d-flex align-items-center" th:if="${pagination.getMaxPage() == 1}">
<p class="m-0 p-0">
<p class="m-0 p-0 me-2">
<i>Showing [[ ${results.size()} ]] of [[ ${pagination.getMaxElement()} ]]
results in [[ ${elapsedTime} ]] seconds</i>
</p>
<form method="GET" th:action="|/${snapadmin_baseUrl}/console/run/${activeQuery.getId()}|">
<select name="pageSize" class="form-select page-size" style="width: 200px">
<option disabled>Page size</option>
<option th:selected="${pagination.getPageSize() == 50}">50</option>
<option th:selected="${pagination.getPageSize() == 100}">100</option>
<option th:selected="${pagination.getPageSize() == 150}">150</option>
<option th:selected="${pagination.getPageSize() == 200}">200</option>
</select>
</form>
</div>
</div>