EN · DE · RU · FR · ES

#1879: DatabaseDialect.kt

projectforge-common/src/main/java/org/projectforge/common/DatabaseDialect.kt Kotlin enum — org.projectforge.common package, projectforge-common/src/main/java/org/projectforge/common/DatabaseDialect.kt 55 lines · 24 code · 27 comments · 4 blank
Kotlin enum defining supported database dialects for Hibernate ORM and Flyway migration tooling. Maps database types to their Hibernate dialect class names and Flyway vendor directory names. Currently supports PostgreSQL (org.hibernate.dialect.PostgreSQLDialect) and HSQLDB (org.hibernate.dialect.HSQLDialect). Acts as the single source of truth for database type configuration across the application bootstrap, Flyway migration script resolution, and Hibernate session factory setup.

Architecture

Kotlin Feature Usage

Enum Constants

ConstantHibernate Dialect ClassFlyway VendorUsage Context
PostgreSQLorg.hibernate.dialect.PostgreSQLDialectpostgresqlProduction deployments — the recommended production database
HSQLorg.hibernate.dialect.HSQLDialecthsqldbDevelopment/testing, embedded setup — used by the setup wizard for single-user/local installations

Companion Object Methods

fromString(asString: String): DatabaseDialect?

Looks up a dialect by its Hibernate class name. Returns null if no match is found — callers must handle the null case (typically by defaulting to HSQL for development). The method uses a simple if-else chain rather than a lookup map, appropriate for the small number of supported dialects.

getFlywayVendorName(databaseProductName: String): String

Maps a JDBC driver product name to the Flyway vendor directory name. This is critical for Flyway database migration script discovery. Flyway organizes migration scripts in vendor-specific directories:

flyway/{module}/migrate/postgresql/V1.1.0__RELEASE-RemoveMGC.sql
flyway/{module}/migrate/hsqldb/V1.1.0__RELEASE-RemoveMGC.sql
The method checks if the product name contains "hsql" (case-insensitive) and returns "hsqldb" or "postgresql" accordingly. This allows the same migration logic to work across database types — vendor-specific SQL differences (e.g., column type syntax, sequence handling) are isolated in the vendor-specific migration scripts.

Historical Context: Commented-Out Databases

The file contains a comment listing previously considered or planned database support:

// Not yet supported:
// MYSQL, ORACLE, MS_SQL_SERVER, DB2, INFORMIX, DERBY, UNKOWN;

This reveals the design intent to support multiple databases — a common requirement for enterprise software. The current implementation supports only PostgreSQL (for production) and HSQLDB (for embedded/development), with the architecture ready for extension.

Database Architecture in ProjectForge

The DatabaseDialect enum is part of a layered database abstraction:

  1. Hibernate ORM: Object-relational mapping — the asString property provides the Hibernate dialect class for hibernate.cfg.xml or Spring Boot auto-configuration
  2. Flyway: Database migration management — the getFlywayVendorName() method routes to vendor-specific SQL migration scripts
  3. Setup Wizard: The ProjectForge setup wizard (both Swing and Lanterna GUIs) uses this enum to present database choices to administrators and configure JDBC connections

Design Decision: Why Kotlin?

This file is one of the earlier Kotlin files in the projectforge-common module (alongside other utility classes). Using Kotlin for this enum provides:

The getFlywayVendorName() method uses a simple substring match (contains("hsql")) rather than an exact comparison. This could produce false positives if a database product name coincidentally contains "hsql" as a substring. A more robust approach would be to match against an exact list of known product names.
The Flyway migration path pattern uses separate directories for init/common (schema creation) and migrate/{vendor} (version-specific migrations). This separation allows the base schema to be database-agnostic while vendor-specific migrations handle differences in SQL syntax or features (such as PostgreSQL vs HSQLDB for sequence handling or the "RemoveMGC" migration visible in the project's flyway directories).

Git History

868d6abb7 2025 -> 2026
63081666f Source file headers: 2024-> 2025.
b6092df09 Copyright 2023 -> 2024
ab45d51fa Copyright 2001-2022 -> 2001-2023.
5f7ef41b8 Copyright 2021 -> 2022
637063dd7 Nothing (source file header fixed)
440d191a2 Setup page: activation of standard plugins. AbstractPlugin: Fix of vendor name for flyway.