All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arakelian.jdbc.handler.ScalarHandler Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arakelian.jdbc.handler;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Timestamp;

import com.google.common.base.Preconditions;

/**
 * ResultSetHandler implementation that converts one ResultSet column into
 * an Object.
 * 
 * @param 
 *            scalar type
 */
public class ScalarHandler implements ResultSetHandler {
    private static final int TYPE_UNKNOWN = 0;
    private static final int TYPE_OBJECT = 1;
    private static final int TYPE_STRING = 2;
    private static final int TYPE_INTEGER = 3;
    private static final int TYPE_BOOLEAN = 4;
    private static final int TYPE_LONG = 5;
    private static final int TYPE_DOUBLE = 6;
    private static final int TYPE_FLOAT = 7;
    private static final int TYPE_SHORT = 8;
    private static final int TYPE_BYTE = 9;
    private static final int TYPE_TIMESTAMP = 10;

    /**
     * True if handle returned last record
     */
    private boolean eof;

    /**
     * The column number to retrieve.
     */
    private int columnIndex;

    /**
     * The column name to retrieve. Either columnName or columnIndex will be used but never both.
     */
    private String columnName;

    /**
     * Request type
     */
    private final Class type;

    /**
     * Conversion type
     */
    private final int conversionType;

    /**
     * Creates a new instance of ScalarHandler. The first column will be returned from
     * handle().
     *
     * @param type
     *            scalar type
     */
    public ScalarHandler(final Class type) {
        this(1, type);
    }

    /**
     * Creates a new instance of ScalarHandler.
     *
     * @param columnIndex
     *            The index of the column to retrieve from the ResultSet (1-based).
     * @param type
     *            scalar type
     */
    public ScalarHandler(final int columnIndex, final Class type) {
        this.columnIndex = columnIndex;
        this.type = Preconditions.checkNotNull(type);
        this.conversionType = getConversionType();
        if (conversionType == TYPE_UNKNOWN) {
            throw new IllegalStateException();
        }
    }

    /**
     * Creates a new instance of ScalarHandler.
     *
     * @param columnName
     *            The name of the column to retrieve from the ResultSet.
     * @param type
     *            scalar type
     */
    public ScalarHandler(final String columnName, final Class type) {
        this.columnName = columnName;
        this.type = Preconditions.checkNotNull(type);
        this.conversionType = getConversionType();
        if (conversionType == TYPE_UNKNOWN) {
            throw new IllegalStateException();
        }
    }

    public final int getColumnIndex() {
        return columnIndex;
    }

    public final String getColumnName() {
        return columnName;
    }

    public final Class getType() {
        return type;
    }

    /**
     * Returns one ResultSet column as an object via the
     * ResultSet.getObject() method that performs type conversions.
     *
     * @param rs
     *            ResultSet to process.
     * @return The column or null if there are no rows in the ResultSet.
     *
     * @throws SQLException
     *             if a database access error occurs
     */
    @Override
    @SuppressWarnings("unchecked")
    public T handle(final ResultSet rs, final ResultSetMetaData rsmd) throws SQLException {
        if (rs.next()) {
            if (columnIndex < 0) {
                columnIndex = rs.findColumn(columnName);
            }
            switch (conversionType) {
            case TYPE_OBJECT:
                return (T) rs.getObject(columnIndex);
            case TYPE_STRING:
                return (T) rs.getString(columnIndex);
            case TYPE_INTEGER:
                final int intValue = rs.getInt(columnIndex);
                if (rs.wasNull()) {
                    return null;
                }
                return (T) Integer.valueOf(intValue);
            case TYPE_BOOLEAN:
                final boolean boolVal = rs.getBoolean(columnIndex);
                if (rs.wasNull()) {
                    return null;
                }
                return (T) Boolean.valueOf(boolVal);
            case TYPE_LONG:
                final long longVal = rs.getLong(columnIndex);
                if (rs.wasNull()) {
                    return null;
                }
                return (T) Long.valueOf(longVal);
            case TYPE_DOUBLE:
                final double doubleVal = rs.getDouble(columnIndex);
                if (rs.wasNull()) {
                    return null;
                }
                return (T) Double.valueOf(doubleVal);
            case TYPE_FLOAT:
                final float floatVal = rs.getFloat(columnIndex);
                if (rs.wasNull()) {
                    return null;
                }
                return (T) Float.valueOf(floatVal);
            case TYPE_SHORT:
                final short shortVal = rs.getShort(columnIndex);
                if (rs.wasNull()) {
                    return null;
                }
                return (T) Short.valueOf(shortVal);
            case TYPE_BYTE:
                final byte byteVal = rs.getByte(columnIndex);
                if (rs.wasNull()) {
                    return null;
                }
                return (T) Byte.valueOf(byteVal);
            case TYPE_TIMESTAMP:
                return (T) rs.getTimestamp(columnIndex);
            default:
                throw new IllegalStateException();
            }
        } else {
            eof = true;
            return null;
        }
    }

    public final void setColumnIndex(final int columnIndex) {
        this.columnIndex = columnIndex;
    }

    public final void setColumnName(final String columnName) {
        this.columnName = columnName;
    }

    @Override
    public boolean wasLast(final T result) {
        return eof;
    }

    private int getConversionType() {
        if (type.equals(String.class)) {
            return TYPE_STRING;
        } else if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
            return TYPE_INTEGER;
        } else if (type.equals(Boolean.TYPE) || type.equals(Boolean.class)) {
            return TYPE_BOOLEAN;
        } else if (type.equals(Long.TYPE) || type.equals(Long.class)) {
            return TYPE_LONG;
        } else if (type.equals(Double.TYPE) || type.equals(Double.class)) {
            return TYPE_DOUBLE;
        } else if (type.equals(Float.TYPE) || type.equals(Float.class)) {
            return TYPE_FLOAT;
        } else if (type.equals(Short.TYPE) || type.equals(Short.class)) {
            return TYPE_SHORT;
        } else if (type.equals(Byte.TYPE) || type.equals(Byte.class)) {
            return TYPE_BYTE;
        } else if (type.equals(Timestamp.class)) {
            return TYPE_TIMESTAMP;
        } else if (type.equals(Object.class)) {
            return TYPE_OBJECT;
        } else {
            return TYPE_UNKNOWN;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy