Skip to content

Commit

Permalink
JDBC / R2DBC repositories should require to set the dialect (#2288)
Browse files Browse the repository at this point in the history
* JDBC / R2DBC repositories should require to set the dialect

* Correct tests
  • Loading branch information
dstepanov authored Jun 13, 2023
1 parent 3196b95 commit 8813351
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@
* @return The dialect to use.
*/
@AliasFor(annotation = Repository.class, member = "dialect")
Dialect dialect() default Dialect.ANSI;
Dialect dialect();

/**
* @return The dialect to use.
*/
@AliasFor(annotation = Repository.class, member = "dialect")
@AliasFor(annotation = JdbcRepository.class, member = "dialect")
String dialectName() default "ANSI";
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
import io.micronaut.data.annotation.Query;
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.jdbc.mapper.SqlResultConsumer;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.tck.entities.Author;
import io.micronaut.data.tck.entities.AuthorDTO;
import io.micronaut.data.tck.entities.Book;

@JdbcRepository(dialectName = "H2")
@JdbcRepository(dialect = Dialect.H2)
public interface H2AuthorRepository extends io.micronaut.data.tck.repositories.AuthorRepository {

@Query("select *, author.name as author_name, author.nick_name as author_nick_name from book as book inner join author as author where book.title = :title and book.total_pages > :pages")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/
package io.micronaut.data.jdbc.h2;

import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.tck.repositories.CompanyRepository;

import io.micronaut.data.jdbc.annotation.JdbcRepository;

@JdbcRepository(dialectName = "H2")
@JdbcRepository(dialect = Dialect.H2)
public interface H2CompanyRepository extends CompanyRepository {
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@

import java.util.EnumSet;

import static io.micronaut.data.annotation.Join.Type.*;
import static io.micronaut.data.annotation.Join.Type.ALL_TYPES;
import static io.micronaut.data.annotation.Join.Type.DEFAULT;
import static io.micronaut.data.annotation.Join.Type.FETCH;
import static io.micronaut.data.annotation.Join.Type.INNER;
import static io.micronaut.data.annotation.Join.Type.LEFT;
import static io.micronaut.data.annotation.Join.Type.LEFT_FETCH;
import static io.micronaut.data.annotation.Join.Type.RIGHT;
import static io.micronaut.data.annotation.Join.Type.RIGHT_FETCH;

/**
* The SQL dialect to use.
Expand All @@ -33,7 +40,7 @@ public enum Dialect {
/**
* H2 database.
*/
H2(true, false, true,
H2(true, false,
EnumSet.of(
DEFAULT,
LEFT,
Expand All @@ -46,7 +53,7 @@ public enum Dialect {
/**
* MySQL 5.5 or above.
*/
MYSQL(true, true, false, EnumSet.of(
MYSQL(true, true, EnumSet.of(
DEFAULT,
LEFT,
LEFT_FETCH,
Expand All @@ -58,74 +65,65 @@ public enum Dialect {
/**
* Postgres 9.5 or later.
*/
POSTGRES(true, false, true, ALL_TYPES),
POSTGRES(true, false, ALL_TYPES),
/**
* SQL server 2012 or above.
*/
SQL_SERVER(false, false, false, ALL_TYPES),
SQL_SERVER(false, false, ALL_TYPES),
/**
* Oracle 12c or above.
*/
ORACLE(true, true, false, ALL_TYPES, true),
ORACLE(true, true, ALL_TYPES, true),
/**
* Ansi compliant SQL.
*/
ANSI(true, false, true, ALL_TYPES);
ANSI(true, false, ALL_TYPES);

private final boolean supportsBatch;
private final boolean stringUUID;
private final boolean supportsArrays;

private final boolean supportsJsonEntity;

private final EnumSet<Join.Type> joinTypesSupported;

/**
* Allows customization of batch support.
* @param supportsBatch If batch is supported
* @param stringUUID Does the dialect require a string UUID
* @param supportsArrays Does the dialect supports arrays
*
* @param supportsBatch If batch is supported
* @param stringUUID Does the dialect require a string UUID
* @param joinTypesSupported EnumSet of supported join types.
*/
Dialect(boolean supportsBatch, boolean stringUUID, boolean supportsArrays, EnumSet<Join.Type> joinTypesSupported) {
this(supportsBatch, stringUUID, supportsArrays, joinTypesSupported, false);
Dialect(boolean supportsBatch, boolean stringUUID, EnumSet<Join.Type> joinTypesSupported) {
this(supportsBatch, stringUUID, joinTypesSupported, false);
}

/**
* The constructor with all parameters.
*
* @param supportsBatch If batch is supported
* @param stringUUID Does the dialect require a string UUID
* @param supportsArrays Does the dialect supports arrays
* @param supportsBatch If batch is supported
* @param stringUUID Does the dialect require a string UUID
* @param joinTypesSupported EnumSet of supported join types.
* @param supportsJsonEntity Whether JSON entity is supported
*/
Dialect(boolean supportsBatch, boolean stringUUID, boolean supportsArrays, EnumSet<Join.Type> joinTypesSupported, boolean supportsJsonEntity) {
Dialect(boolean supportsBatch, boolean stringUUID, EnumSet<Join.Type> joinTypesSupported, boolean supportsJsonEntity) {
this.supportsBatch = supportsBatch;
this.stringUUID = stringUUID;
this.supportsArrays = supportsArrays;
this.joinTypesSupported = joinTypesSupported;
this.supportsJsonEntity = supportsJsonEntity;
}

/**
* Some drivers and dialects do not support JDBC batching. This allows customization.
*
* @return True if batch is supported.
*/
public final boolean allowBatch() {
return supportsBatch;
}

/**
* Some databases support arrays and the use of {@link java.sql.Connection#createArrayOf(String, Object[])}.
* @return True if arrays are supported.
*/
public final boolean supportsArrays() {
return supportsArrays;
}

/**
* Returns compatible dialect dataype.
*
* @param type the type
* @return The dialect compatible DataType
* @since 2.0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ interface LikeRepository extends CrudRepository<Like, LikeId> {
void "test jdbc compile embedded id count query"() {
given:
def repository = buildRepository('test.LikeRepository', """
import io.micronaut.data.model.query.builder.sql.SqlQueryBuilder;
import io.micronaut.data.model.query.builder.sql.Dialect;import io.micronaut.data.model.query.builder.sql.SqlQueryBuilder;
@javax.persistence.Entity
@javax.persistence.Table(name = "likes")
Expand Down Expand Up @@ -291,7 +291,7 @@ class LikeId {
}
}
@io.micronaut.data.jdbc.annotation.JdbcRepository
@io.micronaut.data.jdbc.annotation.JdbcRepository(dialect = Dialect.ANSI)
@io.micronaut.context.annotation.Executable
interface LikeRepository extends CrudRepository<Like, LikeId> {
long countByLikeIdImageIdentifier(UUID likeIdImageIdentifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,5 @@
* @return The dialect to use.
*/
@AliasFor(annotation = Repository.class, member = "dialect")
Dialect dialect() default Dialect.ANSI;

/**
* @return The dialect to use.
*/
@AliasFor(annotation = Repository.class, member = "dialect")
@AliasFor(member = "dialect")
String dialectName() default "ANSI";
Dialect dialect();
}

0 comments on commit 8813351

Please sign in to comment.