/** * Alias for {@link #toCql()}. */ @Override public String toString() { return toCql(); } }
/** * Appends the rendering of this identifier to the given {@link StringBuilder}, then returns that * {@link StringBuilder}. If {@literal null} is given, a new {@link StringBuilder} is created, appended to, and * returned. */ public StringBuilder toCql(StringBuilder builder) { return builder.append(toCql()); }
private String getTableName(Object entity) { Assert.notNull(entity, "Entity must not be null"); return operations.getTableName(entity.getClass()).toCql(); } }
/** * Returns the row's column value as an instance of the given type. * * @throws ClassCastException if the value cannot be converted to the requested type. */ @Nullable public <T> T get(CqlIdentifier columnName, Class<T> requestedType) { return get(columnName.toCql(), requestedType); }
@Override public String toString() { return getAlias().map(cqlIdentifier -> String.format("%s AS %s", getExpression(), cqlIdentifier.toCql())) .orElseGet(this::getExpression); } }
@Override public String toString() { String parameters = StringUtils.collectionToDelimitedString(getParameters(), ", "); return getAlias() .map(cqlIdentifier -> String.format("%s(%s) AS %s", getExpression(), parameters, cqlIdentifier.toCql())) .orElseGet(() -> String.format("%s(%s)", getExpression(), parameters)); } }
private Collection<Clause> getWhereClauses(ConvertingPropertyAccessor accessor, CassandraPersistentEntity<?> entity) { Assert.isTrue(entity.isCompositePrimaryKey(), String.format("Entity [%s] is not a composite primary key", entity.getName())); Collection<Clause> clauses = new ArrayList<>(); for (CassandraPersistentProperty property : entity) { TypeCodec<Object> codec = getCodec(property); Object value = accessor.getProperty(property, codec.getJavaType().getRawType()); clauses.add(QueryBuilder.eq(property.getRequiredColumnName().toCql(), value)); } return clauses; }
@Override public boolean hasProperty(CassandraPersistentProperty property) { return this.udtValue.getType().contains(property.getRequiredColumnName().toCql()); } }
@Override public WriteResult delete(Object entity, QueryOptions options) { Assert.notNull(entity, "Entity must not be null"); Assert.notNull(options, "QueryOptions must not be null"); CqlIdentifier tableName = getTableName(entity); Delete delete = QueryUtils.createDeleteQuery(tableName.toCql(), entity, options, getConverter()); maybeEmitEvent(new BeforeDeleteEvent<>(delete, entity.getClass(), tableName)); // noinspection ConstantConditions WriteResult result = getCqlOperations().execute(new StatementCallback(delete)); maybeEmitEvent(new AfterDeleteEvent<>(delete, entity.getClass(), tableName)); return result; }
private void addColumns(CassandraPersistentEntity<?> entity, List<Selector> selectors) { entity.doWithProperties((PropertyHandler<CassandraPersistentProperty>) property -> { if (property.isCompositePrimaryKey()) { CassandraPersistentEntity<?> primaryKeyEntity = mappingContext.getRequiredPersistentEntity(property); addColumns(primaryKeyEntity, selectors); } else { selectors.add(ColumnSelector.from(property.getRequiredColumnName().toCql())); } }); }
@Override public ListenableFuture<Long> count(Class<?> entityClass) { Assert.notNull(entityClass, "Entity type must not be null"); Select select = QueryBuilder.select().countAll().from(getTableName(entityClass).toCql()); return getAsyncCqlOperations().queryForObject(select, Long.class); }
@Override public boolean exists(Object id, Class<?> entityClass) { Assert.notNull(id, "Id must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); CassandraPersistentEntity<?> entity = getRequiredPersistentEntity(entityClass); Select select = QueryBuilder.select().from(getTableName(entityClass).toCql()); getConverter().write(id, select.where(), entity); return getCqlOperations().queryForResultSet(select).iterator().hasNext(); }
@Override public <T> Mono<T> selectOneById(Object id, Class<T> entityClass) { Assert.notNull(id, "Id must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); CassandraPersistentEntity<?> entity = getRequiredPersistentEntity(entityClass); Select select = QueryBuilder.select().all().from(entity.getTableName().toCql()); getConverter().write(id, select.where(), entity); return selectOne(select, entityClass); }
@Nullable @SuppressWarnings("unchecked") public <T> T getPropertyValue(CassandraPersistentProperty property) { String spelExpression = property.getSpelExpression(); if (spelExpression != null) { return this.evaluator.evaluate(spelExpression); } String name = property.getRequiredColumnName().toCql(); DataType fieldType = this.udtValue.getType().getFieldType(name); return this.udtValue.get(name, this.codecRegistry.codecFor(fieldType)); }
@Override public ListenableFuture<Boolean> exists(Object id, Class<?> entityClass) { Assert.notNull(id, "Id must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); CassandraPersistentEntity<?> entity = getRequiredPersistentEntity(entityClass); Select select = QueryBuilder.select().from(entity.getTableName().toCql()); getConverter().write(id, select.where(), entity); return new MappingListenableFutureAdapter<>(getAsyncCqlOperations().queryForResultSet(select), resultSet -> resultSet.iterator().hasNext()); }
@Override public <T> EntityWriteResult<T> update(T entity, UpdateOptions options) { Assert.notNull(entity, "Entity must not be null"); Assert.notNull(options, "UpdateOptions must not be null"); CqlIdentifier tableName = getTableName(entity); Update update = QueryUtils.createUpdateQuery(tableName.toCql(), entity, options, getConverter()); maybeEmitEvent(new BeforeSaveEvent<>(entity, tableName, update)); // noinspection ConstantConditions WriteResult result = getCqlOperations().execute(new StatementCallback(update)); maybeEmitEvent(new AfterSaveEvent<>(entity, tableName)); return EntityWriteResult.of(result, entity); }
@Override public List<T> findAll() { Select select = QueryBuilder.select().all().from(entityInformation.getTableName().toCql()); return operations.select(select, entityInformation.getJavaType()); }
private static com.datastax.driver.core.querybuilder.Update update(CqlIdentifier table, Update mappedUpdate, Filter filter) { com.datastax.driver.core.querybuilder.Update update = QueryBuilder.update(table.toCql()); for (AssignmentOp assignmentOp : mappedUpdate.getUpdateOperations()) { update.with(getAssignment(assignmentOp)); } for (CriteriaDefinition criteriaDefinition : filter) { update.where(toClause(criteriaDefinition)); } return update; }
<T> Mono<EntityWriteResult<T>> doInsert(T entity, WriteOptions options, CqlIdentifier tableName) { CassandraPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entity.getClass()); Insert insert = QueryUtils.createInsertQuery(tableName.toCql(), entity, options, getConverter(), persistentEntity); // noinspection ConstantConditions Mono<EntityWriteResult<T>> result = getReactiveCqlOperations() // .execute(new StatementCallback(insert)) // .doOnSubscribe(it -> maybeEmitEvent(new BeforeSaveEvent<>(entity, tableName, insert))) // .map(it -> EntityWriteResult.of(it, entity)) // .next(); return result.doOnNext(it -> maybeEmitEvent(new AfterSaveEvent<>(entity, tableName))); }
@Override public Mono<Boolean> deleteById(Object id, Class<?> entityClass) { Assert.notNull(id, "Id must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); CassandraPersistentEntity<?> entity = getRequiredPersistentEntity(entityClass); CqlIdentifier tableName = entity.getTableName(); Delete delete = QueryBuilder.delete().from(tableName.toCql()); getConverter().write(id, delete.where(), entity); Mono<Boolean> result = getReactiveCqlOperations().execute(delete) .doOnSubscribe(it -> maybeEmitEvent(new BeforeDeleteEvent<>(delete, entityClass, tableName))); return result.doOnNext(it -> maybeEmitEvent(new AfterDeleteEvent<>(delete, entityClass, tableName))); }