[stringtemplate-interest] Template overloading

Andrew Goodnough Andrew.Goodnough at wicourts.gov
Thu Feb 9 07:16:32 PST 2006


I've done exactly this for generating database-specific SQL.  We are
switching from Sybase to Postgres so we have a need to create both.  I
created 3 templates:

sybase.stg    //Contains sybase-specific templates
postgres.stg //Contains postgres-specific templates
shared.stg    //Contains db agnostic templates

You will probably want to put most of the template code in the shared
template group file and only use the db specific template when you find
some syntax that is not general.  The shared.stg tends to contain the
outline of what needs to be done while the specific template does the
real work.  Like:

-shared.stg-
doStuff(userType) ::= <<
--START SCRIPT--
<userType:dropUserType()>
--END SCRIPT--
>>

-sybase.stg-
delim() ::= "<\n>GO"

dropUserType() ::= <<
EXEC sp_droptype '<it.name>'<delim()>
>>

-postgres.stg-
delim() ::= ";"

dropUserType() ::= <<
DROP DOMAIN '<it.name>'<delim()>
>>


The Java code uses the standard super group idea:
====
        InputStream sharedIs =
getClass().getResourceAsStream("shared.stg");
        sharedReader = new InputStreamReader(sharedIs);
        StringTemplateGroup sharedTemplates = new
StringTemplateGroup(sharedReader, AngleBracketTemplateLexer.class);
        
        InputStream specificIs = getClass().getResourceAsStream(dbms +
".stg");  // dbms can be 'sybase' or 'postgres'
        specificReader = new InputStreamReader(specificIs);
        StringTemplateGroup specificTemplates = new
StringTemplateGroup(specificReader, AngleBracketTemplateLexer.class);
        specificTemplates.setSuperGroup(sharedTemplates);
        
        StringTemplate fileTpl =
specificTemplates.getInstanceOf(templateName);
==== 

I've implemented a lot of logic in Java Beans that I pass to my
templates to render.  I've got quite a bit of DDL and DML working in
production so if you start going down this road in earnest, I can send
you my templates and code if you like.

Andy Goodnough
Wisconsin Courts
http://wicourts.gov/


>>> Florian Heidenreich <florian.heidenreich at inf.tu-dresden.de>
02/09/06 8:02 am >>> 
Hello!

I'm currently writing a SQL code generator using StringTemplate.

One idea I had is to build a StringTemplateGroup for Standard SQL and 
overload dialect specific templates in a child group (e.g. for MySQL or

Oracle). My goal is to replace/overload the specific templates from 
Standard SQL with the ones from the specific dialect while keeping the

templates which are equal in both versions.

Is this possible with StringTemplate and if so, how can I achieve
that?

Thank you!

Best regards,
~ Florian
_______________________________________________
stringtemplate- interest mailing list
stringtemplate- interest at antlr.org
http://www.antlr.org:8080/mailman/listinfo/stringtemplate- interest



More information about the stringtemplate-interest mailing list