kddl
Kotlin SQL DDL format and associated formatters towards PostgresQL and PlantUML.
Usage
Here's the example model.kddl
file, which should be enough to understand the syntax by example.
// Definition for database geo
// a database contains schemas
database geo {
option id_suffix = '_key' // default is '_id'
// a schema contains tables and links
schema infra {
// a table contains fields, either given a type or a destination table
table zone {
*code varchar(10) // '*' stands for 'part of pk', otherwise pk is generated as needed
name varchar(50) // not null by default
description text? // '?' stands for nullable field
active boolean = false // default value
}
table department : zone // inherit a table from another (for engines which support table inheritance like PostgresQL)
table city : zone { hasTrain boolean? } // declarations can be inlined
table link {
distance integer // types are : boolean, integer, long, float, double, time, date, datetz, datetime, datetimetz, varchar(*n*), text
src_id --> zone // mandatory foreign key field
dst_id --> zone // idem
hub_id ..> zone (down) // nullable foreign key field
}
city *--> department (up) // plantuml arrow direction can be specified
}
schema client {
table contact {
gender, lastname, firstname // field types are optional, use a coma to disambiguate
}
table location {
name varchar(50) = 'untitled' // string literals use single quotes
address text?
}
location *..> infra.zone // foreign key referencing a table in another schema
}
}
To generate the plantuml graph definition script, do:
:::shell
kddl -i model.kddl -f plantuml > model.pu
Installation (linux)
Linux
You first need to clone this repository and launch the install.sh
script, which will install the kddl library in your local maven repository, along with its dependencies, among which a forked version of the net.akerhurst.language:agl-processor.
To install the kddl
command, do:
:::shell
sudo ln -s /<path_to_kddl_repository>/kddl.sh /usr/local/sbin
the install.sh
shell script, which will install the library dependencies in your local maven repository (this shouldn't need sudo rights).
Then you can invoke the kddl.sh
script of the cloned repository from anywhere. You'll typically want to add a symbolic link from /usr/local/bin/kddl
towards kddl.sh
.
. To call it from anywhere, do
Other platforms
Please adapt the installation and run scripts.
TODO
- plugins for gradle / maven / kobalt
- foreign key links could be written reverse
limitations
- a field can be implied in one foreign key at most
- multivalued foreign keys fields must be named after target primary key fields