1 16 17 package org.springframework.jdbc.object; 18 19 import java.util.Map ; 20 21 import javax.sql.DataSource ; 22 23 import org.springframework.dao.DataAccessException; 24 import org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException; 25 import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; 26 import org.springframework.jdbc.core.namedparam.NamedParameterUtils; 27 import org.springframework.jdbc.support.KeyHolder; 28 29 45 public class SqlUpdate extends SqlOperation { 46 47 51 private int maxRowsAffected = 0; 52 53 57 private int requiredRowsAffected = 0; 58 59 60 66 public SqlUpdate() { 67 } 68 69 74 public SqlUpdate(DataSource ds, String sql) { 75 setDataSource(ds); 76 setSql(sql); 77 } 78 79 88 public SqlUpdate(DataSource ds, String sql, int[] types) { 89 setDataSource(ds); 90 setSql(sql); 91 setTypes(types); 92 } 93 94 106 public SqlUpdate(DataSource ds, String sql, int[] types, int maxRowsAffected) { 107 setDataSource(ds); 108 setSql(sql); 109 setTypes(types); 110 this.maxRowsAffected = maxRowsAffected; 111 } 112 113 114 120 public void setMaxRowsAffected(int maxRowsAffected) { 121 this.maxRowsAffected = maxRowsAffected; 122 } 123 124 132 public void setRequiredRowsAffected(int requiredRowsAffected) { 133 this.requiredRowsAffected = requiredRowsAffected; 134 } 135 136 145 protected void checkRowsAffected(int rowsAffected) throws JdbcUpdateAffectedIncorrectNumberOfRowsException { 146 if (this.maxRowsAffected > 0 && rowsAffected > this.maxRowsAffected) { 147 throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(getSql(), this.maxRowsAffected, rowsAffected); 148 } 149 if (this.requiredRowsAffected > 0 && rowsAffected != this.requiredRowsAffected) { 150 throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(getSql(), this.requiredRowsAffected, rowsAffected); 151 } 152 } 153 154 155 161 public int update(Object [] params) throws DataAccessException { 162 validateParameters(params); 163 int rowsAffected = getJdbcTemplate().update(newPreparedStatementCreator(params)); 164 checkRowsAffected(rowsAffected); 165 return rowsAffected; 166 } 167 168 175 public int update(Object [] params, KeyHolder generatedKeyHolder) throws DataAccessException { 176 validateParameters(params); 177 int rowsAffected = getJdbcTemplate().update(newPreparedStatementCreator(params), generatedKeyHolder); 178 checkRowsAffected(rowsAffected); 179 return rowsAffected; 180 } 181 182 185 public int update() throws DataAccessException { 186 return update((Object []) null); 187 } 188 189 192 public int update(int p1) throws DataAccessException { 193 return update(new Object [] {new Integer (p1)}); 194 } 195 196 199 public int update(int p1, int p2) throws DataAccessException { 200 return update(new Object [] {new Integer (p1), new Integer (p2)}); 201 } 202 203 206 public int update(long p1) throws DataAccessException { 207 return update(new Object [] {new Long (p1)}); 208 } 209 210 213 public int update(long p1, long p2) throws DataAccessException { 214 return update(new Object [] {new Long (p1), new Long (p2)}); 215 } 216 217 220 public int update(String p) throws DataAccessException { 221 return update(new Object [] {p}); 222 } 223 224 227 public int update(String p1, String p2) throws DataAccessException { 228 return update(new Object [] {p1, p2}); 229 } 230 231 238 public int updateByNamedParam(Map paramMap) throws DataAccessException { 239 validateNamedParameters(paramMap); 240 Object [] params = NamedParameterUtils.buildValueArray(getSql(), paramMap); 241 String sqlToUse = NamedParameterUtils.substituteNamedParameters(getSql(), new MapSqlParameterSource(paramMap)); 242 int rowsAffected = getJdbcTemplate().update(newPreparedStatementCreator(sqlToUse, params)); 243 checkRowsAffected(rowsAffected); 244 return rowsAffected; 245 } 246 247 255 public int updateByNamedParam(Map paramMap, KeyHolder generatedKeyHolder) throws DataAccessException { 256 validateNamedParameters(paramMap); 257 Object [] params = NamedParameterUtils.buildValueArray(getSql(), paramMap); 258 int rowsAffected = getJdbcTemplate().update(newPreparedStatementCreator(params), generatedKeyHolder); 259 checkRowsAffected(rowsAffected); 260 return rowsAffected; 261 } 262 263 } 264 | Popular Tags |