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

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

import com.arakelian.jdbc.utils.DatabaseUtils;

/**
 * ResultSetHandler implementation that converts the next ResultSet row
 * into a Map. This class is thread safe.
 */
public class MapHandler implements ResultSetHandler> {
    public static final MapHandler SINGLETON = new MapHandler();

    /**
     * Array of column names, or null to use {@link ResultSetMetaData#getColumnName(int)}.
     */
    private String[] columnNames;

    public MapHandler() {
        this(null);
    }

    public MapHandler(final String[] columnNames) {
        this.columnNames = columnNames;
    }

    public final String[] getColumnNames() {
        return columnNames;
    }

    /**
     * Converts the first row in the ResultSet into a Map.
     *
     * @param rs
     *            ResultSet to process.
     * @return A Map with the values from the first row or null if there
     *         are no rows in the ResultSet.
     *
     * @throws SQLException
     *             if a database access error occurs
     */
    @Override
    public Map handle(final ResultSet rs, final ResultSetMetaData rsmd) throws SQLException {
        if (columnNames == null) {
            columnNames = DatabaseUtils.getColumnNames(rsmd);
        }
        if (rs.next()) {
            final Map map = createMap();
            final int cols = rsmd.getColumnCount();
            for (int i = 1; i <= cols; i++) { // 1-based!
                final String key = columnNames != null && i <= columnNames.length ? columnNames[i - 1]
                        : rsmd.getColumnName(i);
                final Object value = rs.getObject(i);
                if (key != null) {
                    map.put(key, value);
                }
            }
            return map;
        } else {
            return null;
        }
    }

    @Override
    public boolean wasLast(final Map result) {
        return result == null;
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy