Skip to content

Commit

Permalink
Added Sequence generation support
Browse files Browse the repository at this point in the history
  • Loading branch information
mipo256 committed Dec 9, 2024
1 parent e4ba477 commit d500fed
Show file tree
Hide file tree
Showing 40 changed files with 803 additions and 51 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@
</developers>

<profiles>

<profile>
<id>no-jacoco</id>
<build>
Expand Down
31 changes: 31 additions & 0 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,37 @@
</plugins>
</build>
</profile>
<profile>
<id>mariadb</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>mariadb-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<includes>
<include>**/*IntegrationTests.java</include>
</includes>
<excludes>
<exclude/>
</excludes>
<systemPropertyVariables>
<spring.profiles.active>mariadb</spring.profiles.active>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>all-dbs</id>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.OptimisticLockingFailureException;
Expand All @@ -37,6 +44,7 @@
import org.springframework.data.relational.core.query.Query;
import org.springframework.data.relational.core.sql.LockMode;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.util.Pair;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
Expand All @@ -60,6 +68,7 @@
* @author Radim Tlusty
* @author Chirag Tailor
* @author Diego Krupitza
* @author Mikhail Polivakha
* @since 1.1
*/
public class DefaultDataAccessStrategy implements DataAccessStrategy {
Expand Down Expand Up @@ -103,30 +112,35 @@ public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, Relation
public <T> Object insert(T instance, Class<T> domainType, Identifier identifier, IdValueSource idValueSource) {

SqlIdentifierParameterSource parameterSource = sqlParametersFactory.forInsert(instance, domainType, identifier,
idValueSource);
idValueSource);

String insertSql = sql(domainType).getInsert(parameterSource.getIdentifiers());

return insertStrategyFactory.insertStrategy(idValueSource, getIdColumn(domainType)).execute(insertSql,
parameterSource);
parameterSource);
}

@Override
public <T> Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domainType, IdValueSource idValueSource) {

Assert.notEmpty(insertSubjects, "Batch insert must contain at least one InsertSubject");
SqlIdentifierParameterSource[] sqlParameterSources = insertSubjects.stream()
.map(insertSubject -> sqlParametersFactory.forInsert(insertSubject.getInstance(), domainType,
insertSubject.getIdentifier(), idValueSource))
.toArray(SqlIdentifierParameterSource[]::new);
.map(insertSubject -> sqlParametersFactory.forInsert(
insertSubject.getInstance(),
domainType,
insertSubject.getIdentifier(),
idValueSource
)
)
.toArray(SqlIdentifierParameterSource[]::new);

String insertSql = sql(domainType).getInsert(sqlParameterSources[0].getIdentifiers());

return insertStrategyFactory.batchInsertStrategy(idValueSource, getIdColumn(domainType)).execute(insertSql,
sqlParameterSources);
sqlParameterSources);
}

@Override
@Override
public <S> boolean update(S instance, Class<S> domainType) {

SqlIdentifierParameterSource parameterSource = sqlParametersFactory.forUpdate(instance, domainType);
Expand Down Expand Up @@ -445,5 +459,4 @@ private Class<?> getBaseType(PersistentPropertyPath<RelationalPersistentProperty

return baseProperty.getOwner().getType();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,4 @@ public Object[] execute(String sql, SqlParameterSource[] sqlParameterSources) {
return new Object[sqlParameterSources.length];
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
* @since 3.2
*/
public class MappingJdbcConverter extends MappingRelationalConverter implements JdbcConverter, ApplicationContextAware {


private static final Log LOG = LogFactory.getLog(MappingJdbcConverter.class);
private static final Converter<Iterable<?>, Map<?, ?>> ITERABLE_OF_ENTRY_TO_MAP_CONVERTER = new IterableOfEntryToMapConverter();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Set;

import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.util.Pair;
import org.springframework.jdbc.core.namedparam.AbstractSqlParameterSource;

/**
Expand All @@ -35,9 +36,11 @@
*/
class SqlIdentifierParameterSource extends AbstractSqlParameterSource {

private final Set<SqlIdentifier> identifiers = new HashSet<>();
private final Set<SqlIdentifier> sqlIdentifiers = new HashSet<>();
private final Map<String, Object> namesToValues = new HashMap<>();

private Pair<SqlIdentifier, Object> idToValue;

@Override
public boolean hasValue(String paramName) {
return namesToValues.containsKey(paramName);
Expand All @@ -54,30 +57,34 @@ public String[] getParameterNames() {
}

Set<SqlIdentifier> getIdentifiers() {
return Collections.unmodifiableSet(identifiers);
return Collections.unmodifiableSet(sqlIdentifiers);
}

void addValue(SqlIdentifier name, Object value) {
addValue(name, value, Integer.MIN_VALUE);
}

void addValue(SqlIdentifier identifier, Object value, int sqlType) {
void addValue(SqlIdentifier sqlIdentifier, Object value, int sqlType) {

identifiers.add(identifier);
String name = BindParameterNameSanitizer.sanitize(identifier.getReference());
sqlIdentifiers.add(sqlIdentifier);
String name = prepareSqlIdentifierName(sqlIdentifier);
namesToValues.put(name, value);
registerSqlType(name, sqlType);
}

void addAll(SqlIdentifierParameterSource others) {
void addAll(SqlIdentifierParameterSource others) {

for (SqlIdentifier identifier : others.getIdentifiers()) {

String name = BindParameterNameSanitizer.sanitize( identifier.getReference());
String name = prepareSqlIdentifierName(identifier);
addValue(identifier, others.getValue(name), others.getSqlType(name));
}
}

private static String prepareSqlIdentifierName(SqlIdentifier sqlIdentifier) {
return BindParameterNameSanitizer.sanitize(sqlIdentifier.getReference());
}

int size() {
return namesToValues.size();
}
Expand Down
Loading

0 comments on commit d500fed

Please sign in to comment.