|
| 1 | +# Compiler module |
| 2 | + |
| 3 | +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`](../compiler/src/main/java/org/sqlcomponents/compiler/java/JavaCompiler.java). |
| 4 | + |
| 5 | +For how metadata reaches `Application`, see [Core module](core.md). For where output is written in this repo’s workflow, see [Datastore module](datastore.md). |
| 6 | + |
| 7 | +## End-to-end pipeline (`JavaCompiler.compile`) |
| 8 | + |
| 9 | +At a high level, `JavaCompiler`: |
| 10 | + |
| 11 | +1. **Optional Flyway migrations** — If Flyway is on the classpath and `src/main/resources/db/migration` exists relative to the process working directory, runs Flyway against a datasource built from `Application`, then closes the datasource. This is best-effort and environment-specific; see source for details. |
| 12 | +2. **Map relational model to ORM view** — Constructs [`JavaMapper`](../compiler/src/main/java/org/sqlcomponents/compiler/mapper/JavaMapper.java), which extends core’s [`Mapper`](../core/src/main/java/org/sqlcomponents/core/mapper/Mapper.java), and assigns the resulting `ORM` on `Application`. |
| 13 | +3. **Emit Java** — Uses FreeMarker via [`FTLTemplate`](../compiler/src/main/java/org/sqlcomponents/compiler/template/FTLTemplate.java) to render: |
| 14 | + - **Manager** — [`Manager.ftl`](../compiler/src/main/resources/template/java/Manager.ftl) → `DataManager.java` under the root package folder. |
| 15 | + - **Store** — [`Store.ftl`](../compiler/src/main/resources/template/java/Store.ftl) → `{Entity}Store.java` under each entity’s DAO package. |
| 16 | + - **Model / record** — [`Record.ftl`](../compiler/src/main/resources/template/java/Record.ftl) → `{Entity}.java` under the bean package. |
| 17 | + - **Enum / type** — For entities whose type is an enumerated DB type, [`Enum.ftl`](../compiler/src/main/resources/template/java/Enum.ftl) → `{Entity}Type.java`. |
| 18 | + |
| 19 | +Entity processing uses a **parallel stream** over `orm.getEntities()` for throughput. |
| 20 | + |
| 21 | +`Application.compile(Compiler)` (in core) **deletes the entire `srcFolder` tree** before calling `Compiler.compile`, so every run is a clean generation. |
| 22 | + |
| 23 | +## `JavaMapper` |
| 24 | + |
| 25 | +[`JavaMapper`](../compiler/src/main/java/org/sqlcomponents/compiler/mapper/JavaMapper.java) 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. |
| 26 | + |
| 27 | +## FreeMarker templates |
| 28 | + |
| 29 | +Templates live under [`compiler/src/main/resources/template/java/`](../compiler/src/main/resources/template/java/). `FTLTemplate` loads them from the classpath relative to `JavaCompiler`. |
| 30 | + |
| 31 | +Notable groups: |
| 32 | + |
| 33 | +| Subdirectory / file | Purpose | |
| 34 | +|---------------------|-----------| |
| 35 | +| `Manager.ftl`, `Store.ftl`, `Record.ftl`, `Enum.ftl` | Top-level artifacts per entity or application. | |
| 36 | +| `column/*.ftl` | Fragments for SQL/expression handling per Java column kind (e.g. `BooleanColumn.ftl`, `UUIDColumn.ftl`, geometry columns). | |
| 37 | +| `method/*.ftl` | Statement builders: `SelectStatement`, `InsertStatement`, `UpdateStatement`, `DeleteStatement`, `MViewRefresh`. | |
| 38 | +| `query/*.ftl`, `clause/*.ftl` | Query and WHERE composition. | |
| 39 | +| `base.ftl`, `jdbcbase.ftl`, `SqlBuilder.ftl`, … | Shared includes. | |
| 40 | +| `template/directive/` | Custom FreeMarker directives (e.g. column selection). | |
| 41 | + |
| 42 | +Generated `package-info.java` files are also written with a minimal `package …;` declaration for each output package. |
| 43 | + |
| 44 | +## Dependencies (`compiler/pom.xml`) |
| 45 | + |
| 46 | +- **FreeMarker** — Template engine. |
| 47 | +- **JTS Core** — Geometry types referenced by `JavaMapper` / templates for spatial columns. |
| 48 | +- **Flyway** (`provided` / optional usage) — Migration hook described above. |
| 49 | +- **core** — Same-version `org.sqlcomponents:core`. |
| 50 | + |
| 51 | +Drivers and pooling come from the **parent** POM when running in tests or integrated apps. |
| 52 | + |
| 53 | +## How to run code generation in practice |
| 54 | + |
| 55 | +### Properties-based setup (tests / local dev) |
| 56 | + |
| 57 | +[`CompilerTestUtil.getApplication()`](../compiler/src/test/java/org/sqlcomponents/compiler/java/util/CompilerTestUtil.java) builds an `Application` when `SQLCOMPONENTS_CONFIG` is **not** set: |
| 58 | + |
| 59 | +- Reads [`database.properties`](../database.properties) from the current working directory or parent (`../database.properties`). |
| 60 | +- Uses `DATABASE_TYPE` env var or defaults to `postgres` for property key prefix (`postgres.datasource.url`, …). |
| 61 | +- Sets `rootPackage`, encryption lists, insert/update maps, and **`srcFolder`** to `datastore/src/main/java` (or `../datastore/src/main/java` depending on cwd). |
| 62 | + |
| 63 | +Then: |
| 64 | + |
| 65 | +```java |
| 66 | +Application application = CompilerTestUtil.getApplication(); |
| 67 | +application.compile(new JavaCompiler()); |
| 68 | +``` |
| 69 | + |
| 70 | +Run from Maven with the working directory that matches your paths (often `compiler/` for tests). |
| 71 | + |
| 72 | +### YAML-based setup (CI / explicit config) |
| 73 | + |
| 74 | +When **`SQLCOMPONENTS_CONFIG`** points to a YAML file: |
| 75 | + |
| 76 | +- `Application` is built with [`CoreConsts.buildApplication(File)`](../core/src/main/java/org/sqlcomponents/core/utils/CoreConsts.java). |
| 77 | +- **`SOURCE_FOLDER`** must be set to the output directory for generated sources (throws if missing). |
| 78 | + |
| 79 | +## SPI: `Compiler` interface |
| 80 | + |
| 81 | +The compiler module is the reference implementation of: |
| 82 | + |
| 83 | +```java |
| 84 | +void compile(Application application) throws SQLException; |
| 85 | +``` |
| 86 | + |
| 87 | +Other implementations could target different languages or layouts as long as they accept `Application`. |
| 88 | + |
| 89 | +## Related documentation |
| 90 | + |
| 91 | +- [Core module](core.md) |
| 92 | +- [Datastore module](datastore.md) |
| 93 | +- [Project structure](project-structure.md) |
| 94 | +- [Documentation index](README.md) |
0 commit comments