FileDocCategorySizeDatePackage
Update.javaAPI DocHibernate 3.2.54695Wed Aug 10 15:23:56 BST 2005org.hibernate.sql

Update

public class Update extends Object
An SQL UPDATE statement
author
Gavin King

Fields Summary
private String
tableName
private String[]
primaryKeyColumnNames
private String
versionColumnName
private String
where
private String
assignments
private String
comment
private Map
columns
private Map
whereColumns
private org.hibernate.dialect.Dialect
dialect
Constructors Summary
public Update(org.hibernate.dialect.Dialect dialect)

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

		return addColumn(columnName, "?");
	
public org.hibernate.sql.UpdateaddColumn(java.lang.String columnName, java.lang.String value)

		columns.put(columnName, value);
		return this;
	
public org.hibernate.sql.UpdateaddColumn(java.lang.String columnName, java.lang.Object value, org.hibernate.type.LiteralType type)

		return addColumn( columnName, type.objectToSQLString(value, dialect) );
	
public org.hibernate.sql.UpdateaddColumns(java.lang.String[] columnNames, java.lang.String value)

		for ( int i=0; i<columnNames.length; i++ ) {
			addColumn( columnNames[i], value );
		}
		return this;
	
public org.hibernate.sql.UpdateaddColumns(java.lang.String[] columnNames)

		for ( int i=0; i<columnNames.length; i++ ) {
			addColumn( columnNames[i] );
		}
		return this;
	
public org.hibernate.sql.UpdateaddColumns(java.lang.String[] columnNames, boolean[] updateable)

		for ( int i=0; i<columnNames.length; i++ ) {
			if ( updateable[i] ) addColumn( columnNames[i] );
		}
		return this;
	
public org.hibernate.sql.UpdateaddWhereColumn(java.lang.String columnName)

		return addWhereColumn(columnName, "=?");
	
public org.hibernate.sql.UpdateaddWhereColumn(java.lang.String columnName, java.lang.String value)

		whereColumns.put(columnName, value);
		return this;
	
public org.hibernate.sql.UpdateaddWhereColumns(java.lang.String[] columnNames)

		for ( int i=0; i<columnNames.length; i++ ) {
			addWhereColumn( columnNames[i] );
		}
		return this;
	
public org.hibernate.sql.UpdateaddWhereColumns(java.lang.String[] columnNames, java.lang.String value)

		for ( int i=0; i<columnNames.length; i++ ) {
			addWhereColumn( columnNames[i], value );
		}
		return this;
	
public org.hibernate.sql.UpdateappendAssignmentFragment(java.lang.String fragment)

		if ( assignments == null ) {
			assignments = fragment;
		}
		else {
			assignments += ", " + fragment;
		}
		return this;
	
public java.lang.StringgetTableName()

		return tableName;
	
public org.hibernate.sql.UpdatesetComment(java.lang.String comment)

		this.comment = comment;
		return this;
	
public org.hibernate.sql.UpdatesetPrimaryKeyColumnNames(java.lang.String[] primaryKeyColumnNames)

		this.primaryKeyColumnNames = primaryKeyColumnNames;
		return this;
	
public org.hibernate.sql.UpdatesetTableName(java.lang.String tableName)

		this.tableName = tableName;
		return this;
	
public org.hibernate.sql.UpdatesetVersionColumnName(java.lang.String versionColumnName)

		this.versionColumnName = versionColumnName;
		return this;
	
public org.hibernate.sql.UpdatesetWhere(java.lang.String where)

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

		StringBuffer buf = new StringBuffer( (columns.size() * 15) + tableName.length() + 10 );
		if ( comment!=null ) {
			buf.append( "/* " ).append( comment ).append( " */ " );
		}
		buf.append( "update " ).append( tableName ).append( " set " );
		boolean assignmentsAppended = false;
		Iterator iter = columns.entrySet().iterator();
		while ( iter.hasNext() ) {
			Map.Entry e = (Map.Entry) iter.next();
			buf.append( e.getKey() ).append( '=" ).append( e.getValue() );
			if ( iter.hasNext() ) {
				buf.append( ", " );
			}
			assignmentsAppended = true;
		}
		if ( assignments != null ) {
			if ( assignmentsAppended ) {
				buf.append( ", " );
			}
			buf.append( assignments );
		}

		boolean conditionsAppended = false;
		if ( primaryKeyColumnNames != null || where != null || !whereColumns.isEmpty() || versionColumnName != null ) {
			buf.append( " where " );
		}
		if ( primaryKeyColumnNames != null ) {
			buf.append( StringHelper.join( "=? and ", primaryKeyColumnNames ) ).append( "=?" );
			conditionsAppended = true;
		}
		if ( where != null ) {
			if ( conditionsAppended ) {
				buf.append( " and " );
			}
			buf.append( where );
			conditionsAppended = true;
		}
		iter = whereColumns.entrySet().iterator();
		while ( iter.hasNext() ) {
			final Map.Entry e = (Map.Entry) iter.next();
			if ( conditionsAppended ) {
				buf.append( " and " );
			}
			buf.append( e.getKey() ).append( e.getValue() );
			conditionsAppended = true;
		}
		if ( versionColumnName != null ) {
			if ( conditionsAppended ) {
				buf.append( " and " );
			}
			buf.append( versionColumnName ).append( "=?" );
		}

		return buf.toString();