KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: Update.java,v 1.8 2005/06/14 17:26:19 steveebersole Exp $
2
package org.hibernate.sql;
3
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import org.hibernate.type.LiteralType;
8 import org.hibernate.util.StringHelper;
9
10 import org.apache.commons.collections.SequencedHashMap;
11
12 /**
13  * An SQL <tt>UPDATE</tt> statement
14  *
15  * @author Gavin King
16  */

17 public class Update {
18
19     private String JavaDoc tableName;
20     private String JavaDoc[] primaryKeyColumnNames;
21     private String JavaDoc versionColumnName;
22     private String JavaDoc where;
23     private String JavaDoc assignments;
24     private String JavaDoc comment;
25
26     private Map JavaDoc columns = new SequencedHashMap();
27     private Map JavaDoc whereColumns = new SequencedHashMap();
28
29     public String JavaDoc getTableName() {
30         return tableName;
31     }
32
33     public Update appendAssignmentFragment(String JavaDoc fragment) {
34         if ( assignments == null ) {
35             assignments = fragment;
36         }
37         else {
38             assignments += ", " + fragment;
39         }
40         return this;
41     }
42
43     public Update setTableName(String JavaDoc tableName) {
44         this.tableName = tableName;
45         return this;
46     }
47
48     public Update setPrimaryKeyColumnNames(String JavaDoc[] primaryKeyColumnNames) {
49         this.primaryKeyColumnNames = primaryKeyColumnNames;
50         return this;
51     }
52
53     public Update setVersionColumnName(String JavaDoc versionColumnName) {
54         this.versionColumnName = versionColumnName;
55         return this;
56     }
57
58
59     public Update setComment(String JavaDoc comment) {
60         this.comment = comment;
61         return this;
62     }
63
64     public Update addColumns(String JavaDoc[] columnNames) {
65         for ( int i=0; i<columnNames.length; i++ ) {
66             addColumn( columnNames[i] );
67         }
68         return this;
69     }
70
71     public Update addColumns(String JavaDoc[] columnNames, boolean[] updateable) {
72         for ( int i=0; i<columnNames.length; i++ ) {
73             if ( updateable[i] ) addColumn( columnNames[i] );
74         }
75         return this;
76     }
77
78     public Update addColumns(String JavaDoc[] columnNames, String JavaDoc value) {
79         for ( int i=0; i<columnNames.length; i++ ) {
80             addColumn( columnNames[i], value );
81         }
82         return this;
83     }
84
85     public Update addColumn(String JavaDoc columnName) {
86         return addColumn(columnName, "?");
87     }
88
89     public Update addColumn(String JavaDoc columnName, String JavaDoc value) {
90         columns.put(columnName, value);
91         return this;
92     }
93
94     public Update addColumn(String JavaDoc columnName, Object JavaDoc value, LiteralType type) throws Exception JavaDoc {
95         return addColumn( columnName, type.objectToSQLString(value) );
96     }
97
98     public Update addWhereColumns(String JavaDoc[] columnNames) {
99         for ( int i=0; i<columnNames.length; i++ ) {
100             addWhereColumn( columnNames[i] );
101         }
102         return this;
103     }
104
105     public Update addWhereColumns(String JavaDoc[] columnNames, String JavaDoc value) {
106         for ( int i=0; i<columnNames.length; i++ ) {
107             addWhereColumn( columnNames[i], value );
108         }
109         return this;
110     }
111
112     public Update addWhereColumn(String JavaDoc columnName) {
113         return addWhereColumn(columnName, "=?");
114     }
115
116     public Update addWhereColumn(String JavaDoc columnName, String JavaDoc value) {
117         whereColumns.put(columnName, value);
118         return this;
119     }
120
121     public Update setWhere(String JavaDoc where) {
122         this.where=where;
123         return this;
124     }
125
126     public String JavaDoc toStatementString() {
127         StringBuffer JavaDoc buf = new StringBuffer JavaDoc( (columns.size() * 15) + tableName.length() + 10 );
128         if ( comment!=null ) {
129             buf.append( "/* " ).append( comment ).append( " */ " );
130         }
131         buf.append( "update " ).append( tableName ).append( " set " );
132         boolean assignmentsAppended = false;
133         Iterator JavaDoc iter = columns.entrySet().iterator();
134         while ( iter.hasNext() ) {
135             Map.Entry JavaDoc e = (Map.Entry JavaDoc) iter.next();
136             buf.append( e.getKey() ).append( '=' ).append( e.getValue() );
137             if ( iter.hasNext() ) {
138                 buf.append( ", " );
139             }
140             assignmentsAppended = true;
141         }
142         if ( assignments != null ) {
143             if ( assignmentsAppended ) {
144                 buf.append( ", " );
145             }
146             buf.append( assignments );
147         }
148
149         boolean conditionsAppended = false;
150         if ( primaryKeyColumnNames != null || where != null || !whereColumns.isEmpty() || versionColumnName != null ) {
151             buf.append( " where " );
152         }
153         if ( primaryKeyColumnNames != null ) {
154             buf.append( StringHelper.join( "=? and ", primaryKeyColumnNames ) ).append( "=?" );
155             conditionsAppended = true;
156         }
157         if ( where != null ) {
158             if ( conditionsAppended ) {
159                 buf.append( " and " );
160             }
161             buf.append( where );
162             conditionsAppended = true;
163         }
164         iter = whereColumns.entrySet().iterator();
165         while ( iter.hasNext() ) {
166             final Map.Entry JavaDoc e = (Map.Entry JavaDoc) iter.next();
167             if ( conditionsAppended ) {
168                 buf.append( " and " );
169             }
170             buf.append( e.getKey() ).append( e.getValue() );
171             conditionsAppended = true;
172         }
173         if ( versionColumnName != null ) {
174             if ( conditionsAppended ) {
175                 buf.append( " and " );
176             }
177             buf.append( versionColumnName ).append( "=?" );
178         }
179
180         return buf.toString();
181     }
182 }
183
Popular Tags