aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/storage
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-10-13 10:31:54 -0700
committerAnton Tananaev <anton@traccar.org>2022-10-13 10:31:54 -0700
commit4c8e98ed409df6e12128591eb1d8d0cf0856deb7 (patch)
tree83da3517bb14977ab4809aecf3681b777785e83d /src/main/java/org/traccar/storage
parentacedd28b8584a18022d9e2343558f9acbad1666f (diff)
downloadtrackermap-server-4c8e98ed409df6e12128591eb1d8d0cf0856deb7.tar.gz
trackermap-server-4c8e98ed409df6e12128591eb1d8d0cf0856deb7.tar.bz2
trackermap-server-4c8e98ed409df6e12128591eb1d8d0cf0856deb7.zip
Refactor database code (fix #4961)
Diffstat (limited to 'src/main/java/org/traccar/storage')
-rw-r--r--src/main/java/org/traccar/storage/DatabaseStorage.java25
-rw-r--r--src/main/java/org/traccar/storage/QueryBuilder.java54
-rw-r--r--src/main/java/org/traccar/storage/query/Columns.java3
-rw-r--r--src/main/java/org/traccar/storage/query/Condition.java8
4 files changed, 42 insertions, 48 deletions
diff --git a/src/main/java/org/traccar/storage/DatabaseStorage.java b/src/main/java/org/traccar/storage/DatabaseStorage.java
index 8ca464147..884c8fca8 100644
--- a/src/main/java/org/traccar/storage/DatabaseStorage.java
+++ b/src/main/java/org/traccar/storage/DatabaseStorage.java
@@ -57,7 +57,7 @@ public class DatabaseStorage extends Storage {
if (request.getColumns() instanceof Columns.All) {
query.append('*');
} else {
- query.append(formatColumns(request.getColumns(), clazz, "get", c -> c));
+ query.append(formatColumns(request.getColumns().getColumns(clazz, "set"), c -> c));
}
query.append(" FROM ").append(getStorageName(clazz));
query.append(formatCondition(request.getCondition()));
@@ -76,16 +76,17 @@ public class DatabaseStorage extends Storage {
@Override
public <T> long addObject(T entity, Request request) throws StorageException {
+ List<String> columns = request.getColumns().getColumns(entity.getClass(), "get");
StringBuilder query = new StringBuilder("INSERT INTO ");
query.append(getStorageName(entity.getClass()));
query.append("(");
- query.append(formatColumns(request.getColumns(), entity.getClass(), "set", c -> c));
+ query.append(formatColumns(columns, c -> c));
query.append(") VALUES (");
- query.append(formatColumns(request.getColumns(), entity.getClass(), "set", c -> ':' + c));
+ query.append(formatColumns(columns, c -> ':' + c));
query.append(")");
try {
QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString(), true);
- builder.setObject(entity);
+ builder.setObject(entity, columns);
return builder.executeUpdate();
} catch (SQLException e) {
throw new StorageException(e);
@@ -94,14 +95,15 @@ public class DatabaseStorage extends Storage {
@Override
public <T> void updateObject(T entity, Request request) throws StorageException {
+ List<String> columns = request.getColumns().getColumns(entity.getClass(), "get");
StringBuilder query = new StringBuilder("UPDATE ");
query.append(getStorageName(entity.getClass()));
query.append(" SET ");
- query.append(formatColumns(request.getColumns(), entity.getClass(), "set", c -> c + " = :" + c));
+ query.append(formatColumns(columns, c -> c + " = :" + c));
query.append(formatCondition(request.getCondition()));
try {
QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString());
- builder.setObject(entity);
+ builder.setObject(entity, columns);
for (Map.Entry<String, Object> variable : getConditionVariables(request.getCondition()).entrySet()) {
builder.setValue(variable.getKey(), variable.getValue());
}
@@ -135,12 +137,10 @@ public class DatabaseStorage extends Storage {
query.append(Permission.getStorageName(ownerClass, propertyClass));
var conditions = new LinkedList<Condition>();
if (ownerId > 0) {
- conditions.add(new Condition.Equals(
- Permission.getKey(ownerClass), Permission.getKey(ownerClass), ownerId));
+ conditions.add(new Condition.Equals(Permission.getKey(ownerClass), ownerId));
}
if (propertyId > 0) {
- conditions.add(new Condition.Equals(
- Permission.getKey(propertyClass), Permission.getKey(propertyClass), propertyId));
+ conditions.add(new Condition.Equals(Permission.getKey(propertyClass), propertyId));
}
Condition combinedCondition = Condition.merge(conditions);
query.append(formatCondition(combinedCondition));
@@ -230,9 +230,8 @@ public class DatabaseStorage extends Storage {
return results;
}
- private String formatColumns(
- Columns columns, Class<?> clazz, String type, Function<String, String> mapper) {
- return columns.getColumns(clazz, type).stream().map(mapper).collect(Collectors.joining(", "));
+ private String formatColumns(List<String> columns, Function<String, String> mapper) {
+ return columns.stream().map(mapper).collect(Collectors.joining(", "));
}
private String formatCondition(Condition genericCondition) throws StorageException {
diff --git a/src/main/java/org/traccar/storage/QueryBuilder.java b/src/main/java/org/traccar/storage/QueryBuilder.java
index a58ebe2b4..fa71a8e8f 100644
--- a/src/main/java/org/traccar/storage/QueryBuilder.java
+++ b/src/main/java/org/traccar/storage/QueryBuilder.java
@@ -37,10 +37,12 @@ import java.sql.Timestamp;
import java.sql.Types;
import java.util.Date;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.Set;
@SuppressWarnings("UnusedReturnValue")
public final class QueryBuilder {
@@ -283,36 +285,32 @@ public final class QueryBuilder {
return this;
}
- public QueryBuilder setObject(Object object) throws SQLException {
-
- Method[] methods = object.getClass().getMethods();
-
- for (Method method : methods) {
- if (method.getName().startsWith("get") && method.getParameterTypes().length == 0
- && !method.getName().equals("getClass")) {
- String name = method.getName().substring(3);
- try {
- if (method.getReturnType().equals(boolean.class)) {
- setBoolean(name, (Boolean) method.invoke(object));
- } else if (method.getReturnType().equals(int.class)) {
- setInteger(name, (Integer) method.invoke(object));
- } else if (method.getReturnType().equals(long.class)) {
- setLong(name, (Long) method.invoke(object), name.endsWith("Id"));
- } else if (method.getReturnType().equals(double.class)) {
- setDouble(name, (Double) method.invoke(object));
- } else if (method.getReturnType().equals(String.class)) {
- setString(name, (String) method.invoke(object));
- } else if (method.getReturnType().equals(Date.class)) {
- setDate(name, (Date) method.invoke(object));
- } else if (method.getReturnType().equals(byte[].class)) {
- setBlob(name, (byte[]) method.invoke(object));
- } else {
- setString(name, objectMapper.writeValueAsString(method.invoke(object)));
- }
- } catch (IllegalAccessException | InvocationTargetException | JsonProcessingException error) {
- LOGGER.warn("Get property error", error);
+ public QueryBuilder setObject(Object object, List<String> columns) throws SQLException {
+
+ try {
+ for (String column : columns) {
+ Method method = object.getClass().getMethod(
+ "get" + Character.toUpperCase(column.charAt(0)) + column.substring(1));
+ if (method.getReturnType().equals(boolean.class)) {
+ setBoolean(column, (Boolean) method.invoke(object));
+ } else if (method.getReturnType().equals(int.class)) {
+ setInteger(column, (Integer) method.invoke(object));
+ } else if (method.getReturnType().equals(long.class)) {
+ setLong(column, (Long) method.invoke(object), column.endsWith("Id"));
+ } else if (method.getReturnType().equals(double.class)) {
+ setDouble(column, (Double) method.invoke(object));
+ } else if (method.getReturnType().equals(String.class)) {
+ setString(column, (String) method.invoke(object));
+ } else if (method.getReturnType().equals(Date.class)) {
+ setDate(column, (Date) method.invoke(object));
+ } else if (method.getReturnType().equals(byte[].class)) {
+ setBlob(column, (byte[]) method.invoke(object));
+ } else {
+ setString(column, objectMapper.writeValueAsString(method.invoke(object)));
}
}
+ } catch (ReflectiveOperationException | JsonProcessingException e) {
+ LOGGER.warn("Set object error", e);
}
return this;
diff --git a/src/main/java/org/traccar/storage/query/Columns.java b/src/main/java/org/traccar/storage/query/Columns.java
index 545995b3c..a00400b36 100644
--- a/src/main/java/org/traccar/storage/query/Columns.java
+++ b/src/main/java/org/traccar/storage/query/Columns.java
@@ -17,6 +17,7 @@ package org.traccar.storage.query;
import org.traccar.storage.QueryIgnore;
+import java.beans.Introspector;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedList;
@@ -36,7 +37,7 @@ public abstract class Columns {
if (method.getName().startsWith(type) && method.getParameterTypes().length == parameterCount
&& !method.isAnnotationPresent(QueryIgnore.class)
&& !method.getName().equals("getClass")) {
- columns.add(method.getName().substring(3).toLowerCase());
+ columns.add(Introspector.decapitalize(method.getName().substring(3)));
}
}
return columns;
diff --git a/src/main/java/org/traccar/storage/query/Condition.java b/src/main/java/org/traccar/storage/query/Condition.java
index 136b0402b..08b199052 100644
--- a/src/main/java/org/traccar/storage/query/Condition.java
+++ b/src/main/java/org/traccar/storage/query/Condition.java
@@ -34,12 +34,8 @@ public interface Condition {
}
class Equals extends Compare {
- public Equals(String column, String variable) {
- this(column, variable, null);
- }
-
- public Equals(String column, String variable, Object value) {
- super(column, "=", variable, value);
+ public Equals(String column, Object value) {
+ super(column, "=", column, value);
}
}