All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jpattern.orm.session.Session 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.orm.classtool.IOrmClassTool;
import com.jpattern.orm.classtool.IOrmClassToolMap;
import com.jpattern.orm.exception.OrmException;
import com.jpattern.orm.exception.OrmOptimisticLockException;
import com.jpattern.orm.query.IDelete;
import com.jpattern.orm.query.IOrmCustomQuery;
import com.jpattern.orm.query.IOrmQuery;
import com.jpattern.orm.query.ISqlExecutor;
import com.jpattern.orm.query.IUpdate;
import com.jpattern.orm.query.OrmClassToolMapNameSolver;
import com.jpattern.orm.query.OrmCustomQuery;
import com.jpattern.orm.query.OrmDelete;
import com.jpattern.orm.query.OrmQuery;
import com.jpattern.orm.query.OrmUpdate;
import com.jpattern.orm.query.SqlExecutor;
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 Session implements ISessionSqlPerformer {


	private final IOrmClassToolMap ormClassToolMap;
	private final ISessionStrategy sessionStrategy;

	public Session(IOrmClassToolMap ormClassToolMap, ISessionStrategy sessionStrategy) {
		this.ormClassToolMap = ormClassToolMap;
		this.sessionStrategy = sessionStrategy;
	}

	@Override
	public final ITransaction transaction() throws OrmException {
		return transaction(new TransactionDefinition());
	}

	@Override
	public final  IOrmQuery findQuery(Class clazz, Class... joinClasses) throws OrmException {
		final OrmQuery query = new OrmQuery(ormClassToolMap , this, clazz, joinClasses);
		final OrmClassToolMapNameSolver nameSolver = new OrmClassToolMapNameSolver(ormClassToolMap);
		nameSolver.register(clazz);
		if (joinClasses!=null) {
			for (final Class joinClass : joinClasses) {
				nameSolver.register(joinClass);
			}
		}
		query.setNameSolver(nameSolver);
		return query;
	}

	@Override
	public final  IOrmQuery findQuery(Class clazz, String alias) throws OrmException {
		final OrmQuery query = new OrmQuery(ormClassToolMap , this, clazz);
		final OrmClassToolMapNameSolver nameSolver = new OrmClassToolMapNameSolver(ormClassToolMap);
		nameSolver.register(clazz, alias);
		query.setNameSolver(nameSolver);
		return query;
	}

	@Override
	public final IOrmCustomQuery findQuery(String selectClause, Class clazz, Class... joinClasses ) throws OrmException {
		final OrmCustomQuery query = new OrmCustomQuery(selectClause, ormClassToolMap , this, clazz, joinClasses);
		final OrmClassToolMapNameSolver nameSolver = new OrmClassToolMapNameSolver(ormClassToolMap);
		nameSolver.register(clazz);
		if (joinClasses!=null) {
			for (final Class joinClass : joinClasses) {
				nameSolver.register(joinClass);
			}
		}
		query.setNameSolver(nameSolver);
		return query;
	}

	@Override
	public final IOrmCustomQuery findQuery(String selectClause, Class clazz, String alias ) throws OrmException {
		final OrmCustomQuery query = new OrmCustomQuery(selectClause, ormClassToolMap , this, clazz);
		final OrmClassToolMapNameSolver nameSolver = new OrmClassToolMapNameSolver(ormClassToolMap);
		nameSolver.register(clazz, alias);
		query.setNameSolver(nameSolver);
		return query;
	}

	public final IOrmClassToolMap getOrmClassToolMap() {
		return ormClassToolMap;
	}

	@Override
	public final  T find(Class clazz, Object value) throws OrmException {
		return find(clazz, new Object[]{value});
	}

	@Override
	public final  T find(Class clazz, Object[] values) throws OrmException {
		final IOrmClassTool ormClassTool = getOrmClassToolMap().getOrmClassTool(clazz);
		final IResultSetReader resultSetReader = new IResultSetReader() {

			@Override
			public T read(ResultSet resultSet) throws SQLException {
				if ( resultSet.next() ) {
					return ormClassTool.getOrmPersistor().mapRow("", resultSet, 0) ;
				}
				return null;
			}
		};
		final ISqlPerformer sqlExec = sqlPerformer();
		sqlExec.setMaxRows(1);
		return sqlExec.query(ormClassTool.getOrmCRUDQuery().getLoadQuery(), resultSetReader, values);
	}

	@Override
	public final  void save(final T object) throws OrmException {

		@SuppressWarnings("unchecked")
		final IOrmClassTool ormClassTool = (IOrmClassTool) getOrmClassToolMap().getOrmClassTool(object.getClass());
		final ISqlPerformer sqlExec = sqlPerformer();
		final String sql = ormClassTool.getOrmCRUDQuery().getSaveQuery();

		//CHECK IF OBJECT HAS A 'VERSION' FIELD
		if (ormClassTool.getClassMapper().getTableMap().isVersionable()) {
			ormClassTool.getOrmPersistor().increaseVersion(object, true);
		}

		final Object[] args = ormClassTool.getOrmPersistor().allNotGeneratedValues(object);
		if (!ormClassTool.getOrmCRUDQuery().generatedKey()) {
			sqlExec.update(sql, args);
		} else {
			final IGeneratedKeyReader generatedKeyExtractor = new IGeneratedKeyReader() {

				@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) {
			save(object);
		}
		//		if (objects.size()==0) {
		//			return;
		//		}
		//		@SuppressWarnings("unchecked")
		//		final IOrmClassTool ormClassTool = (IOrmClassTool) getOrmClassToolMap().getOrmClassTool(objects.get(0).getClass());
		//
		//		if (!ormClassTool.getOrmCRUDQuery().generatedKey()) {
		//			final ISqlPerformer sqlExec = sqlPerformer();
		//
		//			final IPreparedStatementCreator psc = new IPreparedStatementCreator() {
		//
		//				@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().allValues(object)) {
		//						ps.setObject(++count, value);
		//					}
		//				}
		//				@Override
		//				public int getBatchSize() {
		//					return objects.size();
		//				}
		//			};
		//			sqlExec.batchUpdate(ormClassTool.getOrmCRUDQuery().getSaveQuery(), psc );
		//		} else {
		//			for (final T object : objects) {
		//				save(object);
		//			}
		//		}
	}

	@Override
	public final  void update(T object) throws OrmException {
		@SuppressWarnings("unchecked")
		final
		IOrmClassTool ormClassTool = (IOrmClassTool) getOrmClassToolMap().getOrmClassTool(object.getClass());
		final ISqlPerformer sqlExec = 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) {
			update(object);
		}
		//		if (objects.size()==0) {
		//			return;
		//		}
		//
		//		@SuppressWarnings("unchecked")
		//		final IOrmClassTool ormClassTool = (IOrmClassTool) getOrmClassToolMap().getOrmClassTool(objects.get(0).getClass());
		//		final ISqlPerformer sqlExec = sqlPerformer();
		//
		//		final IPreparedStatementCreator psc = new IPreparedStatementCreator() {
		//
		//			@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().notPrimaryKeyValues(object)) {
		//					ps.setObject(++count, value);
		//				}
		//				for (final Object value : ormClassTool.getOrmPersistor().primaryKeyValues(object)) {
		//					ps.setObject(++count, value);
		//				}
		//			}
		//			@Override
		//			public int getBatchSize() {
		//				return objects.size();
		//			}
		//		};
		//		sqlExec.batchUpdate(ormClassTool.getOrmCRUDQuery().getUpdateQuery(), psc );
	}

	@Override
	public final IUpdate updateQuery(Class clazz) throws OrmException {
		final OrmUpdate update = new OrmUpdate(clazz, ormClassToolMap, this);
		final OrmClassToolMapNameSolver nameSolver = new OrmClassToolMapNameSolver(ormClassToolMap);
		nameSolver.alwaysResolveWithoutAlias(true);
		nameSolver.register(clazz);
		update.setNameSolver(nameSolver);
		return update;
	}

	@Override
	public final IUpdate updateQuery(Class clazz, String alias) throws OrmException {
		final OrmUpdate update = new OrmUpdate(clazz, ormClassToolMap, this);
		final OrmClassToolMapNameSolver nameSolver = new OrmClassToolMapNameSolver(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) getOrmClassToolMap().getOrmClassTool(object.getClass());
		final ISqlPerformer sqlExec = 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) getOrmClassToolMap().getOrmClassTool(objects.get(0).getClass());
		final ISqlPerformer sqlExec = sqlPerformer();

		final IPreparedStatementCreator psc = new IPreparedStatementCreator() {
			@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 IDelete deleteQuery(Class clazz) throws OrmException {
		final OrmDelete delete = new OrmDelete(clazz, ormClassToolMap, this);
		final OrmClassToolMapNameSolver nameSolver = new OrmClassToolMapNameSolver(ormClassToolMap);
		nameSolver.register(clazz);
		nameSolver.alwaysResolveWithoutAlias(true);
		delete.setNameSolver(nameSolver);
		return delete;
	}

	@Override
	public final IDelete deleteQuery(Class clazz, String alias) throws OrmException {
		final OrmDelete delete = new OrmDelete(clazz, ormClassToolMap, this);
		final OrmClassToolMapNameSolver nameSolver = new OrmClassToolMapNameSolver(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 ISqlExecutor sqlExecutor() {
		return new SqlExecutor(this);
	}

	@Override
	public ITransaction transaction(
			ITransactionDefinition transactionDefinition) throws OrmException {
		return sessionStrategy.getTransaction(transactionDefinition);
	}

	@Override
	public ISqlPerformer sqlPerformer() throws OrmException {
		return new SqlPerformer(sessionStrategy.sqlPerformerStrategy());
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy