Skip to content

Commit cd11dea

Browse files
authored
Merge pull request #301 from eclipse/isolate-mongodb
[BUG] MongoDB conversions applied also to other databases
2 parents b6bbcf4 + 32bc587 commit cd11dea

File tree

12 files changed

+128
-32
lines changed

12 files changed

+128
-32
lines changed

CHANGELOG.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version
1313
- Update API using Apache Tinkerpop
1414
- Update package name of Graph to Tinkerpop
1515

16+
== Fixed
17+
18+
- MongoDB conversions applied also to other databases
19+
1620
== [1.1.3] - 2024-10-24
1721

1822
=== Added

jnosql-arangodb/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<description>The Eclipse JNoSQL layer to ArangoDB</description>
2929

3030
<properties>
31-
<arango.driver>7.13.0</arango.driver>
31+
<arango.driver>7.15.0</arango.driver>
3232
</properties>
3333
<dependencies>
3434
<dependency>

jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/QueryAQLConverter.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ private static void definesCondition(CriteriaCondition condition,
130130
char entity, int counter) {
131131

132132
Element document = condition.element();
133+
int localCounter = counter;
133134
switch (condition.condition()) {
134135
case IN:
135136
appendCondition(aql, params, entity, document, IN);
@@ -157,27 +158,26 @@ private static void definesCondition(CriteriaCondition condition,
157158
for (CriteriaCondition dc : document.get(new TypeReference<List<CriteriaCondition>>() {
158159
})) {
159160

160-
if (isFirstCondition(aql, counter)) {
161+
if (isFirstCondition(aql, localCounter)) {
161162
aql.append(AND);
162163
}
163-
definesCondition(dc, aql, params, entity, ++counter);
164+
definesCondition(dc, aql, params, entity, ++localCounter);
164165
}
165166
return;
166167
case OR:
167168

168169
for (CriteriaCondition dc : document.get(new TypeReference<List<CriteriaCondition>>() {
169170
})) {
170-
if (isFirstCondition(aql, counter)) {
171+
if (isFirstCondition(aql, localCounter)) {
171172
aql.append(OR);
172173
}
173-
definesCondition(dc, aql, params, entity, ++counter);
174+
definesCondition(dc, aql, params, entity, ++localCounter);
174175
}
175176
return;
176177
case NOT:
177178
CriteriaCondition documentCondition = document.get(CriteriaCondition.class);
178-
aql.append(NOT);
179-
aql.append(START_EXPRESSION);
180-
definesCondition(documentCondition, aql, params, entity, ++counter);
179+
aql.append(NOT).append(START_EXPRESSION);
180+
definesCondition(documentCondition, aql, params, entity, ++localCounter);
181181
aql.append(END_EXPRESSION);
182182
return;
183183
default:

jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/communication/DocumentQueryConversor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ private DocumentQueryConversor() {
3434

3535
public static Bson convert(CriteriaCondition condition) {
3636
Element document = condition.element();
37-
Object value = ValueUtil.convert(document.value());
37+
Object value = ValueUtil.convert(document.value(), MongoDBValueWriteDecorator.MONGO_DB_VALUE_WRITER);
3838
return switch (condition.condition()) {
3939
case EQUALS -> Filters.eq(document.name(), value);
4040
case GREATER_THAN -> Filters.gt(document.name(), value);
4141
case GREATER_EQUALS_THAN -> Filters.gte(document.name(), value);
4242
case LESSER_THAN -> Filters.lt(document.name(), value);
4343
case LESSER_EQUALS_THAN -> Filters.lte(document.name(), value);
4444
case IN -> {
45-
List<Object> inList = ValueUtil.convertToList(document.value());
45+
List<Object> inList = ValueUtil.convertToList(document.value(), MongoDBValueWriteDecorator.MONGO_DB_VALUE_WRITER);
4646
yield Filters.in(document.name(), inList.toArray());
4747
}
4848
case NOT -> {
@@ -69,7 +69,7 @@ public static Bson convert(CriteriaCondition condition) {
6969
.map(DocumentQueryConversor::convert).toList());
7070
}
7171
case BETWEEN -> {
72-
List<Object> betweenList = ValueUtil.convertToList(document.value());
72+
List<Object> betweenList = ValueUtil.convertToList(document.value(), MongoDBValueWriteDecorator.MONGO_DB_VALUE_WRITER);
7373
yield Filters.and(Filters.gte(document.name(), betweenList.get(0)),
7474
Filters.lte(document.name(), betweenList.get(1)));
7575

jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/communication/MongoDBUtils.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,13 @@
2727
import java.util.Map;
2828
import java.util.function.Function;
2929
import java.util.function.Predicate;
30-
import java.util.function.UnaryOperator;
3130
import java.util.stream.StreamSupport;
3231

3332
import static java.util.stream.StreamSupport.stream;
3433

3534
final class MongoDBUtils {
3635
static final String ID_FIELD = "_id";
3736

38-
private static final Function<Object, String> KEY_DOCUMENT = d -> cast(d).name();
39-
private static final UnaryOperator<Object> VALUE_DOCUMENT = d -> MongoDBUtils.convert(cast(d).value());
4037

4138
private MongoDBUtils() {
4239
}
@@ -48,7 +45,7 @@ static Document getDocument(CommunicationEntity entity) {
4845
}
4946

5047
private static Object convert(Value value) {
51-
Object val = ValueUtil.convert(value);
48+
Object val = ValueUtil.convert(value, MongoDBValueWriteDecorator.MONGO_DB_VALUE_WRITER);
5249
if (val instanceof Element subDocument) {
5350
Object converted = convert(subDocument.value());
5451
return new Document(subDocument.name(), converted);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2024 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.eclipse.jnosql.databases.mongodb.communication;
16+
17+
import org.eclipse.jnosql.communication.ValueWriter;
18+
import org.eclipse.jnosql.communication.ValueWriterDecorator;
19+
20+
import java.util.UUID;
21+
22+
final class MongoDBValueWriteDecorator<T, S> implements ValueWriter<T, S> {
23+
24+
@SuppressWarnings("rawtypes")
25+
static final ValueWriter MONGO_DB_VALUE_WRITER = new MongoDBValueWriteDecorator();
26+
27+
@SuppressWarnings("rawtypes")
28+
private static final ValueWriter DEFAULT = ValueWriterDecorator.getInstance();
29+
30+
private static final UUIDValueWriter UUID_VALUE_WRITER = new UUIDValueWriter();
31+
32+
33+
@Override
34+
public boolean test(Class<?> type) {
35+
return UUID_VALUE_WRITER.test(type) || DEFAULT.test(type);
36+
}
37+
38+
@SuppressWarnings("unchecked")
39+
@Override
40+
public S write(T type) {
41+
if(type != null && UUID_VALUE_WRITER.test(type.getClass())) {
42+
return (S) UUID_VALUE_WRITER.write((UUID) type);
43+
} else {
44+
return (S) DEFAULT.write(type);
45+
}
46+
}
47+
48+
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
org.eclipse.jnosql.databases.mongodb.communication.BinaryValueReader
2-
org.eclipse.jnosql.databases.mongodb.communication.UUIDValueReader

jnosql-mongodb/src/main/resources/META-INF/services/org.eclipse.jnosql.communication.ValueWriter

Lines changed: 0 additions & 1 deletion
This file was deleted.

jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/communication/UUIDValueReader.java renamed to jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/communication/MongoDBValueWriteDecoratorTest.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,20 @@
1414
*/
1515
package org.eclipse.jnosql.databases.mongodb.communication;
1616

17-
import org.eclipse.jnosql.communication.ValueReader;
17+
import org.junit.jupiter.api.Test;
1818

1919
import java.util.UUID;
2020

21-
public class UUIDValueReader implements ValueReader {
21+
import static org.junit.jupiter.api.Assertions.*;
2222

23-
@Override
24-
public boolean test(Class<?> type) {
25-
return UUID.class.equals(type);
26-
}
23+
@SuppressWarnings("rawtypes")
24+
class MongoDBValueWriteDecoratorTest {
2725

26+
private final MongoDBValueWriteDecorator<Object, String> valueWriter = new MongoDBValueWriteDecorator<>();
2827

29-
@SuppressWarnings("unchecked")
30-
@Override
31-
public <T> T read(Class<T> type, Object value) {
32-
if (value instanceof UUID) {
33-
return (T) value;
34-
}
35-
return null;
28+
@Test
29+
void shouldTestUUIDType() {
30+
assertTrue(valueWriter.test(UUID.class));
3631
}
32+
3733
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2024 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
* Alessandro Moscatelli
15+
*/
16+
package org.eclipse.jnosql.databases.mongodb.integration;
17+
18+
import jakarta.nosql.Column;
19+
import jakarta.nosql.Entity;
20+
import jakarta.nosql.Id;
21+
22+
import java.util.UUID;
23+
24+
@Entity
25+
public record MongoDBBook(@Id UUID id, @Column String title, @Column String author) {
26+
}

0 commit comments

Comments
 (0)