Below are some practical examples of using FreeMarker templates from XSQL-Script.
 

1 XML from select

Copy
<xsql-script name='systables'>
   <body>

    <!-- define a template to generate an XML -->
    <set name='xmlsample'>
       <freemarker.template>

<![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<tables>
   <#list rstables.getRows() as row>
   <table id='${row["tabid"]}'>${row["tabname"]}</table>
   </#list>
</tables>
]]>
       </freemarker.template>
    </set>

    <-- select data -->
    
    <set name='rstables'>
       	<sql.toResultSet>
       	   <!-- TEMPLATE requires scrollable resultset to get size -->
       	   <select scroll='true'>
       	      <columns>*</columns>
       	      <from table='systables' />
       	      <where>
       	          tabname LIKE 'syscol%'
       	      </where>
       	    </select>      
       	</sql.toResultSet>
    </set>

	<!-- process -->

    <println>
       <freemarker.template.process>
          <xmlsample/>
       </freemarker.template.process>
    </println>

   </body>
</xsql-script>
<?xml version="1.0" encoding="UTF-8"?>
<tables>
   <table id='34'>syscolattribs</table>
   <table id='5'>syscolauth</table>
   <table id='15'>syscoldepend</table>
   <table id='2'>syscolumns</table>
</tables>
 

2 ResultSet metadata

Example
Copy
<xsql-script name='ftl_sample_1'>
   <body>

	<!-- setup a table of planets -->
	<vtable name='planets'>
		<column name='planet' type='string' />
		<column name='radius' type='integer' />
	</vtable>
	
	<vtable.insert name='planets'><string>Venus</string><number>6</number></vtable.insert>
	<vtable.insert name='planets'><string>Earth</string><number>6</number></vtable.insert>
	<vtable.insert name='planets'><string>Mars</string><number>3</number></vtable.insert>
	<vtable.insert name='planets'><string>Jupiter</string><number>70</number></vtable.insert>

	<set name='t2'>
		<freemarker.template>
<![CDATA[
======================
==== Planets List ====
======================

COLUMNS METADATA
    <#list planets.getCols() as col>
        columnName ${col.getColumnName()}
        columnLabel ${col.getColumnLabel()!}
        columnCombo ${col.getColumnCombo()!}
        getColumnType ${col.getColumnType()}
        getColumnStyle ${col.getColumnStyle()}
        getColumnHref ${col.getColumnHref()!}
        isNullable ${col.isNullable()}
        isRightAligned ${col.isRightAligned() ? string("true", "false")}
        ----
    </#list>

RESULTSET METADATA
ResultSet ${planets.hasHeaders() ? string("has headers", "has NO headers") }

    <#if planets.hasHeaders()>
        PROCESS HEADERS
    <#else>
        NO HEADERS
    </#if>

-- PROCESING ROWS USING COLUMN NAMES --

<#list planets.getRows() as row>
    ${row["planet"]} has a radius of ${row["radius"]}K km
</#list>

-- PROCESING ROWS USING COLUMN INFO --

<#list planets.getRows() as row>
    ROW
    <#list planets.getCols() as col>
        COL ${row[col.getColumnName()]}
    </#list>
</#list>
]]>

		</freemarker.template>
	</set>


	<println>
		<freemarker.template.process>
			<t2/>
		</freemarker.template.process>
	</println>

   </body>
</xsql-script>
======================
==== Planets List ====
======================

COLUMNS METADATA
  columnName planet
  columnLabel 
  columnCombo 
  getColumnType 1
  getColumnStyle 
  getColumnHref 
  isNullable 0
  isRightAligned false
----
  columnName radius
  columnLabel 
  columnCombo 
  getColumnType 4
  getColumnStyle text-align: right;
  getColumnHref 
  isNullable 0
  isRightAligned true
----

RESULTSET DATA
ResultSet has NO headers
   NO HEADERS

-- PROCESING ROWS USING COLUMN NAMES --

   Venus has a radius of 6K km
   Earth has a radius of 6K km
   Mars has a radius of 3K km
   Jupiter has a radius of 70K km

-- PROCESING ROWS USING COLUMN INFO --

  ROW
     COL Venus
     COL 6
  ROW
     COL Earth
     COL 6
  ROW
     COL Mars
     COL 3
  ROW
     COL Jupiter
     COL 70