-
Notifications
You must be signed in to change notification settings - Fork 0
Reading operations
For reading operations the option that I have found is the following:
package uk.co.caeldev.cassitory.statements;
import com.datastax.driver.core.Session;
import uk.co.caeldev.cassitory.pojos.UserDtoStatement;
import uk.co.caeldev.cassitory.statements.repositories.BaseRepository;
import java.util.Map;
public class UserDtoStatementBaseStatementRepository extends BaseRepository<UserDtoStatement> {
public UserDtoStatementBaseStatementRepository(Session session, String preferredReadingTable) {
super(session);
}
@Override
protected Map<String, String> getSaveQueries() {
return UserDtoStatementQueriesCreator.saveQueries;
}
@Override
protected Map<String, String> getDeleteQueries() {
return UserDtoStatementQueriesCreator.deleteQueries;
}
@Override
protected Map<String, String> getSelectQueries() {
return UserDtoStatementQueriesCreator.selectQueries;
}
@Override
protected Map<String, Object[]> getSaveParameters(UserDtoStatement entity) {
UserDtoStatementParametersCreator userDtoStatementParametersCreator = new UserDtoStatementParametersCreator(entity);
return userDtoStatementParametersCreator.createSaveParameters();
}
@Override
protected Map<String, Object[]> getDeleteParameters(UserDtoStatement entity) {
UserDtoStatementParametersCreator userDtoStatementParametersCreator = new UserDtoStatementParametersCreator(entity);
return userDtoStatementParametersCreator.createDeleteParameters();
}
public List<UserDto> get(QUERIES queryName, Object ... params) {
// Get the query from QueriesCreator by queryName
// Execute the query binding the parameters and get the resultset.
// Map from resultset to an entity from String or have the capability to infer the type
}
public List<UserDto> getAllUsers() {
//if the default table is users
//read from users table
//else
//read from users_by_name table
}
public List<UserDto> getAllUsersByName() {
//if the default table is users
//read from users table
//else
//read from users_by_name table
}
}
enum QUERIES {
GETALLUSERS("getAllUsers"), GETALLUSERSBYNAME("getAllUsersByName")
}
@CassitoryEntity(tables = {"users", "users_by_name"})
@Query(name = "getAllUsers", query = "select * from users;", table="users")
@Query(name = "getAllUsers", query = "select * from users_by_name;", table="users_by_name")
@Query(name = "getAllUsersByName", query = "select * from users where name = ?;", table="users")
@Query(name = "getAllUsersByName", query = "select * from users_by_name where name = ?;", table="users_by_name")
public class UserDto {
@Mapping(table = "users", field = "name",pk = true)
@Mapping(table = "users_by_name", field = "fullName", pk = true)
private String fullName;
public String getFullName() {
return fullName;
}
}
One of the first things that you can noticed right away in the entity is a new annotation Query that holds all the queries supported by the repository.
it will drive the generation of the enum QUERIES and also add a few methods in Repository class generated.
the method
public List<UserDto> get(QUERIES queryName, Object... params)
is going to be moved to the base repository and exposed as public. Also for each query the base repo needs to create all the prepared statements.
The second thing to have in mind is the that as you can noticed there can be multiple queries associated to the same query name. This is because soon there will be an option to choose whether you want the data from one table or the other all driven by configuration of your app. So in this way you will have a capability of switching or reading from either both tables.