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

com.arakelian.jdbc.handler.KeyedHandler 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.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;

public final class KeyedHandler extends AbstractKeyedHandler {
    /**
     * Column index
     */
    private int columnIndex;

    /**
     * Key column index or column name.
     */
    private String columnName;

    public KeyedHandler(final ResultSetHandler rowHandler) {
        this(rowHandler, 1);
    }

    public KeyedHandler(final ResultSetHandler rowHandler, final int columnIndex) {
        super(rowHandler);
        this.columnIndex = columnIndex;
    }

    public KeyedHandler(final ResultSetHandler rowHandler, final String columnName) {
        super(rowHandler);
        this.columnName = columnName;
    }

    public final int getColumnIndex() {
        return columnIndex;
    }

    public final String getColumnName() {
        return columnName;
    }

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

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

    /**
     * This factory method is called by handle() to retrieve the key value from the
     * current ResultSet row.
     *
     * @param rs
     *            ResultSet to create a key from
     * @return K from the configured key column name/index
     * @throws SQLException
     *             if a database access error occurs
     */
    @Override
    protected String createKey(final ResultSet rs) throws SQLException {
        if (columnIndex <= 0) {
            columnIndex = rs.findColumn(columnName);
        }
        return rs.getString(columnIndex);
    }

    @Override
    protected Map createMap() {
        return new LinkedHashMap<>();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy