databene

 
  • Increase font size
  • Default font size
  • Decrease font size

Base File Format

<?xml version="1.0" encoding="iso-8859-1"?>
<setup xmlns="http://databene.org/benerator/0.6.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://databene.org/benerator/0.6.0 http://databene.org/benerator-0.6.0.xsd">

    ...content here...

</setup>

Hello World

Descriptor:

    <import defaults="true"/>
    <generate name="message" count="5" consumer="ConsoleExporter" >
        <attribute name="text" type="string" pattern="(Hello|Hi|Howdy) (World|Earth|Planet)"/>
    </generate>

Output:

message[text=Hello World]
message[text=Hi World]
message[text=Hello Planet]
message[text=Hello Planet]
message[text=Howdy Earth]

 

Simple-Type Data Generation

Regular-Expression-based

<attribute name="wecomeMessage" type="string" pattern="(Hello|Hi|Howdy) (World|Earth|Planet)"/>
<attribute name="phone" pattern="[0-9]{3}-[2-9]{1}[0-9]{2}-[0-9]{4} {0,1}[0-9]{1,5}" />

 

List-based

<attribute name="rank" type="string" pattern="A,B,C"/>

 

Script-based

<variable name="block" pattern="[A-Z]" />
<variable name="buildingNo" pattern="[1-9]" />
<attribute name="building" script="ftl:Building ${block}${buildingNo}"/>

 

Decisions in FreeMarker

A common requirement is to have alternative ways to render some data, depending on context or necessary data conversion, e.g. for entities with multifield constraints or mapping  of null values or booleans. The simplest way 

The simplest way to configure this is a FreeMarker condition expression in the general form:

<attribute name="test" script="ftl:[#ftl][#if condition]expression[#else]expression[/#if]" /> 

An example that mixes data of two alternatives alt1 and alt2 (possibly taken from two different data sets) is:

<attribute name="test" script="ftl:[#ftl][#if random < percentage]${alt1}[#else]${alt2}[/#if]" />  

Please note, that random and percentage can be variables or properties defined before and may not be wrapped with ${}!

 

Exporting entities in different formats

Writing entities to a CSV file

    <generate name="Person" count="5">
        <id name="id" type="int"/>
        <attribute name="name" type="string"/>
        <consumer class="org.databene.platform.csv.CSVEntityExporter">
            <property name="uri"      value="users.csv"/>
            <property name="encoding" value="UTF-8"/>
            <property name="columns"  value="id,name"/>
        </consumer>
    </generate>

creates a file 'users.csv' in UTF-8 encoding with the columns id and name:

id,name
1,GCZJWGBIOIVUS
2,IQMJKBSLETWOZQRIPEE
3,IKBSQEFCCHG
4,A
5,XGQOLYXSVWPJLRMVCLDTLMBVT

 

Database related

Defining a database

<database id="db" url="jdbc:hsqldb:hsql://localhost" driver="org.hsqldb.jdbcDriver" user="sa" password="" schema="public" batch="false"/>

 

Executing DDL

from a file

<execute uri="create-tables.sql" target="db" onError="fatal"/>

 

inline

<execute target="db" type="sql" onError="warn">
  CREATE TABLE db_example (
    id   int         NOT NULL,
    name varchar(16) NOT NULL,
    PRIMARY KEY (id)
  );
  INSERT INTO db_example (id, name) VALUES (1, "Alpha");
</execute>  

 

Importing data from a from a DbUnit XML file

<iterate source="shop.dbunit.xml" consumer="db"/>

 

Exporting generated data to a SQL file

You need to configure the SQLEntityExporter to usse a database dialect for SQL file generation:

<database id="db" ... />

<consumer class="SQLEntityExporter">
    <property name="uri" value="my.sql" />
    <property name="dialect" value="oracle"
</consumer>

 

Available dialects are

dialect database system
db2
DB2
derbyDerby
firebird
Firebird
hsql
HSQL
h2
H2
oracle
Oracle
postgres
PostgreSQL
sql_server
SQL Server