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

org.mentabean.sql.TableAlias Maven / Gradle / Ivy

There is a newer version: 2.2.4
Show newest version
package org.mentabean.sql;

import org.mentabean.BeanConfig;
import org.mentabean.BeanSession;
import org.mentabean.DBField;
import org.mentabean.util.PropertiesProxy;

/**
 * 
 * This class encapsulates a proxy to help construct queries that are fully refactorable.
 * 
 * @author Sergio Oliveira Jr.
 *
 * @param 
 */
public class TableAlias {
	
	private final Class beanClass;
	private final String prefix;
	private final BeanSession session;
	private final BeanConfig config;
	private final E proxy;
	
	public TableAlias(BeanSession session, BeanConfig config, Class beanClass) {
		this(session, config, beanClass, null);
	}
	
	public TableAlias(BeanSession session, BeanConfig config, Class beanClass, String prefix) {
		this.beanClass = beanClass;
		this.prefix = prefix;
		this.session = session;
		this.config = config;
		this.proxy = PropertiesProxy.create(beanClass);
	}
	
	/**
	 * Return the db columns of a select statements.
	 * 
	 * @return the columns to build a select statement
	 */
	public String columns() {
		if (prefix != null) {
			return session.buildSelect(beanClass, prefix);
		} else {
			return session.buildSelect(beanClass);
		}
	}
	
	/**
	 * Return the table name.
	 * 
	 * @return the table name
	 */
	public String tableName() {
		if (prefix != null) {
			return config.getTableName() + " " + prefix;
		} else {
			return config.getTableName();
		}
	}
	
	/**
	 * Return the db column name for this bean property.
	 * 
	 * @param prop this is a filler parameter because a proxy call will be performed!
	 * @return the db column name of this property
	 */
	public String column(Object prop) {
		
		String propName = PropertiesProxy.getPropertyName();
		
		DBField field = config.getField(propName);
		
		if (field == null) throw new IllegalStateException("Cannot field field for property: " + propName);
		
		if (prefix != null) {
			return prefix + "." + field.getDbName();
		} else {
			return field.getDbName();
		}
	}
	
	public E pxy() {
		return proxy;
	}
	
	public String prefix() {
		return prefix;
	}
	
	public Class beanClass() {
		return beanClass;
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy