|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 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 | + * https://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 org.springframework.data.r2dbc.convert; |
| 17 | + |
| 18 | +import reactor.core.publisher.Mono; |
| 19 | + |
| 20 | +import org.apache.commons.logging.Log; |
| 21 | +import org.apache.commons.logging.LogFactory; |
| 22 | + |
| 23 | +import org.springframework.data.mapping.PersistentProperty; |
| 24 | +import org.springframework.data.mapping.PersistentPropertyAccessor; |
| 25 | +import org.springframework.data.r2dbc.mapping.OutboundRow; |
| 26 | +import org.springframework.data.relational.core.dialect.Dialect; |
| 27 | +import org.springframework.data.relational.core.mapping.RelationalPersistentProperty; |
| 28 | +import org.springframework.data.relational.core.sql.SqlIdentifier; |
| 29 | +import org.springframework.data.util.ReflectionUtils; |
| 30 | +import org.springframework.r2dbc.core.DatabaseClient; |
| 31 | +import org.springframework.r2dbc.core.Parameter; |
| 32 | +import org.springframework.util.ClassUtils; |
| 33 | +import org.springframework.util.NumberUtils; |
| 34 | + |
| 35 | +/** |
| 36 | + * Support class for generating identifier values through a database sequence. |
| 37 | + * |
| 38 | + * @author Mikhail Polivakha |
| 39 | + * @author Mark Paluch |
| 40 | + * @since 3.5 |
| 41 | + * @see org.springframework.data.relational.core.mapping.Sequence |
| 42 | + */ |
| 43 | +class SequenceEntityCallbackDelegate { |
| 44 | + |
| 45 | + private static final Log LOG = LogFactory.getLog(SequenceEntityCallbackDelegate.class); |
| 46 | + |
| 47 | + private final Dialect dialect; |
| 48 | + private final DatabaseClient databaseClient; |
| 49 | + |
| 50 | + public SequenceEntityCallbackDelegate(Dialect dialect, DatabaseClient databaseClient) { |
| 51 | + this.dialect = dialect; |
| 52 | + this.databaseClient = databaseClient; |
| 53 | + } |
| 54 | + |
| 55 | + @SuppressWarnings("unchecked") |
| 56 | + protected Mono<Object> generateSequenceValue(RelationalPersistentProperty property, OutboundRow row, |
| 57 | + PersistentPropertyAccessor<Object> accessor) { |
| 58 | + |
| 59 | + Class<?> targetType = ClassUtils.resolvePrimitiveIfNecessary(property.getType()); |
| 60 | + |
| 61 | + return getSequenceValue(property).map(it -> { |
| 62 | + |
| 63 | + Object sequenceValue = it; |
| 64 | + if (sequenceValue instanceof Number && Number.class.isAssignableFrom(targetType)) { |
| 65 | + sequenceValue = NumberUtils.convertNumberToTargetClass((Number) sequenceValue, |
| 66 | + (Class<? extends Number>) targetType); |
| 67 | + } |
| 68 | + |
| 69 | + row.append(property.getColumnName(), Parameter.from(sequenceValue)); |
| 70 | + accessor.setProperty(property, sequenceValue); |
| 71 | + |
| 72 | + return accessor.getBean(); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + protected boolean hasValue(PersistentProperty<?> property, PersistentPropertyAccessor<Object> propertyAccessor) { |
| 77 | + |
| 78 | + Object identifier = propertyAccessor.getProperty(property); |
| 79 | + |
| 80 | + if (property.getType().isPrimitive()) { |
| 81 | + |
| 82 | + Object primitiveDefault = ReflectionUtils.getPrimitiveDefault(property.getType()); |
| 83 | + return !primitiveDefault.equals(identifier); |
| 84 | + } |
| 85 | + |
| 86 | + return identifier != null; |
| 87 | + } |
| 88 | + |
| 89 | + private Mono<Object> getSequenceValue(RelationalPersistentProperty property) { |
| 90 | + |
| 91 | + SqlIdentifier sequence = property.getSequence(); |
| 92 | + |
| 93 | + if (sequence != null && !dialect.getIdGeneration().sequencesSupported()) { |
| 94 | + LOG.warn(""" |
| 95 | + Entity type '%s' is marked for sequence usage but configured dialect '%s' |
| 96 | + does not support sequences. Falling back to identity columns. |
| 97 | + """.formatted(property.getOwner().getType(), ClassUtils.getQualifiedName(dialect.getClass()))); |
| 98 | + return Mono.empty(); |
| 99 | + } |
| 100 | + |
| 101 | + String sql = dialect.getIdGeneration().createSequenceQuery(sequence); |
| 102 | + return databaseClient // |
| 103 | + .sql(sql) // |
| 104 | + .map((r, rowMetadata) -> r.get(0)) // |
| 105 | + .one(); |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments