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 java.sql.ResultSet;
030: import java.sql.ResultSetMetaData;
031: import java.sql.SQLException;
032: import java.util.Iterator;
033:
034: import org.databene.commons.converter.AnyConverter;
035: import org.databene.model.data.Entity;
036: import org.databene.model.data.EntityDescriptor;
037: import org.databene.platform.bean.BeanDescriptorProvider;
038:
039: /**
040: * Iterates a ResultSet returning Entity objects.
041: *
042: * @author Volker Bergmann
043: * |since 0.3.04
044: */
045: public class ResultSetEntityIterator implements Iterator<Entity> {
046:
047: private Iterator<ResultSet> resultSetIterator;
048:
049: private BeanDescriptorProvider beanDescriptorProvider;
050:
051: private EntityDescriptor descriptor;
052:
053: public ResultSetEntityIterator(
054: Iterator<ResultSet> resultSetIterator,
055: EntityDescriptor descriptor) {
056: this .resultSetIterator = resultSetIterator;
057: this .descriptor = descriptor;
058: this .beanDescriptorProvider = new BeanDescriptorProvider();
059: }
060:
061: public boolean hasNext() {
062: return resultSetIterator.hasNext();
063: }
064:
065: public Entity next() {
066: if (!hasNext())
067: throw new IllegalStateException(
068: "No more row available. Check 'hasNext()' before calling next()!");
069: try {
070: Entity entity = new Entity(descriptor);
071: ResultSet resultSet = resultSetIterator.next();
072: ResultSetMetaData metaData = resultSet.getMetaData();
073: int columnCount = metaData.getColumnCount();
074: for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
075: String columnName = metaData.getColumnName(columnIndex);
076: String abstractType = descriptor
077: .getComponentDescriptor(columnName).getType();
078: Object javaValue = javaValue(resultSet, columnIndex,
079: abstractType);
080: entity.setComponent(columnName, javaValue);
081: }
082: return entity;
083: } catch (SQLException e) {
084: throw new RuntimeException(e);
085: }
086: }
087:
088: public void remove() {
089: throw new UnsupportedOperationException();
090: }
091:
092: // private helpers ----------------------------------------------------------------------------------------
093:
094: private Object javaValue(ResultSet resultSet, int columnIndex,
095: String abstractType) throws SQLException {
096: if ("date".equals(abstractType))
097: return resultSet.getDate(columnIndex);
098: else if ("timestamp".equals(abstractType))
099: return resultSet.getTimestamp(columnIndex);
100: else if ("string".equals(abstractType))
101: return resultSet.getString(columnIndex);
102: // try generic conversion
103: Object driverValue = resultSet.getObject(columnIndex);
104: Class<? extends Object> javaType = beanDescriptorProvider
105: .concreteType(abstractType);
106: Object javaValue = AnyConverter.convert(driverValue, javaType);
107: return javaValue;
108: }
109:
110: }
|