| 1 | /* |
| 2 | * Copyright (C) 2012-2015 DataStax Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.datastax.driver.mapping; |
| 17 | |
| 18 | import com.datastax.driver.core.ExecutionInfo; |
| 19 | import com.datastax.driver.core.ProtocolVersion; |
| 20 | import com.datastax.driver.core.ResultSet; |
| 21 | import com.datastax.driver.core.Row; |
| 22 | |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Iterator; |
| 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * A {@code ResultSet} mapped to an entity class. |
| 30 | */ |
| 31 | public class Result<T> implements Iterable<T> { |
| 32 | |
| 33 | private final ResultSet rs; |
| 34 | private final EntityMapper<T> mapper; |
| 35 | private final ProtocolVersion protocolVersion; |
| 36 | private final boolean useAlias; |
| 37 | |
| 38 | Result(ResultSet rs, EntityMapper<T> mapper, ProtocolVersion protocolVersion, boolean useAlias) { |
| 39 | this.rs = rs; |
| 40 | this.mapper = mapper; |
| 41 | this.protocolVersion = protocolVersion; |
| 42 | this.useAlias = useAlias; |
| 43 | } |
| 44 | |
| 45 | private T map(Row row) { |
| 46 | T entity = mapper.newEntity(); |
| 47 | for (ColumnMapper<T> cm : mapper.allColumns()) { |
| 48 | String name = cm.getAlias() != null && this.useAlias ? cm.getAlias() : cm.getColumnName(); |
| 49 | if (!row.getColumnDefinitions().contains(name)) { |
| 50 | continue; |
| 51 | } |
| 52 | ByteBuffer bytes = row.getBytesUnsafe(name); |
| 53 | if (bytes != null) |
| 54 | cm.setValue(entity, cm.getDataType().deserialize(bytes, protocolVersion)); |
| 55 | } |
| 56 | return entity; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Test whether this mapped result set has more results. |
| 61 | * |
| 62 | * @return whether this mapped result set has more results. |
| 63 | */ |
| 64 | public boolean isExhausted() { |
| 65 | return rs.isExhausted(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the next result (i.e. the entity corresponding to the next row |
| 70 | * in the result set). |
| 71 | * |
| 72 | * @return the next result in this mapped result set or null if it is exhausted. |
| 73 | */ |
| 74 | public T one() { |
| 75 | Row row = rs.one(); |
| 76 | return row == null ? null : map(row); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns all the remaining results (entities) in this mapped result set |
| 81 | * as a list. |
| 82 | * |
| 83 | * @return a list containing the remaining results of this mapped result |
| 84 | * set. The returned list is empty if and only the result set is exhausted. |
| 85 | */ |
| 86 | public List<T> all() { |
| 87 | List<Row> rows = rs.all(); |
| 88 | List<T> entities = new ArrayList<T>(rows.size()); |
| 89 | for (Row row : rows) { |
| 90 | entities.add(map(row)); |
| 91 | } |
| 92 | return entities; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * An iterator over the entities of this mapped result set. |
| 97 | * <p/> |
| 98 | * The {@link Iterator#next} method is equivalent to calling {@link #one}. |
| 99 | * So this iterator will consume results and after a full iteration, the |
| 100 | * mapped result set (and underlying {@code ResultSet}) will be empty. |
| 101 | * <p/> |
| 102 | * The returned iterator does not support the {@link Iterator#remove} method. |
| 103 | * |
| 104 | * @return an iterator that will consume and return the remaining rows of |
| 105 | * this mapped result set. |
| 106 | */ |
| 107 | @Override |
| 108 | public Iterator<T> iterator() { |
| 109 | return new Iterator<T>() { |
| 110 | private final Iterator<Row> rowIterator = rs.iterator(); |
| 111 | |
| 112 | @Override |
| 113 | public boolean hasNext() { |
| 114 | return rowIterator.hasNext(); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public T next() { |
| 119 | return map(rowIterator.next()); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void remove() { |
| 124 | throw new UnsupportedOperationException(); |
| 125 | } |
| 126 | }; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns information on the execution of this query. |
| 131 | * <p/> |
| 132 | * The returned object includes basic information such as the queried hosts, |
| 133 | * but also the Cassandra query trace if tracing was enabled for the query. |
| 134 | * |
| 135 | * @return the execution info for this query. |
| 136 | */ |
| 137 | public ExecutionInfo getExecutionInfo() { |
| 138 | return rs.getExecutionInfo(); |
| 139 | } |
| 140 | } |