samples/table2ddl.ysl2
author Edouard Tisserant <edouard.tisserant@gmail.com>
Wed, 28 Apr 2021 23:40:51 +0200
changeset 78 0b05c2bce9e4
parent 0 76005e62091d
permissions -rw-r--r--
Fix expension of macros in pointers with default values.

For example :

in xsl decl widget_class(%name, *clsname="%nameWidget", match="widget[@type='%name']", mode="widget_class") alias template {
| class `text **clsname` extends Widget{
content;
| }
};

widget_class('Input');

gives now :

<xsl:template match="widget[@type='Input']" mode="widget_class">
<xsl:text>class </xsl:text>
<xsl:text>InputWidget</xsl:text>
<xsl:text> extends Widget{
</xsl:text>
<xsl:text>}
</xsl:text>
</xsl:template>

Without the fix, <xsl:text>InputWidget</xsl:text> would be <xsl:text>%nameWidget</xsl:text>
include yslt.yml2

define operator "“(.*?)”" as call "idl2ddl" with "type", "%1";

tstylesheet {
    const "idtype", "'INTEGER'";

    template "/" apply "module|class", 0;

    template "module" {
        param "namespace", "''";
        choose {
            when "$namespace=''"
                apply "module|class", 0 with "namespace", "@name";
            otherwise
                apply "module|class", 0 with "namespace", "concat($namespace,'_', @name)";
        }
    }

    template "class" {
        param "namespace", "''";

        choose {
            when "$namespace=''"    | CREATE TABLE «@name» (
            otherwise               | CREATE TABLE «$namespace»_«@name» (
        }

        if "count(attr[@name='id'])=0" |> id «$idtype»`if "count(attr)>0" > , `

        const "name", "@name";
        foreach "//class[class/@name=$name]" |> «@name»_fk «$idtype»,

        apply "attr";

        | );

        apply "class", 0 with "namespace", "$namespace";
    }

    template "attr" | «@name» “@type”`if "position() != last()" > , `

    function "idl2ddl" {
        param "type";

        choose {
            when "$type='string'"   > VARCHAR
            when "$type='long'"     > INTEGER
        }
    }
}