/** * Create a {@link EntityWriteResult} from {@link ResultSet} and an entity. * * @param resultSet must not be {@literal null}. * @param entity must not be {@literal null}. * @return the {@link EntityWriteResult} for {@link ResultSet} and an entity. */ static <T> EntityWriteResult<T> of(ResultSet resultSet, T entity) { return new EntityWriteResult<>(resultSet, entity); }
@Override public <S extends T> List<S> saveAll(Iterable<S> entities) { Assert.notNull(entities, "The given Iterable of entities must not be null"); List<S> result = new ArrayList<>(); for (S entity : entities) { result.add(operations.insert(entity, INSERT_NULLS).getEntity()); } return result; }
@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 <T> Mono<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()); Mono<EntityWriteResult<T>> result = getReactiveCqlOperations() // .execute(new StatementCallback(update)) // .doOnSubscribe(it -> maybeEmitEvent(new BeforeSaveEvent<>(entity, tableName, update))) // .map(it -> EntityWriteResult.of(it, entity)) // .next(); return result.doOnNext(it -> maybeEmitEvent(new AfterSaveEvent<>(entity, tableName))); }
@Override public <T> ListenableFuture<EntityWriteResult<T>> insert(T entity, InsertOptions options) { Assert.notNull(entity, "Entity must not be null"); Assert.notNull(options, "InsertOptions must not be null"); CassandraPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entity.getClass()); CqlIdentifier tableName = persistentEntity.getTableName(); Insert insert = QueryUtils.createInsertQuery(tableName.toCql(), entity, options, getConverter(), persistentEntity); maybeEmitEvent(new BeforeSaveEvent<>(entity, tableName, insert)); return new MappingListenableFutureAdapter<>(getAsyncCqlOperations().execute(new AsyncStatementCallback(insert)), resultSet -> { maybeEmitEvent(new AfterSaveEvent<>(entity, tableName)); return EntityWriteResult.of(resultSet, entity); }); }
@Override public <S extends T> S save(S entity) { Assert.notNull(entity, "Entity must not be null"); return operations.insert(entity, INSERT_NULLS).getEntity(); }
/** * Create a {@link EntityWriteResult} from {@link WriteResult} and an entity. * * @param result must not be {@literal null}. * @param entity must not be {@literal null}. * @return the {@link EntityWriteResult} for {@link WriteResult} and an entity. */ static <T> EntityWriteResult<T> of(WriteResult result, T entity) { return new EntityWriteResult<>(result.getExecutionInfo(), result.wasApplied(), result.getRows(), entity); }
<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 <T> T insert(T entity) { return insert(entity, InsertOptions.empty()).getEntity(); }
<T> EntityWriteResult<T> doInsert(T entity, WriteOptions options, CqlIdentifier tableName) { CassandraPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entity.getClass()); Insert insert = QueryUtils.createInsertQuery(tableName.toCql(), entity, options, getConverter(), persistentEntity); maybeEmitEvent(new BeforeSaveEvent<>(entity, tableName, insert)); // noinspection ConstantConditions WriteResult result = getCqlOperations().execute(new StatementCallback(insert)); maybeEmitEvent(new AfterSaveEvent<>(entity, tableName)); return EntityWriteResult.of(result, entity); }
@Override public <T> T update(T entity) { return update(entity, UpdateOptions.empty()).getEntity(); }
@Override public <T> ListenableFuture<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)); return new MappingListenableFutureAdapter<>(getAsyncCqlOperations().execute(new AsyncStatementCallback(update)), resultSet -> { maybeEmitEvent(new AfterSaveEvent<>(entity, tableName)); return EntityWriteResult.of(resultSet, entity); }); }