KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > sql > Delete


1 //$Id: Delete.java,v 1.3 2005/06/07 20:06:10 steveebersole Exp $
2
package org.hibernate.sql;
3
4 import org.hibernate.util.StringHelper;
5
6 /**
7  * An SQL <tt>DELETE</tt> statement
8  *
9  * @author Gavin King
10  */

11 public class Delete {
12
13     private String JavaDoc tableName;
14     private String JavaDoc[] primaryKeyColumnNames;
15     private String JavaDoc versionColumnName;
16     private String JavaDoc where;
17
18     private String JavaDoc comment;
19     public Delete setComment(String JavaDoc comment) {
20         this.comment = comment;
21         return this;
22     }
23
24     public Delete setTableName(String JavaDoc tableName) {
25         this.tableName = tableName;
26         return this;
27     }
28
29     public String JavaDoc toStatementString() {
30         StringBuffer JavaDoc buf = new StringBuffer JavaDoc( 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 JavaDoc where) {
60         this.where=where;
61         return this;
62     }
63
64     public Delete setPrimaryKeyColumnNames(String JavaDoc[] primaryKeyColumnNames) {
65         this.primaryKeyColumnNames = primaryKeyColumnNames;
66         return this;
67     }
68
69     public Delete setVersionColumnName(String JavaDoc versionColumnName) {
70         this.versionColumnName = versionColumnName;
71         return this;
72     }
73
74 }
75
Popular Tags