Search code examples
jooq

Configure the org.jooq.conf.Settings.backslashEscaping property


In the book "Beginning jOOQ" by Tayo Koleoso it is written:

Caution For your own peace of mind, go ahead and configure the
org.jooq.conf.Settings.backslashEscaping property on
your Settings object. MySQL and some versions of PostgreSQL
support non-standard escape characters that can cause you a lot of
grief when you least expect it. This property lets jOOQ properly handle
this “feature” from MySQL.

The problem is that to the best of my ability I can't find in the book how toconfigure this.

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>jooq</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jooq</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jooq</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties

spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/testdb
spring.datasource.username=testdb
spring.datasource.password=testdb
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

Controller

@Controller
@RequestMapping("/start")
public class StartController {
    @GetMapping("/")
    public void start() {

        try (Connection connection = DriverManager.
                getConnection("jdbc:mysql://localhost/test?user=testdb&password=testdb")) {
            DSLContext context = DSL.using(connection, SQLDialect.MYSQL);


            ResultQuery resultQuery = context.
                        resultQuery("SELECT * FROM edens_car.complete_car_listing");
            List<CompleteVehicleRecord> allVehicles =
            resultQuery.fetchInto(CompleteVehicleRecord.class);

        } catch (SQLException sqlex) {
            assert true;
        }
    }
}

I just organised the Spring Controller for my convenience, don't pay attention to it.


Solution

  • Adapting your example code

    You can pass custom Settings to one of the DSL.using() overloads, specifically:

    DSLContext context = DSL.using(connection, SQLDialect.MYSQL, settings);
    

    Noting that these methods are just convenience for creating your own DefaultConfiguration

    A Spring Boot DefaultConfigurationCustomizer

    Alternatively, if you're using Spring Boot, then this article shows how to use a DefaultConfigurationCustomizer, e.g.:

    @Bean
    public DefaultConfigurationCustomizer configurationCustomiser() {
        return (DefaultConfiguration c) -> c.settings()
            .withBackslashEscaping(...);
    }