Chapter 7. DDL generator for Castor JDO

7.1. Castor DDL Generator - An Introduction

Describes how to use the DDL Generator, and what features and options are currently supported.

Castor DDL Generator creates SQL scripts to drop/create tables, unique keys, sequences and so on based on the input of a Castor JDO mapping file.

java org.castor.ddlgen.Main -m mapping.xml
	

This will generate the SQL script mapping.sql in the same directory mapping.xml is located.

7.1.1. DDL Generator Options

The DDL Generator has a number of different options which may be set. Some of them are specified at the command line while others need to be configured through a property file. Most of the options are located in global properties file org/castor/ddlgen/ddlgen.properties, but there are also some options that are specific for one database engine. These DB-specific properties can be found at org/castor/ddlgen/engine/<database>/<database>.properties.

7.1.1.1. Command Line Options

Table 7.1. Command line options

OptionArgsDescriptionOptional?
mfilenameCastor JDO mapping file to generate DDL for.Required
ofilenameName of file the generated DDL will be written to. If not specified the generated DDL will be written to a file named similar as the Castor JDO mapping source file with xml extension being replaced by sql.Optional
edatabase engineName of database engine to generate DDL for. The engine used as default can be specified with org.castor.ddlgen.DefaultEngine option of global properties file.Optional
cfilenameAlternative global properties file to be used when generating DDL.Optional
dfilenameAlternative database specific properties file to be used when generating DDL.Optional
h Shows help/usage information.Optional

7.1.2. Database Engines

The DDL Generator supports generation of SQL scripts for the following database engines:

Table 7.2. Description of the attributes

NameDatabase engineProperty file
db2DB/2org/castor/ddlgen/engine/db2/db2.properties
derbyApache Derbyorg/castor/ddlgen/engine/derby/derby.properties
hsqlHypersonic SQLorg/castor/ddlgen/engine/hsql/hsql.properties
mssqlMicrosoft SQL Serverorg/castor/ddlgen/engine/mssql/mssql.properties
mysqlMySQLorg/castor/ddlgen/engine/mysql/mysql.properties
oracleOracleorg/castor/ddlgen/engine/oracle/oracle.properties
pointbaseBorland Pointbaseorg/castor/ddlgen/engine/pointbase/pointbase.properties
postgresqlPostgreSQLorg/castor/ddlgen/engine/postgresql/postgresql.properties
sapdbSAP DB / MaxDBorg/castor/ddlgen/engine/sapdb/sapdb.properties
sybaseSybaseorg/castor/ddlgen/engine/sybase/sybase.properties

7.2. Using the Ant task for the Castor DDL Generator

Describes how to use the Ant task for the Castor DDL Generator and its features.

An alternative to using the command line as shown in the previous section, the Castor DDL Generator Ant Task can be used to call the DDL generator for class generation. The only requirement is that the castor-<version>-anttasks.jar must be made available to your Ant installation.

7.2.1. Configuration

Please find below the complete list of parameters that can be set on the Castor source generator.

Table 7.3. 

AttributeDescriptionRequired?
fileThe name of the Castor JDO mapping file to use as input for DDL generation.Yes
ddlFileNameThe name of the DDL file to be generated.Yes
databaseEngineThe name of database engine to generate DDL for.Yes
globalPropertiesName of a custom (global) properties file to be used during DDL generation.No
databaseEnginePropertiesName of a custom database specific properties file to be used during DDL generation.No

Alternatively to specifying the file property, it is possible to work with a nested <FileSet> element or with the dir property.

7.2.2. Example

Below is an example of how to use this task from within an Ant target definition named 'castor:ddl:src':

<target name="castor:ddl:src" depends="init"
           description="Generate a DDL script from a JDO mapping file.">

  <taskdef name="castor-ddlgen"
           classname="org.castor.anttask.CastorDDLGenTask"
           classpathref="castor.class.path" />
  <mkdir dir="generated" />
  <castor-ddlgen file="src/main/resources/mapping.xml"
                 ddlFileName="target/generated/ddl/mapping.sql"
                 databaseEngine="mysql"/>
</target>
		

7.3. Castor DDL Generator - Type Mapping

This section describes the mapping between Castor sql type java.sql.Types constant, java data type, sql type of supported database.

Table 7.4. 

