FileDocCategorySizeDatePackage
QuerySelect.javaAPI DocHibernate 3.2.55459Thu Jun 02 21:04:14 BST 2005org.hibernate.sql

QuerySelect

public class QuerySelect extends Object
A translated HQL query
author
Gavin King

Fields Summary
private org.hibernate.dialect.Dialect
dialect
private JoinFragment
joins
private StringBuffer
select
private StringBuffer
where
private StringBuffer
groupBy
private StringBuffer
orderBy
private StringBuffer
having
private String
comment
private boolean
distinct
private static final HashSet
DONT_SPACE_TOKENS
Constructors Summary
public QuerySelect(org.hibernate.dialect.Dialect dialect)

	 
		//dontSpace.add("'");
		DONT_SPACE_TOKENS.add(".");
		DONT_SPACE_TOKENS.add("+");
		DONT_SPACE_TOKENS.add("-");
		DONT_SPACE_TOKENS.add("/");
		DONT_SPACE_TOKENS.add("*");
		DONT_SPACE_TOKENS.add("<");
		DONT_SPACE_TOKENS.add(">");
		DONT_SPACE_TOKENS.add("=");
		DONT_SPACE_TOKENS.add("#");
		DONT_SPACE_TOKENS.add("~");
		DONT_SPACE_TOKENS.add("|");
		DONT_SPACE_TOKENS.add("&");
		DONT_SPACE_TOKENS.add("<=");
		DONT_SPACE_TOKENS.add(">=");
		DONT_SPACE_TOKENS.add("=>");
		DONT_SPACE_TOKENS.add("=<");
		DONT_SPACE_TOKENS.add("!=");
		DONT_SPACE_TOKENS.add("<>");
		DONT_SPACE_TOKENS.add("!#");
		DONT_SPACE_TOKENS.add("!~");
		DONT_SPACE_TOKENS.add("!<");
		DONT_SPACE_TOKENS.add("!>");
		DONT_SPACE_TOKENS.add("("); //for MySQL
		DONT_SPACE_TOKENS.add(")");
	
		this.dialect = dialect;
		joins = new QueryJoinFragment(dialect, false);
	
Methods Summary
public voidaddOrderBy(java.lang.String orderByString)

		if ( orderBy.length() > 0 ) orderBy.append(", ");
		orderBy.append(orderByString);
	
public voidaddSelectColumn(java.lang.String columnName, java.lang.String alias)

		addSelectFragmentString(columnName + ' " + alias);
	
public voidaddSelectFragmentString(java.lang.String fragment)

		if ( fragment.length()>0 && fragment.charAt(0)=='," ) fragment = fragment.substring(1);
		fragment = fragment.trim();
		if ( fragment.length()>0 ) {
			if ( select.length()>0 ) select.append(", ");
			select.append(fragment);
		}
	
private static voidappendTokens(java.lang.StringBuffer buf, java.util.Iterator iter)

		boolean lastSpaceable=true;
		boolean lastQuoted=false;
		while ( iter.hasNext() ) {
			String token = (String) iter.next();
			boolean spaceable = !DONT_SPACE_TOKENS.contains(token);
			boolean quoted = token.startsWith("'");
			if (spaceable && lastSpaceable) {
				if ( !quoted || !lastQuoted ) buf.append(' ");
			}
			lastSpaceable = spaceable;
			buf.append(token);
			lastQuoted = token.endsWith("'");
		}
	
public org.hibernate.sql.QuerySelectcopy()

		QuerySelect copy = new QuerySelect(dialect);
		copy.joins = this.joins.copy();
		copy.select.append( this.select.toString() );
		copy.where.append( this.where.toString() );
		copy.groupBy.append( this.groupBy.toString() );
		copy.orderBy.append( this.orderBy.toString() );
		copy.having.append( this.having.toString() );
		copy.comment = this.comment;
		copy.distinct = this.distinct;
		return copy;
	
public JoinFragmentgetJoinFragment()

		return joins;
	
public voidprependWhereConditions(java.lang.String conditions)

		if (where.length() > 0) {
			where.insert(0, conditions + " and ");
		}
		else {
			where.append(conditions);
		}
	
public voidsetComment(java.lang.String comment)

		this.comment = comment;
	
public voidsetDistinct(boolean distinct)

		this.distinct = distinct;
	
public voidsetGroupByTokens(java.util.Iterator tokens)

		//if ( groupBy.length()>0 ) groupBy.append(" and ");
		appendTokens(groupBy, tokens);
	
public voidsetHavingTokens(java.util.Iterator tokens)

		//if ( having.length()>0 ) having.append(" and ");
		appendTokens(having, tokens);
	
public voidsetOrderByTokens(java.util.Iterator tokens)

		//if ( orderBy.length()>0 ) orderBy.append(" and ");
		appendTokens(orderBy, tokens);
	
public voidsetWhereTokens(java.util.Iterator tokens)

		//if ( conjunctiveWhere.length()>0 ) conjunctiveWhere.append(" and ");
		appendTokens(where, tokens);
	
public java.lang.StringtoQueryString()

		StringBuffer buf = new StringBuffer(50);
		if (comment!=null) buf.append("/* ").append(comment).append(" */ ");
		buf.append("select ");
		if (distinct) buf.append("distinct ");
		String from = joins.toFromFragmentString();
		if ( from.startsWith(",") ) {
			from = from.substring(1);
		}
		else if ( from.startsWith(" inner join") ){
			from = from.substring(11);
		}

		buf.append( select.toString() )
			.append(" from")
			.append(from);

		String outerJoinsAfterWhere = joins.toWhereFragmentString().trim();
		String whereConditions = where.toString().trim();
		boolean hasOuterJoinsAfterWhere = outerJoinsAfterWhere.length() > 0;
		boolean hasWhereConditions = whereConditions.length() > 0;
		if (hasOuterJoinsAfterWhere || hasWhereConditions) {
			buf.append(" where ");
			if (hasOuterJoinsAfterWhere) {
				buf.append( outerJoinsAfterWhere.substring(4) );
			}
			if (hasWhereConditions) {
				if (hasOuterJoinsAfterWhere) {
					buf.append(" and (");
				}
				buf.append(whereConditions);
				if (hasOuterJoinsAfterWhere) {
					buf.append(")");
				}
			}
		}

		if ( groupBy.length() > 0 ) buf.append(" group by ").append( groupBy.toString() );
		if ( having.length() > 0 ) buf.append(" having ").append( having.toString() );
		if ( orderBy.length() > 0 ) buf.append(" order by ").append( orderBy.toString() );

		return dialect.transformSelectString( buf.toString() );