Skip to content

Commit bc139fc

Browse files
committed
create delete query wrapper
Signed-off-by: Otavio Santana <[email protected]>
1 parent 3ca6ade commit bc139fc

File tree

2 files changed

+63
-15
lines changed

2 files changed

+63
-15
lines changed

jnosql-couchbase-driver/src/main/java/org/eclipse/jnosql/communication/couchbase/document/DefaultCouchbaseDocumentManager.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,19 @@
2727
import com.couchbase.client.java.search.SearchQuery;
2828
import com.couchbase.client.java.search.result.SearchResult;
2929
import com.couchbase.client.java.search.result.SearchRow;
30+
import jakarta.nosql.Sort;
3031
import jakarta.nosql.document.Document;
32+
import jakarta.nosql.document.DocumentCondition;
3133
import jakarta.nosql.document.DocumentDeleteQuery;
3234
import jakarta.nosql.document.DocumentEntity;
3335
import jakarta.nosql.document.DocumentQuery;
3436

3537
import java.time.Duration;
3638
import java.util.ArrayList;
39+
import java.util.Collections;
3740
import java.util.List;
3841
import java.util.Objects;
42+
import java.util.Optional;
3943
import java.util.logging.Level;
4044
import java.util.logging.Logger;
4145
import java.util.stream.Collectors;
@@ -135,21 +139,13 @@ public Iterable<DocumentEntity> update(Iterable<DocumentEntity> entities) {
135139
public void delete(DocumentDeleteQuery query) {
136140
Objects.requireNonNull(query, "query is required");
137141

138-
139142
Collection collection = bucket.collection(query.getDocumentCollection());
140-
141-
QueryConverter.QueryConverterResult delete = QueryConverter.delete(query, database);
142-
if (nonNull(delete.getStatement())) {
143-
ParameterizedN1qlQuery n1qlQuery = N1qlQuery.parameterized(delete.getStatement(), delete.getParams());
144-
bucket.query(n1qlQuery);
145-
}
146-
147-
if (!delete.getKeys().isEmpty()) {
148-
delete.getKeys()
149-
.stream()
150-
.map(s -> getPrefix(query.getDocumentCollection(), s))
151-
.forEach(bucket::remove);
152-
}
143+
DocumentQuery delete = DeleteQueryWrapper.of(query);
144+
Stream<DocumentEntity> entities = select(delete);
145+
entities.map(d -> d.find(ID_FIELD))
146+
.filter(Objects::nonNull)
147+
.map(Objects::toString)
148+
.forEach(collection::remove);
153149

154150
}
155151

@@ -209,7 +205,6 @@ public Stream<DocumentEntity> n1qlQuery(String n1ql) throws NullPointerException
209205
}
210206

211207

212-
213208
@Override
214209
public void close() {
215210
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.eclipse.jnosql.communication.couchbase.document;
2+
3+
import jakarta.nosql.Sort;
4+
import jakarta.nosql.document.DocumentCondition;
5+
import jakarta.nosql.document.DocumentDeleteQuery;
6+
import jakarta.nosql.document.DocumentQuery;
7+
8+
import java.util.Collections;
9+
import java.util.List;
10+
import java.util.Optional;
11+
12+
final class DeleteQueryWrapper implements DocumentQuery {
13+
14+
private final DocumentDeleteQuery query;
15+
16+
private DeleteQueryWrapper(DocumentDeleteQuery query) {
17+
this.query = query;
18+
}
19+
20+
@Override
21+
public long getLimit() {
22+
return 0;
23+
}
24+
25+
@Override
26+
public long getSkip() {
27+
return 0;
28+
}
29+
30+
@Override
31+
public String getDocumentCollection() {
32+
return query.getDocumentCollection();
33+
}
34+
35+
@Override
36+
public Optional<DocumentCondition> getCondition() {
37+
return this.query.getCondition();
38+
}
39+
40+
@Override
41+
public List<Sort> getSorts() {
42+
return Collections.emptyList();
43+
}
44+
45+
@Override
46+
public List<String> getDocuments() {
47+
return Collections.emptyList();
48+
}
49+
50+
static DocumentQuery of(DocumentDeleteQuery query) {
51+
return new DeleteQueryWrapper(query);
52+
}
53+
}

0 commit comments

Comments
 (0)