001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.jdbc.object; 018 019import java.sql.ResultSet; 020import java.sql.SQLException; 021import java.util.Map; 022import javax.sql.DataSource; 023 024import org.springframework.jdbc.core.RowMapper; 025 026/** 027 * Reusable RDBMS query in which concrete subclasses must implement 028 * the abstract updateRow(ResultSet, int, context) method to update each 029 * row of the JDBC ResultSet and optionally map contents into an object. 030 * 031 * <p>Subclasses can be constructed providing SQL, parameter types 032 * and a DataSource. SQL will often vary between subclasses. 033 * 034 * @author Thomas Risberg 035 * @see org.springframework.jdbc.object.SqlQuery 036 */ 037public abstract class UpdatableSqlQuery<T> extends SqlQuery<T> { 038 039 /** 040 * Constructor to allow use as a JavaBean 041 */ 042 public UpdatableSqlQuery() { 043 setUpdatableResults(true); 044 } 045 046 /** 047 * Convenient constructor with DataSource and SQL string. 048 * @param ds DataSource to use to get connections 049 * @param sql SQL to run 050 */ 051 public UpdatableSqlQuery(DataSource ds, String sql) { 052 super(ds, sql); 053 setUpdatableResults(true); 054 } 055 056 057 /** 058 * Implementation of the superclass template method. This invokes the subclass's 059 * implementation of the {@code updateRow()} method. 060 */ 061 @Override 062 protected RowMapper<T> newRowMapper(Object[] parameters, Map<?, ?> context) { 063 return new RowMapperImpl(context); 064 } 065 066 /** 067 * Subclasses must implement this method to update each row of the 068 * ResultSet and optionally create object of the result type. 069 * @param rs ResultSet we're working through 070 * @param rowNum row number (from 0) we're up to 071 * @param context passed to the execute() method. 072 * It can be {@code null} if no contextual information is need. If you 073 * need to pass in data for each row, you can pass in a HashMap with 074 * the primary key of the row being the key for the HashMap. That way 075 * it is easy to locate the updates for each row 076 * @return an object of the result type 077 * @throws SQLException if there's an error updateing data. 078 * Subclasses can simply not catch SQLExceptions, relying on the 079 * framework to clean up. 080 */ 081 protected abstract T updateRow(ResultSet rs, int rowNum, Map<?, ?> context) throws SQLException; 082 083 084 /** 085 * Implementation of RowMapper that calls the enclosing 086 * class's {@code updateRow()} method for each row. 087 */ 088 protected class RowMapperImpl implements RowMapper<T> { 089 090 private final Map<?, ?> context; 091 092 public RowMapperImpl(Map<?, ?> context) { 093 this.context = context; 094 } 095 096 @Override 097 public T mapRow(ResultSet rs, int rowNum) throws SQLException { 098 T result = updateRow(rs, rowNum, this.context); 099 rs.updateRow(); 100 return result; 101 } 102 } 103 104}