Skip to content

DATACOUCH-1066 - N1qlExpression as QueryCriteria key. #1081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4b2a954
DATACOUCH-635 - Prepare next development iteration.
mp911de Oct 28, 2020
6c7e882
DATACOUCH-635 - After release cleanups.
mp911de Oct 28, 2020
17e2483
DATACOUCH-635 - Enable maintenance branch build.
mp911de Oct 29, 2020
02aca19
DATACOUCH-641 - Enable Maven caching for Jenkins jobs.
mp911de Oct 30, 2020
d50b09c
DATACOUCH-639 - Updated changelog.
mp911de Nov 11, 2020
9d671a8
DATACOUCH-639 - Prepare 4.1.1 (2020.0.1).
mp911de Nov 11, 2020
18ea50b
DATACOUCH-639 - Release version 4.1.1 (2020.0.1).
mp911de Nov 11, 2020
6ed5f52
DATACOUCH-639 - Prepare next development iteration.
mp911de Nov 11, 2020
b5d96b8
DATACOUCH-639 - After release cleanups.
mp911de Nov 11, 2020
3138b4a
DATACOUCH-662 Upgrade Couchbase Java SDK to 3.0.10 for 4.1.x. (#283)
mikereiche Dec 8, 2020
9d50ed9
DATACOUCH-637 - Updated changelog.
mp911de Dec 9, 2020
b73fd67
DATACOUCH-638 - Updated changelog.
mp911de Dec 9, 2020
d83036f
DATACOUCH-640 - Updated changelog.
mp911de Dec 9, 2020
3e8bda0
DATACOUCH-648 - Updated changelog.
mp911de Dec 9, 2020
d313bac
DATACOUCH-648 - Prepare 4.1.2 (2020.0.2).
mp911de Dec 9, 2020
57154e2
DATACOUCH-648 - Release version 4.1.2 (2020.0.2).
mp911de Dec 9, 2020
b2a0e69
DATACOUCH-648 - Prepare next development iteration.
mp911de Dec 9, 2020
d925df2
DATACOUCH-648 - After release cleanups.
mp911de Dec 9, 2020
e97ef65
DATACOUCH-630 - Add expiry to replace 4.1.x. (#286)
mikereiche Dec 9, 2020
f33cd3c
DATACOUCH-666 - Handle immutable entity in replace. (#288)
mikereiche Dec 9, 2020
788f7d9
DATACOUCH-672 - reinstate replace expiry tests (#289)
mikereiche Dec 9, 2020
dfd2dd6
DATACOUCH-650 - Implements CrudRepository and ReactiveCrudRepository.…
schauder Nov 12, 2020
57c21a7
DATACOUCH-650 - Polishing.
mp911de Nov 25, 2020
0d110de
DATACOUCH-661 - Fix integrations tests from 650. (#281)
mikereiche Dec 8, 2020
aa52337
DATACOUCH-666 - Set Ccouchbase java sdk back to 3.0.10 after inadvert…
mikereiche Dec 9, 2020
8991562
DATACOUCH-664 - Use Docker hub credentials for all CI jobs.
gregturn Dec 15, 2020
06500e1
Revert commits for DATACOUCH-650 that shouldn't had been backported t…
mp911de Jan 13, 2021
f788293
Updated changelog.
christophstrobl Jan 13, 2021
d2a6ced
Prepare 4.1.3 (2020.0.3).
christophstrobl Jan 13, 2021
5a55a79
Release version 4.1.3 (2020.0.3).
christophstrobl Jan 13, 2021
4da3b9b
Prepare next development iteration.
christophstrobl Jan 13, 2021
d2be5d1
After release cleanups.
christophstrobl Jan 13, 2021
01880de
Updated changelog.
christophstrobl Jan 13, 2021
346020f
This commit fixes the generated query by N1QLQueryCreator when the cr…
mmonti Feb 5, 2021
cbb7d4d
This commit modified the addMetaIfRequired() to support META() on id …
mmonti Feb 5, 2021
66d18f0
This commit adds a version of the public methods where(), and() and o…
mmonti Feb 5, 2021
51d349b
DATACOUCH-1066 - N1qlExpression as QueryCriteria key.
mikereiche Feb 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Collection;

import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.util.Assert;

import com.couchbase.client.core.msg.kv.DurabilityLevel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors
* Copyright 2012-2021 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,13 +27,16 @@
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
import org.springframework.lang.Nullable;

import static org.springframework.data.couchbase.core.query.N1QLExpression.*;

/**
* @author Michael Nitschinger
* @author Michael Reiche
* @author Mauro Monti
*/
public class QueryCriteria implements QueryCriteriaDefinition {

private final String key;
private final N1QLExpression key;
/**
* Holds the chain itself, the current operator being always the last one.
*/
Expand All @@ -46,15 +49,15 @@ public class QueryCriteria implements QueryCriteriaDefinition {
private Object[] value;
private String format;

QueryCriteria(List<QueryCriteria> chain, String key, Object[] value, ChainOperator chainOperator) {
QueryCriteria(List<QueryCriteria> chain, N1QLExpression key, Object[] value, ChainOperator chainOperator) {
this(chain, key, value, chainOperator, null, null);
}

QueryCriteria(List<QueryCriteria> chain, String key, Object value, ChainOperator chainOperator) {
QueryCriteria(List<QueryCriteria> chain, N1QLExpression key, Object value, ChainOperator chainOperator) {
this(chain, key, new Object[] { value }, chainOperator, null, null);
}

QueryCriteria(List<QueryCriteria> chain, String key, Object[] value, ChainOperator chainOperator, String operator,
QueryCriteria(List<QueryCriteria> chain, N1QLExpression key, Object[] value, ChainOperator chainOperator, String operator,
String format) {
this.criteriaChain = chain;
criteriaChain.add(this);
Expand All @@ -70,9 +73,16 @@ Object[] getValue() {
}

/**
* Static factory method to create a Criteria using the provided key.
* Static factory method to create a Criteria using the provided String key.
*/
public static QueryCriteria where(String key) {
return where(x(key));
}

/**
* Static factory method to create a Criteria using the provided N1QLExpression key.
*/
public static QueryCriteria where(N1QLExpression key) {
return new QueryCriteria(new ArrayList<>(), key, null, null);
}

Expand All @@ -83,21 +93,29 @@ private static QueryCriteria wrap(QueryCriteria criteria) {
}

public QueryCriteria and(String key) {
return and(x(key));
}

public QueryCriteria and(N1QLExpression key) {
return new QueryCriteria(this.criteriaChain, key, null, ChainOperator.AND);
}

public QueryCriteria and(QueryCriteria criteria) {
return new QueryCriteria(this.criteriaChain, null, criteria, ChainOperator.AND);
}

public QueryCriteria or(QueryCriteria criteria) {
return new QueryCriteria(this.criteriaChain, null, criteria, ChainOperator.OR);
public QueryCriteria or(String key) {
return or(x(key));
}

public QueryCriteria or(String key) {
public QueryCriteria or(N1QLExpression key) {
return new QueryCriteria(this.criteriaChain, key, null, ChainOperator.OR);
}

public QueryCriteria or(QueryCriteria criteria) {
return new QueryCriteria(this.criteriaChain, null, criteria, ChainOperator.OR);
}

public QueryCriteria eq(@Nullable Object o) {
return is(o);
}
Expand Down Expand Up @@ -343,7 +361,7 @@ public String export() { // used only by tests
*/
private StringBuilder exportSingle(StringBuilder sb, int[] paramIndexPtr, JsonValue parameters,
CouchbaseConverter converter) {
String fieldName = maybeBackTic(key);
String fieldName = key == null ? null : key.toString(); // maybeBackTic(key);
int valueLen = value == null ? 0 : value.length;
Object[] v = new Object[valueLen + 2];
v[0] = fieldName;
Expand Down Expand Up @@ -377,7 +395,7 @@ private StringBuilder exportSingle(StringBuilder sb, int[] paramIndexPtr, JsonVa
* @param parameters - parameters of the query. If operands are parameterized, their values are added to parameters
* @return string containing part of N1QL query
*/
private String maybeWrapValue(String key, Object value, int[] paramIndexPtr, JsonValue parameters,
private String maybeWrapValue(N1QLExpression key, Object value, int[] paramIndexPtr, JsonValue parameters,
CouchbaseConverter converter) {
if (paramIndexPtr != null) {
if (paramIndexPtr[0] >= 0) {
Expand All @@ -397,10 +415,10 @@ private String maybeWrapValue(String key, Object value, int[] paramIndexPtr, Jso
JsonObject params = (JsonObject) parameters;
// from StringBasedN1qlQueryParser.getNamedPlaceholderValues()
try {
params.put(key, convert(converter, value));
params.put(key.toString(), convert(converter, value));
} catch (InvalidArgumentException iae) {
if (value instanceof Object[]) {
params.put(key, JsonArray.from((Object[]) value));
params.put(key.toString(), JsonArray.from((Object[]) value));
} else {
throw iae;
}
Expand All @@ -416,7 +434,7 @@ private String maybeWrapValue(String key, Object value, int[] paramIndexPtr, Jso
} else if (value == null) {
return "null";
} else if (value instanceof Object[]) { // convert array into sequence of comma-separated values
StringBuffer l = new StringBuffer();
StringBuilder l = new StringBuilder();
l.append("[");
Object[] array = (Object[]) value;
for (int i = 0; i < array.length; i++) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public Pageable first() {
return delegate.first();
}

@Override
public Pageable withPage(int i) {
return new CountPageable(delegate.withPage(i));
}

public boolean hasPrevious() {
return delegate.hasPrevious();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors
* Copyright 2012-2021 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,12 +16,15 @@
package org.springframework.data.couchbase.repository.query;

import static org.springframework.data.couchbase.core.query.QueryCriteria.where;
import static org.springframework.data.couchbase.core.query.N1QLExpression.*;
import static org.springframework.data.couchbase.core.query.QueryCriteria.*;

import java.util.Iterator;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
import org.springframework.data.couchbase.core.query.N1QLExpression;
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.data.couchbase.core.query.QueryCriteria;
import org.springframework.data.domain.Sort;
Expand All @@ -36,32 +39,38 @@
/**
* @author Michael Nitschinger
* @author Michael Reiche
* @author Mauro Monti
*/
public class N1qlQueryCreator extends AbstractQueryCreator<Query, QueryCriteria> {

private static final String META_ID_PROPERTY = "id";
private static final String META_CAS_PROPERTY = "cas";

private final ParameterAccessor accessor;
private final MappingContext<?, CouchbasePersistentProperty> context;
private final QueryMethod queryMethod;
private final CouchbaseConverter converter;
private final String bucketName;

public N1qlQueryCreator(final PartTree tree, final ParameterAccessor accessor, QueryMethod queryMethod,
CouchbaseConverter converter) {
public N1qlQueryCreator(final PartTree tree, final ParameterAccessor accessor, final QueryMethod queryMethod,
final CouchbaseConverter converter, final String bucketName) {
super(tree, accessor);
this.accessor = accessor;
this.context = converter.getMappingContext();
this.queryMethod = queryMethod;
this.converter = converter;
this.bucketName = bucketName;
}

@Override
protected QueryCriteria create(final Part part, final Iterator<Object> iterator) {
PersistentPropertyPath<CouchbasePersistentProperty> path = context.getPersistentPropertyPath(part.getProperty());
CouchbasePersistentProperty property = path.getLeafProperty();
return from(part, property, where(path.toDotPath(cvtr)), iterator);
return from(part, property, where(addMetaIfRequired(path, property)), iterator);
}

static Converter<? super CouchbasePersistentProperty, String> cvtr = (
source) -> new StringBuilder(source.getName().length() + 2).append('`').append(source.getName()).append('`')
source) -> new StringBuilder(source.getFieldName().length() + 2).append('`').append(source.getFieldName()).append('`')
.toString();

@Override
Expand All @@ -73,7 +82,7 @@ protected QueryCriteria and(final Part part, final QueryCriteria base, final Ite
PersistentPropertyPath<CouchbasePersistentProperty> path = context.getPersistentPropertyPath(part.getProperty());
CouchbasePersistentProperty property = path.getLeafProperty();

return from(part, property, base.and(path.toDotPath()), iterator);
return from(part, property, base.and(addMetaIfRequired(path, property)), iterator);
}

@Override
Expand Down Expand Up @@ -149,4 +158,15 @@ private QueryCriteria from(final Part part, final CouchbasePersistentProperty pr
}
}

private N1QLExpression addMetaIfRequired(final PersistentPropertyPath<CouchbasePersistentProperty> persistentPropertyPath,
final CouchbasePersistentProperty property) {
if (property.isIdProperty()) {
return path(meta(i(bucketName)), i(META_ID_PROPERTY));
}
if (property.isVersionProperty()) {
return path(meta(i(bucketName)), i(META_CAS_PROPERTY));
}
return x(persistentPropertyPath.toDotPath(cvtr));
}

}
Loading