1 package org.hibernate.sql; 3 4 import org.hibernate.util.StringHelper; 5 6 11 public class Delete { 12 13 private String tableName; 14 private String [] primaryKeyColumnNames; 15 private String versionColumnName; 16 private String where; 17 18 private String comment; 19 public Delete setComment(String comment) { 20 this.comment = comment; 21 return this; 22 } 23 24 public Delete setTableName(String tableName) { 25 this.tableName = tableName; 26 return this; 27 } 28 29 public String toStatementString() { 30 StringBuffer buf = new StringBuffer ( tableName.length() + 10 ); 31 if ( comment!=null ) { 32 buf.append( "/* " ).append(comment).append( " */ " ); 33 } 34 buf.append( "delete from " ).append(tableName); 35 if ( where != null || primaryKeyColumnNames != null || versionColumnName != null ) { 36 buf.append( " where " ); 37 } 38 boolean conditionsAppended = false; 39 if ( primaryKeyColumnNames != null ) { 40 buf.append( StringHelper.join( "=? and ", primaryKeyColumnNames ) ).append( "=?" ); 41 conditionsAppended = true; 42 } 43 if ( where!=null ) { 44 if ( conditionsAppended ) { 45 buf.append( " and " ); 46 } 47 buf.append( where ); 48 conditionsAppended = true; 49 } 50 if ( versionColumnName!=null ) { 51 if ( conditionsAppended ) { 52 buf.append( " and " ); 53 } 54 buf.append( versionColumnName ).append( "=?" ); 55 } 56 return buf.toString(); 57 } 58 59 public Delete setWhere(String where) { 60 this.where=where; 61 return this; 62 } 63 64 public Delete setPrimaryKeyColumnNames(String [] primaryKeyColumnNames) { 65 this.primaryKeyColumnNames = primaryKeyColumnNames; 66 return this; 67 } 68 69 public Delete setVersionColumnName(String versionColumnName) { 70 this.versionColumnName = versionColumnName; 71 return this; 72 } 73 74 } 75 | Popular Tags |