DATACOUCH-296 Add support for delete n1ql queries

Changes to support delete query by query derivation and SpEL based
queries.
- Adds a N1qlMutateQueryCreator which constucts the delete query
using dsl.
- Adds new SpEL values delete and returning required for delete
queries.

Original pull request: #144.
This commit is contained in:
Subhashni Balakrishnan
2017-04-14 13:50:50 -07:00
parent 2febc3709c
commit 70343b0ac1
16 changed files with 553 additions and 303 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2017 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.
@@ -45,6 +45,7 @@ import java.util.List;
* This tests PaginAndSortingRepository features in the Couchbase connector.
*
* @author Simon Baslé
* @author Subhashni Balakrishnan
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = IntegrationTestApplicationConfig.class)
@@ -185,4 +186,12 @@ public class N1qlCouchbaseRepositoryTests {
Sort sort = new Sort(Sort.Direction.DESC, "attendees");
List<Party> parties = partyRepository.findParties(sort);
}
@Test
public void testDeleteQuery() {
partyRepository.save(new Party("testDeleteQuery", "delete", "delete", null, 0, null));
List<Party> partyList = partyRepository.removeByDescriptionOrName("delete", "delete");
assertTrue(partyList.size() == 1);
}
}

View File

@@ -120,4 +120,19 @@ public class N1qlPlaceholderTests {
"one over the other in findAllWithMixedParamsInQuery", e.getMessage());
}
}
@Test
public void deleteQueryTest() {
String included = "90";
String excluded = "New Year";
int max = 200;
List<Party> result = partyRepository.removeWithPositionalParams(excluded, included, max);
assertEquals(10, result.size());
for (Party party : result) {
assertTrue(party.getDescription().contains(included));
assertFalse(party.getDescription().contains(excluded));
assertTrue(party.getAttendees() < max);
}
}
}

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2017 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.
* 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.springframework.data.couchbase.repository;
import java.util.Date;
@@ -14,6 +30,7 @@ import org.springframework.data.repository.query.Param;
/**
* @author Simon Baslé
* @author Subhashni Balakrishnan
*/
@ViewIndexed(designDoc = "party", viewName = "all")
@N1qlSecondaryIndexed(indexName = "party")
@@ -59,10 +76,15 @@ public interface PartyRepository extends CouchbaseRepository<Party, String> {
" AND `desc` NOT LIKE '%' || $1 || '%'")
List<Party> findAllWithPositionalParams(String ex, String inc, long minimumAttendees);
@Query("#{#n1ql.delete} WHERE #{#n1ql.filter} AND `desc` LIKE '%' || $2 || '%' AND attendees < $3" +
" AND `desc` NOT LIKE '%' || $1 || '%' #{#n1ql.returning}")
List<Party> removeWithPositionalParams(String ex, String inc, long minimumAttendees);
@Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND `desc` LIKE '%' || $2 || '%' AND attendees >= $3" +
" AND `desc` NOT LIKE '%' || $1 || '%' AND `desc` != \"this is \\\"$excluded\\\"\"")
List<Party> findAllWithPositionalParamsAndQuotedNamedParams(@Param("excluded") String ex, @Param("included") String inc, @Param("min") long min);
List<Party> findByDescriptionOrName(String description, String name);
List<Party> removeByDescriptionOrName(String description, String name);
}