The compiler module (org.sqlcomponents:compiler) turns a populated Application (from core) into Java source files: a DataManager, per-entity Store classes, Record (or similar) model types, and enum-style artifacts for certain database types. It implements org.sqlcomponents.core.compiler.Compiler via JavaCompiler.
For how metadata reaches Application, see Core module. For where output is written in this repo’s workflow, see Datastore module.
At a high level, JavaCompiler:
- Optional Flyway migrations — If Flyway is on the classpath and
src/main/resources/db/migrationexists relative to the process working directory, runs Flyway against a datasource built fromApplication, then closes the datasource. This is best-effort and environment-specific; see source for details. - Map relational model to ORM view — Constructs
JavaMapper, which extends core’sMapper, and assigns the resultingORMonApplication. - Emit Java — Uses FreeMarker via
FTLTemplateto render:- Manager —
Manager.ftl→DataManager.javaunder the root package folder. - Store —
Store.ftl→{Entity}Store.javaunder each entity’s DAO package. - Model / record —
Record.ftl→{Entity}.javaunder the bean package. - Enum / type — For entities whose type is an enumerated DB type,
Enum.ftl→{Entity}Type.java.
- Manager —
Entity processing uses a parallel stream over orm.getEntities() for throughput.
Application.compile(Compiler) (in core) deletes the entire srcFolder tree before calling Compiler.compile, so every run is a clean generation.
JavaMapper is responsible for mapping SQL column semantics (via core’s Column / ColumnType) to Java types—for example primitives, String, UUID, BigDecimal, JTS geometry types, JSON node types, and time API classes. The mapper feeds the templates so generated fields and method signatures match the database.
Templates live under compiler/src/main/resources/template/java/. FTLTemplate loads them from the classpath relative to JavaCompiler.
Notable groups:
| Subdirectory / file | Purpose |
|---|---|
Manager.ftl, Store.ftl, Record.ftl, Enum.ftl |
Top-level artifacts per entity or application. |
column/*.ftl |
Fragments for SQL/expression handling per Java column kind (e.g. BooleanColumn.ftl, UUIDColumn.ftl, geometry columns). |
method/*.ftl |
Statement builders: SelectStatement, InsertStatement, UpdateStatement, DeleteStatement, MViewRefresh. |
query/*.ftl, clause/*.ftl |
Query and WHERE composition. |
base.ftl, jdbcbase.ftl, SqlBuilder.ftl, … |
Shared includes. |
template/directive/ |
Custom FreeMarker directives (e.g. column selection). |
Generated package-info.java files are also written with a minimal package …; declaration for each output package.
- FreeMarker — Template engine.
- JTS Core — Geometry types referenced by
JavaMapper/ templates for spatial columns. - Flyway (
provided/ optional usage) — Migration hook described above. - core — Same-version
org.sqlcomponents:core.
Drivers and pooling come from the parent POM when running in tests or integrated apps.
CompilerTestUtil.getApplication() builds an Application when SQLCOMPONENTS_CONFIG is not set:
- Reads
database.propertiesfrom the current working directory or parent (../database.properties). - Uses
DATABASE_TYPEenv var or defaults topostgresfor property key prefix (postgres.datasource.url, …). - Sets
rootPackage, encryption lists, insert/update maps, andsrcFoldertodatastore/src/main/java(or../datastore/src/main/javadepending on cwd).
Then:
Application application = CompilerTestUtil.getApplication();
application.compile(new JavaCompiler());Run from Maven with the working directory that matches your paths (often compiler/ for tests).
When SQLCOMPONENTS_CONFIG points to a YAML file:
Applicationis built withCoreConsts.buildApplication(File).SOURCE_FOLDERmust be set to the output directory for generated sources (throws if missing).
The compiler module is the reference implementation of:
void compile(Application application) throws SQLException;Other implementations could target different languages or layouts as long as they accept Application.