Skip to content

Commit

Permalink
Support VIEW INDEX
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillKurdyukov committed Aug 23, 2024
1 parent 3715208 commit f721d07
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.ydb.data.core.dialect;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
import org.springframework.data.relational.core.dialect.AbstractDialect;
Expand Down Expand Up @@ -53,7 +54,12 @@ public LockClause.Position getClausePosition() {

@Override
protected Function<Select, CharSequence> getAfterFromTable() {
final var invokeMethod = new AtomicBoolean();
return select -> {
if (invokeMethod.get()) { // @MappedCollection without VIEW INDEX!
return "";
}

var tables = select.getFrom().getTables();
if (tables.size() != 1) {
return "";
Expand All @@ -63,8 +69,10 @@ protected Function<Select, CharSequence> getAfterFromTable() {
.getMethod()
.getAnnotation(ViewIndex.class);

return viewIndex != null ?
"VIEW " + viewIndex.name() + " AS " + tables.get(0).getReferenceName() : "";
invokeMethod.set(true);

return viewIndex != null ? " VIEW " + viewIndex.value() + " AS " + tables.get(0).getReferenceName()
.toSql(INSTANCE.getIdentifierProcessing()) : "";
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package tech.ydb.data.repository;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author Kirill Kurdyukov
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ViewIndex {

String name();
String value() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.Import;
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
import tech.ydb.data.repository.support.SimpleYdbJdbcRepository;

/**
* @author Madiyar Nurgazin
* @author Kirill Kurdyukov
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(AbstractYdbJdbcConfiguration.class)
@EnableJdbcRepositories(repositoryBaseClass = SimpleYdbJdbcRepository.class)
public @interface EnableYdbRepositories {
public @interface EnableYdbJdbcRepositories {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import tech.ydb.data.repository.config.AbstractYdbJdbcConfiguration;
import tech.ydb.data.repository.config.EnableYdbRepositories;
import tech.ydb.data.repository.config.EnableYdbJdbcRepositories;

/**
* @author Madiyar Nurgazin
*/
@Configuration
@EnableYdbRepositories
@EnableYdbJdbcRepositories
@EnableJdbcAuditing
public class YdbJdbcConfiguration extends AbstractYdbJdbcConfiguration {
@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public void crudTest() {
book = createBook(3, "Title", "Isbn", 2024, Set.of(), Set.of(new BookAuthor(2, 3), new BookAuthor(3, 3)));
bookRepository.insert(book);


expected.get(0).setReviews(Set.of(review1, review2, review3));
books = bookRepository.findBooksByAuthorName("Leo Tolstoy");
Assertions.assertEquals(expected, books);
Expand All @@ -103,6 +102,8 @@ public void crudTest() {
Optional<Author> author = authorRepository.findById(author2.getId());
Assertions.assertTrue(author.isPresent());
Assertions.assertTrue(author.get().getBooks().isEmpty());

Assertions.assertEquals(expected.get(0), bookRepository.findBookByIsbn("1234").get(0));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.util.List;
import java.util.Optional;

import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.query.Param;
import tech.ydb.data.books.entity.Book;
import tech.ydb.data.repository.ViewIndex;
import tech.ydb.data.repository.YdbCrudRepository;

/**
Expand All @@ -16,5 +16,8 @@ public interface BookRepository extends YdbCrudRepository<Book, Long> {
" join authors on authors.id = books_authors.author_id where name = :author")
List<Book> findBooksByAuthorName(@Param("author") String author);

@ViewIndex("isbn_books_index")
List<Book> findBookByIsbn(String isbn);

Optional<Book> findBookByTitle(String title);
}
6 changes: 6 additions & 0 deletions spring-data-dialect/src/test/resources/changelogs/books.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ databaseChangeLog:
- column:
name: year
type: bigint
- createIndex:
indexName: isbn_books_index
tableName: books
columns:
- column:
name: isbn
- insert:
tableName: books
columns:
Expand Down

0 comments on commit f721d07

Please sign in to comment.