FileDocCategorySizeDatePackage
InFragment.javaAPI DocHibernate 3.2.52278Sun Jun 05 01:36:26 BST 2005org.hibernate.sql

InFragment

public class InFragment extends Object
An SQL IN expression.
... in(...)
author
Gavin King

Fields Summary
public static final String
NULL
public static final String
NOT_NULL
private String
columnName
private List
values
Constructors Summary
Methods Summary
public org.hibernate.sql.InFragmentaddValue(java.lang.Object value)

param
value, an SQL literal, NULL, or NOT_NULL


	        	 
	    
		values.add(value);
		return this;
	
public org.hibernate.sql.InFragmentsetColumn(java.lang.String columnName)

		this.columnName = columnName;
		return this;
	
public org.hibernate.sql.InFragmentsetColumn(java.lang.String alias, java.lang.String columnName)

		this.columnName = StringHelper.qualify(alias, columnName);
		return setColumn(this.columnName);
	
public org.hibernate.sql.InFragmentsetFormula(java.lang.String alias, java.lang.String formulaTemplate)

		this.columnName = StringHelper.replace(formulaTemplate, Template.TEMPLATE, alias);
		return setColumn(this.columnName);
	
public java.lang.StringtoFragmentString()

		if ( values.size()==0 ) return "1=2";
		StringBuffer buf = new StringBuffer( values.size() * 5 );
		buf.append(columnName);
		//following doesn't handle (null, not null) but unnecessary
		//since this would mean all rows
		if ( values.size()>1 ) {
			boolean allowNull = false;
			buf.append(" in (");
			Iterator iter = values.iterator();
			while ( iter.hasNext() ) {
				Object value = iter.next();
				if ( NULL.equals(value) ) {
					allowNull = true;
				}
				else if ( NOT_NULL.equals(value) ) {
					throw new IllegalArgumentException("not null makes no sense for in expression");
				}
				else {
					buf.append(value);
					buf.append(", ");
				}
			}
			buf.setLength( buf.length()-2 );
			buf.append(')");
			if (allowNull) {
				buf.insert(0, " is null or ")
					.insert(0, columnName)
					.insert(0, '(")
					.append(')");
			}
		}
		else {
			Object value = values.iterator().next();
			if ( NULL.equals(value) ) {
				buf.append(" is null");
			}
			else if ( NOT_NULL.equals(value) ) {
				buf.append(" is not null");
			}
			else {
				buf.append("=").append(value);
			}
		}
		return buf.toString();