samples/table2ddl.ysl2
author Volker Birk <vb@pep.foundation>
Thu, 26 Apr 2018 19:26:10 +0200
changeset 18 e14c2324eb0e
parent 0 76005e62091d
permissions -rw-r--r--
new distribution version
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     1
include yslt.yml2
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     2
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     3
define operator "“(.*?)”" as call "idl2ddl" with "type", "%1";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     4
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     5
tstylesheet {
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     6
    const "idtype", "'INTEGER'";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     7
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     8
    template "/" apply "module|class", 0;
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
     9
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    10
    template "module" {
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    11
        param "namespace", "''";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    12
        choose {
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    13
            when "$namespace=''"
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    14
                apply "module|class", 0 with "namespace", "@name";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    15
            otherwise
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    16
                apply "module|class", 0 with "namespace", "concat($namespace,'_', @name)";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    17
        }
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    18
    }
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    19
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    20
    template "class" {
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    21
        param "namespace", "''";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    22
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    23
        choose {
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    24
            when "$namespace=''"    | CREATE TABLE «@name» (
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    25
            otherwise               | CREATE TABLE «$namespace»_«@name» (
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    26
        }
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    27
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    28
        if "count(attr[@name='id'])=0" |> id «$idtype»`if "count(attr)>0" > , `
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    29
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    30
        const "name", "@name";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    31
        foreach "//class[class/@name=$name]" |> «@name»_fk «$idtype»,
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    32
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    33
        apply "attr";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    34
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    35
        | );
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    36
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    37
        apply "class", 0 with "namespace", "$namespace";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    38
    }
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    39
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    40
    template "attr" | «@name» “@type”`if "position() != last()" > , `
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    41
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    42
    function "idl2ddl" {
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    43
        param "type";
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    44
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    45
        choose {
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    46
            when "$type='string'"   > VARCHAR
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    47
            when "$type='long'"     > INTEGER
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    48
        }
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    49
    }
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    50
}