com.jpattern.orm.session.OrmSession Maven / Gradle / Ivy
package com.jpattern.orm.session;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.jpattern.logger.ILogger;
import com.jpattern.orm.classtool.IOrmClassTool;
import com.jpattern.orm.classtool.IOrmClassToolMap;
import com.jpattern.orm.exception.OrmException;
import com.jpattern.orm.exception.OrmNotUniqueResultException;
import com.jpattern.orm.exception.OrmOptimisticLockException;
import com.jpattern.orm.logger.OrmLogger;
import com.jpattern.orm.query.DeleteQuery;
import com.jpattern.orm.query.CustomFindQuery;
import com.jpattern.orm.query.FindQuery;
import com.jpattern.orm.query.SqlExecutor;
import com.jpattern.orm.query.UpdateQuery;
import com.jpattern.orm.query.OrmClassToolMapNameSolver;
import com.jpattern.orm.query.OrmCustomFindQuery;
import com.jpattern.orm.query.OrmDeleteQuery;
import com.jpattern.orm.query.OrmFindQuery;
import com.jpattern.orm.query.OrmUpdateQuery;
import com.jpattern.orm.query.PlainSqlExecutor;
import com.jpattern.orm.script.IScriptExecutor;
import com.jpattern.orm.script.ScriptExecutor;
import com.jpattern.orm.transaction.ITransaction;
import com.jpattern.orm.transaction.ITransactionDefinition;
import com.jpattern.orm.transaction.TransactionDefinition;
/**
*
* @author Francesco Cina
*
* 27/giu/2011
*/
public class OrmSession implements SessionSqlPerformer {
private final ILogger logger = OrmLogger.getOrmLogger(this.getClass());
private final IOrmClassToolMap ormClassToolMap;
private final SessionStrategy sessionStrategy;
public OrmSession(IOrmClassToolMap ormClassToolMap, SessionStrategy sessionStrategy) {
this.ormClassToolMap = ormClassToolMap;
this.sessionStrategy = sessionStrategy;
}
@Override
public final ITransaction transaction() throws OrmException {
return this.transaction(new TransactionDefinition());
}
@Override
public final FindQuery findQuery(Class clazz) throws OrmException {
final OrmFindQuery query = new OrmFindQuery(this.ormClassToolMap , this, clazz);
final OrmClassToolMapNameSolver nameSolver = new OrmClassToolMapNameSolver(this.ormClassToolMap);
nameSolver.register(clazz);
query.setNameSolver(nameSolver);
return query;
}
@Override
public final FindQuery findQuery(Class clazz, String alias) throws OrmException {
final OrmFindQuery query = new OrmFindQuery(this.ormClassToolMap , this, clazz);
final OrmClassToolMapNameSolver nameSolver = new OrmClassToolMapNameSolver(this.ormClassToolMap);
nameSolver.register(clazz, alias);
query.setNameSolver(nameSolver);
return query;
}
@Override
public final CustomFindQuery findQuery(String selectClause, Class> clazz, String alias ) throws OrmException {
final OrmCustomFindQuery query = new OrmCustomFindQuery(selectClause, this.ormClassToolMap , this, clazz);
final OrmClassToolMapNameSolver nameSolver = new OrmClassToolMapNameSolver(this.ormClassToolMap);
nameSolver.register(clazz, alias);
query.setNameSolver(nameSolver);
return query;
}
public final IOrmClassToolMap getOrmClassToolMap() {
return this.ormClassToolMap;
}
@Override
public final T find(Class clazz, Object value) throws OrmException {
return this.find(clazz, new Object[]{value});
}
@Override
public final T find(Class clazz, Object[] values) throws OrmException {
final IOrmClassTool ormClassTool = this.getOrmClassToolMap().getOrmClassTool(clazz);
final ResultSetReader resultSetReader = new ResultSetReader() {
@Override
public T read(ResultSet resultSet) throws SQLException {
if ( resultSet.next() ) {
return ormClassTool.getOrmPersistor().mapRow("", resultSet, 0) ;
}
return null;
}
};
final SqlPerformer sqlExec = this.sqlPerformer();
sqlExec.setMaxRows(1);
return sqlExec.query(ormClassTool.getOrmCRUDQuery().getLoadQuery(), resultSetReader, values);
}
@Override
public final T findUnique(Class clazz, Object value) throws OrmException, OrmNotUniqueResultException {
final T result = this.find(clazz, value);
if (result==null) {
throw new OrmNotUniqueResultException("The query execution returned a number of objects different than 1");
}
return result;
}
@Override
public final T findUnique(Class clazz, Object[] values) throws OrmException, OrmNotUniqueResultException {
final T result = this.find(clazz, values);
if (result==null) {
throw new OrmNotUniqueResultException("The query execution returned a number of objects different than 1");
}
return result;
}
@Override
public final void save(final T object) throws OrmException {
@SuppressWarnings("unchecked")
final IOrmClassTool ormClassTool = (IOrmClassTool) this.getOrmClassToolMap().getOrmClassTool(object.getClass());
final SqlPerformer sqlExec = this.sqlPerformer();
// final String sql = ormClassTool.getOrmCRUDQuery().getSaveQuery();
//CHECK IF OBJECT HAS A 'VERSION' FIELD
if (ormClassTool.getClassMapper().getTableMap().isVersionable()) {
ormClassTool.getOrmPersistor().increaseVersion(object, true);
}
if (!ormClassTool.getOrmPersistor().useKeyGenerators(object)) {
this.logger.debug("Saving object without using generators");
final Object[] args = ormClassTool.getOrmPersistor().allValues(object);
final String sql = ormClassTool.getOrmCRUDQuery().getSaveQueryWithoutGenerators();
sqlExec.update(sql, args);
} else {
this.logger.debug("Saving object using generators");
final Object[] args = ormClassTool.getOrmPersistor().allNotGeneratedValues(object);
final String sql = ormClassTool.getOrmCRUDQuery().getSaveQuery();
final GeneratedKeyReader generatedKeyExtractor = new GeneratedKeyReader() {
@Override
public void read(ResultSet generatedKeyResultSet) throws SQLException {
if (generatedKeyResultSet.next()) {
ormClassTool.getOrmPersistor().updateGeneratedValues(generatedKeyResultSet, object);
}
}
@Override
public String[] generatedColumnNames() {
return ormClassTool.getClassMapper().getAllGeneratedColumnDBNames();
}
};
sqlExec.update(sql, generatedKeyExtractor, args);
}
}
@Override
public final void save(final List objects) throws OrmException {
for (final T object : objects) {
this.save(object);
}
}
@Override
public final void update(T object) throws OrmException {
@SuppressWarnings("unchecked")
final
IOrmClassTool ormClassTool = (IOrmClassTool) this.getOrmClassToolMap().getOrmClassTool(object.getClass());
final SqlPerformer sqlExec = this.sqlPerformer();
//CHECK IF OBJECT HAS A 'VERSION' FIELD
if (ormClassTool.getClassMapper().getTableMap().isVersionable()) {
final int rightVersion = sqlExec.queryForInt(ormClassTool.getOrmCRUDQuery().getBeanVersionQuery(), ormClassTool.getOrmPersistor().primaryKeyAndVersionValues(object));
if ( rightVersion==0 ) {
throw new OrmOptimisticLockException("The bean of class [" + object.getClass() + "] cannot be updated. Version in the DB is not the expected one.");
}
ormClassTool.getOrmPersistor().increaseVersion(object, false);
}
//UPDATE OBJECT
final Object[] pkArgs = ormClassTool.getOrmPersistor().primaryKeyValues(object);
final Object[] npkArgs = ormClassTool.getOrmPersistor().notPrimaryKeyValues(object);
final Object[] args = new Object[ npkArgs.length + pkArgs.length ];
int i=0;
for (final Object value : npkArgs) {
args[i++] = value;
}
for (final Object value : pkArgs) {
args[i++] = value;
}
sqlExec.update(ormClassTool.getOrmCRUDQuery().getUpdateQuery(), args);
}
@Override
public final void update(final List objects) throws OrmException {
for (final T object : objects) {
this.update(object);
}
}
@Override
public final UpdateQuery updateQuery(Class> clazz) throws OrmException {
final OrmUpdateQuery update = new OrmUpdateQuery(clazz, this.ormClassToolMap, this);
final OrmClassToolMapNameSolver nameSolver = new OrmClassToolMapNameSolver(this.ormClassToolMap);
nameSolver.alwaysResolveWithoutAlias(true);
nameSolver.register(clazz);
update.setNameSolver(nameSolver);
return update;
}
@Override
public final UpdateQuery updateQuery(Class> clazz, String alias) throws OrmException {
final OrmUpdateQuery update = new OrmUpdateQuery(clazz, this.ormClassToolMap, this);
final OrmClassToolMapNameSolver nameSolver = new OrmClassToolMapNameSolver(this.ormClassToolMap);
nameSolver.alwaysResolveWithoutAlias(true);
nameSolver.register(clazz, alias);
update.setNameSolver(nameSolver);
return update;
}
@Override
public final void delete(T object) throws OrmException {
@SuppressWarnings("unchecked")
final
IOrmClassTool ormClassTool = (IOrmClassTool) this.getOrmClassToolMap().getOrmClassTool(object.getClass());
final SqlPerformer sqlExec = this.sqlPerformer();
sqlExec.update(ormClassTool.getOrmCRUDQuery().getDeleteQuery(), ormClassTool.getOrmPersistor().primaryKeyValues(object));
}
@Override
public final void delete(final List objects) throws OrmException {
if (objects.size()==0) {
return;
}
@SuppressWarnings("unchecked")
final IOrmClassTool ormClassTool = (IOrmClassTool) this.getOrmClassToolMap().getOrmClassTool(objects.get(0).getClass());
final SqlPerformer sqlExec = this.sqlPerformer();
final PreparedStatementCreator psc = new PreparedStatementCreator() {
@Override
public void set(PreparedStatement ps, int i) throws SQLException {
final T object = objects.get(i);
int count = 0;
for (final Object value : ormClassTool.getOrmPersistor().primaryKeyValues(object)) {
ps.setObject(++count, value);
}
}
@Override
public int getBatchSize() {
return objects.size();
}
};
sqlExec.batchUpdate(ormClassTool.getOrmCRUDQuery().getDeleteQuery(), psc );
}
@Override
public final DeleteQuery deleteQuery(Class> clazz) throws OrmException {
final OrmDeleteQuery delete = new OrmDeleteQuery(clazz, this.ormClassToolMap, this);
final OrmClassToolMapNameSolver nameSolver = new OrmClassToolMapNameSolver(this.ormClassToolMap);
nameSolver.register(clazz);
nameSolver.alwaysResolveWithoutAlias(true);
delete.setNameSolver(nameSolver);
return delete;
}
@Override
public final DeleteQuery deleteQuery(Class> clazz, String alias) throws OrmException {
final OrmDeleteQuery delete = new OrmDeleteQuery(clazz, this.ormClassToolMap, this);
final OrmClassToolMapNameSolver nameSolver = new OrmClassToolMapNameSolver(this.ormClassToolMap);
nameSolver.register(clazz, alias);
nameSolver.alwaysResolveWithoutAlias(true);
delete.setNameSolver(nameSolver);
return delete;
}
@Override
public final IScriptExecutor scriptExecutor() throws OrmException {
return new ScriptExecutor(this);
}
@Override
public final SqlExecutor sqlExecutor() {
return new PlainSqlExecutor(this);
}
@Override
public ITransaction transaction(
ITransactionDefinition transactionDefinition) throws OrmException {
return this.sessionStrategy.getTransaction(transactionDefinition);
}
@Override
public SqlPerformer sqlPerformer() throws OrmException {
return new PlainSqlPerformer(this.sessionStrategy.sqlPerformerStrategy());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy