Search in sources :

Example 26 with Query

use of org.springframework.data.cassandra.core.query.Query in project spring-data-cassandra by spring-projects.

the class AsyncCassandraTemplateIntegrationTests method shouldSelectByQueryWithSorting.

// DATACASS-343
@Test
void shouldSelectByQueryWithSorting() {
    UserToken token1 = new UserToken();
    token1.setUserId(Uuids.endOf(System.currentTimeMillis()));
    token1.setToken(Uuids.startOf(System.currentTimeMillis()));
    token1.setUserComment("foo");
    UserToken token2 = new UserToken();
    token2.setUserId(token1.getUserId());
    token2.setToken(Uuids.endOf(System.currentTimeMillis() + 100));
    token2.setUserComment("bar");
    getUninterruptibly(template.insert(token1));
    getUninterruptibly(template.insert(token2));
    Query query = Query.query(where("userId").is(token1.getUserId())).sort(Sort.by("token"));
    assertThat(getUninterruptibly(template.select(query, UserToken.class))).containsSequence(token1, token2);
}
Also used : Query(org.springframework.data.cassandra.core.query.Query) UserToken(org.springframework.data.cassandra.domain.UserToken) Test(org.junit.jupiter.api.Test)

Example 27 with Query

use of org.springframework.data.cassandra.core.query.Query in project spring-data-cassandra by spring-projects.

the class AsyncCassandraTemplateIntegrationTests method deleteColumnsByQueryShouldRemoveColumn.

// DATACASS-343
@Test
void deleteColumnsByQueryShouldRemoveColumn() {
    User user = new User("heisenberg", "Walter", "White");
    getUninterruptibly(template.insert(user));
    Query query = Query.query(where("id").is("heisenberg")).columns(Columns.from("lastname"));
    assertThat(getUninterruptibly(template.delete(query, User.class))).isTrue();
    User loaded = getUser(user.getId());
    assertThat(loaded.getFirstname()).isEqualTo("Walter");
    assertThat(loaded.getLastname()).isNull();
}
Also used : User(org.springframework.data.cassandra.domain.User) Query(org.springframework.data.cassandra.core.query.Query) Test(org.junit.jupiter.api.Test)

Example 28 with Query

use of org.springframework.data.cassandra.core.query.Query in project spring-data-cassandra by spring-projects.

the class AsyncCassandraTemplateIntegrationTests method updateShouldUpdateEntityByQuery.

// DATACASS-343
@Test
void updateShouldUpdateEntityByQuery() {
    User user = new User("heisenberg", "Walter", "White");
    getUninterruptibly(template.insert(user));
    Query query = Query.query(where("id").is("heisenberg"));
    boolean result = getUninterruptibly(template.update(query, Update.empty().set("firstname", "Walter Hartwell"), User.class));
    assertThat(result).isTrue();
    assertThat(getUser(user.getId()).getFirstname()).isEqualTo("Walter Hartwell");
}
Also used : User(org.springframework.data.cassandra.domain.User) Query(org.springframework.data.cassandra.core.query.Query) Test(org.junit.jupiter.api.Test)

Example 29 with Query

use of org.springframework.data.cassandra.core.query.Query in project spring-data-cassandra by spring-projects.

the class StatementFactory method createSelect.

