This commit is contained in:
Francesco 2023-09-20 12:03:22 +02:00
parent 154bb1fcb8
commit 3d9de0246e
6 changed files with 77 additions and 137 deletions

View File

@ -1,23 +1,29 @@
package tech.ailef.dbadmin.dbmapping;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.web.multipart.MultipartFile;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.CriteriaUpdate;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import tech.ailef.dbadmin.dto.CompareOperator;
import tech.ailef.dbadmin.dto.QueryFilter;
import tech.ailef.dbadmin.exceptions.DbAdminException;
@SuppressWarnings("rawtypes")
public class AdvancedJpaRepository extends SimpleJpaRepository {
@ -145,22 +151,39 @@ public class AdvancedJpaRepository extends SimpleJpaRepository {
}
return finalPredicates;
}
// @SuppressWarnings("unchecked")
// public List<Object> distinctFieldValues(DbField field) {
// CriteriaBuilder cb = entityManager.getCriteriaBuilder();
//
// Class<?> outputType = field.getType().getJavaClass();
// if (field.getConnectedType() != null) {
// outputType = field.getConnectedSchema().getPrimaryKey().getType().getJavaClass();
// }
//
// CriteriaQuery query = cb.createQuery(outputType);
// Root root = query.from(schema.getJavaClass());
//
// query.select(root.get(field.getJavaName()).as(outputType)).distinct(true);
//
// return entityManager.createQuery(query).getResultList();
// }
public void update(DbObjectSchema schema, Map<String, String> params, Map<String, MultipartFile> files) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaUpdate update = cb.createCriteriaUpdate(schema.getJavaClass());
Root employee = update.from(schema.getJavaClass());
for (DbField field : schema.getSortedFields()) {
if (field.isPrimaryKey()) continue;
String stringValue = params.get(field.getName());
Object value = null;
if (stringValue != null && stringValue.isBlank()) stringValue = null;
if (stringValue != null) {
value = field.getType().parseValue(stringValue);
} else {
try {
MultipartFile file = files.get(field.getJavaName());
if (file != null)
value = file.getBytes();
} catch (IOException e) {
throw new DbAdminException(e);
}
}
update.set(employee.get(field.getJavaName()), value);
}
String pkName = schema.getPrimaryKey().getJavaName();
update.where(cb.equal(employee.get(pkName), params.get(schema.getPrimaryKey().getName())));
Query query = entityManager.createQuery(update);
int rowCount = query.executeUpdate();
}
}

View File

@ -129,14 +129,18 @@ public class DbAdminRepository {
* @param schema
* @param params
*/
@Transactional
public void update(DbObjectSchema schema, Map<String, String> params, Map<String, MultipartFile> files) {
Object[] updateArray = schema.getUpdateArray(params, files);
// Object[] updateArray = schema.getUpdateArray(params, files);
//
// String updateFields =
// schema.getSortedFields().stream().map(f -> "`" + f.getName() + "` = ?").collect(Collectors.joining(", "));
//
// String query = "UPDATE `" + schema.getTableName() + "` SET " + updateFields + " WHERE `" + schema.getPrimaryKey().getName() + "` = ?";
// jdbcTemplate.update(query, updateArray);
String updateFields =
schema.getSortedFields().stream().map(f -> "`" + f.getName() + "` = ?").collect(Collectors.joining(", "));
String query = "UPDATE `" + schema.getTableName() + "` SET " + updateFields + " WHERE `" + schema.getPrimaryKey().getName() + "` = ?";
jdbcTemplate.update(query, updateArray);
schema.getJpaRepository().update(schema, params, files);
}
@SuppressWarnings("unchecked")
@ -206,17 +210,6 @@ public class DbAdminRepository {
insert.execute(allValues);
return primaryKey;
}
// String fieldsString =
// schema.getSortedFields().stream().skip(primaryKey == null ? 1 : 0).map(f -> "`" + f.getName() + "`").collect(Collectors.joining(", "));
//
// String placeholdersString =
// schema.getSortedFields().stream().skip(primaryKey == null ? 1 : 0).map(f -> "?").collect(Collectors.joining(", "));
// Object[] array = schema.getInsertArray(values, files);
//
// String query = "INSERT INTO " + schema.getTableName() + " (" + fieldsString + ") VALUES (" + placeholdersString + ");";
// jdbcTemplate.update(query, array);
// return primaryKey;
}

View File

