|
22 | 22 | import java.util.List;
|
23 | 23 | import java.util.Optional;
|
24 | 24 | import java.util.Set;
|
25 |
| -import java.util.stream.Collectors; |
26 | 25 |
|
| 26 | +import org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentEntity; |
27 | 27 | import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
|
28 | 28 | import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
|
29 | 29 | import org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty;
|
@@ -167,8 +167,14 @@ public List<Selector> getMappedSelectors(Columns columns, CassandraPersistentEnt
|
167 | 167 |
|
168 | 168 | Field field = createPropertyField(entity, column);
|
169 | 169 |
|
170 |
| - columns.getSelector(column).ifPresent(selector -> getCqlIdentifier(column, field) |
171 |
| - .ifPresent(cqlIdentifier -> selectors.add(getMappedSelector(selector, cqlIdentifier)))); |
| 170 | + columns.getSelector(column).ifPresent(selector -> { |
| 171 | + |
| 172 | + List<CqlIdentifier> mappedColumnNames = getCqlIdentifier(column, field); |
| 173 | + |
| 174 | + for (CqlIdentifier mappedColumnName : mappedColumnNames) { |
| 175 | + selectors.add(getMappedSelector(selector, mappedColumnName)); |
| 176 | + } |
| 177 | + }); |
172 | 178 | }
|
173 | 179 |
|
174 | 180 | if (columns.isEmpty()) {
|
@@ -207,17 +213,15 @@ private Selector getMappedSelector(Selector selector, CqlIdentifier cqlIdentifie
|
207 | 213 |
|
208 | 214 | FunctionCall functionCall = (FunctionCall) selector;
|
209 | 215 |
|
210 |
| - List<Object> mappedParameters = functionCall.getParameters().stream().map(obj -> { |
| 216 | + FunctionCall mappedFunctionCall = FunctionCall.from(functionCall.getExpression(), |
| 217 | + functionCall.getParameters().stream().map(obj -> { |
211 | 218 |
|
212 |
| - if (obj instanceof Selector) { |
213 |
| - return getMappedSelector((Selector) obj, cqlIdentifier); |
214 |
| - } |
215 |
| - |
216 |
| - return obj; |
217 |
| - }) // |
218 |
| - .collect(Collectors.toList()); |
| 219 | + if (obj instanceof Selector) { |
| 220 | + return getMappedSelector((Selector) obj, cqlIdentifier); |
| 221 | + } |
219 | 222 |
|
220 |
| - FunctionCall mappedFunctionCall = FunctionCall.from(functionCall.getExpression(), mappedParameters.toArray()); |
| 223 | + return obj; |
| 224 | + }).toArray()); |
221 | 225 |
|
222 | 226 | return functionCall.getAlias() //
|
223 | 227 | .map(mappedFunctionCall::as) //
|
@@ -252,11 +256,10 @@ public List<CqlIdentifier> getMappedColumnNames(Columns columns, CassandraPersis
|
252 | 256 | for (ColumnName column : columns) {
|
253 | 257 |
|
254 | 258 | Field field = createPropertyField(entity, column);
|
255 |
| - |
256 | 259 | field.getProperty().ifPresent(seen::add);
|
257 | 260 |
|
258 | 261 | columns.getSelector(column).filter(selector -> selector instanceof ColumnSelector)
|
259 |
| - .ifPresent(columnSelector -> getCqlIdentifier(column, field).ifPresent(columnNames::add)); |
| 262 | + .ifPresent(columnSelector -> columnNames.addAll(getCqlIdentifier(column, field))); |
260 | 263 | }
|
261 | 264 |
|
262 | 265 | if (columns.isEmpty()) {
|
@@ -293,40 +296,49 @@ public Sort getMappedSort(Sort sort, CassandraPersistentEntity<?> entity) {
|
293 | 296 |
|
294 | 297 | Field field = createPropertyField(entity, columnName);
|
295 | 298 |
|
296 |
| - Order mappedOrder = getCqlIdentifier(columnName, field) |
297 |
| - .map(cqlIdentifier -> new Order(order.getDirection(), cqlIdentifier.toString())).orElse(order); |
| 299 | + List<CqlIdentifier> mappedColumnNames = getCqlIdentifier(columnName, field); |
298 | 300 |
|
299 |
| - mappedOrders.add(mappedOrder); |
| 301 | + if (mappedColumnNames.isEmpty()) { |
| 302 | + mappedOrders.add(order); |
| 303 | + } else { |
| 304 | + for (CqlIdentifier mappedColumnName : mappedColumnNames) { |
| 305 | + mappedOrders.add(new Order(order.getDirection(), mappedColumnName.toString())); |
| 306 | + } |
| 307 | + } |
300 | 308 | }
|
301 | 309 |
|
302 | 310 | return Sort.by(mappedOrders);
|
303 | 311 | }
|
304 | 312 |
|
305 |
| - private Optional<CqlIdentifier> getCqlIdentifier(ColumnName column, Field field) { |
| 313 | + private List<CqlIdentifier> getCqlIdentifier(ColumnName column, Field field) { |
306 | 314 |
|
| 315 | + List<CqlIdentifier> identifiers = new ArrayList<>(1); |
307 | 316 | try {
|
308 | 317 | if (field.getProperty().isPresent()) {
|
309 | 318 |
|
310 |
| - return field.getProperty().map(cassandraPersistentProperty -> { |
| 319 | + CassandraPersistentProperty property = field.getProperty().get(); |
311 | 320 |
|
312 |
| - if (cassandraPersistentProperty.isCompositePrimaryKey()) { |
313 |
| - throw new IllegalArgumentException( |
314 |
| - "Cannot use composite primary key directly. Reference a property of the composite primary key"); |
315 |
| - } |
| 321 | + if (property.isCompositePrimaryKey()) { |
316 | 322 |
|
317 |
| - return cassandraPersistentProperty.getRequiredColumnName(); |
318 |
| - }); |
319 |
| - } |
| 323 | + BasicCassandraPersistentEntity<?> primaryKeyEntity = mappingContext.getRequiredPersistentEntity(property); |
320 | 324 |
|
321 |
| - if (column.getColumnName().isPresent()) { |
322 |
| - return column.getColumnName().map(CqlIdentifier::fromCql); |
| 325 | + primaryKeyEntity.forEach(it -> { |
| 326 | + identifiers.add(it.getRequiredColumnName()); |
| 327 | + }); |
| 328 | + } else { |
| 329 | + identifiers.add(property.getRequiredColumnName()); |
| 330 | + } |
| 331 | + } else if (column.getColumnName().isPresent()) { |
| 332 | + identifiers.add(CqlIdentifier.fromCql(column.getColumnName().get())); |
| 333 | + } else { |
| 334 | + column.getCqlIdentifier().ifPresent(identifiers::add); |
323 | 335 | }
|
324 | 336 |
|
325 |
| - return column.getCqlIdentifier(); |
326 |
| - |
327 | 337 | } catch (IllegalStateException cause) {
|
328 | 338 | throw new IllegalArgumentException(cause.getMessage(), cause);
|
329 | 339 | }
|
| 340 | + |
| 341 | + return identifiers; |
330 | 342 | }
|
331 | 343 |
|
332 | 344 | Field createPropertyField(@Nullable CassandraPersistentEntity<?> entity, ColumnName key) {
|
|
0 commit comments