/*
* Copyright 2016-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.core.cql;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.data.cassandra.SessionFactory;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.Statement;
import com.datastax.driver.core.exceptions.DriverException;
/**
* This is the central class in the CQL core package. It simplifies the use of CQL and helps to avoid common
* errors. It executes core CQL workflow, leaving application code to provide CQL and extract results. This class
* executes CQL queries or updates, initiating iteration over {@link ResultSet}s and catching {@link DriverException}
* exceptions and translating them to the generic, more informative exception hierarchy defined in the
* {@code org.springframework.dao} package.
*
* Code using this class need only implement callback interfaces, giving them a clearly defined contract. The
* {@link PreparedStatementCreator} callback interface creates a prepared statement given a Connection, providing CQL
* and any necessary parameters. The {@link ResultSetExtractor} interface extracts values from a {@link ResultSet}. See
* also {@link PreparedStatementBinder} and {@link RowMapper} for two popular alternative callback interfaces.
*
* Can be used within a service implementation via direct instantiation with a {@link Session} reference, or get
* prepared in an application context and given to services as bean reference. Note: The {@link Session} should always
* be configured as a bean in the application context, in the first case given to the service directly, in the second
* case to the prepared template.
*
* Because this class is parameterizable by the callback interfaces and the
* {@link org.springframework.dao.support.PersistenceExceptionTranslator} interface, there should be no need to subclass
* it.
*
* All CQL operations performed by this class are logged at debug level, using
* "org.springframework.data.cassandra.core.cqlTemplate" as log category.
*
* NOTE: An instance of this class is thread-safe once configured.
*
* @author David Webb
* @author Matthew Adams
* @author Ryan Scheidter
* @author Antoine Toulme
* @author John Blum
* @author Mark Paluch
* @author Mike Barlotta
* @see PreparedStatementCreator
* @see PreparedStatementBinder
* @see PreparedStatementCallback
* @see ResultSetExtractor
* @see RowCallbackHandler
* @see RowMapper
* @see org.springframework.dao.support.PersistenceExceptionTranslator
*/
public class CqlTemplate extends CassandraAccessor implements CqlOperations {
/**
* Create a new, uninitialized {@link CqlTemplate}. Note: The {@link SessionFactory} has to be set before using the
* instance.
*
* @see #setSessionFactory(SessionFactory)
*/
public CqlTemplate() {}
/**
* Create a new {@link CqlTemplate} initialized with the given {@link Session}.
*
* @param session the active Cassandra {@link Session}.
* @throws IllegalStateException if {@link Session} is {@literal null}.
* @see com.datastax.driver.core.Session
*/
public CqlTemplate(Session session) {
Assert.notNull(session, "Session must not be null");
setSession(session);
}
/**
* Constructs a new {@link CqlTemplate} with the given {@link SessionFactory}.
*
* @param sessionFactory the active Cassandra {@link SessionFactory}, must not be {@literal null}.
* @see SessionFactory
*/
public CqlTemplate(SessionFactory sessionFactory) {
Assert.notNull(sessionFactory, "SessionFactory must not be null");
setSessionFactory(sessionFactory);
}
// -------------------------------------------------------------------------
// Methods dealing with a plain com.datastax.driver.core.Session
// -------------------------------------------------------------------------
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.cqlOperations#execute(org.springframework.data.cassandra.core.cql.SessionCallback)
*/
@Override
public T execute(SessionCallback 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);
}
}
// -------------------------------------------------------------------------
// Methods dealing with static CQL
// -------------------------------------------------------------------------
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.cqlOperations#execute(java.lang.String)
*/
@Override
public boolean execute(String cql) throws DataAccessException {
Assert.hasText(cql, "CQL must not be empty");
return queryForResultSet(cql).wasApplied();
}
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.cqlOperations#query(java.lang.String, org.springframework.data.cassandra.core.cql.ResultSetExtractor)
*/
@Override
@Nullable
public T query(String cql, ResultSetExtractor resultSetExtractor) throws DataAccessException {
Assert.hasText(cql, "CQL must not be empty");
Assert.notNull(resultSetExtractor, "ResultSetExtractor must not be null");
try {
if (logger.isDebugEnabled()) {
logger.debug("Executing CQL Statement [{}]", cql);
}
SimpleStatement statement = applyStatementSettings(new SimpleStatement(cql));
ResultSet results = getCurrentSession().execute(statement);
return resultSetExtractor.extractData(results);
} catch (DriverException e) {
throw translateException("Query", cql, e);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.cqlOperations#query(java.lang.String, org.springframework.data.cassandra.core.cql.RowCallbackHandler)
*/
@Override
public void query(String cql, RowCallbackHandler rowCallbackHandler) throws DataAccessException {
query(cql, newResultSetExtractor(rowCallbackHandler));
}
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.cqlOperations#query(java.lang.String, org.springframework.data.cassandra.core.cql.RowMapper)
*/
@Override
public List query(String cql, RowMapper rowMapper) throws DataAccessException {
// noinspection ConstantConditions
return query(cql, newResultSetExtractor(rowMapper));
}
/*
* (non-Javadoc)
* @see org.springframework.data.cassandra.core.cqlOperations#queryForList(java.lang.String)
*/
@Override
public List