diff options
author | Anton Tananaev <anton@traccar.org> | 2022-06-16 18:19:57 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-06-16 18:19:57 -0700 |
commit | ee9abd94265eb704b5f5dd2262f1a3460bbe2a8f (patch) | |
tree | d4362b6cf9b62ba952366a4eb6081fc576987e80 /src/main/java/org/traccar/storage | |
parent | 0de7889d5548ab16bc8dc31e8734dc48dd51d43d (diff) | |
download | trackermap-server-ee9abd94265eb704b5f5dd2262f1a3460bbe2a8f.tar.gz trackermap-server-ee9abd94265eb704b5f5dd2262f1a3460bbe2a8f.tar.bz2 trackermap-server-ee9abd94265eb704b5f5dd2262f1a3460bbe2a8f.zip |
Refactor config usage
Diffstat (limited to 'src/main/java/org/traccar/storage')
-rw-r--r-- | src/main/java/org/traccar/storage/DatabaseStorage.java | 21 | ||||
-rw-r--r-- | src/main/java/org/traccar/storage/QueryBuilder.java | 17 |
2 files changed, 23 insertions, 15 deletions
diff --git a/src/main/java/org/traccar/storage/DatabaseStorage.java b/src/main/java/org/traccar/storage/DatabaseStorage.java index 661e792d4..052f11ad2 100644 --- a/src/main/java/org/traccar/storage/DatabaseStorage.java +++ b/src/main/java/org/traccar/storage/DatabaseStorage.java @@ -16,6 +16,7 @@ package org.traccar.storage; import com.fasterxml.jackson.databind.ObjectMapper; +import org.traccar.config.Config; import org.traccar.model.BaseModel; import org.traccar.model.Device; import org.traccar.model.Group; @@ -27,6 +28,7 @@ import org.traccar.storage.query.Limit; import org.traccar.storage.query.Order; import org.traccar.storage.query.Request; +import javax.inject.Inject; import javax.sql.DataSource; import java.sql.SQLException; import java.util.HashMap; @@ -38,10 +40,13 @@ import java.util.stream.Collectors; public class DatabaseStorage extends Storage { + private final Config config; private final DataSource dataSource; private final ObjectMapper objectMapper; - public DatabaseStorage(DataSource dataSource, ObjectMapper objectMapper) { + @Inject + public DatabaseStorage(Config config, DataSource dataSource, ObjectMapper objectMapper) { + this.config = config; this.dataSource = dataSource; this.objectMapper = objectMapper; } @@ -55,7 +60,7 @@ public class DatabaseStorage extends Storage { query.append(formatOrder(request.getOrder())); query.append(formatLimit(request.getLimit())); try { - QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString()); + QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString()); for (Map.Entry<String, Object> variable : getConditionVariables(request.getCondition()).entrySet()) { builder.setValue(variable.getKey(), variable.getValue()); } @@ -75,7 +80,7 @@ public class DatabaseStorage extends Storage { query.append(formatColumns(request.getColumns(), entity.getClass(), "set", c -> ':' + c)); query.append(")"); try { - QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString(), true); + QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString(), true); builder.setObject(entity); return builder.executeUpdate(); } catch (SQLException e) { @@ -91,7 +96,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, objectMapper, query.toString()); + QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString()); builder.setObject(entity); for (Map.Entry<String, Object> variable : getConditionVariables(request.getCondition()).entrySet()) { builder.setValue(variable.getKey(), variable.getValue()); @@ -108,7 +113,7 @@ public class DatabaseStorage extends Storage { query.append(getStorageName(clazz)); query.append(formatCondition(request.getCondition())); try { - QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString()); + QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString()); for (Map.Entry<String, Object> variable : getConditionVariables(request.getCondition()).entrySet()) { builder.setValue(variable.getKey(), variable.getValue()); } @@ -136,7 +141,7 @@ public class DatabaseStorage extends Storage { Condition combinedCondition = Condition.merge(conditions); query.append(formatCondition(combinedCondition)); try { - QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString()); + QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString()); for (Map.Entry<String, Object> variable : getConditionVariables(combinedCondition).entrySet()) { builder.setValue(variable.getKey(), variable.getValue()); } @@ -154,7 +159,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, objectMapper, query.toString(), true); + QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString(), true); for (var entry : permission.get().entrySet()) { builder.setLong(entry.getKey(), entry.getValue()); } @@ -172,7 +177,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, objectMapper, query.toString(), true); + QueryBuilder builder = QueryBuilder.create(config, 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 8502d5f0b..910ebf170 100644 --- a/src/main/java/org/traccar/storage/QueryBuilder.java +++ b/src/main/java/org/traccar/storage/QueryBuilder.java @@ -19,7 +19,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.traccar.Context; +import org.traccar.config.Config; import org.traccar.config.Keys; import org.traccar.model.Permission; @@ -42,10 +42,12 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +@SuppressWarnings("UnusedReturnValue") public final class QueryBuilder { private static final Logger LOGGER = LoggerFactory.getLogger(QueryBuilder.class); + private final Config config; private final ObjectMapper objectMapper; private final Map<String, List<Integer>> indexMap = new HashMap<>(); @@ -55,8 +57,9 @@ public final class QueryBuilder { private final boolean returnGeneratedKeys; private QueryBuilder( - DataSource dataSource, ObjectMapper objectMapper, + Config config, DataSource dataSource, ObjectMapper objectMapper, String query, boolean returnGeneratedKeys) throws SQLException { + this.config = config; this.objectMapper = objectMapper; this.query = query; this.returnGeneratedKeys = returnGeneratedKeys; @@ -133,14 +136,14 @@ public final class QueryBuilder { } public static QueryBuilder create( - DataSource dataSource, ObjectMapper objectMapper, String query) throws SQLException { - return new QueryBuilder(dataSource, objectMapper, query, false); + Config config, DataSource dataSource, ObjectMapper objectMapper, String query) throws SQLException { + return new QueryBuilder(config, dataSource, objectMapper, query, false); } public static QueryBuilder create( - DataSource dataSource, ObjectMapper objectMapper, String query, + Config config, DataSource dataSource, ObjectMapper objectMapper, String query, boolean returnGeneratedKeys) throws SQLException { - return new QueryBuilder(dataSource, objectMapper, query, returnGeneratedKeys); + return new QueryBuilder(config, dataSource, objectMapper, query, returnGeneratedKeys); } private List<Integer> indexes(String name) { @@ -396,7 +399,7 @@ public final class QueryBuilder { } private void logQuery() { - if (Context.getConfig().getBoolean(Keys.LOGGER_QUERIES)) { + if (config.getBoolean(Keys.LOGGER_QUERIES)) { LOGGER.info(query); } } |