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.IDialect;
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 IDialect dialect;

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

	/* (non-Javadoc)
	 * @see com.jpattern.orm.generator.IOrmCRUDQueryGenerator#generate()
	 */
	@Override
	public IOrmCRUDQuery generate() {
		final String deleteQuery = generateDeleteQuery();
		final String loadQuery = generateLoadQuery();
		final String saveQuery = generateSaveQuery();
		final String updateQuery = generateUpdateQuery();
		final String baseSelectClause = generateBaseSelectClause();
		final String baseFromClause = generateBaseFromClause();
		final String beanVersionQuery = generateBeanVersionQuery();
		return new OrmCRUDQuery(deleteQuery, loadQuery, saveQuery, updateQuery, baseSelectClause, baseFromClause, generatedKey, beanVersionQuery);
	}

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

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

	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() {
		final StringBuilder builder = new StringBuilder("INSERT INTO ");
		builder.append(classMapper.getTableMap().getTableNameWithSchema());
		builder.append(" (");
		builder.append( columnToCommaSepareted( getColumnNames(classMapper , "", classMapper.getAllColumnJavaNames()) ));
		builder.append(") VALUES (");
		builder.append( questionCommaSepareted( classMapper, getColumnNames(classMapper , "", classMapper.getAllColumnJavaNames()) ));
		builder.append(")");
		final String query = builder.toString();
		logger.info("generateSaveQuery", "Generated INSERT query 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) {
		final StringBuilder builder = new StringBuilder();
		final int length = columnNames.size();

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

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

		if (length > 0) {
			for (int i=0; i0) {
					builder.append( queryParameter );
					builder.append(", ");
				}
			}
			final AColumnValueGenerator columnValueGenerator = classMapper.getTableMap().getGeneratorByColumnName(columnNames.get(length-1));
			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 columnNames) {
		final StringBuilder builder = new StringBuilder();
		final int length = columnNames.size();

		if (length > 0) {
			for (int i=0; i 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 - 2025 Weber Informatics LLC | Privacy Policy