Skip to content
Snippets Groups Projects

kddl

Kotlin SQL DDL format and associated formatters towards PostgresQL and PlantUML.

Usage

Here's the example.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 for this model, do:

kddl -i example.kddl -f plantuml > example.pu
plantuml -Tpng example.pu
eog example.png

And here's the result:

You may need to install plantuml, with something like sudo apt install plantuml.

To generate the PostgreSQL creation script for this model, do:

kddl -i example.kddl -f postgresql > example.sql

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.

git clone git@gitlab.renegat.net:claude/kddl.git
./install.sh

To install the kddl command everywhere, do:

ln -s ~/<path_to_kddl_repository>/kddl.sh ~/bin/kddl

Other platforms

Please adapt the installation and run scripts.

TODO

  • switch to kotlin antlr and ast?
  • reverse enginering via modality
  • plugins for gradle / maven / kobalt
  • foreign key links could be written reverse
  • db version handling
  • make database {} surrounding optional
  • allow reverse arrows
  • only table names are quoted for now

limitations

  • a field can be implied in one foreign key at most
  • multivalued foreign keys fields must be named after target primary key fields