| 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.*; |
| 19 | |
| 20 | import java.util.*; |
| 21 | |
| 22 | /** |
| 23 | * A {@code ResultSet} mapped to an entity class. |
| 24 | */ |
| 25 | public class Result<T> implements Iterable<T> { |
| 26 | |
| 27 | private final ResultSet rs; |
| 28 | private final EntityMapper<T> mapper; |
| 29 | private final ProtocolVersion protocolVersion; |
| 30 | private final boolean useAlias; |
| 31 | |
| 32 | Result(ResultSet rs, EntityMapper<T> mapper, ProtocolVersion protocolVersion, boolean useAlias) { |
| 33 | this.rs = rs; |
| 34 | this.mapper = mapper; |
| 35 | this.protocolVersion = protocolVersion; |
| 36 | this.useAlias = useAlias; |
| 37 | } |
| 38 | |
| 39 | private T map(Row row) { |
| 40 | T entity = mapper.newEntity(); |
| 41 | for (ColumnMapper<T> cm : mapper.allColumns()) { |
| 42 | String name = cm.getAlias() != null && this.useAlias ? cm.getAlias() : cm.getColumnName(); |
| 43 | if (!row.getColumnDefinitions().contains(name)) |
| 44 | continue; |
| 45 | |
| 46 | Object value; |
| 47 | TypeCodec<Object> customCodec = cm.getCustomCodec(); |
| 48 | if (customCodec != null) |
| 49 | value = row.get(name, customCodec); |
| 50 | else |
| 51 | value = row.get(name, cm.getJavaType()); |
| 52 | |
| 53 | if (shouldSetValue(value)) { |
| 54 | cm.setValue(entity, value); |
| 55 | } |
| 56 | } |
| 57 | return entity; |
| 58 | } |
| 59 | |
| 60 | private static boolean shouldSetValue(Object value) { |
| 61 | if (value == null) |
| 62 | return false; |
| 63 | if (value instanceof Collection) |
| 64 | return !((Collection) value).isEmpty(); |
| 65 | if (value instanceof Map) |
| 66 | return !((Map) value).isEmpty(); |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Test whether this mapped result set has more results. |
| 72 | * |
| 73 | * @return whether this mapped result set has more results. |
| 74 | */ |
| 75 | public boolean isExhausted() { |
| 76 | return rs.isExhausted(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns the next result (i.e. the entity corresponding to the next row |
| 81 | * in the result set). |
| 82 | * |
| 83 | * @return the next result in this mapped result set or null if it is exhausted. |
| 84 | */ |
| 85 | public T one() { |
| 86 | Row row = rs.one(); |
| 87 | return row == null ? null : map(row); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns all the remaining results (entities) in this mapped result set |
| 92 | * as a list. |
| 93 | * |
| 94 | * @return a list containing the remaining results of this mapped result |
| 95 | * set. The returned list is empty if and only the result set is exhausted. |
| 96 | */ |
| 97 | public List<T> all() { |
| 98 | List<Row> rows = rs.all(); |
| 99 | List<T> entities = new ArrayList<T>(rows.size()); |
| 100 | for (Row row : rows) { |
| 101 | entities.add(map(row)); |
| 102 | } |
| 103 | return entities; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * An iterator over the entities of this mapped result set. |
| 108 | * <p/> |
| 109 | * The {@link Iterator#next} method is equivalent to calling {@link #one}. |
| 110 | * So this iterator will consume results and after a full iteration, the |
| 111 | * mapped result set (and underlying {@code ResultSet}) will be empty. |
| 112 | * <p/> |
| 113 | * The returned iterator does not support the {@link Iterator#remove} method. |
| 114 | * |
| 115 | * @return an iterator that will consume and return the remaining rows of |
| 116 | * this mapped result set. |
| 117 | */ |
| 118 | @Override |
| 119 | public Iterator<T> iterator() { |
| 120 | return new Iterator<T>() { |
| 121 | private final Iterator<Row> rowIterator = rs.iterator(); |
| 122 | |
| 123 | @Override |
| 124 | public boolean hasNext() { |
| 125 | return rowIterator.hasNext(); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public T next() { |
| 130 | return map(rowIterator.next()); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void remove() { |
| 135 | throw new UnsupportedOperationException(); |
| 136 | } |
| 137 | }; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Returns information on the execution of this query. |
| 142 | * <p/> |
| 143 | * The returned object includes basic information such as the queried hosts, |
| 144 | * but also the Cassandra query trace if tracing was enabled for the query. |
| 145 | * |
| 146 | * @return the execution info for this query. |
| 147 | */ |
| 148 | public ExecutionInfo getExecutionInfo() { |
| 149 | return rs.getExecutionInfo(); |
| 150 | } |
| 151 | } |