velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Clifton C. Craig" <...@icsaward.com>
Subject Learning Velocity/Anakia
Date Mon, 11 Oct 2004 18:30:48 GMT
Hello all,

I'm experimenting with Velocity/Anakia to create a SQL DDL script 
generator. The purpose of the generator is to read an XML document and 
xlat it into SQL DDL which will then be fed in through JDBC to the 
database to create a database. I've started this project but I am having 
difficulty adjusting to the syntax and use of Velocity. I will include 
what I have accomplished so far to illustrate what I'm talking about. 
Basically I'm struggling with white-space and formatting issues as well 
as a way to externalize details of my XML schema. What I'm doing 
currently is hard-coding the element and attribute names from my XML 
document in my .vsl. I want to pass them in as properties or references 
somehow. I tried setting up a velocity.properties file but once I did 
that Velocity was no longer able to find my .vsl template. I tried 
setting the path property in the velocity.properties file and that 
became an issue because the templates are stored in a Windows directory 
with spaces in the names. (eg. "Documents And Settings") I tried several 
renditions of this but what it boiled down to was I had to use the 
primitive DOS 8.3 naming convention in my path or velocity would lose 
it. I believe this is due to the Commons collections external properties 
stuff but I haven't tested it to confirm my suspicions. Anyhow, that's 
my 1st major concern. My 2nd problem is formatting of my Velocity code 
and/or it's output. I can get away with both being a little sloppy 
because it's just me using it now and the SQL syntax is very generous 
with whitespace. However both need serious improvement. It seems that 
anything I do to help readability suffers in sloppy output and anything 
I do to format the output suffers in readability. I am brand new to this 
project so I know the following example will look silly to many but 
understand that I am learning.

This is my .vsl template:
## This is the SQL DDL template
#*
        M A C R O  D E F S
*#
#macro( createTables )
#set ($allTables = $root.selectNodes("database/table"))
#foreach ( $table in $allTables )
#if( $table.getAttributeValue("hsqltype") )
    #set ($type = "${table.getAttributeValue('hsqltype')} ")
#else
    #set ($type = "")
#end
CREATE $!{type}TABLE $table.getAttributeValue("name")
    #defineColumns($table);
#end
#end

#macro( defineColumns $table )
#set ($cols = $table.selectNodes("col"))
#set ( $more='' )
#foreach ($col in $cols)
    #if( $col.getAttributeValue("size") )
        #set ( $attr = "(${col.getAttributeValue('size')}" )
        #if( $col.getAttributeValue("scale") )
            #set ( $attr = "$!{attr},${col.getAttributeValue('scale')}" )
        #end
        #set ( $attr = "$!{attr})" )
    #elseif( $col.getAttributeValue("precision") )
        #set ( $attr = "(${col.getAttributeValue('precision')}" )
        #if( $col.getAttributeValue("scale") )
            #set ( $attr = "$!{attr},${col.getAttributeValue('scale')}" )
        #end
        #set ( $attr = "$!{attr})" )
    #else
        #set ( $attr = "" )
    #end
    $!{more}$col.getAttributeValue("name") 
$col.getAttributeValue("type") $!{attr}
    #if( $col.selectNodes("default") && 
$col.selectNodes("default").size() > 0 )
        DEFAULT '$col.selectNodes("default").get(0).getText()'
    #end
    #if( $col.getAttribute("allownull") )
        #if( $col.getAttributeValue("allownull")=='yes' ||  
$col.getAttributeValue("allownull")=='true' ) NULL #else NOT NULL #end
    #end
    #set ( $more=',' )
#end
#end
#createTables()

This is some sample XML that I feed through it:
<data-model>
    <database>
        <table name="testtable">
            <pk name="id"/>
            <col name="id" type="int"/>
            <col name="compositekey1" type="int"/>
            <col name="compositekey2" type="dec" size="9" scale="3"/>
            <col name="compositekey3" type="varchar" size="10"/>
            <col name="uniquekey" type="varchar" size="30"/>
            <col name="nonnullval" type="varchar" size="30" allownull="no"/>
            <col name="nonnullval2" type="varchar" size="30" allownull="no">
                <default>DEFAULT VALUE</default>
            </col>
            <col name="nullableval" type="varchar" allownull="yes"/>
            <unique-key cols="compositekey1,compositekey2,compositekey3"/>
            <unique-key cols="uniquekey"/>
        </table>
    </database>
</data-model>

And this is the output:
CREATE TABLE testtable
                id int
                    ,compositekey1 int
                                    ,compositekey2 dec (9,3)
                            ,compositekey3 varchar (10)
                            ,uniquekey varchar (30)
                            ,nonnullval varchar (30)
             NOT NULL                             ,nonnullval2 varchar (30)
             NOT NULL                 DEFAULT DEFAULT VALUE
                ,nullableval varchar
             NULL         ;

Help!

-- 
Clifton C. Craig, Software Engineer
Intelligent Computer Systems -  A Division of GBG
2101 Embassy Drive
Lancaster, PA  17603

Phone:  717-295-7977 ext. 621
Fax:  717-295-7683
ccc@icsaward.com
ccraig@gbg.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message