1 package org.hibernate.sql; 3 4 import java.util.Iterator ; 5 import java.util.Map ; 6 7 import org.hibernate.type.LiteralType; 8 import org.hibernate.util.StringHelper; 9 10 import org.apache.commons.collections.SequencedHashMap; 11 12 17 public class Update { 18 19 private String tableName; 20 private String [] primaryKeyColumnNames; 21 private String versionColumnName; 22 private String where; 23 private String assignments; 24 private String comment; 25 26 private Map columns = new SequencedHashMap(); 27 private Map whereColumns = new SequencedHashMap(); 28 29 public String getTableName() { 30 return tableName; 31 } 32 33 public Update appendAssignmentFragment(String 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 tableName) { 44 this.tableName = tableName; 45 return this; 46 } 47 48 public Update setPrimaryKeyColumnNames(String [] primaryKeyColumnNames) { 49 this.primaryKeyColumnNames = primaryKeyColumnNames; 50 return this; 51 } 52 53 public Update setVersionColumnName(String versionColumnName) { 54 this.versionColumnName = versionColumnName; 55 return this; 56 } 57 58 59 public Update setComment(String comment) { 60 this.comment = comment; 61 return this; 62 } 63 64 public Update addColumns(String [] 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 [] 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 [] columnNames, String 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 columnName) { 86 return addColumn(columnName, "?"); 87 } 88 89 public Update addColumn(String columnName, String value) { 90 columns.put(columnName, value); 91 return this; 92 } 93 94 public Update addColumn(String columnName, Object value, LiteralType type) throws Exception { 95 return addColumn( columnName, type.objectToSQLString(value) ); 96 } 97 98 public Update addWhereColumns(String [] 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 [] columnNames, String 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 columnName) { 113 return addWhereColumn(columnName, "=?"); 114 } 115 116 public Update addWhereColumn(String columnName, String value) { 117 whereColumns.put(columnName, value); 118 return this; 119 } 120 121 public Update setWhere(String where) { 122 this.where=where; 123 return this; 124 } 125 126 public String toStatementString() { 127 StringBuffer buf = new StringBuffer ( (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 iter = columns.entrySet().iterator(); 134 while ( iter.hasNext() ) { 135 Map.Entry e = (Map.Entry ) 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 e = (Map.Entry ) 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 |