Castor TypeJDBC TypeJava Object TypeMySQLPostgreSQLOracleDerbyMSSQLSapDBDB2SybaseHSQLPointBase
BITBITjava.lang.BooleanTINYINT(1)BOOLEANBOOLEANCHAR FOR BIT DATABITBOOLEAN BITBITBOOLEAN
TINYINTTINYINTjava.lang.ByteTINYINTSMALLINTSMALLINTSMALLINTTINYINTSMALLINTSMALLINTTINYINTTINYINTSMALLINT
SMALLINTSMALLINTjava.lang.ShortSMALLINTSMALLINTSMALLINTSMALLINTSMALLINTSMALLINTSMALLINTSMALLINTSMALLINTSMALLINT
INTEGERINTEGERjava.lang.IntegerINTEGERINTEGERINTEGERINTEGERINTEGERINTEGERINTEGERINTEGERINTEGERINTEGER
BIGINTBIGINTjava.lang.LongBIGINTBIGINTNUMERICBIGINTBIGINTINTEGERBIGINTINTEGERBIGINTNUMERIC
FLOATFLOATjava.lang.DoubleFLOATDOUBLE PRECISIONFLOATFLOATFLOATFLOATFLOATFLOATFLOATFLOAT
DOUBLEDOUBLEjava.lang.DoubleDOUBLEDOUBLE PRECISIONDOUBLE PRECISIONDOUBLEDOUBLE PRECISIONDOUBLE PRECISIONDOUBLEDOUBLE PRECISIONDOUBLE PRECISIONDOUBLE PRECISION
REALREALjava.lang.FloatREALREALREALREALREALDOUBLE PRECISIONREALREALREALREAL
NUMERICNUMERICjava.math.BigDecimalNUMERICNUMERICNUMERICNUMERICNUMERICNUMERICNUMERICNUMERICNUMERICNUMERIC
DECIMALDECIMALjava.math.BigDecimalDECIMALNUMERICDECIMALDECIMALDECIMALDECIMALDECIMALDECIMALDECIMALDECIMAL
CHARCHARjava.lang.StringCHARCHARCHARCHARCHARCHARCHARCHARCHARCHAR
VARCHARVARCHARjava.lang.StringVARCHARVARCHARVARCHAR2VARCHARVARCHARVARCHARVARCHARVARCHARVARCHARVARCHAR
DATEDATEjava.sql.DateDATEDATEDATEDATEDATETIMEDATEDATEDATETIMEDATEDATE
TIMETIMEjava.sql.TimeTIMETIMEDATETIMEDATETIMETIMETIMEDATETIMETIMETIME
TIMESTAMPTIMESTAMPjava.sql.TimestampTIMESTAMPTIMESTAMPTIMESTAMPTIMESTAMPTIMESTAMPTIMESTAMPTIMESTAMPTIMESTAMPTIMESTAMPTIMESTAMP
BINARYBINARYbyte[]BINARYBYTEARAWCHAR [n] FOR BIT DATABINARYBLOBCHAR [n] FOR BIT DATABINARYBINARYBLOB
VARBINARYVARBINARYbyte[]VARBINARYBYTEALONG RAWVARCHAR [] FOR BIT DATAVARBINARYBLOBVARCHAR [] FOR BIT DATAVARBINARYVARBINARYBLOB
LONGVARBINARYLONGVARBINARYbyte[]VARBINARYBYTEALONG RAWLONG VARCHAR FOR BIT DATAIMAGEBLOBLONG VARCHAR FOR BIT DATAVARBINARYLONGVARBINARYBLOB
OTHEROTHERjava.lang.ObjectBLOBBYTEABLOBBLOBIMAGEBLOBBLOBIMAGEOTHERBLOB
JAVA_OBJECTJAVA_OBJECTjava.lang.ObjectBLOBBYTEABLOBBLOBIMAGEBLOBBLOBIMAGEOBJECTBLOB
BLOBBLOBjava.io.InputStreamBLOBBYTEABLOBBLOBIMAGEBLOBBLOBIMAGEOBJECTBLOB
CLOBCLOBjava.sql.ClobTEXTTEXTCLOBCLOBTEXTCLOBCLOBTEXTOBJECTCLOB

7.3.1. JDBC Types not supported by Castor

The following JDBC types are not supported by Castor yet.

  • ARRAY
  • DISTINCT
  • REF
  • STRUCT

7.4. Castor DDL Generator - Properties

Describes the properties available on the Castor DDL Generator.

7.4.1. Overview