@ -17,8 +17,13 @@ public class DbFieldValue {
}
public String getFormattedValue() {
if (field.getFormat() == null) return value == null ? "NULL" : value.toString();
return String.format(field.getFormat(), value);
if (value == null) return null;
if (field.getFormat() == null) {
return value.toString();
} else {
return String.format(field.getFormat(), value);
}
}
public DbField getField() {

View File

@ -10,8 +10,6 @@ import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.web.multipart.MultipartFile;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.ManyToMany;
@ -181,101 +179,6 @@ public class DbObjectSchema {
&& f.getPrimitiveField().getAnnotation(Filterable.class) != null;
}).toList();
}
// public List<Object> getFieldValues(DbField field) {
// return jpaRepository.distinctFieldValues(field);
// }
public Object[] getInsertArray(Map<String, String> params, Map<String, MultipartFile> files) {
int currentIndex = 0;
String pkValue = params.get(getPrimaryKey().getName());
if (pkValue == null || pkValue.isBlank())
pkValue = null;
Object[] row;
if (pkValue == null) {
row = new Object[getSortedFields().size() - 1];
} else {
row = new Object[getSortedFields().size()];
}
for (DbField field : getSortedFields()) {
// Skip the primary key if the value is null
// If it is autogenerated, it will be filled by the database
// otherwise it will throw an error
if (field.isPrimaryKey() && pkValue == null) {
continue;
}
String name = field.getName();
String stringValue = params.get(name);
Object value = null;
if (stringValue != null && stringValue.isBlank()) stringValue = null;
if (stringValue != null) {
value = stringValue;
} else {
value = files.get(name);
}
// String type = params.get("__dbadmin_" + name + "_type");
// if (type == null)
// throw new RuntimeException("Missing type hidden field for: " + name);
try {
if (value == null)
row[currentIndex++] = null;
else
row[currentIndex++] = value; //DbFieldType.valueOf(type).parseValue(value);
} catch (IllegalArgumentException | SecurityException e) {
e.printStackTrace();
}
}
return row;
}
public Object[] getUpdateArray(Map<String, String> params, Map<String, MultipartFile> files) {
Object[] row = new Object[getSortedFields().size() + 1];
int currentIndex = 0;
DbField primaryKey = getPrimaryKey();
String pkValue = params.get(primaryKey.getName());
for (DbField field : getSortedFields()) {
String name = field.getName();
String stringValue = params.get(name);
Object value = null;
if (stringValue != null && stringValue.isBlank()) stringValue = null;
if (stringValue != null) {
value = stringValue;
} else {
value = files.get(name);
}
// String type = params.get("__dbadmin_" + name + "_type");
// if (type == null)
// throw new RuntimeException("Missing type hidden field for: " + name);
try {
if (value == null)
row[currentIndex++] = null;
else
row[currentIndex++] = value; //DbFieldType.valueOf(type).parseValue(value);
} catch (IllegalArgumentException | SecurityException e) {
e.printStackTrace();
}
}
row[currentIndex] = primaryKey.getType().parseValue(pkValue);
return row;
}
@Override
public String toString() {

View File

@ -48,13 +48,24 @@
</a>
</th:block>
<th:block th:if="${!field.isPrimaryKey()}">
<span th:text="${object.get(field).getFormattedValue()}" th:if="${!field.isBinary()}">
</span>
<th:block th:if="${!field.isBinary()}">
<span th:if="${object.get(field).getFormattedValue() == null}" class="font-monospace null-label">
NULL
</span>
<span th:unless="${object.get(field).getFormattedValue() == null}"
th:text="${object.get(field).getFormattedValue()}">
</span>
</th:block>
<span th:unless="${!field.isBinary()}">
<th:block th:if="${object.get(field).getValue()}">
<a class="text-decoration-none null-label"
th:href="|/dbadmin/download/${schema.getJavaClass().getName()}/${field.getName()}/${object.get(schema.getPrimaryKey()).getValue()}|">
<i class="align-middle bi bi-box-arrow-down"></i><span class="align-middle"> Download</span>
<i class="align-middle bi bi-box-arrow-down"></i><span class="align-middle"> Download
<!--/*--> <span class="text-muted">([[ ${object.get(field).getValue().length} ]] bytes)</span> <!--*/-->
</span>
</a>
</th:block>
<th:block th:unless="${object.get(field).getValue()}">

View File

@ -162,6 +162,11 @@
<input type="hidden" th:value="${page.getPagination().getCurrentPage()}" th:name="page">
<input type="hidden" th:value="${query}" th:name="query">
<input type="hidden" name="pageSize">
<th:block th:each="p : ${queryParams.keySet()}">
<input th:each="v : ${queryParams.get(p)}"
th:name="${p}" th:value="${v}" type="hidden"
th:if="${p.startsWith('filter_')}">
</th:block>
<select class="form-select page-size">
<option disabled>Page size</option>
<option th:selected="${page.getPagination().getPageSize() == 50}">50</option>