FileDocCategorySizeDatePackage
SimpleSelect.javaAPI DocHibernate 3.2.54486Sun Jul 24 01:53:06 BST 2005org.hibernate.sql

SimpleSelect

public class SimpleSelect extends Object
An SQL SELECT statement with no table joins
author
Gavin King

Fields Summary
private String
tableName
private String
orderBy
private org.hibernate.dialect.Dialect
dialect
private org.hibernate.LockMode
lockMode
private String
comment
private List
columns
private Map
aliases
private List
whereTokens
Constructors Summary
public SimpleSelect(org.hibernate.dialect.Dialect dialect)

		this.dialect = dialect;
	
Methods Summary
public org.hibernate.sql.SimpleSelectaddColumn(java.lang.String columnName)

		columns.add(columnName);
		//aliases.put( columnName, DEFAULT_ALIAS.toAliasString(columnName) );
		return this;
	
public org.hibernate.sql.SimpleSelectaddColumn(java.lang.String columnName, java.lang.String alias)

		columns.add(columnName);
		aliases.put(columnName, alias);
		return this;
	
public org.hibernate.sql.SimpleSelectaddColumns(java.lang.String[] columnNames, java.lang.String[] columnAliases)


	      
		for ( int i=0; i<columnNames.length; i++ ) {
			if ( columnNames[i]!=null  ) {
				addColumn( columnNames[i], columnAliases[i] );
			}
		}
		return this;
	
public org.hibernate.sql.SimpleSelectaddColumns(java.lang.String[] columns, java.lang.String[] aliases, boolean[] ignore)

		for ( int i=0; i<ignore.length; i++ ) {
			if ( !ignore[i] && columns[i]!=null ) {
				addColumn( columns[i], aliases[i] );
			}
		}
		return this;
	
public org.hibernate.sql.SimpleSelectaddColumns(java.lang.String[] columnNames)

		for ( int i=0; i<columnNames.length; i++ ) {
			if ( columnNames[i]!=null ) addColumn( columnNames[i] );
		}
		return this;
	
public org.hibernate.sql.SimpleSelectaddCondition(java.lang.String lhs, java.lang.String op, java.lang.String rhs)

		and();
		whereTokens.add( lhs + ' " + op + ' " + rhs );
		return this;
	
public org.hibernate.sql.SimpleSelectaddCondition(java.lang.String lhs, java.lang.String condition)

		and();
		whereTokens.add( lhs + ' " + condition );
		return this;
	
public org.hibernate.sql.SimpleSelectaddCondition(java.lang.String[] lhs, java.lang.String op, java.lang.String[] rhs)

		for ( int i=0; i<lhs.length; i++ ) {
			addCondition( lhs[i], op, rhs[i] );
		}
		return this;
	
public org.hibernate.sql.SimpleSelectaddCondition(java.lang.String[] lhs, java.lang.String condition)

		for ( int i=0; i<lhs.length; i++ ) {
			if ( lhs[i]!=null ) addCondition( lhs[i], condition );
		}
		return this;
	
public org.hibernate.sql.SimpleSelectaddWhereToken(java.lang.String token)

		whereTokens.add(token);
		return this;
	
private voidand()

		if ( whereTokens.size()>0 ) {
			whereTokens.add("and");
		}
	
public org.hibernate.sql.SimpleSelectsetComment(java.lang.String comment)

		this.comment = comment;
		return this;
	
public org.hibernate.sql.SimpleSelectsetLockMode(org.hibernate.LockMode lockMode)

		this.lockMode = lockMode;
		return this;
	
public org.hibernate.sql.SimpleSelectsetOrderBy(java.lang.String orderBy)

		this.orderBy = orderBy;
		return this;
	
public org.hibernate.sql.SimpleSelectsetTableName(java.lang.String tableName)

		this.tableName = tableName;
		return this;
	
public java.lang.StringtoStatementString()

		StringBuffer buf = new StringBuffer( 
				columns.size()*10 + 
				tableName.length() + 
				whereTokens.size() * 10 + 
				10 
			);
		
		if ( comment!=null ) {
			buf.append("/* ").append(comment).append(" */ ");
		}
		
		buf.append("select ");
		Set uniqueColumns = new HashSet();
		Iterator iter = columns.iterator();
		boolean appendComma = false;
		while ( iter.hasNext() ) {
			String col = (String) iter.next();
			String alias = (String) aliases.get(col);
			if ( uniqueColumns.add(alias==null ? col : alias) ) {
				if (appendComma) buf.append(", ");
				buf.append(col);
				if ( alias!=null && !alias.equals(col) ) {
					buf.append(" as ")
						.append(alias);
				}
				appendComma = true;
			}
		}
		
		buf.append(" from ")
			.append( dialect.appendLockHint(lockMode, tableName) );
		
		if ( whereTokens.size() > 0 ) {
			buf.append(" where ")
				.append( toWhereClause() );
		}
		
		if (orderBy!=null) buf.append(orderBy);
		
		if (lockMode!=null) {
			buf.append( dialect.getForUpdateString(lockMode) );
		}

		return dialect.transformSelectString( buf.toString() );
	
public java.lang.StringtoWhereClause()

		StringBuffer buf = new StringBuffer( whereTokens.size() * 5 );
		Iterator iter = whereTokens.iterator();
		while ( iter.hasNext() ) {
			buf.append( iter.next() );
			if ( iter.hasNext() ) buf.append(' ");
		}
		return buf.toString();