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