private StatementBuilder<Select> createSelect(Query query, CassandraPersistentEntity<?> entity, Filter filter, List<Selector> selectors, CqlIdentifier tableName) {
    Sort sort = Optional.of(query.getSort()).map(querySort -> getQueryMapper().getMappedSort(querySort, entity)).orElse(Sort.unsorted());
    StatementBuilder<Select> select = createSelectAndOrder(selectors, tableName, filter, sort);
    if (query.getLimit() > 0) {
        select.apply(it -> it.limit(Math.toIntExact(query.getLimit())));
    }
    if (query.isAllowFiltering()) {
        select.apply(Select::allowFiltering);
    }
    select.onBuild(statementBuilder -> query.getPagingState().ifPresent(statementBuilder::setPagingState));
    query.getQueryOptions().ifPresent(it -> select.transform(statement -> QueryOptionsUtil.addQueryOptions(statement, it)));
    return select;
}
Also used : QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) QueryOptions(org.springframework.data.cassandra.core.cql.QueryOptions) TermFactory(org.springframework.data.cassandra.core.cql.util.TermFactory) Relation(com.datastax.oss.driver.api.querybuilder.relation.Relation) Assignment(com.datastax.oss.driver.api.querybuilder.update.Assignment) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Insert(com.datastax.oss.driver.api.querybuilder.insert.Insert) Delete(com.datastax.oss.driver.api.querybuilder.delete.Delete) SetAtIndexOp(org.springframework.data.cassandra.core.query.Update.SetAtIndexOp) SetOp(org.springframework.data.cassandra.core.query.Update.SetOp) UpdateWithAssignments(com.datastax.oss.driver.api.querybuilder.update.UpdateWithAssignments) EntityProjection(org.springframework.data.projection.EntityProjection) DeleteSelection(com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection) CassandraPersistentEntity(org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity) PersistentPropertyTranslator(org.springframework.data.cassandra.core.mapping.PersistentPropertyTranslator) Predicates(org.springframework.data.util.Predicates) Map(java.util.Map) RemoveOp(org.springframework.data.cassandra.core.query.Update.RemoveOp) Sort(org.springframework.data.domain.Sort) ColumnSelector(org.springframework.data.cassandra.core.query.Columns.ColumnSelector) Select(com.datastax.oss.driver.api.querybuilder.select.Select) PersistentProperty(org.springframework.data.mapping.PersistentProperty) BindMarker(com.datastax.oss.driver.api.querybuilder.BindMarker) ClassUtils(org.springframework.util.ClassUtils) ColumnRelationBuilder(com.datastax.oss.driver.api.querybuilder.relation.ColumnRelationBuilder) Collection(java.util.Collection) Set(java.util.Set) Predicate(org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate) Collectors(java.util.stream.Collectors) Selector(org.springframework.data.cassandra.core.query.Columns.Selector) List(java.util.List) UpdateMapper(org.springframework.data.cassandra.core.convert.UpdateMapper) PropertyDescriptor(java.beans.PropertyDescriptor) QueryMapper(org.springframework.data.cassandra.core.convert.QueryMapper) Optional(java.util.Optional) AddToOp(org.springframework.data.cassandra.core.query.Update.AddToOp) NonNull(org.springframework.lang.NonNull) AssignmentOp(org.springframework.data.cassandra.core.query.Update.AssignmentOp) IncrOp(org.springframework.data.cassandra.core.query.Update.IncrOp) SetAtKeyOp(org.springframework.data.cassandra.core.query.Update.SetAtKeyOp) WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) AddToMapOp(org.springframework.data.cassandra.core.query.Update.AddToMapOp) UpdateStart(com.datastax.oss.driver.api.querybuilder.update.UpdateStart) OngoingAssignment(com.datastax.oss.driver.api.querybuilder.update.OngoingAssignment) Condition(com.datastax.oss.driver.api.querybuilder.condition.Condition) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder) CriteriaDefinition(org.springframework.data.cassandra.core.query.CriteriaDefinition) Filter(org.springframework.data.cassandra.core.query.Filter) Function(java.util.function.Function) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ProjectionInformation(org.springframework.data.projection.ProjectionInformation) FunctionCall(org.springframework.data.cassandra.core.query.Columns.FunctionCall) StatementBuilder(org.springframework.data.cassandra.core.cql.util.StatementBuilder) Nullable(org.springframework.lang.Nullable) Mode(org.springframework.data.cassandra.core.query.Update.AddToOp.Mode) ConditionBuilder(com.datastax.oss.driver.api.querybuilder.condition.ConditionBuilder) EntityWriter(org.springframework.data.convert.EntityWriter) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) Update(org.springframework.data.cassandra.core.query.Update) CassandraConverter(org.springframework.data.cassandra.core.convert.CassandraConverter) Query(org.springframework.data.cassandra.core.query.Query) QueryOptionsUtil(org.springframework.data.cassandra.core.cql.QueryOptionsUtil) Term(com.datastax.oss.driver.api.querybuilder.term.Term) Collections(java.util.Collections) Where(org.springframework.data.cassandra.core.convert.Where) Columns(org.springframework.data.cassandra.core.query.Columns) Assert(org.springframework.util.Assert) Select(com.datastax.oss.driver.api.querybuilder.select.Select) Sort(org.springframework.data.domain.Sort)

Example 30 with Query

use of org.springframework.data.cassandra.core.query.Query in project spring-data-cassandra by spring-projects.

the class QueryMapperUnitTests method shouldMapPropertyToColumnName.

// DATACASS-343
@Test
void shouldMapPropertyToColumnName() {
    Query query = Query.query(Criteria.where("firstName").is("bar"));
    Filter mappedObject = queryMapper.getMappedObject(query, persistentEntity);
    CriteriaDefinition mappedCriteriaDefinition = mappedObject.iterator().next();
    assertThat(mappedCriteriaDefinition.getColumnName()).isEqualTo(ColumnName.from(CqlIdentifier.fromCql("first_name")));
    assertThat(mappedCriteriaDefinition.getColumnName().toString()).isEqualTo("first_name");
}
Also used : Query(org.springframework.data.cassandra.core.query.Query) Filter(org.springframework.data.cassandra.core.query.Filter) CriteriaDefinition(org.springframework.data.cassandra.core.query.CriteriaDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

Query (org.springframework.data.cassandra.core.query.Query)72 Test (org.junit.jupiter.api.Test)65 User (org.springframework.data.cassandra.domain.User)22 Filter (org.springframework.data.cassandra.core.query.Filter)18 Update (org.springframework.data.cassandra.core.query.Update)17 CriteriaDefinition (org.springframework.data.cassandra.core.query.CriteriaDefinition)15 Select (com.datastax.oss.driver.api.querybuilder.select.Select)13 UserToken (org.springframework.data.cassandra.domain.UserToken)8 Group (org.springframework.data.cassandra.domain.Group)7 QueryOptions (org.springframework.data.cassandra.core.cql.QueryOptions)6 Columns (org.springframework.data.cassandra.core.query.Columns)6 Delete (com.datastax.oss.driver.api.querybuilder.delete.Delete)5 WriteOptions (org.springframework.data.cassandra.core.cql.WriteOptions)5 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)4 Collection (java.util.Collection)4 LinkedHashMap (java.util.LinkedHashMap)4 StepVerifier (reactor.test.StepVerifier)4 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)3 Uuids (com.datastax.oss.driver.api.core.uuid.Uuids)3