org.castor.ddlgen
Class AbstractGenerator

java.lang.Object
  extended byorg.castor.ddlgen.AbstractGenerator
All Implemented Interfaces:
Generator
Direct Known Subclasses:
Db2Generator, DerbyGenerator, HsqlGenerator, MssqlGenerator, MysqlGenerator, OracleGenerator, PointBaseGenerator, PostgresqlGenerator, SapdbGenerator, SybaseGenerator

public abstract class AbstractGenerator
extends java.lang.Object
implements Generator

AbstractGenerator is the base class for various DDL generator of specific DB and handles following tasks:

  • Extract information from Mapping to Schema
  • Loop through the schema and provide a skeleton for DDL creation

    AbstractGenerator will automatically extract necessary information for DDL creation. That information is handled by Schema.

    To create new generator for a DBMS, you should:

  • Overwrite this class to create new generator for a DBMS.
  • If the syntax of DBMS is different to standard DDL syntax, you should overwrite SchemaObject (Table, Field, KeyGenerator, Index, ForeignKey,...) classes. The class SchemaObjectFactory who handles the SchemaObject creation must be overwritten.
  • You must overwrite the TypeMapper if mapping between JDBC types and specific DBMS’s types is different among various DBMS.

    The example bellow shows how to create a generator for DB2:

  • Generator for DB2
    public class Db2Generator extends AbstractGenerator {
    
        public Db2Generator(final String globConf, final String dbConf)
                throws GeneratorException {
            super(globConf, dbConf);
            setTypeMapper(new Db2TypeMapper(getConf()));
        }
    }   
     
  • TypeMapper for DB2
    public final class Db2TypeMapper extends AbstractTypeMapper {
        public Db2TypeMapper(final Configuration conf) {
            super(conf);
        }
     
        protected void initialize(final Configuration conf) {
            // numeric types
            this.add(new NotSupportedType("bit"));
            LOG.warn("Db2 does not support 'TINY' type, use SMALLINT instead.");
            this.add(new NoParamType("tinyint", "SMALLINT"));
            this.add(new NoParamType("smallint", "SMALLINT"));
            this.add(new NoParamType("integer", "INTEGER"));
            this.add(new NoParamType("bigint", "BIGINT"));
        }
    }
    
  • Field for DB2
     
    public class Db2Field extends Field {
        public Db2Field() {
            super();
        }
    
        public String toDDL() throws GeneratorException {
            StringBuffer buff = new StringBuffer();
            buff.append(getName()).append(" ");
            buff.append(getType().toDDL(this));
            
            if (isIdentity()) {
                buff.append(" NOT NULL");
            }
            
            KeyGenerator keyGen = getKeyGenerator();
            if (keyGen != null && isIdentity()) {
                
                if (KeyGenerator.IDENTITY_KEY.equalsIgnoreCase(keyGen.getName())) {
                    buff.append(" GENERATED BY DEFAULT AS IDENTITY ").
                        append("START WITH 1 INCREMENT BY 1");
                }
            }
    
            return buff.toString();
        }
    }
    
  • Field for DB2
     
    public class Db2SchemaFactory extends SchemaFactory {
        public Db2SchemaFactory() {
            super();
        }
        public Field createField() {
            return new Db2Field();
        }
    
    }
    
    The GeneratorFactory class handles the specific database generator creation. For example:
      Generator generator = GeneratorFactory.
          createDDLGenerator(“mysql”, “ddl.properties”, “mysql.properties”);
     
    And to generate DDL, it should specify the printer and call generateDDL method.
      generator.setPrinter(System.out);
      Mapping mapping = new Mapping();
      mapping.loadMapping("mapping.xml");
      generator.generateDDL(mapping);            
     

    Since:
    1.1
    Version:
    $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
    Author:
    Le Duc Bao, Ralf Joachim

    Field Summary
     
    Fields inherited from interface org.castor.ddlgen.Generator
    GLOBAL_CONFIG_NAME, GLOBAL_CONFIG_PATH
     
    Constructor Summary
    protected AbstractGenerator(DDLGenConfiguration configuration)
              Constructor for AbstractGenerator.
     
    Method Summary
    protected  java.lang.String createForeignKeyDDL(Table table)
              Generate DDL for foreign key.
     java.lang.String createIndex(Table table)
              Generate DDL for indices of given table.
     void createSchema()
              Extracting informations from mapping to schema, this is done by 3 steps.
     java.lang.String generateCreate()
              Generate DDL for create statementof table.
     void generateDDL()
              Generate DDL for a mapping document.
     java.lang.String generateDrop()
              Generate DDL for drop statement of table.
     java.lang.String generateForeignKey()
              Generate DDL for foreign keys.
    abstract  java.lang.String generateHeader()
              Generate header comment.
     java.lang.String generateIndex()
              Generate DDL for indices.
     java.lang.String generateKeyGenerator()
              Generate DDL for key generators (sequence/trigger).
     java.lang.String generatePrimaryKey()
              Generate DDL for primany keys.
     DDLGenConfiguration getConfiguration()
              Get configuration of generator.
     Mapping getMapping()
              Get mapping document.
     MappingHelper getMappingHelper()
              Get mapping helper.
     java.io.PrintStream getPrinter()
              Get print stream.
     Schema getSchema()
              Get schema.
     SchemaFactory getSchemaFactory()
              Get schema factory.
     TypeMapper getTypeMapper()
              Get type mapper.
     void setKeyGenRegistry(KeyGeneratorRegistry keyGenRegistry)
              Set key generator registry.
     void setMapping(Mapping mapping)
              Set mapping document.
    protected  void setMappingHelper(MappingHelper mappingHelper)
              Set mapping helper.
     void setPrinter(java.io.PrintStream printer)
              Set print stream.
    protected  void setSchemaFactory(SchemaFactory schemaFactory)
              Set schema factory.
     void setTypeMapper(TypeMapper typeMapper)
              Set type mapper.
    protected  void write(java.lang.String s)
              Write given string to print stream.
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     
    Methods inherited from interface org.castor.ddlgen.Generator
    getEngineConfigName, getEngineConfigPath, getEngineName, initialize
     

    Constructor Detail

    AbstractGenerator

    protected AbstractGenerator(DDLGenConfiguration configuration)
    Constructor for AbstractGenerator.

    Parameters:
    configuration - Configuration to use by the generator.
    Method Detail

    getConfiguration

    public final DDLGenConfiguration getConfiguration()
    Get configuration of generator.

    Returns:
    Configuration of generator.

    setKeyGenRegistry

    public final void setKeyGenRegistry(KeyGeneratorRegistry keyGenRegistry)
    Set key generator registry.

    Specified by:
    setKeyGenRegistry in interface Generator
    Parameters:
    keyGenRegistry - Key generator registry.

    setMappingHelper

    protected final void setMappingHelper(MappingHelper mappingHelper)
    Set mapping helper.

    Parameters:
    mappingHelper - Mapping helper.

    getMappingHelper

    public final MappingHelper getMappingHelper()
    Get mapping helper.

    Returns:
    Mapping helper.

    setTypeMapper

    public final void setTypeMapper(TypeMapper typeMapper)
    Set type mapper.

    Parameters:
    typeMapper - Type mapper.

    getTypeMapper

    public final TypeMapper getTypeMapper()
    Get type mapper.

    Returns:
    Type mapper.

    setSchemaFactory

    protected final void setSchemaFactory(SchemaFactory schemaFactory)
    Set schema factory.

    Parameters:
    schemaFactory - Schema factory.

    getSchemaFactory

    public final SchemaFactory getSchemaFactory()
    Get schema factory.

    Returns:
    Schema factory.

    setMapping

    public final void setMapping(Mapping mapping)
    Set mapping document.

    Specified by:
    setMapping in interface Generator
    Parameters:
    mapping - Mapping document.

    getMapping

    public final Mapping getMapping()
    Get mapping document.

    Returns:
    Mapping document.

    getSchema

    public final Schema getSchema()
    Get schema.

    Returns:
    Schema

    setPrinter

    public final void setPrinter(java.io.PrintStream printer)
    Set print stream.

    Specified by:
    setPrinter in interface Generator
    Parameters:
    printer - Print stream.

    getPrinter

    public final java.io.PrintStream getPrinter()
    Get print stream.

    Returns:
    Print stream.

    generateDDL

    public final void generateDDL()
                           throws GeneratorException
    Generate DDL for a mapping document.

    Specified by:
    generateDDL in interface Generator
    Throws:
    GeneratorException - If failed to generate DDL.

    generateDrop

    public final java.lang.String generateDrop()
                                        throws GeneratorException
    Generate DDL for drop statement of table.

    Returns:
    DDL to drop table.
    Throws:
    GeneratorException - If failed to generate DDL.

    generateCreate

    public final java.lang.String generateCreate()
                                          throws GeneratorException
    Generate DDL for create statementof table.
     CREATE TABLE prod (
      id INTEGER NOT NULL,
      name CHAR(16)
     );
     
     CREATE TABLE prod_detail (
      id INTEGER NOT NULL,
      prod_id CHAR(16)
     );
     

    Returns:
    DDL for create table.
    Throws:
    GeneratorException - If failed to generate DDL.

    generatePrimaryKey

    public final java.lang.String generatePrimaryKey()
                                              throws GeneratorException
    Generate DDL for primany keys.

    Returns:
    DDL for primary keys.
    Throws:
    GeneratorException - If failed to generate DDL.

    generateForeignKey

    public final java.lang.String generateForeignKey()
                                              throws GeneratorException
    Generate DDL for foreign keys.
     ALTER TABLE `prod_group` ADD CONSTRAINT `FK_prod_group_1` 
     FOREIGN KEY `FK_prod_group_1` (`id`, `name`)
     REFERENCES `category` (`id`, `name`)
     ON DELETE SET NULL
     ON UPDATE CASCADE;
     

    Returns:
    DDL for foreign keys.
    Throws:
    GeneratorException - If failed to generate DDL.

    generateIndex

    public final java.lang.String generateIndex()
                                         throws GeneratorException
    Generate DDL for indices.

    Returns:
    DDL for indices.
    Throws:
    GeneratorException - If failed to generate DDL.

    generateKeyGenerator

    public final java.lang.String generateKeyGenerator()
                                                throws GeneratorException
    Generate DDL for key generators (sequence/trigger).

    Returns:
    DDL for key generators.
    Throws:
    GeneratorException - If failed to generate DDL.

    createForeignKeyDDL

    protected final java.lang.String createForeignKeyDDL(Table table)
                                                  throws GeneratorException
    Generate DDL for foreign key.

    Parameters:
    table - Table to generate DDL of foreign key for.
    Returns:
    DDL for foreign key.
    Throws:
    GeneratorException - If failed to generate DDL.

    createIndex

    public final java.lang.String createIndex(Table table)
                                       throws GeneratorException
    Generate DDL for indices of given table.

    Parameters:
    table - Table to generate DDL of indices for.
    Returns:
    DDL for indices of given table.
    Throws:
    GeneratorException - If failed to generate DDL.

    generateHeader

    public abstract java.lang.String generateHeader()
    Generate header comment.

    Returns:
    Header comment.

    createSchema

    public final void createSchema()
                            throws GeneratorException
    Extracting informations from mapping to schema, this is done by 3 steps.
    • Create key generators
    • Create tables
    • Create additional tables for many-many relations

    Throws:
    GeneratorException - If failed to create schema objects.

    write

    protected final void write(java.lang.String s)
    Write given string to print stream.

    Parameters:
    s - String to write to print stream.


    Intalio Inc. (C) 1999-2007. All rights reserved http://www.intalio.com