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.merge.result.impl.memory;
19  
20  import com.google.common.base.Preconditions;
21  import lombok.RequiredArgsConstructor;
22  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
23  
24  import java.sql.SQLException;
25  
26  /**
27   * Memory query result row.
28   */
29  @RequiredArgsConstructor
30  public final class MemoryQueryResultRow {
31      
32      private final Object[] data;
33      
34      public MemoryQueryResultRow(final QueryResult queryResult) throws SQLException {
35          data = load(queryResult);
36      }
37      
38      private Object[] load(final QueryResult queryResult) throws SQLException {
39          int columnCount = queryResult.getMetaData().getColumnCount();
40          Object[] result = new Object[columnCount];
41          for (int i = 0; i < columnCount; i++) {
42              result[i] = queryResult.getValue(i + 1, Object.class);
43          }
44          return result;
45      }
46      
47      /**
48       * Get data from cell.
49       * 
50       * @param columnIndex column index
51       * @return data from cell
52       */
53      public Object getCell(final int columnIndex) {
54          Preconditions.checkArgument(columnIndex > 0 && columnIndex < data.length + 1);
55          return data[columnIndex - 1];
56      }
57      
58      /**
59       * Set data for cell.
60       *
61       * @param columnIndex column index
62       * @param value data for cell
63       */
64      public void setCell(final int columnIndex, final Object value) {
65          Preconditions.checkArgument(columnIndex > 0 && columnIndex < data.length + 1);
66          data[columnIndex - 1] = value;
67      }
68  }