| 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 | import com.google.common.base.Function; |
| 20 | import com.google.common.util.concurrent.Futures; |
| 21 | import com.google.common.util.concurrent.ListenableFuture; |
| 22 | |
| 23 | import java.util.*; |
| 24 | |
| 25 | /** |
| 26 | * A result set whose rows are mapped to an entity class. |
| 27 | */ |
| 28 | public class Result<T> implements PagingIterable<Result<T>, T> { |
| 29 | |
| 30 | private final ResultSet rs; |
| 31 | private final EntityMapper<T> mapper; |
| 32 | private final boolean useAlias; |
| 33 | |
| 34 | Result(ResultSet rs, EntityMapper<T> mapper) { |
| 35 | this(rs, mapper, false); |
| 36 | } |
| 37 | |
| 38 | Result(ResultSet rs, EntityMapper<T> mapper, boolean useAlias) { |
| 39 | this.rs = rs; |
| 40 | this.mapper = mapper; |
| 41 | this.useAlias = useAlias; |
| 42 | } |
| 43 | |
| 44 | private T map(Row row) { |
| 45 | T entity = mapper.newEntity(); |
| 46 | for (PropertyMapper col : mapper.allColumns) { |
| 47 | String name = col.alias != null && this.useAlias ? col.alias : col.columnName; |
| 48 | if (!row.getColumnDefinitions().contains(name)) |
| 49 | continue; |
| 50 | |
| 51 | Object value; |
| 52 | TypeCodec<Object> customCodec = col.customCodec; |
| 53 | if (customCodec != null) |
| 54 | value = row.get(name, customCodec); |
| 55 | else |
| 56 | value = row.get(name, col.javaType); |
| 57 | |
| 58 | if (shouldSetValue(value)) { |
| 59 | col.setValue(entity, value); |
| 60 | } |
| 61 | } |
| 62 | return entity; |
| 63 | } |
| 64 | |
| 65 | private static boolean shouldSetValue(Object value) { |
| 66 | if (value == null) |
| 67 | return false; |
| 68 | if (value instanceof Collection) |
| 69 | return !((Collection) value).isEmpty(); |
| 70 | if (value instanceof Map) |
| 71 | return !((Map) value).isEmpty(); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public boolean isExhausted() { |
| 77 | return rs.isExhausted(); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public T one() { |
| 82 | Row row = rs.one(); |
| 83 | return row == null ? null : map(row); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public List<T> all() { |
| 88 | List<Row> rows = rs.all(); |
| 89 | List<T> entities = new ArrayList<T>(rows.size()); |
| 90 | for (Row row : rows) { |
| 91 | entities.add(map(row)); |
| 92 | } |
| 93 | return entities; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Iterator<T> iterator() { |
| 98 | return new Iterator<T>() { |
| 99 | private final Iterator<Row> rowIterator = rs.iterator(); |
| 100 | |
| 101 | @Override |
| 102 | public boolean hasNext() { |
| 103 | return rowIterator.hasNext(); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public T next() { |
| 108 | return map(rowIterator.next()); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void remove() { |
| 113 | throw new UnsupportedOperationException(); |
| 114 | } |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public ExecutionInfo getExecutionInfo() { |
| 120 | return rs.getExecutionInfo(); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public List<ExecutionInfo> getAllExecutionInfo() { |
| 125 | return rs.getAllExecutionInfo(); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public ListenableFuture<Result<T>> fetchMoreResults() { |
| 130 | return Futures.transform(rs.fetchMoreResults(), new Function<ResultSet, Result<T>>() { |
| 131 | @Override |
| 132 | public Result<T> apply(ResultSet rs) { |
| 133 | return Result.this; |
| 134 | } |
| 135 | }); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public boolean isFullyFetched() { |
| 140 | return rs.isFullyFetched(); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public int getAvailableWithoutFetching() { |
| 145 | return rs.getAvailableWithoutFetching(); |
| 146 | } |
| 147 | |
| 148 | } |