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.util.Calendar;
38  
39  /**
40   * JDBC query result for stream loading.
41   */
42  public final class JDBCStreamQueryResult extends AbstractStreamQueryResult {
43      
44      @Getter
45      private final ResultSet resultSet;
46      
47      public JDBCStreamQueryResult(final ResultSet resultSet) throws SQLException {
48          super(new JDBCQueryResultMetaData(resultSet.getMetaData()));
49          this.resultSet = resultSet;
50      }
51      
52      @Override
53      public boolean next() throws SQLException {
54          return resultSet.next();
55      }
56      
57      @Override
58      public Object getValue(final int columnIndex, final Class<?> type) throws SQLException {
59          if (boolean.class == type) {
60              return resultSet.getBoolean(columnIndex);
61          }
62          if (byte.class == type) {
63              return resultSet.getByte(columnIndex);
64          }
65          if (short.class == type) {
66              return resultSet.getShort(columnIndex);
67          }
68          if (int.class == type) {
69              return resultSet.getInt(columnIndex);
70          }
71          if (long.class == type) {
72              return resultSet.getLong(columnIndex);
73          }
74          if (float.class == type) {
75              return resultSet.getFloat(columnIndex);
76          }
77          if (double.class == type) {
78              return resultSet.getDouble(columnIndex);
79          }
80          if (String.class == type) {
81              return resultSet.getString(columnIndex);
82          }
83          if (BigDecimal.class == type) {
84              return resultSet.getBigDecimal(columnIndex);
85          }
86          if (byte[].class == type) {
87              return resultSet.getBytes(columnIndex);
88          }
89          if (Date.class == type) {
90              return resultSet.getDate(columnIndex);
91          }
92          if (Time.class == type) {
93              return resultSet.getTime(columnIndex);
94          }
95          if (Timestamp.class == type) {
96              return resultSet.getTimestamp(columnIndex);
97          }
98          if (Blob.class == type) {
99              return resultSet.getBlob(columnIndex);
100         }
101         if (Clob.class == type) {
102             return resultSet.getClob(columnIndex);
103         }
104         if (Array.class == type) {
105             return resultSet.getArray(columnIndex);
106         }
107         return resultSet.getObject(columnIndex);
108     }
109     
110     @Override
111     public Object getCalendarValue(final int columnIndex, final Class<?> type, final Calendar calendar) throws SQLException {
112         if (Date.class == type) {
113             return resultSet.getDate(columnIndex, calendar);
114         }
115         if (Time.class == type) {
116             return resultSet.getTime(columnIndex, calendar);
117         }
118         if (Timestamp.class == type) {
119             return resultSet.getTimestamp(columnIndex, calendar);
120         }
121         throw new UnsupportedDataTypeConversionException(type, calendar).toSQLException();
122     }
123     
124     @SuppressWarnings("deprecation")
125     @Override
126     public InputStream getInputStream(final int columnIndex, final String type) throws SQLException {
127         switch (type) {
128             case "Ascii":
129                 return resultSet.getAsciiStream(columnIndex);
130             case "Unicode":
131                 return resultSet.getUnicodeStream(columnIndex);
132             case "Binary":
133                 return resultSet.getBinaryStream(columnIndex);
134             default:
135                 throw new UnsupportedStreamCharsetConversionException(type).toSQLException();
136         }
137     }
138     
139     @Override
140     public Reader getCharacterStream(final int columnIndex) throws SQLException {
141         return resultSet.getCharacterStream(columnIndex);
142     }
143     
144     @Override
145     public boolean wasNull() throws SQLException {
146         return resultSet.wasNull();
147     }
148     
149     @Override
150     public void close() throws SQLException {
151         resultSet.close();
152     }
153 }