View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   */
26  
27  package com.rapiddweller.platform.db;
28  
29  import com.rapiddweller.common.converter.AnyConverter;
30  import com.rapiddweller.model.data.ComplexTypeDescriptor;
31  import com.rapiddweller.model.data.ComponentDescriptor;
32  import com.rapiddweller.model.data.DataModel;
33  import com.rapiddweller.model.data.Entity;
34  import com.rapiddweller.model.data.SimpleTypeDescriptor;
35  import com.rapiddweller.script.PrimitiveType;
36  
37  import java.sql.ResultSet;
38  import java.sql.ResultSetMetaData;
39  import java.sql.SQLException;
40  
41  /**
42   * Converts a SQL {@link ResultSet} to a Benerator {@link Entity}.<br/><br/>
43   * Created: 24.08.2010 12:29:56
44   *
45   * @author Volker Bergmann
46   * @since 0.6.4
47   */
48  public class ResultSet2EntityConverter {
49  
50    /**
51     * Convert entity.
52     *
53     * @param resultSet  the result set
54     * @param descriptor the descriptor
55     * @return the entity
56     * @throws SQLException the sql exception
57     */
58    public static Entity convert(ResultSet resultSet,
59                                 ComplexTypeDescriptor descriptor)
60        throws SQLException {
61      EntityEntity.html#Entity">Entity entity = new Entity(descriptor);
62      ResultSetMetaData metaData = resultSet.getMetaData();
63      int columnCount = metaData.getColumnCount();
64      for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
65        String columnName = metaData.getColumnName(columnIndex);
66        String typeName = null;
67        if (descriptor != null) {
68          ComponentDescriptor component =
69              descriptor.getComponent(columnName);
70          if (component != null) {
71            SimpleTypeDescriptorcom/rapiddweller/model/data/SimpleTypeDescriptor.html#SimpleTypeDescriptor">SimpleTypeDescriptor type = (SimpleTypeDescriptor) component
72                .getTypeDescriptor();
73            PrimitiveType primitiveType = type.getPrimitiveType();
74            typeName =
75                (primitiveType != null ? primitiveType.getName() :
76                    "string");
77          } else {
78            typeName = "string";
79          }
80        } else {
81          typeName = "string";
82        }
83        DataModel dataModel =
84            (descriptor != null ? descriptor.getDataModel() : null);
85        Object javaValue =
86            javaValue(resultSet, columnIndex, typeName, dataModel);
87        entity.setComponent(columnName, javaValue);
88      }
89      return entity;
90    }
91  
92    // TODO v1.0 perf: use a dedicated converter for each column
93    private static Object javaValue(ResultSet resultSet, int columnIndex,
94                                    String primitiveType, DataModel dataModel)
95        throws SQLException {
96      if ("date".equals(primitiveType)) {
97        return resultSet.getDate(columnIndex);
98      } else if ("timestamp".equals(primitiveType)) {
99        return resultSet.getTimestamp(columnIndex);
100     } else if ("string".equals(primitiveType)) {
101       return resultSet.getString(columnIndex);
102     }
103     // try generic conversion
104     Object driverValue = resultSet.getObject(columnIndex);
105     Object javaValue = driverValue;
106     if (dataModel != null) {
107       Class<?> javaType = dataModel.getBeanDescriptorProvider()
108           .concreteType(primitiveType);
109       javaValue = AnyConverter.convert(driverValue, javaType);
110     }
111     return javaValue;
112   }
113 
114 }