aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/storage/query
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/traccar/storage/query')
-rw-r--r--src/main/java/org/traccar/storage/query/Columns.java81
-rw-r--r--src/main/java/org/traccar/storage/query/Condition.java211
-rw-r--r--src/main/java/org/traccar/storage/query/Limit.java30
-rw-r--r--src/main/java/org/traccar/storage/query/Order.java46
-rw-r--r--src/main/java/org/traccar/storage/query/Request.java58
5 files changed, 426 insertions, 0 deletions
diff --git a/src/main/java/org/traccar/storage/query/Columns.java b/src/main/java/org/traccar/storage/query/Columns.java
new file mode 100644
index 000000000..a00400b36
--- /dev/null
+++ b/src/main/java/org/traccar/storage/query/Columns.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public abstract class Columns {
+
+ public abstract List<String> getColumns(Class<?> clazz, String type);
+
+ protected List<String> getAllColumns(Class<?> clazz, String type) {
+ List<String> columns = new LinkedList<>();
+ Method[] methods = clazz.getMethods();
+ for (Method method : methods) {
+ int parameterCount = type.equals("set") ? 1 : 0;
+ if (method.getName().startsWith(type) && method.getParameterTypes().length == parameterCount
+ && !method.isAnnotationPresent(QueryIgnore.class)
+ && !method.getName().equals("getClass")) {
+ columns.add(Introspector.decapitalize(method.getName().substring(3)));
+ }
+ }
+ return columns;
+ }
+
+ public static class All extends Columns {
+ @Override
+ public List<String> getColumns(Class<?> clazz, String type) {
+ return getAllColumns(clazz, type);
+ }
+ }
+
+ public static class Include extends Columns {
+ private final List<String> columns;
+
+ public Include(String... columns) {
+ this.columns = Arrays.stream(columns).collect(Collectors.toList());
+ }
+
+ @Override
+ public List<String> getColumns(Class<?> clazz, String type) {
+ return columns;
+ }
+ }
+
+ public static class Exclude extends Columns {
+ private final Set<String> columns;
+
+ public Exclude(String... columns) {
+ this.columns = Arrays.stream(columns).collect(Collectors.toSet());
+ }
+
+ @Override
+ public List<String> getColumns(Class<?> clazz, String type) {
+ return getAllColumns(clazz, type).stream()
+ .filter(column -> !columns.contains(column))
+ .collect(Collectors.toList());
+ }
+ }
+
+}
diff --git a/src/main/java/org/traccar/storage/query/Condition.java b/src/main/java/org/traccar/storage/query/Condition.java
new file mode 100644
index 000000000..08b199052
--- /dev/null
+++ b/src/main/java/org/traccar/storage/query/Condition.java
@@ -0,0 +1,211 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.storage.query;
+
+import org.traccar.model.GroupedModel;
+
+import java.util.List;
+
+public interface Condition {
+
+ static Condition merge(List<Condition> conditions) {
+ Condition result = null;
+ var iterator = conditions.iterator();
+ if (iterator.hasNext()) {
+ result = iterator.next();
+ while (iterator.hasNext()) {
+ result = new Condition.And(result, iterator.next());
+ }
+ }
+ return result;
+ }
+
+ class Equals extends Compare {
+ public Equals(String column, Object value) {
+ super(column, "=", column, value);
+ }
+ }
+
+ class Compare implements Condition {
+ private final String column;
+ private final String operator;
+ private final String variable;
+ private final Object value;
+
+ public Compare(String column, String operator, String variable, Object value) {
+ this.column = column;
+ this.operator = operator;
+ this.variable = variable;
+ this.value = value;
+ }
+
+ public String getColumn() {
+ return column;
+ }
+
+ public String getOperator() {
+ return operator;
+ }
+
+ public String getVariable() {
+ return variable;
+ }
+
+ public Object getValue() {
+ return value;
+ }
+ }
+
+ class Between implements Condition {
+ private final String column;
+ private final String fromVariable;
+ private final Object fromValue;
+ private final String toVariable;
+ private final Object toValue;
+
+ public Between(String column, String fromVariable, Object fromValue, String toVariable, Object toValue) {
+ this.column = column;
+ this.fromVariable = fromVariable;
+ this.fromValue = fromValue;
+ this.toVariable = toVariable;
+ this.toValue = toValue;
+ }
+
+ public String getColumn() {
+ return column;
+ }
+
+ public String getFromVariable() {
+ return fromVariable;
+ }
+
+ public Object getFromValue() {
+ return fromValue;
+ }
+
+ public String getToVariable() {
+ return toVariable;
+ }
+
+ public Object getToValue() {
+ return toValue;
+ }
+ }
+
+ class Or extends Binary {
+ public Or(Condition first, Condition second) {
+ super(first, second, "OR");
+ }
+ }
+
+ class And extends Binary {
+ public And(Condition first, Condition second) {
+ super(first, second, "AND");
+ }
+ }
+
+ class Binary implements Condition {
+ private final Condition first;
+ private final Condition second;
+ private final String operator;
+
+ public Binary(Condition first, Condition second, String operator) {
+ this.first = first;
+ this.second = second;
+ this.operator = operator;
+ }
+
+ public Condition getFirst() {
+ return first;
+ }
+
+ public Condition getSecond() {
+ return second;
+ }
+
+ public String getOperator() {
+ return operator;
+ }
+ }
+
+ class Permission implements Condition {
+ private final Class<?> ownerClass;
+ private final long ownerId;
+ private final Class<?> propertyClass;
+ private final long propertyId;
+ private final boolean excludeGroups;
+
+ private Permission(
+ Class<?> ownerClass, long ownerId, Class<?> propertyClass, long propertyId, boolean excludeGroups) {
+ this.ownerClass = ownerClass;
+ this.ownerId = ownerId;
+ this.propertyClass = propertyClass;
+ this.propertyId = propertyId;
+ this.excludeGroups = excludeGroups;
+ }
+
+ public Permission(Class<?> ownerClass, long ownerId, Class<?> propertyClass) {
+ this(ownerClass, ownerId, propertyClass, 0, false);
+ }
+
+ public Permission(Class<?> ownerClass, Class<?> propertyClass, long propertyId) {
+ this(ownerClass, 0, propertyClass, propertyId, false);
+ }
+
+ public Permission excludeGroups() {
+ return new Permission(this.ownerClass, this.ownerId, this.propertyClass, this.propertyId, true);
+ }
+
+ public Class<?> getOwnerClass() {
+ return ownerClass;
+ }
+
+ public long getOwnerId() {
+ return ownerId;
+ }
+
+ public Class<?> getPropertyClass() {
+ return propertyClass;
+ }
+
+ public long getPropertyId() {
+ return propertyId;
+ }
+
+ public boolean getIncludeGroups() {
+ boolean ownerGroupModel = GroupedModel.class.isAssignableFrom(ownerClass);
+ boolean propertyGroupModel = GroupedModel.class.isAssignableFrom(propertyClass);
+ return (ownerGroupModel || propertyGroupModel) && !excludeGroups;
+ }
+ }
+
+ class LatestPositions implements Condition {
+ private final long deviceId;
+
+ public LatestPositions(long deviceId) {
+ this.deviceId = deviceId;
+ }
+
+ public LatestPositions() {
+ this(0);
+ }
+
+ public long getDeviceId() {
+ return deviceId;
+ }
+ }
+
+}
diff --git a/src/main/java/org/traccar/storage/query/Limit.java b/src/main/java/org/traccar/storage/query/Limit.java
new file mode 100644
index 000000000..9673e5426
--- /dev/null
+++ b/src/main/java/org/traccar/storage/query/Limit.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.storage.query;
+
+public class Limit {
+
+ private final int value;
+
+ public Limit(int value) {
+ this.value = value;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+}
diff --git a/src/main/java/org/traccar/storage/query/Order.java b/src/main/java/org/traccar/storage/query/Order.java
new file mode 100644
index 000000000..f10970381
--- /dev/null
+++ b/src/main/java/org/traccar/storage/query/Order.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.storage.query;
+
+public class Order {
+
+ private final String column;
+ private final boolean descending;
+ private final int limit;
+
+ public Order(String column) {
+ this(column, false, 0);
+ }
+
+ public Order(String column, boolean descending, int limit) {
+ this.column = column;
+ this.descending = descending;
+ this.limit = limit;
+ }
+
+ public String getColumn() {
+ return column;
+ }
+
+ public boolean getDescending() {
+ return descending;
+ }
+
+ public int getLimit() {
+ return limit;
+ }
+
+}
diff --git a/src/main/java/org/traccar/storage/query/Request.java b/src/main/java/org/traccar/storage/query/Request.java
new file mode 100644
index 000000000..b9c2c963c
--- /dev/null
+++ b/src/main/java/org/traccar/storage/query/Request.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.storage.query;
+
+public class Request {
+
+ private final Columns columns;
+ private final Condition condition;
+ private final Order order;
+
+ public Request(Columns columns) {
+ this(columns, null, null);
+ }
+
+ public Request(Condition condition) {
+ this(null, condition, null);
+ }
+
+ public Request(Columns columns, Condition condition) {
+ this(columns, condition, null);
+ }
+
+ public Request(Columns columns, Order order) {
+ this(columns, null, order);
+ }
+
+ public Request(Columns columns, Condition condition, Order order) {
+ this.columns = columns;
+ this.condition = condition;
+ this.order = order;
+ }
+
+ public Columns getColumns() {
+ return columns;
+ }
+
+ public Condition getCondition() {
+ return condition;
+ }
+
+ public Order getOrder() {
+ return order;
+ }
+
+}