Skip to content

GH-174 #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spring-data-jdbc-ydb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

<junit5.version>5.10.2</junit5.version>
<lombok.version>1.18.30</lombok.version>
<spring.version>3.2.1</spring.version>
<spring.version>3.4.0</spring.version>
<liquibase.version>4.24.0</liquibase.version>

<ydb.sdk.version>2.2.9</ydb.sdk.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tech.ydb.data.core.dialect;

import java.lang.reflect.Method;
import java.util.function.Function;

import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
import org.springframework.data.relational.core.dialect.AbstractDialect;
import org.springframework.data.relational.core.dialect.InsertRenderContext;
Expand All @@ -10,10 +12,12 @@
import org.springframework.data.relational.core.sql.IdentifierProcessing;
import org.springframework.data.relational.core.sql.LockOptions;
import org.springframework.data.relational.core.sql.Select;

import tech.ydb.data.repository.ViewIndex;

/**
* @author Madiyar Nurgazin
* @author Mikhail Polivakha
*/
public class YdbDialect extends AbstractDialect {
public static final YdbDialect INSTANCE = new YdbDialect();
Expand Down Expand Up @@ -55,13 +59,23 @@ public LockClause.Position getClausePosition() {
protected Function<Select, CharSequence> getAfterFromTable() {
return select -> {
var tables = select.getFrom().getTables();

if (tables.size() != 1) {
return "";
}

var viewIndex = ExposeInvocationInterceptor.currentInvocation()
.getMethod()
.getAnnotation(ViewIndex.class);
Method repositoryMethod = null;
try {
repositoryMethod = ExposeInvocationInterceptor.currentInvocation().getMethod();
} catch (IllegalStateException e) {
// the assumption is that JdbcRepositoryBeanPostProcessor made a choice to not expose metadata for this Spring Data JDBC repository
}

if (repositoryMethod == null) {
return "";
}

var viewIndex = repositoryMethod.getAnnotation(ViewIndex.class);

if (viewIndex != null && (viewIndex.tableName().isEmpty() ||
viewIndex.tableName().equals(tables.get(0).getName().toSql(IdentifierProcessing.NONE)))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.ydb.data.repository.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.jdbc.core.convert.DefaultJdbcTypeFactory;
import org.springframework.data.jdbc.core.convert.JdbcArrayColumns;
Expand All @@ -15,9 +16,12 @@

/**
* @author Madiyar Nurgazin
* @author Mikhail Polivakha
*/
@Configuration
@Import(JdbcRepositoryBeanPostProcessor.class)
public class AbstractYdbJdbcConfiguration extends AbstractJdbcConfiguration {

@Override
public JdbcConverter jdbcConverter(
JdbcMappingContext mappingContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package tech.ydb.data.repository.config;

import java.util.Arrays;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.util.ReflectionUtils;

import tech.ydb.data.repository.ViewIndex;

/**
* Enabling the exposure of the metadata for the {@link JdbcRepositoryFactoryBean}. Enables
* only for those {@link RepositoryFactoryBeanSupport factory beans} that have any {@link ViewIndex}
* annotated methods.
*
* @author Mikhail Polivakha
*/
@SuppressWarnings("rawtypes")
public class JdbcRepositoryBeanPostProcessor implements BeanPostProcessor {

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof JdbcRepositoryFactoryBean rfbs && hasAnyViewIndexMethods(rfbs)) {
rfbs.setExposeMetadata(true);
}
return bean;
}

/**
* Unfortunately, {@link JdbcRepositoryFactoryBean#getRepositoryInformation()} call is not possible at this stage, since
* {@link RepositoryFactoryBeanSupport#factory} is not initialized at this point yet. Still, we have
* to use {@link BeanPostProcessor#postProcessBeforeInitialization(Object, String)} since the
* {@link RepositoryFactoryBeanSupport#setExposeMetadata(boolean) expose metadata} call needs to be done before the {@link InitializingBean#afterPropertiesSet()}
* to be propagated into the underlying {@link org.springframework.data.repository.core.support.RepositoryFactorySupport factory support}
*/
private static boolean hasAnyViewIndexMethods(JdbcRepositoryFactoryBean rfbs) {
return Arrays
.stream(ReflectionUtils.getAllDeclaredMethods(rfbs.getObjectType()))
.anyMatch(method -> AnnotationUtils.getAnnotation(method, ViewIndex.class) != null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@

/**
* @author Madiyar Nurgazin
* @author Mikhail Polivakha
*/
@Configuration
@EnableJdbcRepositories
@EnableJdbcRepositories(
considerNestedRepositories = true,
basePackages = "tech.ydb.data"
)
@EnableJdbcAuditing
@Import(AbstractYdbJdbcConfiguration.class)
public class YdbJdbcConfiguration {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package tech.ydb.data.repository.config;

import java.lang.reflect.Field;
import java.util.Optional;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.util.ReflectionUtils;

import tech.ydb.data.YdbBaseTest;
import tech.ydb.data.repository.ViewIndex;

/**
* Tests for {@link JdbcRepositoryBeanPostProcessor}.
*
* @author Mikhail Polivakha
*/
class JdbcRepositoryBeanPostProcessorTest extends YdbBaseTest {

@Autowired
private RepositoryFactoryBeanSupport<UserRepository, User, Long> userFactoryBeanSupport;

@Autowired
private RepositoryFactoryBeanSupport<AddressRepository, Address, Long> addressFactoryBeanSupport;

@Test
void shouldExposeMetadataOnlyForRepositoriesWithViewIndexMethods() {

// given.
Field exposeMetadataField = ReflectionUtils.findField(RepositoryFactoryBeanSupport.class, "exposeMetadata");
exposeMetadataField.setAccessible(true);

// when.
Object userFactoryExposedMetadata = ReflectionUtils.getField(exposeMetadataField, userFactoryBeanSupport);
Object addressFactoryExposedMetadata = ReflectionUtils.getField(exposeMetadataField, addressFactoryBeanSupport);

// then.
Assertions.assertThat(userFactoryExposedMetadata).isEqualTo(true);
Assertions.assertThat(addressFactoryExposedMetadata).isEqualTo(false);
}

@Table
static class User {

@Id
private Long id;

private String name;
}

@Table
static class Address {

@Id
private Long id;
}

interface UserRepository extends CrudRepository<User, Long> {

@ViewIndex(indexName = "name_authors_index", tableName = "authors")
Optional<User> findUserByName(String name);
}

interface AddressRepository extends CrudRepository<Address, Long> {

}
}