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