At startup, the DDL Generator first evaluates the command line options. Next it loads the global properties on the command line if specified, otherwise the default properties included with the DDL Generator. There are two important properties loaded at startup:

org.castor.ddlgen.Generators

tells the DDL Generator about the supported database engines.

org.castor.ddlgen.DefaultEngine

The database engine for which to generate a SQL script. Can be overridden on the command line

The last step at startup is to read the specific configuration of the database engine being used. A custom configuration can be provided on the command line to override the default.

7.4.2. Global properties

Please find below a list of global properties to control various advanced options of the DDL Generator.

Table 7.5. Command line options

OptionDescriptionValuesDefaultSince
     
org.castor.ddlgen.GeneratorsGenerator classes of supported database engines.  1.1
org.castor.ddlgen.DefaultEngineName of default database engine. Will be overwritten by engine specified on commandline.db2, derby, hsql, mssql, mysql, oracle, pointbase, postgresql, sapdb or sybasemysql1.1
org.castor.ddlgen.SchemaNameName of the database schema. test1.1
org.castor.ddlgen.GroupStatementsHow to group generated DDL statements?TABLE or DDLTYPETABLE1.1
org.castor.ddlgen.CharFormatHow to format characters of generated DDL statements?SENSITIVE, UPPER or LOWERSENSITIVE1.1
org.castor.ddlgen.NewlineCharacter sequence to write for newline. \n1.1
org.castor.ddlgen.IndentionCharacter sequence to write for indented lines. \t1.1
org.castor.ddlgen.GenerateSchemaGenerate SCHEMA statements.true or falsetrue1.1
org.castor.ddlgen.GenerateDropGenerate DROP statements.true or falsetrue1.1
org.castor.ddlgen.GenerateCreateGenerate CREATE statements.true or falsetrue1.1
org.castor.ddlgen.GeneratePrimaryKeyGenerate PRIMARYKEY statement.true or falsetrue1.1
org.castor.ddlgen.GenerateForeignKeyGenerate FOREIGNKEY statement.true or falsetrue1.1
org.castor.ddlgen.GenerateIndexGenerate INDEX statements (Not supported yet).true or falsefalse1.1
org.castor.ddlgen.GenerateKeyGeneratorGenerate KEYGENERATOR statements.true or falsetrue1.1
default_tinyint_precisionDefault precision of tinyint values.  1.1
default_smallint_precisionDefault precision of smallint values.  1.1
default_integer_precisionDefault precision of integer values.  1.1
default_bigint_precisionDefault precision of bigint values. 191.1
default_bigint_decimalsDefault decimals of bigint values. 01.1
default_float_precisionDefault precision of float values. 381.1
 Default precision of tinyint values.  1.1
default_float_decimalsDefault decimals of float values. 71.1
default_double_precisionDefault precision of double values. 531.1
default_double_decimalsDefault decimals of double values. 151.1
default_real_precisionDefault precision of real values. 381.1
default_real_decimalsDefault decimals of real values. 71.1
default_numeric_precisionDefault precision of numeric values. 651.1
default_numeric_decimalsDefault decimals of numeric values. 301.1
default_decimal_precisionDefault precision of decimal values. 651.1
default_decimal_decimalsDefault decimals of decimal values. 301.1
default_char_lengthDefault length of char values. 2561.1
default_varchar_lengthDefault length of varchar values. 2561.1
default_longvarchar_lengthDefault length of longvarchar values. 10241.1
default_date_precisionDefault precision of date values.  1.1
default_time_precisionDefault precision of time values.  1.1
default_timestamp_precisionDefault precision of timestamp values. 191.1
default_binary_lengthDefault length of binary values. 2561.1
default_varbinary_lengthDefault length of varbinary values. 2561.1
default_longvarbinary_lengthDefault length of longvarbinary values. 10241.1
default_other_lengthDefault length of other values. 10241.1
default_javaobject_lengthDefault length of javaobject values. 10241.1
default_blob_lengthDefault length of blob values. 10241.1
default_clob_lengthDefault length of clob values. 10241.1

7.4.2.1. Supported database engines

The supported database engines are defined as follows:

#
# generator classes of supported database engines
#
org.castor.ddlgen.Generators=\
  org.castor.ddlgen.engine.db2.Db2Generator,\
  org.castor.ddlgen.engine.derby.DerbyGenerator,\
  org.castor.ddlgen.engine.hsql.HsqlGenerator,\
  org.castor.ddlgen.engine.mssql.MssqlGenerator,\
  org.castor.ddlgen.engine.mysql.MysqlGenerator,\
  org.castor.ddlgen.engine.oracle.OracleGenerator,\
  org.castor.ddlgen.engine.pointbase.PointBaseGenerator,\
  org.castor.ddlgen.engine.postgresql.PostgresqlGenerator,\
  org.castor.ddlgen.engine.sapdb.SapdbGenerator,\
  org.castor.ddlgen.engine.sybase.SybaseGenerator
			

7.4.2.2. Grouping of DDL statements

There are 2 supported modes to group DDL statements. For a simple example, the output of both modes is:

drop A if exist
create A(IDA int);
alter table A add primary key (IDA)

drop B if exist
create B(IDB int);
alter table B add primary key (IDB)
			

Figure 7.1. org.castor.ddlgen.GroupStatements=TABLE


drop A if exist
drop B if exist

create A(IDA int);
create B(IDB int);

alter table A add primary key (IDA)
alter table B add primary key (IDB)
			

Figure 7.2. org.castor.ddlgen.GroupStatements=DDLTYPE


7.4.3. Specific properties

Below you can find a list of specific properties to control various advanced options of the DDL Generator.

7.4.3.1. Properties common for all database engines

Table 7.6. Common properties for all databases

OptionDescriptionValuesDefaultSince
org.castor.ddlgen.KeyGeneratorFactoriesKey generator factory classes supported by DB/2.  1.1
org.castor.ddlgen.HeaderCommentComment to add to head of generated script.  1.1

7.4.3.1.1. Key generators

The key generators supported by a database engine are defined as follows (example taken from Oracle):

#
# key generator factory classes of supported database engines
#
org.castor.ddlgen.KeyGeneratorFactories=\
  org.castor.ddlgen.keygenerator.HighLowKeyGeneratorFactory,\
  org.castor.ddlgen.keygenerator.MaxKeyGeneratorFactory,\
  org.castor.ddlgen.keygenerator.UUIDKeyGeneratorFactory,\
  org.castor.ddlgen.engine.oracle.OracleSequenceKeyGeneratorFactory
				

7.4.3.2. Properties for db2, hsql, Oracle, Postgresql and sapdb

Table 7.7. Common properties for all databases

OptionDescriptionValuesDefaultSince
org.castor.ddlgen.TriggerTemplateTemplate to create TRIGGER statements.  1.1

7.4.3.2.1. Trigger template

Below you can take a look at the default trigger template defined for Oracle. The DDL Generator will replace the parameters in brackets with appropriate values (e.g. <table_name>).

#
# trigger template
#
org.castor.ddlgen.TriggerTemplate=\
CREATE TRIGGER <trigger_name>
  BEFORE INSERT OR UPDATE ON <table_name>
  FOR EACH ROW
  DECLARE
    iCounter <table_name>.<pk_name>%TYPE;
    cannot_change_counter EXCEPTION;
  BEGIN
    IF INSERTING THEN
      Select <sequence_name>.NEXTVAL INTO iCounter FROM Dual;
      :new.<pk_name> := iCounter;
    END IF;

    IF UPDATING THEN
      IF NOT (:new.<pk_name> = :old.<pk_name>) THEN
        RAISE cannot_change_counter;
      END IF;
    END IF;

EXCEPTION
  WHEN cannot_change_counter THEN
    raise_application_error(-20000, 'Cannot Change Counter Value');
END;
					

Figure 7.3. Default trigegr template for Oracle


7.4.3.3. Properties for MySQL only

Table 7.8. Common properties for all databases

OptionDescriptionValuesDefaultSince
org.castor.ddlgen.engine.mysql.StorageEngineStorage engine to be used. If left empty the default configured at the database server will be used.MYISAM, InnoDB, MERGE, MEMORY, BDB or ISAM 1.1
org.castor.ddlgen.engine.mysql.ForeignKeyOnDeleteStrategyDelete strategy for foreign keys. If not specified NO ACTION will be used by default.CASCADE, RESTRICT, SET NULL, NO ACTION 1.1
org.castor.ddlgen.engine.mysql.ForeignKeyOnUpdateStrategyUpdate strategy for foreign keys. If not specified NO ACTION will be used by default.CASCADE, RESTRICT, SET NULL, NO ACTION 1.1