001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.platform.db;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.databene.commons.ArrayFormat;
032: import org.databene.commons.ConversionException;
033: import org.databene.commons.Converter;
034:
035: import java.sql.ResultSet;
036: import java.sql.SQLException;
037:
038: /**
039: * Converts a ResultSet's current cursor position to an array of objects or, if it is of size 1, to a single object.<br/>
040: * <br/>
041: * Created: 15.08.2007 18:19:25
042: * @author Volker Bergmann
043: */
044: public class ResultSetConverter implements Converter<ResultSet, Object> {
045:
046: private boolean simplifying;
047:
048: public ResultSetConverter() {
049: this (true);
050: }
051:
052: public ResultSetConverter(boolean simplifying) {
053: this .simplifying = simplifying;
054: }
055:
056: // Converter interface ---------------------------------------------------------------------------------------------
057:
058: public Class<Object> getTargetType() {
059: return Object.class;
060: }
061:
062: public Object convert(ResultSet resultSet)
063: throws ConversionException {
064: Object[] tmp = convertToArray(resultSet);
065: return (!simplifying || tmp.length > 1 ? tmp : tmp[0]);
066: }
067:
068: // static convenience methods --------------------------------------------------------------------------------------
069:
070: public static Object convert(ResultSet resultSet,
071: boolean simplifying) throws ConversionException {
072: Object[] tmp = convertToArray(resultSet);
073: return (!simplifying || tmp.length > 1 ? tmp : tmp[0]);
074: }
075:
076: // java.lang.Object overrides --------------------------------------------------------------------------------------
077:
078: @Override
079: public String toString() {
080: return getClass().getSimpleName();
081: }
082:
083: // private helpers -------------------------------------------------------------------------------------------------
084:
085: private static Object[] convertToArray(ResultSet resultSet)
086: throws ConversionException {
087: try {
088: int columnCount = resultSet.getMetaData().getColumnCount();
089: Object[] cells = new Object[columnCount];
090: for (int i = 0; i < columnCount; i++)
091: cells[i] = resultSet.getObject(i + 1);
092: if (logger.isDebugEnabled())
093: logger.debug("Converted: " + ArrayFormat.format(cells));
094: return cells;
095: } catch (SQLException e) {
096: throw new ConversionException(e);
097: }
098: }
099:
100: private static final Log logger = LogFactory
101: .getLog(ResultSetConverter.class);
102: }
|