| 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);
author: Le Duc Bao author: Ralf Joachim version: $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $ since: 1.1 |