ScalarHandler.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.dbutils.handlers;

  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;

  20. import org.apache.commons.dbutils.ResultSetHandler;

  21. /**
  22.  * {@code ResultSetHandler} implementation that converts one
  23.  * {@code ResultSet} column into an Object. This class is thread safe.
  24.  *
  25.  * @param <T> The type of the scalar
  26.  * @see org.apache.commons.dbutils.ResultSetHandler
  27.  */
  28. public class ScalarHandler<T> implements ResultSetHandler<T> {

  29.     /**
  30.      * The column number to retrieve.
  31.      */
  32.     private final int columnIndex;

  33.     /**
  34.      * The column name to retrieve.  Either columnName or columnIndex
  35.      * will be used but never both.
  36.      */
  37.     private final String columnName;

  38.     /**
  39.      * Creates a new instance of ScalarHandler.  The first column will
  40.      * be returned from {@code handle()}.
  41.      */
  42.     public ScalarHandler() {
  43.         this(1, null);
  44.     }

  45.     /**
  46.      * Creates a new instance of ScalarHandler.
  47.      *
  48.      * @param columnIndex The index of the column to retrieve from the
  49.      * {@code ResultSet}.
  50.      */
  51.     public ScalarHandler(final int columnIndex) {
  52.         this(columnIndex, null);
  53.     }

  54.     /** Helper constructor
  55.      * @param columnIndex The index of the column to retrieve from the
  56.      * {@code ResultSet}.
  57.      * @param columnName The name of the column to retrieve from the
  58.      * {@code ResultSet}.
  59.      */
  60.     private ScalarHandler(final int columnIndex, final String columnName) {
  61.         this.columnIndex = columnIndex;
  62.         this.columnName = columnName;
  63.     }

  64.     /**
  65.      * Creates a new instance of ScalarHandler.
  66.      *
  67.      * @param columnName The name of the column to retrieve from the
  68.      * {@code ResultSet}.
  69.      */
  70.     public ScalarHandler(final String columnName) {
  71.         this(1, columnName);
  72.     }

  73.     /**
  74.      * Returns one {@code ResultSet} column as an object via the
  75.      * {@code ResultSet.getObject()} method that performs type
  76.      * conversions.
  77.      * @param resultSet {@code ResultSet} to process.
  78.      * @return The column or {@code null} if there are no rows in
  79.      * the {@code ResultSet}.
  80.      *
  81.      * @throws SQLException if a database access error occurs
  82.      * @throws ClassCastException if the class datatype does not match the column type
  83.      *
  84.      * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
  85.      */
  86.     // We assume that the user has picked the correct type to match the column
  87.     // so getObject will return the appropriate type and the cast will succeed.
  88.     @SuppressWarnings("unchecked")
  89.     @Override
  90.     public T handle(final ResultSet resultSet) throws SQLException {

  91.         if (resultSet.next()) {
  92.             if (this.columnName == null) {
  93.                 return (T) resultSet.getObject(this.columnIndex);
  94.             }
  95.             return (T) resultSet.getObject(this.columnName);
  96.         }
  97.         return null;
  98.     }
  99. }