View Javadoc
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  
19  import java.sql.ResultSet;
20  import java.sql.SQLException;
21  
22  import org.apache.commons.dbutils.ResultSetHandler;
23  
24  /**
25   * {@code ResultSetHandler} implementation that converts one
26   * {@code ResultSet} column into an Object. This class is thread safe.
27   *
28   * @param <T> The type of the scalar
29   * @see org.apache.commons.dbutils.ResultSetHandler
30   */
31  public class ScalarHandler<T> implements ResultSetHandler<T> {
32  
33      /**
34       * The column number to retrieve.
35       */
36      private final int columnIndex;
37  
38      /**
39       * The column name to retrieve.  Either columnName or columnIndex
40       * will be used but never both.
41       */
42      private final String columnName;
43  
44      /**
45       * Creates a new instance of ScalarHandler.  The first column will
46       * be returned from {@code handle()}.
47       */
48      public ScalarHandler() {
49          this(1, null);
50      }
51  
52      /**
53       * Creates a new instance of ScalarHandler.
54       *
55       * @param columnIndex The index of the column to retrieve from the
56       * {@code ResultSet}.
57       */
58      public ScalarHandler(final int columnIndex) {
59          this(columnIndex, null);
60      }
61  
62      /** Helper constructor
63       * @param columnIndex The index of the column to retrieve from the
64       * {@code ResultSet}.
65       * @param columnName The name of the column to retrieve from the
66       * {@code ResultSet}.
67       */
68      private ScalarHandler(final int columnIndex, final String columnName) {
69          this.columnIndex = columnIndex;
70          this.columnName = columnName;
71      }
72  
73      /**
74       * Creates a new instance of ScalarHandler.
75       *
76       * @param columnName The name of the column to retrieve from the
77       * {@code ResultSet}.
78       */
79      public ScalarHandler(final String columnName) {
80          this(1, columnName);
81      }
82  
83      /**
84       * Returns one {@code ResultSet} column as an object via the
85       * {@code ResultSet.getObject()} method that performs type
86       * conversions.
87       * @param resultSet {@code ResultSet} to process.
88       * @return The column or {@code null} if there are no rows in
89       * the {@code ResultSet}.
90       *
91       * @throws SQLException if a database access error occurs
92       * @throws ClassCastException if the class datatype does not match the column type
93       *
94       * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
95       */
96      // We assume that the user has picked the correct type to match the column
97      // so getObject will return the appropriate type and the cast will succeed.
98      @SuppressWarnings("unchecked")
99      @Override
100     public T handle(final ResultSet resultSet) throws SQLException {
101 
102         if (resultSet.next()) {
103             if (this.columnName == null) {
104                 return (T) resultSet.getObject(this.columnIndex);
105             }
106             return (T) resultSet.getObject(this.columnName);
107         }
108         return null;
109     }
110 }