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

com.jpattern.orm.crud.OrmCRUDQueryGenerator Maven / Gradle / Ivy

There is a newer version: 3.5.1
Show newest version
package com.jpattern.orm.crud;

import java.util.ArrayList;
import java.util.List;

import com.jpattern.logger.ILogger;
import com.jpattern.orm.classmapper.IClassMapper;
import com.jpattern.orm.classmapper.ITableMap;
import com.jpattern.orm.dialect.Dialect;
import com.jpattern.orm.logger.OrmLogger;

/**
 * 
 * @author Francesco Cina
 *
 * 22/mag/2011
 */
public class OrmCRUDQueryGenerator implements IOrmCRUDQueryGenerator {

	private final IClassMapper classMapper;
	private boolean generatedKey = false;
	private final ILogger logger = OrmLogger.getOrmLogger(this.getClass());
	private final Dialect dialect;

	public OrmCRUDQueryGenerator(Dialect dialect, IClassMapper classMapper) {
		this.dialect = dialect;
		this.classMapper = classMapper;
	}

	/* (non-Javadoc)
	 * @see com.jpattern.orm.generator.IOrmCRUDQueryGenerator#generate()
	 */
	@Override
	public IOrmCRUDQuery generate() {
		final OrmCRUDQuery crud = new OrmCRUDQuery();
		crud.setDeleteQuery( generateDeleteQuery() );
		crud.setLoadQuery(  generateLoadQuery() );
		crud.setSaveQuery(  generateSaveQuery(false) );
		crud.setSaveQueryWithoutGenerators( generateSaveQuery(true) );
		crud.setUpdateQuery(  generateUpdateQuery() );
		crud.setBaseSelectClause(  generateBaseSelectClause() );
		crud.setBaseFromClause(  generateBaseFromClause() );
		crud.setBeanVersionQuery( generateBeanVersionQuery() );
		return crud;
	}

	private String generateBaseFromClause() {
		return classMapper.getTableMap().getTableNameWithSchema();
	}

	private String generateBaseSelectClause() {
		return columnToCommaSepareted( getColumnNames(classMapper , ICrudConstants.ROW_NAME_PREFIX_PLACEHOLDER, classMapper.getAllColumnJavaNames()), true );
	}

	private String generateUpdateQuery() {
		final StringBuilder builder = new StringBuilder("UPDATE ");
		builder.append(classMapper.getTableMap().getTableNameWithSchema());
		builder.append(" SET ");
		if ( classMapper.getNotPrimaryKeyColumnJavaNames().length>0 ) {
			builder.append( columnToSetClause( getColumnNames(classMapper, "", classMapper.getNotPrimaryKeyColumnJavaNames()) ));
		}
		if ( classMapper.getPrimaryKeyColumnJavaNames().length>0 ) {
			builder.append(" WHERE ");
			builder.append( columnToWhereClause( getColumnNames(classMapper, "", classMapper.getPrimaryKeyColumnJavaNames()) ));
		}
		final String query = builder.toString();
		logger.info("generateUpdateQuery", "Generated UPDATE query for table [" + classMapper.getTableMap().getTableNameWithSchema() + "]:\n" + query );
		return query;
	}

	private String generateSaveQuery(boolean ignoreGenerators) {
		final StringBuilder builder = new StringBuilder("INSERT INTO ");
		builder.append(classMapper.getTableMap().getTableNameWithSchema());
		builder.append(" (");
		builder.append( columnToCommaSepareted( getColumnNames(classMapper , "", classMapper.getAllColumnJavaNames()), ignoreGenerators ));
		builder.append(") VALUES (");
		builder.append( questionCommaSepareted( classMapper, getColumnNames(classMapper , "", classMapper.getAllColumnJavaNames()), ignoreGenerators ));
		builder.append(")");
		final String query = builder.toString();
		logger.info("generateSaveQuery", "Generated INSERT query (with generators? " + !ignoreGenerators + ") for table [" + classMapper.getTableMap().getTableNameWithSchema() + "]:\n" + query );
		return query;
	}

	private String generateLoadQuery() {
		final StringBuilder builder = new StringBuilder("SELECT * FROM ");
		builder.append(classMapper.getTableMap().getTableNameWithSchema());
		if ( classMapper.getPrimaryKeyColumnJavaNames().length>0 ) {
			builder.append(" WHERE ");
			builder.append( columnToWhereClause( getColumnNames(classMapper, "", classMapper.getPrimaryKeyColumnJavaNames()) ));
		}
		final String query = builder.toString();
		logger.info("generateLoadQuery", "Generated SELECT query for table [" + classMapper.getTableMap().getTableNameWithSchema() + "]:\n" + query );
		return query;
	}

