FileDocCategorySizeDatePackage
Select.javaAPI DocHibernate 3.2.54129Tue Jun 14 21:57:04 BST 2005org.hibernate.sql

Select

public class Select extends Object
A simple SQL SELECT statement
author
Gavin King

Fields Summary
private String
selectClause
private String
fromClause
private String
outerJoinsAfterFrom
private String
whereClause
private String
outerJoinsAfterWhere
private String
orderByClause
private String
groupByClause
private String
comment
private org.hibernate.LockMode
lockMode
public final org.hibernate.dialect.Dialect
dialect
private int
guesstimatedBufferSize
Constructors Summary
public Select(org.hibernate.dialect.Dialect dialect)

	
	   
		this.dialect = dialect;
	
Methods Summary
public org.hibernate.LockModegetLockMode()

		return lockMode;
	
public org.hibernate.sql.SelectsetComment(java.lang.String comment)

		this.comment = comment;
		this.guesstimatedBufferSize += comment.length();
		return this;
	
public org.hibernate.sql.SelectsetFromClause(java.lang.String fromClause)
Sets the fromClause.

param
fromClause The fromClause to set

		this.fromClause = fromClause;
		this.guesstimatedBufferSize += fromClause.length();
		return this;
	
public org.hibernate.sql.SelectsetFromClause(java.lang.String tableName, java.lang.String alias)

		this.fromClause = tableName + ' " + alias;
		this.guesstimatedBufferSize += fromClause.length();
		return this;
	
public org.hibernate.sql.SelectsetGroupByClause(java.lang.String groupByClause)

		this.groupByClause = groupByClause;
		this.guesstimatedBufferSize += groupByClause.length();
		return this;
	
public org.hibernate.sql.SelectsetLockMode(org.hibernate.LockMode lockMode)

		this.lockMode = lockMode;
		return this;
	
public org.hibernate.sql.SelectsetOrderByClause(java.lang.String orderByClause)

		this.orderByClause = orderByClause;
		this.guesstimatedBufferSize += orderByClause.length();
		return this;
	
public org.hibernate.sql.SelectsetOuterJoins(java.lang.String outerJoinsAfterFrom, java.lang.String outerJoinsAfterWhere)

		this.outerJoinsAfterFrom = outerJoinsAfterFrom;

		// strip off any leading 'and' token
		String tmpOuterJoinsAfterWhere = outerJoinsAfterWhere.trim();
		if ( tmpOuterJoinsAfterWhere.startsWith("and") ) {
			tmpOuterJoinsAfterWhere = tmpOuterJoinsAfterWhere.substring(4);
		}
		this.outerJoinsAfterWhere = tmpOuterJoinsAfterWhere;

		this.guesstimatedBufferSize += outerJoinsAfterFrom.length() + outerJoinsAfterWhere.length();
		return this;
	
public org.hibernate.sql.SelectsetSelectClause(java.lang.String selectClause)
Sets the selectClause.

param
selectClause The selectClause to set

		this.selectClause = selectClause;
		this.guesstimatedBufferSize += selectClause.length();
		return this;
	
public org.hibernate.sql.SelectsetWhereClause(java.lang.String whereClause)
Sets the whereClause.

param
whereClause The whereClause to set

		this.whereClause = whereClause;
		this.guesstimatedBufferSize += whereClause.length();
		return this;
	
public java.lang.StringtoStatementString()
Construct an SQL SELECT statement from the given clauses

		StringBuffer buf = new StringBuffer(guesstimatedBufferSize);
		if ( StringHelper.isNotEmpty(comment) ) {
			buf.append("/* ").append(comment).append(" */ ");
		}
		
		buf.append("select ").append(selectClause)
				.append(" from ").append(fromClause);
		
		if ( StringHelper.isNotEmpty(outerJoinsAfterFrom) ) {
			buf.append(outerJoinsAfterFrom);
		}
		
		if ( StringHelper.isNotEmpty(whereClause) || StringHelper.isNotEmpty(outerJoinsAfterWhere) ) {
			buf.append(" where " );
			// the outerJoinsAfterWhere needs to come before where clause to properly
			// handle dynamic filters
			if ( StringHelper.isNotEmpty(outerJoinsAfterWhere) ) {
				buf.append(outerJoinsAfterWhere);
				if ( StringHelper.isNotEmpty(whereClause) ) {
					buf.append( " and " );
				}
			}
			if ( StringHelper.isNotEmpty(whereClause) ) {
				buf.append(whereClause);
			}
		}
		
		if ( StringHelper.isNotEmpty(groupByClause) ) {
			buf.append(" group by ").append(groupByClause);
		}
		
		if ( StringHelper.isNotEmpty(orderByClause) ) {
			buf.append(" order by ").append(orderByClause);
		}
		
		if (lockMode!=null) {
			buf.append( dialect.getForUpdateString(lockMode) );
		}
		
		return dialect.transformSelectString( buf.toString() );