001/* 002 * Copyright 2002-2018 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; 022 023import javax.sql.DataSource; 024 025import org.springframework.jdbc.core.RowMapper; 026import org.springframework.lang.Nullable; 027 028/** 029 * Reusable RDBMS query in which concrete subclasses must implement 030 * the abstract updateRow(ResultSet, int, context) method to update each 031 * row of the JDBC ResultSet and optionally map contents into an object. 032 * 033 * <p>Subclasses can be constructed providing SQL, parameter types 034 * and a DataSource. SQL will often vary between subclasses. 035 * 036 * @author Thomas Risberg 037 * @param <T> the result type 038 * @see org.springframework.jdbc.object.SqlQuery 039 */ 040public abstract class UpdatableSqlQuery<T> extends SqlQuery<T> { 041 042 /** 043 * Constructor to allow use as a JavaBean. 044 */ 045 public UpdatableSqlQuery() { 046 setUpdatableResults(true); 047 } 048 049 /** 050 * Convenient constructor with DataSource and SQL string. 051 * @param ds the DataSource to use to get connections 052 * @param sql the SQL to run 053 */ 054 public UpdatableSqlQuery(DataSource ds, String sql) { 055 super(ds, sql); 056 setUpdatableResults(true); 057 } 058 059 060 /** 061 * Implementation of the superclass template method. This invokes the subclass's 062 * implementation of the {@code updateRow()} method. 063 */ 064 @Override 065 protected RowMapper<T> newRowMapper(@Nullable Object[] parameters, @Nullable Map<?, ?> context) { 066 return new RowMapperImpl(context); 067 } 068 069 /** 070 * Subclasses must implement this method to update each row of the 071 * ResultSet and optionally create object of the result type. 072 * @param rs the ResultSet we're working through 073 * @param rowNum row number (from 0) we're up to 074 * @param context passed to the execute() method. 075 * It can be {@code null} if no contextual information is need. If you 076 * need to pass in data for each row, you can pass in a HashMap with 077 * the primary key of the row being the key for the HashMap. That way 078 * it is easy to locate the updates for each row 079 * @return an object of the result type 080 * @throws SQLException if there's an error updateing data. 081 * Subclasses can simply not catch SQLExceptions, relying on the 082 * framework to clean up. 083 */ 084 protected abstract T updateRow(ResultSet rs, int rowNum, @Nullable Map<?, ?> context) throws SQLException; 085 086 087 /** 088 * Implementation of RowMapper that calls the enclosing 089 * class's {@code updateRow()} method for each row. 090 */ 091 protected class RowMapperImpl implements RowMapper<T> { 092 093 @Nullable 094 private final Map<?, ?> context; 095 096 public RowMapperImpl(@Nullable Map<?, ?> context) { 097 this.context = context; 098 } 099 100 @Override 101 public T mapRow(ResultSet rs, int rowNum) throws SQLException { 102 T result = updateRow(rs, rowNum, this.context); 103 rs.updateRow(); 104 return result; 105 } 106 } 107 108}