/** * Configures the {@link CassandraTemplate} used for Cassandra data access operations. * * @param cassandraTemplate {@link CassandraTemplate} used to perform CRUD, Query and general data access operations * on Apache Cassandra. */ public void setCassandraTemplate(CassandraTemplate cassandraTemplate) { this.cassandraOperations = cassandraTemplate; setMappingContext(cassandraTemplate.getConverter().getMappingContext()); }
@Override public <T> T selectOneById(Object id, Class<T> entityClass) { Assert.notNull(id, "Id must not be null"); Assert.notNull(entityClass, "Entity type must not be null"); CqlIdentifier tableName = getTableName(entityClass); Select select = QueryBuilder.select().all().from(tableName.toCql()); getConverter().write(id, select.where(), getRequiredPersistentEntity(entityClass)); Function<Row, T> mapper = getMapper(entityClass, entityClass, tableName); List<T> result = getCqlOperations().query(select, (row, rowNum) -> mapper.apply(row)); return result.isEmpty() ? null : result.get(0); }
@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; }
@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(); }
@SuppressWarnings("unchecked") private <T> Function<Row, T> getMapper(Class<?> entityType, Class<T> targetType, CqlIdentifier tableName) { Class<?> typeToRead = resolveTypeToRead(entityType, targetType); return row -> { maybeEmitEvent(new AfterLoadEvent(row, targetType, tableName)); Object source = getConverter().read(typeToRead, row); T result = (T) (targetType.isInterface() ? getProjectionFactory().createProjection(targetType, source) : source); if (result != null) { maybeEmitEvent(new AfterConvertEvent<>(row, result, tableName)); } 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 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); maybeEmitEvent(new BeforeDeleteEvent<>(delete, entityClass, tableName)); boolean result = getCqlOperations().execute(delete); maybeEmitEvent(new AfterDeleteEvent<>(delete, entityClass, tableName)); return result; }
<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); }