View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.driver.jdbc.type.stream;
19  
20  import lombok.Getter;
21  import org.apache.shardingsphere.infra.exception.kernel.data.UnsupportedDataTypeConversionException;
22  import org.apache.shardingsphere.infra.exception.kernel.data.UnsupportedStreamCharsetConversionException;
23  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.driver.jdbc.metadata.JDBCQueryResultMetaData;
24  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.type.stream.AbstractStreamQueryResult;
25  
26  import java.io.InputStream;
27  import java.io.Reader;
28  import java.math.BigDecimal;
29  import java.sql.Array;
30  import java.sql.Blob;
31  import java.sql.Clob;
32  import java.sql.Date;
33  import java.sql.ResultSet;
34  import java.sql.SQLException;
35  import java.sql.Time;
36  import java.sql.Timestamp;
37  import java.time.ZonedDateTime;
38  import java.util.Calendar;
39  import java.util.Optional;
40  
41  /**
42   * JDBC query result for stream loading.
43   */
44  @Getter
45  public final class JDBCStreamQueryResult extends AbstractStreamQueryResult {
46      
47      private final ResultSet resultSet;
48      
49      private final boolean containsJDBCResultSet;
50      
51      public JDBCStreamQueryResult(final ResultSet resultSet) throws SQLException {
52          this(resultSet, false);
53      }
54      
55      public JDBCStreamQueryResult(final ResultSet resultSet, final boolean containsJDBCResultSet) throws SQLException {
56          super(new JDBCQueryResultMetaData(resultSet.getMetaData()));
57          this.resultSet = resultSet;
58          this.containsJDBCResultSet = containsJDBCResultSet;
59      }
60      
61      @Override
62      public boolean next() throws SQLException {
63          return resultSet.next();
64      }
65      
66      @Override
67      public Object getValue(final int columnIndex, final Class<?> type) throws SQLException {
68          if (boolean.class == type) {
69              return resultSet.getBoolean(columnIndex);
70          }
71          if (byte.class == type) {
72              return resultSet.getByte(columnIndex);
73          }
74          if (short.class == type) {
75              return resultSet.getShort(columnIndex);
76          }
77          if (int.class == type) {
78              return resultSet.getInt(columnIndex);
79          }
80          if (long.class == type) {
81              return resultSet.getLong(columnIndex);
82          }
83          if (float.class == type) {
84              return resultSet.getFloat(columnIndex);
85          }
86          if (double.class == type) {
87              return resultSet.getDouble(columnIndex);
88          }
89          if (String.class == type) {
90              return resultSet.getString(columnIndex);
91          }
92          if (BigDecimal.class == type) {
93              return resultSet.getBigDecimal(columnIndex);
94          }
95          if (byte[].class == type) {
96              return resultSet.getBytes(columnIndex);
97          }
98          if (Date.class == type) {
99              return resultSet.getDate(columnIndex);
100         }
101         if (Time.class == type) {
102             return resultSet.getTime(columnIndex);
103         }
104         if (Timestamp.class == type) {
105             return resultSet.getTimestamp(columnIndex);
106         }
107         if (Blob.class == type) {
108             return resultSet.getBlob(columnIndex);
109         }
110         if (Clob.class == type) {
111             return resultSet.getClob(columnIndex);
112         }
113         if (Array.class == type) {
114             return resultSet.getArray(columnIndex);
115         }
116         if (ZonedDateTime.class == type) {
117             return resultSet.getObject(columnIndex, type);
118         }
119         return resultSet.getObject(columnIndex);
120     }
121     
122     @Override
123     public Object getCalendarValue(final int columnIndex, final Class<?> type, @SuppressWarnings("UseOfObsoleteDateTimeApi") final Calendar calendar) throws SQLException {
124         if (Date.class == type) {
125             return resultSet.getDate(columnIndex, calendar);
126         }
127         if (Time.class == type) {
128             return resultSet.getTime(columnIndex, calendar);
129         }
130         if (Timestamp.class == type) {
131             return resultSet.getTimestamp(columnIndex, calendar);
132         }
133         throw new UnsupportedDataTypeConversionException(type, calendar).toSQLException();
134     }
135     
136     @SuppressWarnings("deprecation")
137     @Override
138     public InputStream getInputStream(final int columnIndex, final String type) throws SQLException {
139         switch (type) {
140             case "Ascii":
141                 return resultSet.getAsciiStream(columnIndex);
142             case "Unicode":
143                 return resultSet.getUnicodeStream(columnIndex);
144             case "Binary":
145                 return resultSet.getBinaryStream(columnIndex);
146             default:
147                 throw new UnsupportedStreamCharsetConversionException(type).toSQLException();
148         }
149     }
150     
151     @Override
152     public Reader getCharacterStream(final int columnIndex) throws SQLException {
153         return resultSet.getCharacterStream(columnIndex);
154     }
155     
156     @Override
157     public boolean wasNull() throws SQLException {
158         return resultSet.wasNull();
159     }
160     
161     @Override
162     public Optional<ResultSet> getJDBCResultSet() {
163         return containsJDBCResultSet ? Optional.of(resultSet) : Optional.empty();
164     }
165     
166     @Override
167     public void close() throws SQLException {
168         resultSet.close();
169     }
170 }