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.type.memory;
19  
20  import lombok.Getter;
21  import lombok.SneakyThrows;
22  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
23  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
24  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.type.memory.row.MemoryQueryResultDataRow;
25  
26  import java.io.BufferedReader;
27  import java.io.ByteArrayInputStream;
28  import java.io.ByteArrayOutputStream;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.InputStreamReader;
32  import java.io.ObjectOutputStream;
33  import java.io.Reader;
34  import java.sql.SQLException;
35  import java.util.Calendar;
36  import java.util.Collection;
37  import java.util.Iterator;
38  
39  /**
40   * Abstract memory query result.
41   */
42  public abstract class AbstractMemoryQueryResult implements QueryResult {
43      
44      @Getter
45      private final QueryResultMetaData metaData;
46      
47      private final Iterator<MemoryQueryResultDataRow> rows;
48      
49      @Getter
50      private MemoryQueryResultDataRow currentRow;
51      
52      @Getter
53      private long rowCount;
54      
55      private boolean wasNull;
56      
57      protected AbstractMemoryQueryResult(final QueryResultMetaData metaData, final Collection<MemoryQueryResultDataRow> rows) {
58          this.metaData = metaData;
59          this.rows = rows.iterator();
60          rowCount = rows.size();
61      }
62      
63      @Override
64      public final boolean next() {
65          if (rows.hasNext()) {
66              currentRow = rows.next();
67              rowCount--;
68              return true;
69          }
70          currentRow = null;
71          return false;
72      }
73      
74      @Override
75      public final Object getValue(final int columnIndex, final Class<?> type) {
76          Object result = currentRow.getValue().get(columnIndex - 1);
77          wasNull = null == result;
78          return result;
79      }
80      
81      @Override
82      public final Object getCalendarValue(final int columnIndex, final Class<?> type, final Calendar calendar) {
83          Object result = currentRow.getValue().get(columnIndex - 1);
84          wasNull = null == result;
85          return result;
86      }
87      
88      @Override
89      public final InputStream getInputStream(final int columnIndex, final String type) {
90          Object value = currentRow.getValue().get(columnIndex - 1);
91          wasNull = null == value;
92          return getInputStream(value);
93      }
94      
95      @SneakyThrows(IOException.class)
96      private InputStream getInputStream(final Object value) {
97          ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
98          ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
99          objectOutputStream.writeObject(value);
100         objectOutputStream.flush();
101         objectOutputStream.close();
102         return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
103     }
104     
105     @Override
106     public Reader getCharacterStream(final int columnIndex) throws SQLException {
107         // TODO Support connection property character encoding
108         return new BufferedReader(new InputStreamReader(getInputStream(columnIndex)));
109     }
110     
111     @Override
112     public final boolean wasNull() {
113         return wasNull;
114     }
115     
116     @Override
117     public final void close() {
118     }
119 }