	private String generateBeanVersionQuery() {
		String query = "";
		final ITableMap tableMap = classMapper.getTableMap();
		if (tableMap.isVersionable()) {
			final StringBuilder builder = new StringBuilder("SELECT count(*) FROM ");
			builder.append(classMapper.getTableMap().getTableNameWithSchema());
			if ( classMapper.getPrimaryKeyColumnJavaNames().length>0 ) {
				builder.append(" WHERE ");
				final String[] pk = classMapper.getPrimaryKeyColumnJavaNames();
				final String[] pkAndVersion = new String[ pk.length + 1 ];
				for (int i=0 ; i<(pk.length); i++) {
					pkAndVersion[i] = pk[i];
				}
				pkAndVersion[pkAndVersion.length-1] = tableMap.getVersionJavaFieldName() ;

				builder.append( columnToWhereClause( getColumnNames(classMapper, "", pkAndVersion ) ));
			}
			builder.append( tableMap.getVersionLoadLockMode().getLockMode() );
			query = builder.toString();
		}
		logger.info("generateBeanVersionQuery", "Generated query to load the version of a record for table [" + classMapper.getTableMap().getTableNameWithSchema() + "]:\n" + query );
		return query;
	}

	private String generateDeleteQuery() {
		final StringBuilder builder = new StringBuilder("DELETE FROM ");
		builder.append(classMapper.getTableMap().getTableNameWithSchema());
		if ( classMapper.getPrimaryKeyColumnJavaNames().length>0 ) {
			builder.append(" WHERE ");
			builder.append( columnToWhereClause( getColumnNames(classMapper, "", classMapper.getPrimaryKeyColumnJavaNames()) ));
		}
		final String query = builder.toString();
		logger.info("generateDeleteQuery", "Generated DELETE query for table [" + classMapper.getTableMap().getTableNameWithSchema() + "]:\n" + query );
		return query;
	}


	private String questionCommaSepareted(IClassMapper classMapper, List columnNames, boolean ignoreGenerators) {
		final StringBuilder builder = new StringBuilder();
		final int length = columnNames.size();

		if (length > 0) {
			for (int i=0; i<(length-1) ; i++) {
				final AColumnValueGenerator columnValueGenerator = classMapper.getTableMap().getGeneratorByColumnName(columnNames.get(i), ignoreGenerators);
				generatedKey = generatedKey || columnValueGenerator.isAutoGenerated();
				final String queryParameter = columnValueGenerator.insertQueryParameter(dialect, "?");
				if (queryParameter.length()>0) {
					builder.append( queryParameter );
					builder.append(", ");
				}
			}
			final AColumnValueGenerator columnValueGenerator = classMapper.getTableMap().getGeneratorByColumnName(columnNames.get(length-1), ignoreGenerators);
			generatedKey = generatedKey || columnValueGenerator.isAutoGenerated();
			builder.append( columnValueGenerator.insertQueryParameter(dialect, "?") );
		}
		return builder.toString();
	}

	private String columnToCommaSepareted(List columnNames, boolean ignoreGenerators) {
		final StringBuilder builder = new StringBuilder();
		final int length = columnNames.size();

		if (length > 0) {
			for (int i=0; i<(length-1) ; i++) {
				final AColumnValueGenerator columnValueGenerator = classMapper.getTableMap().getGeneratorByColumnName(columnNames.get(i), ignoreGenerators);
				final String queryParameter = columnValueGenerator.insertColumn(dialect, columnNames.get(i));
				if (queryParameter.length()>0) {
					builder.append( queryParameter );
					builder.append(", ");
				}
			}
			final AColumnValueGenerator columnValueGenerator = classMapper.getTableMap().getGeneratorByColumnName(columnNames.get(length-1), ignoreGenerators);
			builder.append( columnValueGenerator.insertColumn(dialect, columnNames.get(length-1)) );
		}

		return builder.toString();
	}

	private String columnToWhereClause(List columnNames) {
		final StringBuilder builder = new StringBuilder();
		final int length = columnNames.size();

		if (length > 0) {
			for (int i=0; i<(length-1) ; i++) {
				builder.append(columnNames.get(i) + " = ? AND ");
			}
			builder.append(columnNames.get(length-1) + " = ? ");
		}
		return builder.toString();
	}

	private String columnToSetClause(List columnNames) {
		final StringBuilder builder = new StringBuilder();
		final int length = columnNames.size();

		if (length > 0) {
			for (int i=0; i<(length-1) ; i++) {
				builder.append(columnNames.get(i) + " = ? , ");
			}
			builder.append(columnNames.get(length-1) + " = ? ");
		}
		return builder.toString();
	}

	private List getColumnNames(IClassMapper classMapper, String rowNamePrefix, String[] javaNames) {
		final List result = new ArrayList();
		for (final String javaName : javaNames) {
			result.add( rowNamePrefix + classMapper.getColumnWithJavaName(javaName).getName());
		}
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy