Skip to content

Commit

Permalink
Allow setting additional Hibernate quoting options
Browse files Browse the repository at this point in the history
  • Loading branch information
brunolmfg committed Jan 5, 2023
1 parent 457333c commit a679264
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,23 @@ public static class HibernateOrmConfigPersistenceUnitDatabase {
@ConfigItem
public boolean globallyQuotedIdentifiers;

/**
* Whether Hibernate should skip column definitions when global quoting is enabled.
*/
@ConfigItem
public boolean globallyQuotedIdentifiersSkipColumnDefinitions;

/**
* Whether Hibernate should quote any names that are deemed keywords.
*/
@ConfigItem
public boolean autoQuoteKeyword;

public boolean isAnyPropertySet() {
return !DEFAULT_CHARSET.equals(charset.name())
|| globallyQuotedIdentifiers;
|| globallyQuotedIdentifiers
|| globallyQuotedIdentifiersSkipColumnDefinitions
|| autoQuoteKeyword;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1047,10 +1047,20 @@ private static void producePersistenceUnitDescriptorFromConfig(
descriptor.getProperties().setProperty(AvailableSettings.HBM2DDL_CHARSET_NAME,
persistenceUnitConfig.database.charset.name());

// Quoting options
if (persistenceUnitConfig.database.globallyQuotedIdentifiers) {
descriptor.getProperties().setProperty(AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, "true");
}

if (persistenceUnitConfig.database.globallyQuotedIdentifiersSkipColumnDefinitions) {
descriptor.getProperties().setProperty(AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS_SKIP_COLUMN_DEFINITIONS,
"true");
}

if (persistenceUnitConfig.database.autoQuoteKeyword) {
descriptor.getProperties().setProperty(AvailableSettings.KEYWORD_AUTO_QUOTING_ENABLED, "true");
}

// Query
int batchSize = firstPresent(persistenceUnitConfig.fetch.batchSize, persistenceUnitConfig.batchFetchSize)
.orElse(16);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.hibernate.orm.quoting_options;

import static org.hamcrest.core.StringContains.containsString;

import org.junit.jupiter.api.Test;

import io.restassured.RestAssured;

public abstract class AbstractJPAQuotedTest {

@Test
public void testQuotedIdentifiers() {
RestAssured.when().post("/jpa-test-quoted").then()
.body(containsString("ok"));

RestAssured.when().get("/jpa-test-quoted").then()
.body(containsString("group_name"), containsString("group_value"));
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package io.quarkus.hibernate.orm.quoted_indentifiers;
package io.quarkus.hibernate.orm.quoting_options;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
* Table with reserved name.
* <p>
* http://www.h2database.com/html/advanced.html section "Keywords / Reserved Words".
* Table with reserved name and containing one column with reserved name and column definition.
*
* @see <a href="http://www.h2database.com/html/advanced.html#keywords">H2 Documentation, section
* "Keywords / Reserved Words"</a>
*/
@Entity
@Table(name = "group")
Expand All @@ -19,6 +21,8 @@ public class Group {

private String name;

private String value;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "groupSeq")
public Long getId() {
Expand All @@ -36,4 +40,13 @@ public String getName() {
public void setName(String name) {
this.name = name;
}

@Column(columnDefinition = "varchar(255)")
public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.hibernate.orm.quoting_options;

import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusDevModeTest;

/**
* Failed to fetch entity with reserved name and containing one column with reserved name and column definition.
* <p>
* To resolve the simulated situations, this test activates the options {@code globally-quoted-identifiers} and
* {@code globally-quoted-identifiers-skip-column-definitions}.
*/
public class JPAQuotedIdentifiersTest extends AbstractJPAQuotedTest {

@RegisterExtension
final static QuarkusDevModeTest TEST = new QuarkusDevModeTest()
.withApplicationRoot((jar) -> jar
.addClasses(Group.class, QuotedResource.class)
.addAsResource("application-quoted-identifiers.properties", "application.properties"));

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.hibernate.orm.quoting_options;

import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusDevModeTest;

/**
* Failed to fetch entity with reserved name and containing one column with reserved name and column definition.
* <p>
* To resolve the simulated situations, this test activates the option {@code auto-quote-keyword}.
*/
public class JPAQuotedKeywordsTest extends AbstractJPAQuotedTest {

@RegisterExtension
final static QuarkusDevModeTest TEST = new QuarkusDevModeTest()
.withApplicationRoot((jar) -> jar
.addClasses(Group.class, QuotedResource.class)
.addAsResource("application-quoted-keywords.properties", "application.properties"));

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkus.hibernate.orm.quoted_indentifiers;
package io.quarkus.hibernate.orm.quoting_options;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
Expand All @@ -11,7 +11,7 @@
import javax.ws.rs.core.MediaType;

/**
* Try to fetch entity with reserved name.
* Try to fetch entity with reserved name and containing one column with reserved name and column definition.
*/
@Path("/jpa-test-quoted")
@ApplicationScoped
Expand All @@ -27,6 +27,7 @@ public String create() {
Group group = new Group();
group.setId(1L);
group.setName("group_name");
group.setValue("group_value");
em.merge(group);
return "ok";
}
Expand All @@ -36,7 +37,8 @@ public String create() {
@Transactional
public String selectWithQuotedEntity() {
try {
return em.find(Group.class, 1L).getName();
var group = em.find(Group.class, 1L);
return group.getName() + " " + group.getValue();
} catch (Exception e) {
return "Unable to fetch group.";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ quarkus.datasource.jdbc.url=jdbc:h2:mem:test
quarkus.hibernate-orm.database.generation=drop-and-create

quarkus.hibernate-orm.database.globally-quoted-identifiers=true
quarkus.hibernate-orm.database.globally-quoted-identifiers-skip-column-definitions=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
quarkus.datasource.db-kind=h2
quarkus.datasource.jdbc.url=jdbc:h2:mem:test

#quarkus.hibernate-orm.log.sql=true
quarkus.hibernate-orm.database.generation=drop-and-create

quarkus.hibernate-orm.database.auto-quote-keyword=true
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,19 @@ private static ParsedPersistenceXmlDescriptor generateReactivePersistenceUnit(
desc.getProperties().setProperty(AvailableSettings.HBM2DDL_CHARSET_NAME,
persistenceUnitConfig.database.charset.name());

// Quoting options
if (persistenceUnitConfig.database.globallyQuotedIdentifiers) {
desc.getProperties().setProperty(AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, "true");
}

if (persistenceUnitConfig.database.globallyQuotedIdentifiersSkipColumnDefinitions) {
desc.getProperties().setProperty(AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS_SKIP_COLUMN_DEFINITIONS, "true");
}

if (persistenceUnitConfig.database.autoQuoteKeyword) {
desc.getProperties().setProperty(AvailableSettings.KEYWORD_AUTO_QUOTING_ENABLED, "true");
}

// Query
// TODO ideally we should align on ORM and use 16 as a default, but that would break applications
// because of https://github.com/hibernate/hibernate-reactive/issues/742
Expand Down

0 comments on commit a679264

Please sign in to comment.