diff --git a/src/main/java/tech/ailef/dbadmin/external/controller/DefaultDbAdminController.java b/src/main/java/tech/ailef/dbadmin/external/controller/DefaultDbAdminController.java index f1d38e4..96eeeec 100644 --- a/src/main/java/tech/ailef/dbadmin/external/controller/DefaultDbAdminController.java +++ b/src/main/java/tech/ailef/dbadmin/external/controller/DefaultDbAdminController.java @@ -66,6 +66,7 @@ import tech.ailef.dbadmin.external.dto.CompareOperator; import tech.ailef.dbadmin.external.dto.FacetedSearchRequest; import tech.ailef.dbadmin.external.dto.LogsSearchRequest; import tech.ailef.dbadmin.external.dto.PaginatedResult; +import tech.ailef.dbadmin.external.dto.PaginationInfo; import tech.ailef.dbadmin.external.dto.QueryFilter; import tech.ailef.dbadmin.external.dto.ValidationErrorsContainer; import tech.ailef.dbadmin.external.exceptions.DbAdminException; @@ -101,7 +102,7 @@ public class DefaultDbAdminController { private ConsoleQueryRepository consoleQueryRepository; @Autowired - private JdbcTemplate jdbTemplate; + private JdbcTemplate jdbcTemplate; @Autowired private UserSettingsRepository userSettingsRepo; @@ -596,7 +597,13 @@ public class DefaultDbAdminController { @GetMapping("/console/run/{queryId}") public String consoleRun(Model model, @RequestParam(required = false) String query, @RequestParam(required = false) String queryTitle, + @RequestParam(required = false) Integer page, + @RequestParam(required = false) Integer pageSize, @PathVariable String queryId) { + + if (page == null || page <= 0) page = 1; + if (pageSize == null) pageSize = 50; + long startTime = System.currentTimeMillis(); if (!properties.isSqlConsoleEnabled()) { @@ -623,9 +630,10 @@ public class DefaultDbAdminController { List tabs = consoleQueryRepository.findAll(); model.addAttribute("tabs", tabs); + List results = new ArrayList<>(); if (activeQuery.getSql() != null && !activeQuery.getSql().isBlank()) { try { - List results = jdbTemplate.query(activeQuery.getSql(), (rs, rowNum) -> { + results = jdbcTemplate.query(activeQuery.getSql(), (rs, rowNum) -> { Map result = new HashMap<>(); ResultSetMetaData metaData = rs.getMetaData(); @@ -640,14 +648,32 @@ public class DefaultDbAdminController { result.put(field, o); } - return new DbQueryResultRow(result, query); + DbQueryResultRow row = new DbQueryResultRow(result, query); + + result.keySet().forEach(f -> { + f.setResult(row); + }); + + return row; }); - model.addAttribute("results", new DbQueryResult(results)); } catch (DataAccessException e) { model.addAttribute("error", e.getMessage()); } } - + + if (!results.isEmpty()) { + int maxPage = (int)(Math.ceil ((double)results.size() / pageSize)); + PaginationInfo pagination = new PaginationInfo(page, maxPage, pageSize, results.size(), null, null); + int startOffset = (page - 1) * pageSize; + int endOffset = (page) * pageSize; + + endOffset = Math.min(results.size(), endOffset); + + results = results.subList(startOffset, endOffset); + model.addAttribute("pagination", pagination); + model.addAttribute("results", new DbQueryResult(results)); + } + double elapsedTime = (System.currentTimeMillis() - startTime) / 1000.0; model.addAttribute("elapsedTime", new DecimalFormat("0.0#").format(elapsedTime)); return "console"; diff --git a/src/main/java/tech/ailef/dbadmin/external/dbmapping/query/DbQueryOutputField.java b/src/main/java/tech/ailef/dbadmin/external/dbmapping/query/DbQueryOutputField.java index c089083..a373eb8 100644 --- a/src/main/java/tech/ailef/dbadmin/external/dbmapping/query/DbQueryOutputField.java +++ b/src/main/java/tech/ailef/dbadmin/external/dbmapping/query/DbQueryOutputField.java @@ -4,8 +4,10 @@ import java.util.Objects; import tech.ailef.dbadmin.external.DbAdmin; import tech.ailef.dbadmin.external.dbmapping.DbField; +import tech.ailef.dbadmin.external.dbmapping.DbFieldType; import tech.ailef.dbadmin.external.dbmapping.DbObjectSchema; import tech.ailef.dbadmin.external.exceptions.DbAdminException; +import tech.ailef.dbadmin.external.exceptions.UnsupportedFieldTypeException; public class DbQueryOutputField { private String name; @@ -14,6 +16,8 @@ public class DbQueryOutputField { private DbField dbField; + private DbQueryResultRow result; + public DbQueryOutputField(String name, String table, DbAdmin dbAdmin) { this.name = name; this.table = table; @@ -29,14 +33,26 @@ public class DbQueryOutputField { } } + /** + * Returns the column name of the field + * @return + */ public String getName() { return name; } + /** + * Returns the table name of the field + * @return + */ public String getTable() { return table; } + /** + * Returns true if this field is a primary key + * @return + */ public boolean isPrimaryKey() { return dbField != null && dbField.isPrimaryKey(); } @@ -67,25 +83,55 @@ public class DbQueryOutputField { } /** - * Returns the type of the field, only in the case the field - * has been mapped to a table + * Returns the type of the field. + * If the field has been mapped to a table column returns the + * type of the column, otherwise tries to parse the field + * field type from the raw value returned by the database. * @return */ public String getType() { + // If the field has been mapped to the database if (dbField != null) return dbField.getType().toString(); + + // If the row this fields belongs to is defined + if (result != null) { + try { + DbFieldType type = DbFieldType.fromClass(result.get(this).getClass()); + return type.toString(); + } catch (UnsupportedFieldTypeException e) { + return "-"; + } + } + return "-"; } + /** + * Returns the Java name of the field, if mapped to a table column + * @return + */ public String getJavaName() { if (dbField == null) return null; return dbField.getJavaName(); } + /** + * Returns the Java class of the field, if mapped to a table column + * @return + */ public String getEntityClassName() { if (dbField == null) return null; return dbField.getSchema().getClassName(); } + + /** + * Sets the row object this field belongs to + * @param result + */ + public void setResult(DbQueryResultRow result) { + this.result = result; + } @Override public int hashCode() { diff --git a/src/main/resources/templates/console.html b/src/main/resources/templates/console.html index 4f2c57f..b1d98e7 100644 --- a/src/main/resources/templates/console.html +++ b/src/main/resources/templates/console.html @@ -48,10 +48,124 @@
- - [[ ${results.size()} ]] results in [[ ${elapsedTime} ]] seconds - + + + + + +
+ + + + + + +
diff --git a/src/main/resources/templates/fragments/generic_data_row.html b/src/main/resources/templates/fragments/generic_data_row.html index a1afdd8..531c40c 100644 --- a/src/main/resources/templates/fragments/generic_data_row.html +++ b/src/main/resources/templates/fragments/generic_data_row.html @@ -18,7 +18,9 @@ - + + + NULL