aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/storage
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-11 14:27:01 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-11 14:27:01 -0700
commit4025a42c42e34bb620f4263de05781a10ddc7a9d (patch)
tree56ec382fcb2ad9089556ab3e17f3d9e76a8ec71e /src/main/java/org/traccar/storage
parentaa00ed23f96f91cfc879f02e61333511e3b7b11b (diff)
downloadtrackermap-server-4025a42c42e34bb620f4263de05781a10ddc7a9d.tar.gz
trackermap-server-4025a42c42e34bb620f4263de05781a10ddc7a9d.tar.bz2
trackermap-server-4025a42c42e34bb620f4263de05781a10ddc7a9d.zip
Inject object mapper
Diffstat (limited to 'src/main/java/org/traccar/storage')
-rw-r--r--src/main/java/org/traccar/storage/DatabaseStorage.java19
-rw-r--r--src/main/java/org/traccar/storage/QueryBuilder.java22
2 files changed, 26 insertions, 15 deletions
diff --git a/src/main/java/org/traccar/storage/DatabaseStorage.java b/src/main/java/org/traccar/storage/DatabaseStorage.java
index fc468182e..661e792d4 100644
--- a/src/main/java/org/traccar/storage/DatabaseStorage.java
+++ b/src/main/java/org/traccar/storage/DatabaseStorage.java
@@ -15,6 +15,7 @@
*/
package org.traccar.storage;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.traccar.model.BaseModel;
import org.traccar.model.Device;
import org.traccar.model.Group;
@@ -38,9 +39,11 @@ import java.util.stream.Collectors;
public class DatabaseStorage extends Storage {
private final DataSource dataSource;
+ private final ObjectMapper objectMapper;
- public DatabaseStorage(DataSource dataSource) {
+ public DatabaseStorage(DataSource dataSource, ObjectMapper objectMapper) {
this.dataSource = dataSource;
+ this.objectMapper = objectMapper;
}
@Override
@@ -52,7 +55,7 @@ public class DatabaseStorage extends Storage {
query.append(formatOrder(request.getOrder()));
query.append(formatLimit(request.getLimit()));
try {
- QueryBuilder builder = QueryBuilder.create(dataSource, query.toString());
+ QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString());
for (Map.Entry<String, Object> variable : getConditionVariables(request.getCondition()).entrySet()) {
builder.setValue(variable.getKey(), variable.getValue());
}
@@ -72,7 +75,7 @@ public class DatabaseStorage extends Storage {
query.append(formatColumns(request.getColumns(), entity.getClass(), "set", c -> ':' + c));
query.append(")");
try {
- QueryBuilder builder = QueryBuilder.create(dataSource, query.toString(), true);
+ QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString(), true);
builder.setObject(entity);
return builder.executeUpdate();
} catch (SQLException e) {
@@ -88,7 +91,7 @@ public class DatabaseStorage extends Storage {
query.append(formatColumns(request.getColumns(), entity.getClass(), "set", c -> c + " = :" + c));
query.append(formatCondition(request.getCondition()));
try {
- QueryBuilder builder = QueryBuilder.create(dataSource, query.toString());
+ QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString());
builder.setObject(entity);
for (Map.Entry<String, Object> variable : getConditionVariables(request.getCondition()).entrySet()) {
builder.setValue(variable.getKey(), variable.getValue());
@@ -105,7 +108,7 @@ public class DatabaseStorage extends Storage {
query.append(getStorageName(clazz));
query.append(formatCondition(request.getCondition()));
try {
- QueryBuilder builder = QueryBuilder.create(dataSource, query.toString());
+ QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString());
for (Map.Entry<String, Object> variable : getConditionVariables(request.getCondition()).entrySet()) {
builder.setValue(variable.getKey(), variable.getValue());
}
@@ -133,7 +136,7 @@ public class DatabaseStorage extends Storage {
Condition combinedCondition = Condition.merge(conditions);
query.append(formatCondition(combinedCondition));
try {
- QueryBuilder builder = QueryBuilder.create(dataSource, query.toString());
+ QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString());
for (Map.Entry<String, Object> variable : getConditionVariables(combinedCondition).entrySet()) {
builder.setValue(variable.getKey(), variable.getValue());
}
@@ -151,7 +154,7 @@ public class DatabaseStorage extends Storage {
query.append(permission.get().keySet().stream().map(key -> ':' + key).collect(Collectors.joining(", ")));
query.append(")");
try {
- QueryBuilder builder = QueryBuilder.create(dataSource, query.toString(), true);
+ QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString(), true);
for (var entry : permission.get().entrySet()) {
builder.setLong(entry.getKey(), entry.getValue());
}
@@ -169,7 +172,7 @@ public class DatabaseStorage extends Storage {
query.append(permission
.get().keySet().stream().map(key -> key + " = :" + key).collect(Collectors.joining(" AND ")));
try {
- QueryBuilder builder = QueryBuilder.create(dataSource, query.toString(), true);
+ QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString(), true);
for (var entry : permission.get().entrySet()) {
builder.setLong(entry.getKey(), entry.getValue());
}
diff --git a/src/main/java/org/traccar/storage/QueryBuilder.java b/src/main/java/org/traccar/storage/QueryBuilder.java
index 874a046b4..8502d5f0b 100644
--- a/src/main/java/org/traccar/storage/QueryBuilder.java
+++ b/src/main/java/org/traccar/storage/QueryBuilder.java
@@ -16,6 +16,7 @@
package org.traccar.storage;
import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.Context;
@@ -45,13 +46,18 @@ public final class QueryBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(QueryBuilder.class);
+ private final ObjectMapper objectMapper;
+
private final Map<String, List<Integer>> indexMap = new HashMap<>();
private Connection connection;
private PreparedStatement statement;
private final String query;
private final boolean returnGeneratedKeys;
- private QueryBuilder(DataSource dataSource, String query, boolean returnGeneratedKeys) throws SQLException {
+ private QueryBuilder(
+ DataSource dataSource, ObjectMapper objectMapper,
+ String query, boolean returnGeneratedKeys) throws SQLException {
+ this.objectMapper = objectMapper;
this.query = query;
this.returnGeneratedKeys = returnGeneratedKeys;
if (query != null) {
@@ -126,13 +132,15 @@ public final class QueryBuilder {
return parsedQuery.toString();
}
- public static QueryBuilder create(DataSource dataSource, String query) throws SQLException {
- return new QueryBuilder(dataSource, query, false);
+ public static QueryBuilder create(
+ DataSource dataSource, ObjectMapper objectMapper, String query) throws SQLException {
+ return new QueryBuilder(dataSource, objectMapper, query, false);
}
public static QueryBuilder create(
- DataSource dataSource, String query, boolean returnGeneratedKeys) throws SQLException {
- return new QueryBuilder(dataSource, query, returnGeneratedKeys);
+ DataSource dataSource, ObjectMapper objectMapper, String query,
+ boolean returnGeneratedKeys) throws SQLException {
+ return new QueryBuilder(dataSource, objectMapper, query, returnGeneratedKeys);
}
private List<Integer> indexes(String name) {
@@ -295,7 +303,7 @@ public final class QueryBuilder {
} else if (method.getReturnType().equals(byte[].class)) {
setBlob(name, (byte[]) method.invoke(object));
} else {
- setString(name, Context.getObjectMapper().writeValueAsString(method.invoke(object)));
+ setString(name, objectMapper.writeValueAsString(method.invoke(object)));
}
} catch (IllegalAccessException | InvocationTargetException | JsonProcessingException error) {
LOGGER.warn("Get property error", error);
@@ -378,7 +386,7 @@ public final class QueryBuilder {
String value = resultSet.getString(name);
if (value != null && !value.isEmpty()) {
try {
- method.invoke(object, Context.getObjectMapper().readValue(value, parameterType));
+ method.invoke(object, objectMapper.readValue(value, parameterType));
} catch (InvocationTargetException | IllegalAccessException | IOException error) {
LOGGER.warn("Set property error", error);
}