/** * Creates an instance of {@link CassandraTemplate} initialized with the given {@link SessionFactory} and * {@link CassandraConverter}. * * @param sessionFactory {@link SessionFactory} used to interact with Cassandra; must not be {@literal null}. * @param converter {@link CassandraConverter} used to convert between Java and Cassandra types; must not be * {@literal null}. * @see CassandraConverter * @see SessionFactory */ public CassandraTemplate(SessionFactory sessionFactory, CassandraConverter converter) { this(new CqlTemplate(sessionFactory), converter); }
@Override public List<RingMember> describeRing() throws DataAccessException { return (List<RingMember>) describeRing(RingMemberHostMapper.INSTANCE); }
@Override public boolean execute(String cql, Object... args) throws DataAccessException { return execute(cql, newPreparedStatementBinder(args)); }
@Override public List<Map<String, Object>> queryForList(String cql, Object... args) throws DataAccessException { // noinspection ConstantConditions return query(newPreparedStatementCreator(cql), newPreparedStatementBinder(args), newResultSetExtractor(newColumnMapRowMapper())); }
@Override public ResultSet queryForResultSet(String cql, Object... args) throws DataAccessException { // noinspection ConstantConditions return query(newPreparedStatementCreator(cql), newPreparedStatementBinder(args), rs -> rs); }
@Override public Map<String, Object> queryForMap(Statement statement) throws DataAccessException { // noinspection ConstantConditions return queryForObject(statement, newColumnMapRowMapper()); }
@Override @Nullable public <T> T execute(PreparedStatementCreator preparedStatementCreator, PreparedStatementCallback<T> action) throws DataAccessException { Assert.notNull(preparedStatementCreator, "PreparedStatementCreator must not be null"); Assert.notNull(action, "PreparedStatementCallback object must not be null"); try { if (logger.isDebugEnabled()) { logger.debug("Preparing statement [{}] using {}", toCql(preparedStatementCreator), preparedStatementCreator); } Session session = getCurrentSession(); return action.doInPreparedStatement(session, applyStatementSettings(preparedStatementCreator.createPreparedStatement(session))); } catch (DriverException e) { throw translateException("PreparedStatementCallback", toCql(preparedStatementCreator), e); } }
@Override public <T> List<T> query(String cql, @Nullable PreparedStatementBinder psb, RowMapper<T> rowMapper) throws DataAccessException { // noinspection ConstantConditions return query(newPreparedStatementCreator(cql), psb, newResultSetExtractor(rowMapper)); }
@Override public List<Map<String, Object>> queryForList(String cql) throws DataAccessException { // noinspection ConstantConditions return query(cql, newResultSetExtractor(newColumnMapRowMapper())); }
@Override public <T> T query(Statement statement, ResultSetExtractor<T> resultSetExtractor) throws DataAccessException { Assert.notNull(statement, "CQL Statement must not be null"); Assert.notNull(resultSetExtractor, "ResultSetExtractor must not be null"); try { if (logger.isDebugEnabled()) { logger.debug("Executing CQL Statement [{}]", statement); } return resultSetExtractor.extractData(getCurrentSession().execute(applyStatementSettings(statement))); } catch (DriverException e) { throw translateException("Query", statement.toString(), e); } }
@Override public <T> T execute(SessionCallback<T> action) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); try { return action.doInSession(getCurrentSession()); } catch (DriverException e) { throw translateException("SessionCallback", toCql(action), e); } }
@Override public <T> T query(String cql, @Nullable PreparedStatementBinder psb, ResultSetExtractor<T> resultSetExtractor) throws DataAccessException { return query(newPreparedStatementCreator(cql), psb, resultSetExtractor); }
@Nullable @Override public <T> T execute(String cql, PreparedStatementCallback<T> action) throws DataAccessException { return execute(newPreparedStatementCreator(cql), action); }
private void executeSpecsAndScripts(List<? extends KeyspaceActionSpecification> keyspaceActionSpecifications, List<String> scripts, Cluster cluster) { if (!CollectionUtils.isEmpty(keyspaceActionSpecifications) || !CollectionUtils.isEmpty(scripts)) { Session session = cluster.connect(); try { CqlTemplate template = new CqlTemplate(session); keyspaceActionSpecifications .forEach(keyspaceActionSpecification -> template.execute(toCql(keyspaceActionSpecification))); scripts.forEach(template::execute); } finally { if (session != null) { session.close(); } } } }
private Session getCurrentSession() { SessionFactory sessionFactory = getSessionFactory(); Assert.state(sessionFactory != null, "SessionFactory is null"); return sessionFactory.getSession(); }
@Override public <T> Collection<T> describeRing(HostMapper<T> hostMapper) throws DataAccessException { Assert.notNull(hostMapper, "HostMapper must not be null"); return hostMapper.mapHosts(getHosts()); }
private Set<Host> getHosts() { return getCurrentSession().getCluster().getMetadata().getAllHosts(); }
@Override public <T> List<T> queryForList(String cql, Class<T> elementType, Object... args) throws DataAccessException { // noinspection ConstantConditions return query(newPreparedStatementCreator(cql), newPreparedStatementBinder(args), newResultSetExtractor(newSingleColumnRowMapper(elementType))); }
@Nullable @Override public <T> T query(PreparedStatementCreator preparedStatementCreator, @Nullable PreparedStatementBinder psb, ResultSetExtractor<T> resultSetExtractor) throws DataAccessException { Assert.notNull(preparedStatementCreator, "PreparedStatementCreator must not be null"); Assert.notNull(resultSetExtractor, "ResultSetExtractor object must not be null"); try { if (logger.isDebugEnabled()) { logger.debug("Preparing statement [{}] using {}", toCql(preparedStatementCreator), preparedStatementCreator); } Session session = getCurrentSession(); PreparedStatement preparedStatement = preparedStatementCreator.createPreparedStatement(session); if (logger.isDebugEnabled()) { logger.debug("Executing prepared statement [{}]", preparedStatement); } BoundStatement boundStatement = applyStatementSettings( psb != null ? psb.bindValues(preparedStatement) : preparedStatement.bind()); ResultSet results = session.execute(boundStatement); return resultSetExtractor.extractData(results); } catch (DriverException e) { throw translateException("Query", toCql(preparedStatementCreator), e); } }
@Override public <T> T query(String cql, ResultSetExtractor<T> resultSetExtractor, Object... args) throws DataAccessException { return query(newPreparedStatementCreator(cql), newPreparedStatementBinder(args), resultSetExtractor); }