empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1180729 [1/2] - in /incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db: ./ derby/ h2/ hsql/ mysql/ oracle/ postgresql/ sqlserver/
Date Sun, 09 Oct 2011 22:21:32 GMT
Author: doebele
Date: Sun Oct  9 22:21:31 2011
New Revision: 1180729

URL: http://svn.apache.org/viewvc?rev=1180729&view=rev
Log:
EMPIREDB-103
part 2: Common DDL generation code moved to DBDDLGenerator.java
ready for testing.

Added:
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DerbyDDLGenerator.java   (with props)
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/H2DDLGenerator.java   (with props)
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/MySQLDDLGenerator.java   (with props)
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/postgresql/PostgreDDLGenerator.java   (with props)
Modified:
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDDLGenerator.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DBDatabaseDriverDerby.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/DBDatabaseDriverH2.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/hsql/HSqlDDLGenerator.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/oracle/OracleDDLGenerator.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/postgresql/DBDatabaseDriverPostgreSQL.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/sqlserver/MSSqlDDLGenerator.java

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDDLGenerator.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDDLGenerator.java?rev=1180729&r1=1180728&r2=1180729&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDDLGenerator.java (original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDDLGenerator.java Sun Oct  9 22:21:31 2011
@@ -33,20 +33,25 @@ public abstract class DBDDLGenerator<T e
     private static final Logger log = LoggerFactory.getLogger(DBDDLGenerator.class);
     
     protected T driver;
-    
+
+    // Data types
     protected String DATATYPE_INT_SMALL  = "SMALLINT";  // Integer with small size (usually 16-bit)
-    protected String DATATYPE_INTEGER    = "INTEGER";   // Integer with default size (usually 32-bit) 
+    protected String DATATYPE_INTEGER    = "INT";       // Integer with default size (usually 32-bit) 
     protected String DATATYPE_INT_BIG    = "BIGINT";    // Integer with long size (usually 64-bit)
     protected String DATATYPE_CHAR       = "CHAR";      // Fixed length characters (unicode)
     protected String DATATYPE_VARCHAR    = "VARCHAR";   // variable length characters (unicode)      
     protected String DATATYPE_DATE       = "DATE";
     protected String DATATYPE_TIMESTAMP  = "TIMESTAMP";
-    protected String DATATYPE_BOOLEAN    = "BOOLEAN";
+    protected String DATATYPE_BOOLEAN    = "BIT";
     protected String DATATYPE_DECIMAL    = "DECIMAL";
     protected String DATATYPE_FLOAT      = "FLOAT";     // floating point number (double precision 8 bytes)
     protected String DATATYPE_CLOB       = "CLOB";
     protected String DATATYPE_BLOB       = "BLOB";
     protected String DATATYPE_UNIQUEID   = "CHAR(36)";  // Globally Unique Identifier
+
+    // Options
+    protected boolean namePrimaryKeyConstraint = false; // Add name for primary key constraint
+    protected String  alterColumnPhrase  = " ALTER ";   // Phrase for altering a column
     
     protected DBDDLGenerator(T driver)
     {
@@ -156,7 +161,7 @@ public abstract class DBDDLGenerator<T e
             case BLOB:
                 sql.append(DATATYPE_BLOB);
                 if (size > 0) {
-                    sql.append(" (" + ((long)size) + ") ");
+                    sql.append("(" + ((long)size) + ") ");
                 }    
                 break;
             case UNIQUEID:
@@ -174,10 +179,11 @@ public abstract class DBDDLGenerator<T e
     /**
      * Appends a table column definition to a ddl statement
      * @param c the column which description to append
+     * @param alter true if altering an existing column or false otherwise
      * @param sql the sql builder object
      * @return true if the column was successfully appended or false otherwise
      */
-    protected void appendColumnDesc(DBTableColumn c, StringBuilder sql)
+    protected void appendColumnDesc(DBTableColumn c, boolean alter, StringBuilder sql)
     {
         // Append name
         c.addSQL(sql, DBExpr.CTX_NAME);
@@ -340,15 +346,18 @@ public abstract class DBDDLGenerator<T e
                 continue; // Ignore and continue;
             // Append column
             sql.append((addSeparator) ? ",\r\n   " : "\r\n   ");
-            appendColumnDesc(c, sql);
+            appendColumnDesc(c, false, sql);
             addSeparator = true;
         }
         // Primary Key
         DBIndex pk = t.getPrimaryKey();
         if (pk != null)
         { // add the primary key
-            sql.append(",\r\n CONSTRAINT ");
-            appendElementName(sql, pk.getName());
+            sql.append(",\r\n");
+            if (namePrimaryKeyConstraint) {
+                sql.append(" CONSTRAINT ");
+                appendElementName(sql, pk.getName());
+            }
             sql.append(" PRIMARY KEY (");
             addSeparator = false;
             // columns
@@ -364,6 +373,19 @@ public abstract class DBDDLGenerator<T e
         sql.append(")");
         // Create the table
         addCreateTableStmt(t, sql, script);
+        // Create all Indexes
+        createTableIndexes(t, pk, script);        
+    }
+
+    /**
+     * Appends the DDL-Script for creating all indexes of table (except the primary key) to an SQL-Script 
+     * @param t the table to create
+     * @param pk the primary key index to ignore
+     * @param script the sql script to which to append the dll command(s)
+     */
+    protected void createTableIndexes(DBTable t, DBIndex pk, DBSQLScript script)
+    {
+        StringBuilder sql = new StringBuilder();
         // Create other Indexes (except primary key)
         Iterator<DBIndex> indexes = t.getIndexes().iterator();
         while (indexes.hasNext())
@@ -379,9 +401,9 @@ public abstract class DBDDLGenerator<T e
             sql.append(" ON ");
             t.addSQL(sql, DBExpr.CTX_FULLNAME);
             sql.append(" (");
-            addSeparator = false;
 
             // columns
+            boolean addSeparator = false;
             DBColumn[] idxColumns = idx.getColumns();
             for (int i = 0; i < idxColumns.length; i++)
             {
@@ -395,7 +417,7 @@ public abstract class DBDDLGenerator<T e
             addCreateIndexStmt(idx, sql, script);
         }
     }
-
+    
     /**
      * Appends the DDL-Script for creating the given foreign-key relation to an SQL-Script 
      * @param r the relation to create
@@ -457,11 +479,16 @@ public abstract class DBDDLGenerator<T e
         {
             case CREATE:
                 sql.append(" ADD ");
-                appendColumnDesc(col, sql);
+                appendColumnDesc(col, false, sql);
                 break;
             case ALTER:
-                sql.append(" MODIFY ");
-                appendColumnDesc(col, sql);
+                sql.append(alterColumnPhrase);
+                /*
+                sql.append(" ALTER "); // Derby, H2,
+                sql.append(" MODIFY "); // MySQL, Oracle
+                sql.append(" ALTER COLUMN ");   // HSQL, Postgre, SQLServer
+                */                  
+                appendColumnDesc(col, true, sql);
                 break;
             case DROP:
                 sql.append(" DROP COLUMN ");

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java?rev=1180729&r1=1180728&r2=1180729&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java (original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java Sun Oct  9 22:21:31 2011
@@ -44,6 +44,12 @@ import org.apache.empire.exceptions.Unex
  */
 public class DBTable extends DBRowSet implements Cloneable
 {
+    // Integer size definitions
+    public static final int DEFAULT   = 0;
+    public static final int SMALLINT  = 2;
+    public static final int MEDIUMINT = 4;
+    public static final int BIGINT    = 8;
+
     private final static long serialVersionUID = 1L;
     private static AtomicInteger tableCount  = new AtomicInteger(0);
     private final String   name;
@@ -51,6 +57,7 @@ public class DBTable extends DBRowSet im
     private List<DBIndex>  indexes       = new ArrayList<DBIndex>();
     private boolean        cascadeDelete = false;
     private Boolean        quoteName     = null;
+    
 
     /**
      * Construct a new DBTable object set the specified parameters

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DBDatabaseDriverDerby.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DBDatabaseDriverDerby.java?rev=1180729&r1=1180728&r2=1180729&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DBDatabaseDriverDerby.java (original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DBDatabaseDriverDerby.java Sun Oct  9 22:21:31 2011
@@ -20,28 +20,17 @@ package org.apache.empire.db.derby;
 
 import java.sql.Connection;
 import java.util.GregorianCalendar;
-import java.util.Iterator;
 
-import org.apache.empire.commons.StringUtils;
 import org.apache.empire.data.DataType;
 import org.apache.empire.db.DBCmdType;
-import org.apache.empire.db.DBColumn;
 import org.apache.empire.db.DBCommand;
-import org.apache.empire.db.DBCommandExpr;
+import org.apache.empire.db.DBDDLGenerator;
 import org.apache.empire.db.DBDatabase;
 import org.apache.empire.db.DBDatabaseDriver;
 import org.apache.empire.db.DBDriverFeature;
-import org.apache.empire.db.DBExpr;
-import org.apache.empire.db.DBIndex;
 import org.apache.empire.db.DBObject;
-import org.apache.empire.db.DBRelation;
 import org.apache.empire.db.DBSQLScript;
 import org.apache.empire.db.DBTable;
-import org.apache.empire.db.DBTableColumn;
-import org.apache.empire.db.DBView;
-import org.apache.empire.exceptions.InvalidArgumentException;
-import org.apache.empire.exceptions.NotImplementedException;
-import org.apache.empire.exceptions.NotSupportedException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -75,6 +64,8 @@ public class DBDatabaseDriverDerby exten
     // When set to 'false' (default) Derby's autoincrement feature is used.
     private boolean useSequenceTable = false;
     private String sequenceTableName = "Sequences";
+
+    private DBDDLGenerator<?> ddlGenerator = null; // lazy creation
     
     /**
      * Constructor for the Derby database driver.<br>
@@ -286,81 +277,6 @@ public class DBDatabaseDriverDerby exten
         }
     }
 
-    /** {@inheritDoc} */
-    @Override
-    public void getDDLScript(DBCmdType type, DBObject dbo, DBSQLScript script)
-    {
-        // The Object's database must be attached to this driver
-        if (dbo == null || dbo.getDatabase().getDriver() != this)
-            throw new InvalidArgumentException("dbo", String.valueOf(dbo));
-        // Check Type of object
-        if (dbo instanceof DBDatabase)
-        { // Database
-            switch (type)
-            {
-                case CREATE:
-                    createDatabase((DBDatabase) dbo, script, true);
-                    return;
-                case DROP:
-                    dropObject(((DBDatabase) dbo).getSchema(), "DATABASE", script);
-                    return;
-                default:
-                    throw new NotImplementedException(this, "getDDLScript." + dbo.getClass().getName() + "." + type);
-            }
-        } 
-        else if (dbo instanceof DBTable)
-        { // Table
-            switch (type)
-            {
-                case CREATE:
-                    createTable((DBTable) dbo, script);
-                    return;
-                case DROP:
-                    dropObject(((DBTable) dbo).getName(), "TABLE", script);
-                    return;
-                default:
-                    throw new NotImplementedException(this, "getDDLScript." + dbo.getClass().getName() + "." + type);
-            }
-        } 
-        else if (dbo instanceof DBView)
-        { // View
-            switch (type)
-            {
-                case CREATE:
-                    createView((DBView) dbo, script);
-                    return;
-                case DROP:
-                    dropObject(((DBView) dbo).getName(), "VIEW", script);
-                    return;
-                default:
-                    throw new NotImplementedException(this, "getDDLScript." + dbo.getClass().getName() + "." + type);
-            }
-        } 
-        else if (dbo instanceof DBRelation)
-        { // Relation
-            switch (type)
-            {
-                case CREATE:
-                    createRelation((DBRelation) dbo, script);
-                    return;
-                case DROP:
-                    dropObject(((DBRelation) dbo).getName(), "CONSTRAINT", script);
-                    return;
-                default:
-                    throw new NotImplementedException(this, "getDDLScript." + dbo.getClass().getName() + "." + type);
-            }
-        } 
-        else if (dbo instanceof DBTableColumn)
-        { // Table Column
-            alterTable((DBTableColumn) dbo, type, script);
-            return;
-        } 
-        else
-        { // dll generation not supported for this type
-            throw new NotSupportedException(this, "getDDLScript() for "+dbo.getClass().getName());
-        }
-    }
-
     /**
      * Overridden. Returns a timestamp that is used for record updates created by the database server.
      * 
@@ -374,377 +290,16 @@ public class DBDatabaseDriverDerby exten
         return new java.sql.Timestamp(cal.getTimeInMillis());
     }
 
-    /*
-     * return the sql for creating a Database
-     */
-    protected void createDatabase(DBDatabase db, DBSQLScript script, boolean createSchema)
-    {
-//        // User Master to create Database
-//        if (createSchema)
-//        {   // check database Name
-//            if (StringUtils.isValid(databaseName)==false)
-//                return error(Errors.InvalidProperty, "databaseName");
-//            // Create Database
-//            script.addStmt("CREATE DATABASE " + databaseName + " CHARACTER SET " + characterSet);
-//            script.addStmt("USE " + databaseName);
-//            // appendDDLStmt(db, "SET DATEFORMAT ymd", buf);
-//            // Sequence Table
-//            if (useSequenceTable && db.getTable(sequenceTableName)==null)
-//                new DBSeqTable(sequenceTableName, db);
-//        }
-        // Create all Tables
-        Iterator<DBTable> tables = db.getTables().iterator();
-        while (tables.hasNext())
-        {
-            createTable(tables.next(), script);
-        }
-        // Create Relations
-        Iterator<DBRelation> relations = db.getRelations().iterator();
-        while (relations.hasNext())
-        {
-            createRelation(relations.next(), script);
-        }
-        // Create Views
-        Iterator<DBView> views = db.getViews().iterator();
-        while (views.hasNext())
-        {
-            createView(views.next(), script);
-        }
-    }
-    
-    /**
-     * Returns true if the table has been created successfully.
-     * 
-     * @return true if the table has been created successfully
-     */
-    protected void createTable(DBTable t, DBSQLScript script)
-    {
-        StringBuilder sql = new StringBuilder();
-        sql.append("-- creating table ");
-        sql.append(t.getName());
-        sql.append(" --\r\n");
-        sql.append("CREATE TABLE ");
-        t.addSQL(sql, DBExpr.CTX_FULLNAME);
-        sql.append(" (");
-        boolean addSeparator = false;
-        Iterator<DBColumn> columns = t.getColumns().iterator();
-        while (columns.hasNext())
-        {
-            DBTableColumn c = (DBTableColumn) columns.next();
-            sql.append((addSeparator) ? ",\r\n   " : "\r\n   ");
-            if (appendColumnDesc(c, sql, false) == false)
-                continue; // Ignore and continue;
-            addSeparator = true;
-        }
-        // Primary Key
-        DBIndex pk = t.getPrimaryKey();
-        if (pk != null)
-        { // add the primary key
-            sql.append(", PRIMARY KEY (");
-            addSeparator = false;
-            // columns
-            DBColumn[] keyColumns = pk.getColumns();
-            for (int i = 0; i < keyColumns.length; i++)
-            {
-                sql.append((addSeparator) ? ", " : "");
-                keyColumns[i].addSQL(sql, DBExpr.CTX_NAME);
-                addSeparator = true;
-            }
-            sql.append(")");
-        }
-        sql.append(")");
-        // Comment?
-        String comment = t.getComment();
-        if (StringUtils.isNotEmpty(comment))
-        {   // Add the table comment
-            sql.append(" COMMENT = '");
-            sql.append(comment);
-            sql.append("'");
-        }
-        // Create the table
-        script.addStmt(sql);
-        // Create other Indexes (except primary key)
-        Iterator<DBIndex> indexes = t.getIndexes().iterator();
-        while (indexes.hasNext())
-        {
-            DBIndex idx = indexes.next();
-            if (idx == pk || idx.getType() == DBIndex.PRIMARYKEY)
-                continue;
-
-            // Cretae Index
-            sql.setLength(0);
-            sql.append((idx.getType() == DBIndex.UNIQUE) ? "CREATE UNIQUE INDEX " : "CREATE INDEX ");
-            appendElementName(sql, idx.getName());
-            sql.append(" ON ");
-            t.addSQL(sql, DBExpr.CTX_FULLNAME);
-            sql.append(" (");
-            addSeparator = false;
-
-            // columns
-            DBColumn[] idxColumns = idx.getColumns();
-            for (int i = 0; i < idxColumns.length; i++)
-            {
-                sql.append((addSeparator) ? ", " : "");
-                idxColumns[i].addSQL(sql, DBExpr.CTX_NAME);
-                addSeparator = true;
-            }
-            sql.append(")");
-            // Create Index
-            script.addStmt(sql);
-        }
-    }
-    
-    /**
-     * Appends a table column definition to a ddl statement
-     * @param c the column which description to append
-     * @param sql the sql builder object
-     * @return true if the column was successfully appended or false otherwise
-     */
-    protected boolean appendColumnDesc(DBTableColumn c, StringBuilder sql, boolean alter)
-    {
-        // Append name
-        c.addSQL(sql, DBExpr.CTX_NAME);
-        if(alter){
-        	sql.append(" SET DATA TYPE ");
-        }else{
-        	sql.append(" ");
-        }
-        switch (c.getDataType())
-        {
-            case INTEGER:
-            { // Integer type
-                int size = (int)c.getSize();
-                if (size >= 8) {
-                    sql.append("BIGINT");
-                } else {
-                    sql.append("INT");
-                }
-                break;
-            }
-            case AUTOINC:
-            { // Auto increment
-                int size = (int)c.getSize();
-                if (size >= 8) {
-                    sql.append("BIGINT");
-                } else {
-                    sql.append("INT");
-                }
-                if (!isUseSequenceTable())
-                    sql.append(" GENERATED ALWAYS AS IDENTITY");
-                break;
-            }
-            case TEXT:
-            { // Check fixed or variable length
-                int size = Math.abs((int) c.getSize());
-                if (size == 0)
-                    size = 100;
-                sql.append("VARCHAR(");
-                sql.append(String.valueOf(size));
-                sql.append(")");
-            }
-                break;
-            case CHAR:
-            { // Check fixed or variable length
-                int size = Math.abs((int) c.getSize());
-                if (size == 0)
-                    size = 1;
-                sql.append("CHAR(");
-                sql.append(String.valueOf(size));
-                sql.append(")");
-            }
-                break;
-            case DATE:
-                sql.append("DATE");
-                break;
-            case DATETIME:
-                sql.append("TIMESTAMP");
-                break;
-            case BOOL:
-//            	if ( booleanType== DBDatabaseDriverOracle.BooleanType.CHAR ) <- this looks weird to me :), i commented it out since it is always false for derby
-//                    sql.append("CHAR(1)");
-//                else
-                sql.append("SMALLINT");
-                break;
-            case FLOAT:
-                sql.append("DOUBLE");
-                break;
-            case DECIMAL:
-            { // Decimal
-                sql.append("DECIMAL(");
-                int prec = (int) c.getSize();
-                int scale = (int) ((c.getSize() - prec) * 10 + 0.5);
-                // sql.append((prec+scale).ToString());sql.append(",");
-                sql.append(String.valueOf(prec));
-                sql.append(",");
-                sql.append(String.valueOf(scale));
-                sql.append(")");
-            }
-                break;
-            case CLOB:
-                sql.append("CLOB");
-                if (c.getSize() > 0)
-                {
-                    sql.append("(" + (long) c.getSize() + ") ");
-                }
-                break;
-            case BLOB:
-                sql.append("BLOB");
-                if (c.getSize() > 0)
-                {
-                    sql.append("(" + (long) c.getSize() + ") ");
-                }
-                break;
-            case UNIQUEID:
-                // emulate using java.util.UUID
-                sql.append("CHAR(36)");
-                break;
-            case UNKNOWN:
-                 log.error("Cannot append column of Data-Type 'UNKNOWN'");
-                 return false;
-        }
-        // Default Value
-        if (isDDLColumnDefaults() && !c.isAutoGenerated() && c.getDefaultValue()!=null)
-        {   sql.append(" DEFAULT ");
-            sql.append(getValueString(c.getDefaultValue(), c.getDataType()));
-        }
-        // Nullable
-        if (c.isRequired() ||  c.isAutoGenerated())
-            sql.append(" NOT NULL");
-        // Done
-        return true;
-    }
-
-    /**
-     * Returns true if the relation has been created successfully.
-     * 
-     * @return true if the relation has been created successfully
-     */
-    protected void createRelation(DBRelation r, DBSQLScript script)
-    {
-        DBTable sourceTable = (DBTable) r.getReferences()[0].getSourceColumn().getRowSet();
-        DBTable targetTable = (DBTable) r.getReferences()[0].getTargetColumn().getRowSet();
-
-        StringBuilder sql = new StringBuilder();
-        sql.append("-- creating foreign key constraint ");
-        sql.append(r.getName());
-        sql.append(" --\r\n");
-        sql.append("ALTER TABLE ");
-        sourceTable.addSQL(sql, DBExpr.CTX_FULLNAME);
-        sql.append(" ADD CONSTRAINT ");
-        appendElementName(sql, r.getName());
-        sql.append(" FOREIGN KEY (");
-        // Source Names
-        boolean addSeparator = false;
-        DBRelation.DBReference[] refs = r.getReferences();
-        for (int i = 0; i < refs.length; i++)
-        {
-            sql.append((addSeparator) ? ", " : "");
-            refs[i].getSourceColumn().addSQL(sql, DBExpr.CTX_NAME);
-            addSeparator = true;
-        }
-        // References
-        sql.append(") REFERENCES ");
-        targetTable.addSQL(sql, DBExpr.CTX_FULLNAME);
-        sql.append(" (");
-        // Target Names
-        addSeparator = false;
-        for (int i = 0; i < refs.length; i++)
-        {
-            sql.append((addSeparator) ? ", " : "");
-            refs[i].getTargetColumn().addSQL(sql, DBExpr.CTX_NAME);
-            addSeparator = true;
-        }
-        // done
-        sql.append(")");
-        script.addStmt(sql);
-    }
-
-    /**
-     * Creates an alter table dll statement for adding, modifying or dropping a column.
-     * @param col the column which to add, modify or drop
-     * @param type the type of operation to perform
-     * @param script to which to append the sql statement to
-     * @return true if the statement was successfully appended to the buffer
-     */
-    protected void alterTable(DBTableColumn col, DBCmdType type, DBSQLScript script)
-    {
-        StringBuilder sql = new StringBuilder();
-        sql.append("ALTER TABLE ");
-        col.getRowSet().addSQL(sql, DBExpr.CTX_FULLNAME);
-        switch(type)
-        {
-            case CREATE:
-                sql.append(" ADD ");
-                appendColumnDesc(col, sql, false);
-                break;
-            case ALTER:
-                sql.append(" ALTER ");
-                appendColumnDesc(col, sql, true);
-                break;
-            case DROP:
-                sql.append(" DROP COLUMN ");
-                sql.append(col.getName());
-                break;
-        }
-        // done
-        script.addStmt(sql);
-    }
-
     /**
-     * Returns true if the view has been created successfully.
-     * 
-     * @return true if the view has been created successfully
+     * @see DBDatabaseDriver#getDDLScript(DBCmdType, DBObject, DBSQLScript)  
      */
-    protected void createView(DBView v, DBSQLScript script)
-    {
-        // Create the Command
-        DBCommandExpr cmd = v.createCommand();
-        if (cmd==null)
-        {   // Check whether Error information is available
-            log.error("No command has been supplied for view " + v.getName());
-            // No error information available: Use Errors.NotImplemented
-            throw new NotImplementedException(this, v.getName() + ".createCommand");
-        }
-        // Make sure there is no OrderBy
-        cmd.clearOrderBy();
-
-        // Build String
-        StringBuilder sql = new StringBuilder();
-        sql.append( "CREATE VIEW ");
-        v.addSQL(sql, DBExpr.CTX_FULLNAME);
-        sql.append( " (" );
-        boolean addSeparator = false;
-        for(DBColumn c : v.getColumns())
-        {
-            if (addSeparator)
-                sql.append(", ");
-            // Add Column name
-            c.addSQL(sql, DBExpr.CTX_NAME);
-            // next
-            addSeparator = true;
-        }
-        sql.append(")\r\nAS\r\n");
-        cmd.addSQL( sql, DBExpr.CTX_DEFAULT);
-        // done
-        script.addStmt(sql.toString());
-    }
-    
-    /**
-     * Returns true if the object has been dropped successfully.
-     * 
-     * @return true if the object has been dropped successfully
-     */
-    protected void dropObject(String name, String objType, DBSQLScript script)
+    @Override
+    public void getDDLScript(DBCmdType type, DBObject dbo, DBSQLScript script)
     {
-        if (name == null || name.length() == 0)
-            throw new InvalidArgumentException("name", name);
-        // Create Drop Statement
-        StringBuilder sql = new StringBuilder();
-        sql.append("DROP ");
-        sql.append(objType);
-        sql.append(" ");
-        appendElementName(sql, name);
-        script.addStmt(sql);
+        if (ddlGenerator==null)
+            ddlGenerator = new DerbyDDLGenerator(this);
+        // forward request
+        ddlGenerator.getDDLScript(type, dbo, script); 
     }
 
 }

Added: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DerbyDDLGenerator.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DerbyDDLGenerator.java?rev=1180729&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DerbyDDLGenerator.java (added)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DerbyDDLGenerator.java Sun Oct  9 22:21:31 2011
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.empire.db.derby;
+
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBDDLGenerator;
+import org.apache.empire.db.DBExpr;
+import org.apache.empire.db.DBTableColumn;
+
+public class DerbyDDLGenerator extends DBDDLGenerator<DBDatabaseDriverDerby>
+{
+    public DerbyDDLGenerator(DBDatabaseDriverDerby driver)
+    {
+        super(driver);
+        // set Oracle specific data types
+        initDataTypes();
+    }
+
+    /**
+     * sets Oracle specific data types
+     * @param driver
+     */
+    private void initDataTypes()
+    {   // Override data types
+        DATATYPE_BOOLEAN = "SMALLINT";
+    }
+
+    @Override
+    protected boolean appendColumnDataType(DataType type, double size, DBTableColumn c, StringBuilder sql)
+    {
+        switch (type)
+        {
+            case AUTOINC:
+            { // Auto increment
+                super.appendColumnDataType(type, size, c, sql);
+                if (!driver.isUseSequenceTable())
+                    sql.append(" GENERATED ALWAYS AS IDENTITY");
+                break;
+            }
+            default:
+                // use default
+                return super.appendColumnDataType(type, size, c, sql);
+        }
+        return true;
+    }
+    
+    @Override
+    protected void appendColumnDesc(DBTableColumn c, boolean alter, StringBuilder sql)
+    {
+        // Append name
+        c.addSQL(sql, DBExpr.CTX_NAME);
+        if (alter) {
+            sql.append(" SET DATA TYPE ");
+        } else {
+            sql.append(" ");
+        }
+        // Unknown data type
+        if (!appendColumnDataType(c.getDataType(), c.getSize(), c, sql))
+            return;
+        // Default Value
+        if (driver.isDDLColumnDefaults() && !c.isAutoGenerated() && c.getDefaultValue()!=null)
+        {   sql.append(" DEFAULT ");
+            sql.append(driver.getValueString(c.getDefaultValue(), c.getDataType()));
+        }
+        // Nullable
+        if (c.isRequired() ||  c.isAutoGenerated())
+            sql.append(" NOT NULL");
+    }
+    
+}

Propchange: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DerbyDDLGenerator.java
------------------------------------------------------------------------------
    eol-style = native

Propchange: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DerbyDDLGenerator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/DBDatabaseDriverH2.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/DBDatabaseDriverH2.java?rev=1180729&r1=1180728&r2=1180729&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/DBDatabaseDriverH2.java (original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/DBDatabaseDriverH2.java Sun Oct  9 22:21:31 2011
@@ -20,28 +20,17 @@ package org.apache.empire.db.h2;
 
 import java.sql.Connection;
 import java.util.GregorianCalendar;
-import java.util.Iterator;
 
-import org.apache.empire.commons.StringUtils;
 import org.apache.empire.data.DataType;
 import org.apache.empire.db.DBCmdType;
-import org.apache.empire.db.DBColumn;
 import org.apache.empire.db.DBCommand;
-import org.apache.empire.db.DBCommandExpr;
+import org.apache.empire.db.DBDDLGenerator;
 import org.apache.empire.db.DBDatabase;
 import org.apache.empire.db.DBDatabaseDriver;
 import org.apache.empire.db.DBDriverFeature;
-import org.apache.empire.db.DBExpr;
-import org.apache.empire.db.DBIndex;
 import org.apache.empire.db.DBObject;
-import org.apache.empire.db.DBRelation;
 import org.apache.empire.db.DBSQLScript;
 import org.apache.empire.db.DBTable;
-import org.apache.empire.db.DBTableColumn;
-import org.apache.empire.db.DBView;
-import org.apache.empire.exceptions.InvalidArgumentException;
-import org.apache.empire.exceptions.NotImplementedException;
-import org.apache.empire.exceptions.NotSupportedException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -75,6 +64,8 @@ public class DBDatabaseDriverH2 extends 
     // When set to 'false' (default) H2's autoincrement feature is used.
     private boolean useSequenceTable = false;
     private String sequenceTableName = "Sequences";
+
+    private DBDDLGenerator<?> ddlGenerator = null; // lazy creation
     
     /**
      * Constructor for the H2 database driver.<br>
@@ -289,83 +280,6 @@ public class DBDatabaseDriverH2 extends 
     }
 
     /**
-     * @see DBDatabaseDriver#getDDLScript(DBCmdType, DBObject, DBSQLScript)  
-     */
-    @Override
-    public void getDDLScript(DBCmdType type, DBObject dbo, DBSQLScript script)
-    {
-        // The Object's database must be attached to this driver
-        if (dbo==null || dbo.getDatabase().getDriver()!=this)
-            throw new InvalidArgumentException("dbo", dbo);
-        // Check Type of object
-        if (dbo instanceof DBDatabase)
-        { // Database
-            switch (type)
-            {
-                case CREATE:
-                    createDatabase((DBDatabase) dbo, script, true);
-                    return;
-                case DROP:
-                    dropObject(((DBDatabase) dbo).getSchema(), "DATABASE", script);
-                    return;
-                default:
-                    throw new NotImplementedException(this, "getDDLScript." + dbo.getClass().getName() + "." + type);
-            }
-        } 
-        else if (dbo instanceof DBTable)
-        { // Table
-            switch (type)
-            {
-                case CREATE:
-                    createTable((DBTable) dbo, script);
-                    return;
-                case DROP:
-                    dropObject(((DBTable) dbo).getName(), "TABLE", script);
-                    return;
-                default:
-                    throw new NotImplementedException(this, "getDDLScript." + dbo.getClass().getName() + "." + type);
-            }
-        } 
-        else if (dbo instanceof DBView)
-        { // View
-            switch (type)
-            {
-                case CREATE:
-                    createView((DBView) dbo, script);
-                    return;
-                case DROP:
-                    dropObject(((DBView) dbo).getName(), "VIEW", script);
-                    return;
-                default:
-                    throw new NotImplementedException(this, "getDDLScript." + dbo.getClass().getName() + "." + type);
-            }
-        } 
-        else if (dbo instanceof DBRelation)
-        { // Relation
-            switch (type)
-            {
-                case CREATE:
-                    createRelation((DBRelation) dbo, script);
-                    return;
-                case DROP:
-                    dropObject(((DBRelation) dbo).getName(), "CONSTRAINT", script);
-                    return;
-                default:
-                    throw new NotImplementedException(this, "getDDLScript." + dbo.getClass().getName() + "." + type);
-            }
-        } 
-        else if (dbo instanceof DBTableColumn)
-        { // Table Column
-            alterTable((DBTableColumn) dbo, type, script);
-            return;
-        } 
-        else
-        { // dll generation not supported for this type
-            throw new NotSupportedException(this, "getDDLScript() for "+dbo.getClass().getName());
-        }
-    }
-
-    /**
      * Overridden. Returns a timestamp that is used for record updates created by the database server.
      * 
      * @return the current date and time of the database server.
@@ -378,360 +292,16 @@ public class DBDatabaseDriverH2 extends 
         return new java.sql.Timestamp(cal.getTimeInMillis());
     }
 
-    /*
-     * return the sql for creating a Database
-     */
-    protected boolean createDatabase(DBDatabase db, DBSQLScript script, boolean createSchema)
-    {
-//        // User Master to create Database
-//        if (createSchema)
-//        {   // check database Name
-//            if (StringUtils.isValid(databaseName)==false)
-//                return error(Errors.InvalidProperty, "databaseName");
-//            // Create Database
-//            script.addStmt("CREATE DATABASE " + databaseName + " CHARACTER SET " + characterSet);
-//            script.addStmt("USE " + databaseName);
-//            // appendDDLStmt(db, "SET DATEFORMAT ymd", buf);
-//            // Sequence Table
-//            if (useSequenceTable && db.getTable(sequenceTableName)==null)
-//                new DBSeqTable(sequenceTableName, db);
-//        }
-        // Create all Tables
-        Iterator<DBTable> tables = db.getTables().iterator();
-        while (tables.hasNext())
-        {
-            createTable(tables.next(), script);
-        }
-        // Create Relations
-        Iterator<DBRelation> relations = db.getRelations().iterator();
-        while (relations.hasNext())
-        {
-            createRelation(relations.next(), script);
-        }
-        // Create Views
-        Iterator<DBView> views = db.getViews().iterator();
-        while (views.hasNext())
-        {
-            createView(views.next(), script);
-        }
-        // Done
-        return true;
-    }
-    
-    /**
-     * Returns true if the table has been created successfully.
-     * 
-     * @return true if the table has been created successfully
-     */
-    protected void createTable(DBTable t, DBSQLScript script)
-    {
-        StringBuilder sql = new StringBuilder();
-        sql.append("-- creating table ");
-        sql.append(t.getName());
-        sql.append(" --\r\n");
-        sql.append("CREATE TABLE ");
-        t.addSQL(sql, DBExpr.CTX_FULLNAME);
-        sql.append(" (");
-        boolean addSeparator = false;
-        Iterator<DBColumn> columns = t.getColumns().iterator();
-        while (columns.hasNext())
-        {
-            DBTableColumn c = (DBTableColumn) columns.next();
-            sql.append((addSeparator) ? ",\r\n   " : "\r\n   ");
-            if (appendColumnDesc(c, sql)==false)
-                continue; // Ignore and continue;
-            addSeparator = true;
-        }
-        // Primary Key
-        DBIndex pk = t.getPrimaryKey();
-        if (pk != null)
-        { // add the primary key
-            sql.append(", PRIMARY KEY (");
-            addSeparator = false;
-            // columns
-            DBColumn[] keyColumns = pk.getColumns();
-            for (int i = 0; i < keyColumns.length; i++)
-            {
-                sql.append((addSeparator) ? ", " : "");
-                keyColumns[i].addSQL(sql, DBExpr.CTX_NAME);
-                addSeparator = true;
-            }
-            sql.append(")");
-        }
-        sql.append(")");
-        // Comment?
-        String comment = t.getComment();
-        if (StringUtils.isNotEmpty(comment))
-        {   // Add the table comment
-            sql.append(" COMMENT = '");
-            sql.append(comment);
-            sql.append("'");
-        }
-        // Create the table
-        script.addStmt(sql);
-        // Create other Indexes (except primary key)
-        Iterator<DBIndex> indexes = t.getIndexes().iterator();
-        while (indexes.hasNext())
-        {
-            DBIndex idx = indexes.next();
-            if (idx == pk || idx.getType() == DBIndex.PRIMARYKEY)
-                continue;
-
-            // Cretae Index
-            sql.setLength(0);
-            sql.append((idx.getType() == DBIndex.UNIQUE) ? "CREATE UNIQUE INDEX " : "CREATE INDEX ");
-            appendElementName(sql, idx.getName());
-            sql.append(" ON ");
-            t.addSQL(sql, DBExpr.CTX_FULLNAME);
-            sql.append(" (");
-            addSeparator = false;
-
-            // columns
-            DBColumn[] idxColumns = idx.getColumns();
-            for (int i = 0; i < idxColumns.length; i++)
-            {
-                sql.append((addSeparator) ? ", " : "");
-                idxColumns[i].addSQL(sql, DBExpr.CTX_NAME);
-                addSeparator = true;
-            }
-            sql.append(")");
-            // Create Index
-            script.addStmt(sql);
-        }
-    }
-    
-    /**
-     * Appends a table column definition to a ddl statement
-     * @param c the column which description to append
-     * @param sql the sql builder object
-     * @return true if the column was successfully appended or false otherwise
-     */
-    protected boolean appendColumnDesc(DBTableColumn c, StringBuilder sql)
-    {
-        // Append name
-        c.addSQL(sql, DBExpr.CTX_NAME);
-        sql.append(" ");
-        switch (c.getDataType())
-        {
-            case INTEGER:
-            {
-                int size = (int) c.getSize();
-                if (size >= 8) {
-                    sql.append("BIGINT");
-                } else {
-                    sql.append("INT");
-                }
-                break;
-            }    
-            case AUTOINC:
-            { // Auto increment
-                sql.append("INT");
-                if (useSequenceTable==false)
-                    sql.append(" AUTO_INCREMENT");
-                break;
-            }    
-            case TEXT:
-            { // Check fixed or variable length
-                int size = Math.abs((int) c.getSize());
-                if (size == 0)
-                    size = 100;
-                sql.append("VARCHAR(");
-                sql.append(String.valueOf(size));
-                sql.append(")");
-            }
-                break;
-            case CHAR:
-            { // Check fixed or variable length
-                int size = Math.abs((int) c.getSize());
-                if (size == 0)
-                    size = 1;
-                sql.append("CHAR(");
-                sql.append(String.valueOf(size));
-                sql.append(")");
-            }
-                break;
-            case DATE:
-                sql.append("DATE");
-                break;
-            case DATETIME:
-                sql.append("DATETIME");
-                break;
-            case BOOL:
-                sql.append("BIT");
-                break;
-            case FLOAT:
-                sql.append("DOUBLE");
-                break;
-            case DECIMAL:
-            { // Decimal
-                sql.append("DECIMAL(");
-                int prec = (int) c.getSize();
-                int scale = (int) ((c.getSize() - prec) * 10 + 0.5);
-                // sql.append((prec+scale).ToString());sql.append(",");
-                sql.append(String.valueOf(prec));
-                sql.append(",");
-                sql.append(String.valueOf(scale));
-                sql.append(")");
-            }
-                break;
-            case CLOB:
-                sql.append("LONGTEXT");
-                break;
-            case BLOB:
-                sql.append("BLOB");
-                if (c.getSize() > 0)
-                    sql.append(" (" + (long) c.getSize() + ") ");
-                break;
-            case UNIQUEID:
-                // emulate using java.util.UUID
-                sql.append("CHAR(36)");
-                break;
-            case UNKNOWN:
-                log.warn("Cannot append column of Data-Type 'UNKNOWN'");
-                return false;
-        }
-        // Default Value
-        if (isDDLColumnDefaults() && !c.isAutoGenerated() && c.getDefaultValue()!=null)
-        {   sql.append(" DEFAULT ");
-            sql.append(getValueString(c.getDefaultValue(), c.getDataType()));
-        }
-        // Nullable
-        if (c.isRequired() ||  c.isAutoGenerated())
-            sql.append(" NOT NULL");
-        // Done
-        return true;
-    }
-
-    /**
-     * Returns true if the relation has been created successfully.
-     * 
-     * @return true if the relation has been created successfully
-     */
-    protected void createRelation(DBRelation r, DBSQLScript script)
-    {
-        DBTable sourceTable = (DBTable) r.getReferences()[0].getSourceColumn().getRowSet();
-        DBTable targetTable = (DBTable) r.getReferences()[0].getTargetColumn().getRowSet();
-
-        StringBuilder sql = new StringBuilder();
-        sql.append("-- creating foreign key constraint ");
-        sql.append(r.getName());
-        sql.append(" --\r\n");
-        sql.append("ALTER TABLE ");
-        sourceTable.addSQL(sql, DBExpr.CTX_FULLNAME);
-        sql.append(" ADD CONSTRAINT ");
-        appendElementName(sql, r.getName());
-        sql.append(" FOREIGN KEY (");
-        // Source Names
-        boolean addSeparator = false;
-        DBRelation.DBReference[] refs = r.getReferences();
-        for (int i = 0; i < refs.length; i++)
-        {
-            sql.append((addSeparator) ? ", " : "");
-            refs[i].getSourceColumn().addSQL(sql, DBExpr.CTX_NAME);
-            addSeparator = true;
-        }
-        // References
-        sql.append(") REFERENCES ");
-        targetTable.addSQL(sql, DBExpr.CTX_FULLNAME);
-        sql.append(" (");
-        // Target Names
-        addSeparator = false;
-        for (int i = 0; i < refs.length; i++)
-        {
-            sql.append((addSeparator) ? ", " : "");
-            refs[i].getTargetColumn().addSQL(sql, DBExpr.CTX_NAME);
-            addSeparator = true;
-        }
-        // done
-        sql.append(")");
-        script.addStmt(sql);
-    }
-
-    /**
-     * Creates an alter table dll statement for adding, modifying or dropping a column.
-     * @param col the column which to add, modify or drop
-     * @param type the type of operation to perform
-     * @param script to which to append the sql statement to
-     * @return true if the statement was successfully appended to the buffer
-     */
-    protected void alterTable(DBTableColumn col, DBCmdType type, DBSQLScript script)
-    {
-        StringBuilder sql = new StringBuilder();
-        sql.append("ALTER TABLE ");
-        col.getRowSet().addSQL(sql, DBExpr.CTX_FULLNAME);
-        switch(type)
-        {
-            case CREATE:
-                sql.append(" ADD ");
-                appendColumnDesc(col, sql);
-                break;
-            case ALTER:
-                sql.append(" ALTER ");
-                appendColumnDesc(col, sql);
-                break;
-            case DROP:
-                sql.append(" DROP COLUMN ");
-                sql.append(col.getName());
-                break;
-        }
-        // done
-        script.addStmt(sql);
-    }
-
-    /**
-     * Returns true if the view has been created successfully.
-     * 
-     * @return true if the view has been created successfully
-     */
-    protected void createView(DBView v, DBSQLScript script)
-    {
-        // Create the Command
-        DBCommandExpr cmd = v.createCommand();
-        if (cmd==null)
-        {   // Check whether Error information is available
-            log.error("No command has been supplied for view " + v.getName());
-            throw new NotImplementedException(this, v.getName() + ".createCommand");
-        }
-        // Make sure there is no OrderBy
-        cmd.clearOrderBy();
-
-        // Build String
-        StringBuilder sql = new StringBuilder();
-        sql.append( "CREATE VIEW ");
-        v.addSQL(sql, DBExpr.CTX_FULLNAME);
-        sql.append( " (" );
-        boolean addSeparator = false;
-        for(DBColumn c : v.getColumns())
-        {
-            if (addSeparator)
-                sql.append(", ");
-            // Add Column name
-            c.addSQL(sql, DBExpr.CTX_NAME);
-            // next
-            addSeparator = true;
-        }
-        sql.append(")\r\nAS\r\n");
-        cmd.addSQL( sql, DBExpr.CTX_DEFAULT);
-        // done
-        script.addStmt(sql.toString());
-    }
-    
     /**
-     * Returns true if the object has been dropped successfully.
-     * 
-     * @return true if the object has been dropped successfully
+     * @see DBDatabaseDriver#getDDLScript(DBCmdType, DBObject, DBSQLScript)  
      */
-    protected void dropObject(String name, String objType, DBSQLScript script)
+    @Override
+    public void getDDLScript(DBCmdType type, DBObject dbo, DBSQLScript script)
     {
-        if (name == null || name.length() == 0)
-            throw new InvalidArgumentException("name", name);
-        // Create Drop Statement
-        StringBuilder sql = new StringBuilder();
-        sql.append("DROP ");
-        sql.append(objType);
-        sql.append(" ");
-        appendElementName(sql, name);
-        script.addStmt(sql);
+        if (ddlGenerator==null)
+            ddlGenerator = new H2DDLGenerator(this);
+        // forward request
+        ddlGenerator.getDDLScript(type, dbo, script); 
     }
 
 }

Added: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/H2DDLGenerator.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/H2DDLGenerator.java?rev=1180729&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/H2DDLGenerator.java (added)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/H2DDLGenerator.java Sun Oct  9 22:21:31 2011
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.empire.db.h2;
+
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBDDLGenerator;
+import org.apache.empire.db.DBTableColumn;
+
+public class H2DDLGenerator extends DBDDLGenerator<DBDatabaseDriverH2>
+{
+    public H2DDLGenerator(DBDatabaseDriverH2 driver)
+    {
+        super(driver);
+        // set Oracle specific data types
+        initDataTypes();
+    }
+
+    /**
+     * sets Oracle specific data types
+     * @param driver
+     */
+    private void initDataTypes()
+    {   // Override data types
+        DATATYPE_FLOAT      = "DOUBLE";
+        DATATYPE_CLOB       = "LONGTEXT";
+    }
+
+    @Override
+    protected boolean appendColumnDataType(DataType type, double size, DBTableColumn c, StringBuilder sql)
+    {
+        switch (type)
+        {
+            case AUTOINC:
+            {   // Auto increment
+                super.appendColumnDataType(type, size, c, sql);
+                if (driver.isUseSequenceTable()==false)
+                    sql.append(" AUTO_INCREMENT");
+                break;
+            }    
+            default:
+                // use default
+                return super.appendColumnDataType(type, size, c, sql);
+        }
+        return true;
+    }
+ 
+}
\ No newline at end of file

Propchange: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/H2DDLGenerator.java
------------------------------------------------------------------------------
    eol-style = native

Propchange: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/H2DDLGenerator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/hsql/HSqlDDLGenerator.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/hsql/HSqlDDLGenerator.java?rev=1180729&r1=1180728&r2=1180729&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/hsql/HSqlDDLGenerator.java (original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/hsql/HSqlDDLGenerator.java Sun Oct  9 22:21:31 2011
@@ -1,5 +1,20 @@
 /*
- * ESTEAM Software GmbH, 09.10.2011
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
  */
 package org.apache.empire.db.hsql;
 
@@ -69,7 +84,7 @@ public class HSqlDDLGenerator extends DB
     }
 
     /**
-     * Appends the DDL-Script for creating the a sequence to an SQL-Script<br/>
+     * Appends the DDL-Script for creating a sequence to an SQL-Script<br/>
      * @param db the database to create
      * @param c the column for which to create the sequence
      * @param script the sql script to which to append the dll command(s)

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java?rev=1180729&r1=1180728&r2=1180729&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java (original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java Sun Oct  9 22:21:31 2011
@@ -21,31 +21,21 @@ package org.apache.empire.db.mysql;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.GregorianCalendar;
-import java.util.Iterator;
 
 import org.apache.empire.commons.StringUtils;
 import org.apache.empire.data.DataType;
 import org.apache.empire.db.DBCmdType;
-import org.apache.empire.db.DBColumn;
 import org.apache.empire.db.DBCombinedCmd;
 import org.apache.empire.db.DBCommand;
 import org.apache.empire.db.DBCommandExpr;
+import org.apache.empire.db.DBDDLGenerator;
 import org.apache.empire.db.DBDatabase;
 import org.apache.empire.db.DBDatabaseDriver;
 import org.apache.empire.db.DBDriverFeature;
-import org.apache.empire.db.DBExpr;
-import org.apache.empire.db.DBIndex;
 import org.apache.empire.db.DBObject;
-import org.apache.empire.db.DBRelation;
 import org.apache.empire.db.DBSQLScript;
 import org.apache.empire.db.DBTable;
-import org.apache.empire.db.DBTableColumn;
-import org.apache.empire.db.DBView;
 import org.apache.empire.db.exceptions.InternalSQLException;
-import org.apache.empire.exceptions.InvalidArgumentException;
-import org.apache.empire.exceptions.InvalidPropertyException;
-import org.apache.empire.exceptions.NotImplementedException;
-import org.apache.empire.exceptions.NotSupportedException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -114,12 +104,15 @@ public class DBDatabaseDriverMySQL exten
     // Properties
     private String databaseName = null;
     private String characterSet = "utf8";
+
     // Sequence treatment
     // When set to 'false' (default) MySQL's autoincrement feature is used.
     private boolean useSequenceTable = false;
     private String sequenceTableName = "Sequences";
     private String engine; // The database engine to use when creating new tables
 
+    private DBDDLGenerator<?> ddlGenerator = null; // lazy creation
+    
     /**
      * Constructor for the MySQL database driver.<br>
      */
@@ -148,6 +141,16 @@ public class DBDatabaseDriverMySQL exten
         this.databaseName = databaseName;
     }
 
+    public String getCharacterSet()
+    {
+        return characterSet;
+    }
+
+    public void setCharacterSet(String characterSet)
+    {
+        this.characterSet = characterSet;
+    }
+
     /**
      * Get the table engine that is used when creating new tables
      * @return the table engine
@@ -394,7 +397,7 @@ public class DBDatabaseDriverMySQL exten
            case BOOL:      return "CAST(? AS UNSIGNED)";
            case INTEGER:   return "CAST(? AS SIGNED)";
            case DECIMAL:   return "CAST(? AS DECIMAL)";
-           case FLOAT:    return "CAST(? AS DECIMAL)";
+           case FLOAT:     return "CAST(? AS DECIMAL)";
            case DATE:      return "CAST(? AS DATE)";
            case DATETIME:  return "CAST(? AS DATETIME)";
            // Convert to text
@@ -427,83 +430,6 @@ public class DBDatabaseDriverMySQL exten
     }
 
     /**
-     * @see DBDatabaseDriver#getDDLScript(DBCmdType, DBObject, DBSQLScript)  
-     */
-    @Override
-    public void getDDLScript(DBCmdType type, DBObject dbo, DBSQLScript script)
-    {
-        // The Object's database must be attached to this driver
-        if (dbo==null || dbo.getDatabase().getDriver()!=this)
-            throw new InvalidArgumentException("dbo", dbo);
-        // Check Type of object
-        if (dbo instanceof DBDatabase)
-        { // Database
-            switch (type)
-            {
-                case CREATE:
-                    createDatabase((DBDatabase) dbo, script, true);
-                    return;
-                case DROP:
-                    dropObject(((DBDatabase) dbo).getSchema(), "DATABASE", script);
-                    return;
-                default:
-                    throw new NotImplementedException(this, "getDDLScript." + dbo.getClass().getName() + "." + type);
-            }
-        } 
-        else if (dbo instanceof DBTable)
-        { // Table
-            switch (type)
-            {
-                case CREATE:
-                    createTable((DBTable) dbo, script);
-                    return;
-                case DROP:
-                    dropObject(((DBTable) dbo).getName(), "TABLE", script);
-                    return;
-                default:
-                    throw new NotImplementedException(this, "getDDLScript." + dbo.getClass().getName() + "." + type);
-            }
-        } 
-        else if (dbo instanceof DBView)
-        { // View
-            switch (type)
-            {
-                case CREATE:
-                    createView((DBView) dbo, script);
-                    return;
-                case DROP:
-                    dropObject(((DBView) dbo).getName(), "VIEW", script);
-                    return;
-                default:
-                    throw new NotImplementedException(this, "getDDLScript." + dbo.getClass().getName() + "." + type);
-            }
-        } 
-        else if (dbo instanceof DBRelation)
-        { // Relation
-            switch (type)
-            {
-                case CREATE:
-                    createRelation((DBRelation) dbo, script);
-                    return;
-                case DROP:
-                    dropObject(((DBRelation) dbo).getName(), "CONSTRAINT", script);
-                    return;
-                default:
-                    throw new NotImplementedException(this, "getDDLScript." + dbo.getClass().getName() + "." + type);
-            }
-        } 
-        else if (dbo instanceof DBTableColumn)
-        { // Table Column
-            alterTable((DBTableColumn) dbo, type, script);
-            return;
-        } 
-        else
-        { // dll generation not supported for this type
-            throw new NotSupportedException(this, "getDDLScript() for "+dbo.getClass().getName());
-        }
-    }
-
-    /**
      * Overridden. Returns a timestamp that is used for record updates created by the database server.
      * 
      * @return the current date and time of the database server.
@@ -516,372 +442,16 @@ public class DBDatabaseDriverMySQL exten
         return new java.sql.Timestamp(cal.getTimeInMillis());
     }
 
-    /*
-     * return the sql for creating a Database
-     */
-    protected void createDatabase(DBDatabase db, DBSQLScript script, boolean createSchema)
-    {
-        // User Master to create Database
-        if (createSchema)
-        {   // check database Name
-            if (StringUtils.isEmpty(databaseName))
-                throw new InvalidPropertyException("databaseName", databaseName);
-            // Create Database
-            script.addStmt("CREATE DATABASE IF NOT EXISTS " + databaseName + " CHARACTER SET " + characterSet);
-            script.addStmt("USE " + databaseName);
-            // appendDDLStmt(db, "SET DATEFORMAT ymd", buf);
-            // Sequence Table
-            if (useSequenceTable && db.getTable(sequenceTableName)==null)
-                new DBSeqTable(sequenceTableName, db);
-        }
-        // Create all Tables
-        Iterator<DBTable> tables = db.getTables().iterator();
-        while (tables.hasNext())
-        {
-            createTable(tables.next(), script);
-        }
-        // Create Relations
-        Iterator<DBRelation> relations = db.getRelations().iterator();
-        while (relations.hasNext())
-        {
-            createRelation(relations.next(), script);
-        }
-        // Create Views
-        Iterator<DBView> views = db.getViews().iterator();
-        while (views.hasNext())
-        {
-            createView(views.next(), script);
-        }
-    }
-    
     /**
-     * Returns true if the table has been created successfully.
-     * 
-     * @return true if the table has been created successfully
-     */
-    protected void createTable(DBTable t, DBSQLScript script)
-    {
-        StringBuilder sql = new StringBuilder();
-        sql.append("-- creating table ");
-        sql.append(t.getName());
-        sql.append(" --\r\n");
-        sql.append("CREATE TABLE ");
-        t.addSQL(sql, DBExpr.CTX_FULLNAME);
-        sql.append(" (");
-        boolean addSeparator = false;
-        Iterator<DBColumn> columns = t.getColumns().iterator();
-        while (columns.hasNext())
-        {
-            DBTableColumn c = (DBTableColumn) columns.next();
-            sql.append((addSeparator) ? ",\r\n   " : "\r\n   ");
-            if (appendColumnDesc(c, sql)==false)
-                continue; // Ignore and continue;
-            addSeparator = true;
-        }
-        // Primary Key
-        DBIndex pk = t.getPrimaryKey();
-        if (pk != null)
-        { // add the primary key
-            sql.append(", PRIMARY KEY (");
-            addSeparator = false;
-            // columns
-            DBColumn[] keyColumns = pk.getColumns();
-            for (int i = 0; i < keyColumns.length; i++)
-            {
-                sql.append((addSeparator) ? ", " : "");
-                keyColumns[i].addSQL(sql, DBExpr.CTX_NAME);
-                addSeparator = true;
-            }
-            sql.append(")");
-        }
-        sql.append(")");
-        // Engine
-        if (StringUtils.isNotEmpty(engine)) 
-        {   // Set the table engine
-            sql.append(" ENGINE = ");
-            sql.append(engine);
-        }
-        // Comment?
-        String comment = t.getComment();
-        if (StringUtils.isNotEmpty(comment))
-        {   // Add the table comment
-            sql.append(" COMMENT = '");
-            sql.append(comment);
-            sql.append("'");
-        }
-        // Create the table
-        script.addStmt(sql);
-        // Create other Indexes (except primary key)
-        Iterator<DBIndex> indexes = t.getIndexes().iterator();
-        while (indexes.hasNext())
-        {
-            DBIndex idx = indexes.next();
-            if (idx == pk || idx.getType() == DBIndex.PRIMARYKEY)
-                continue;
-
-            // Cretae Index
-            sql.setLength(0);
-            sql.append((idx.getType() == DBIndex.UNIQUE) ? "CREATE UNIQUE INDEX " : "CREATE INDEX ");
-            appendElementName(sql, idx.getName());
-            sql.append(" ON ");
-            t.addSQL(sql, DBExpr.CTX_FULLNAME);
-            sql.append(" (");
-            addSeparator = false;
-
-            // columns
-            DBColumn[] idxColumns = idx.getColumns();
-            for (int i = 0; i < idxColumns.length; i++)
-            {
-                sql.append((addSeparator) ? ", " : "");
-                idxColumns[i].addSQL(sql, DBExpr.CTX_NAME);
-                addSeparator = true;
-            }
-            sql.append(")");
-            // Create Index
-            script.addStmt(sql);
-        }
-    }
-    
-    /**
-     * Appends a table column definition to a ddl statement
-     * @param c the column which description to append
-     * @param sql the sql builder object
-     * @return true if the column was successfully appended or false otherwise
-     */
-    protected boolean appendColumnDesc(DBTableColumn c, StringBuilder sql)
-    {
-        // Append name
-        c.addSQL(sql, DBExpr.CTX_NAME);
-        sql.append(" ");
-        switch (c.getDataType())
-        {
-            case INTEGER:
-            {
-                int size = (int) c.getSize();
-                if (size >= 8) {
-                    sql.append("BIGINT");
-                } 
-                else 
-                {
-                    sql.append("INT");
-                    if (size > 0)
-                    {   // Set Integer length
-                        sql.append("(");
-                        sql.append(String.valueOf(size));
-                        sql.append(")");
-                    }
-                }
-                break;
-            }    
-            case AUTOINC:
-            { // Auto increment
-                sql.append("INT");
-                if (useSequenceTable==false)
-                    sql.append(" AUTO_INCREMENT");
-                break;
-            }    
-            case TEXT:
-            { // Check fixed or variable length
-                int size = Math.abs((int) c.getSize());
-                if (size == 0)
-                    size = 100;
-                sql.append("VARCHAR(");
-                sql.append(String.valueOf(size));
-                sql.append(")");
-            }
-                break;
-            case CHAR:
-            { // Check fixed or variable length
-                int size = Math.abs((int) c.getSize());
-                if (size == 0)
-                    size = 1;
-                sql.append("CHAR(");
-                sql.append(String.valueOf(size));
-                sql.append(")");
-            }
-                break;
-            case DATE:
-                sql.append("DATE");
-                break;
-            case DATETIME:
-                sql.append("DATETIME");
-                break;
-            case BOOL:
-                sql.append("BIT");
-                break;
-            case FLOAT:
-                sql.append("DOUBLE");
-                break;
-            case DECIMAL:
-            { // Decimal
-                sql.append("DECIMAL(");
-                int prec = (int) c.getSize();
-                int scale = (int) ((c.getSize() - prec) * 10 + 0.5);
-                // sql.append((prec+scale).ToString());sql.append(",");
-                sql.append(String.valueOf(prec));
-                sql.append(",");
-                sql.append(String.valueOf(scale));
-                sql.append(")");
-            }
-                break;
-            case CLOB:
-                sql.append("LONGTEXT");
-                break;
-            case BLOB:
-                sql.append("BLOB");
-                if (c.getSize() > 0)
-                    sql.append(" (" + (long) c.getSize() + ") ");
-                break;
-            case UNIQUEID:
-                // emulate using java.util.UUID
-                sql.append("CHAR(36)");
-                break;
-            case UNKNOWN:
-                log.warn("Cannot append column of Data-Type 'UNKNOWN'");
-                return false;
-        }
-        // Default Value
-        if (isDDLColumnDefaults() && !c.isAutoGenerated() && c.getDefaultValue()!=null)
-        {   sql.append(" DEFAULT ");
-            sql.append(getValueString(c.getDefaultValue(), c.getDataType()));
-        }
-        // Nullable
-        if (c.isRequired() ||  c.isAutoGenerated())
-            sql.append(" NOT NULL");
-        // Done
-        return true;
-    }
-
-    /**
-     * Returns true if the relation has been created successfully.
-     * 
-     * @return true if the relation has been created successfully
-     */
-    protected void createRelation(DBRelation r, DBSQLScript script)
-    {
-        DBTable sourceTable = (DBTable) r.getReferences()[0].getSourceColumn().getRowSet();
-        DBTable targetTable = (DBTable) r.getReferences()[0].getTargetColumn().getRowSet();
-
-        StringBuilder sql = new StringBuilder();
-        sql.append("-- creating foreign key constraint ");
-        sql.append(r.getName());
-        sql.append(" --\r\n");
-        sql.append("ALTER TABLE ");
-        sourceTable.addSQL(sql, DBExpr.CTX_FULLNAME);
-        sql.append(" ADD CONSTRAINT ");
-        appendElementName(sql, r.getName());
-        sql.append(" FOREIGN KEY (");
-        // Source Names
-        boolean addSeparator = false;
-        DBRelation.DBReference[] refs = r.getReferences();
-        for (int i = 0; i < refs.length; i++)
-        {
-            sql.append((addSeparator) ? ", " : "");
-            refs[i].getSourceColumn().addSQL(sql, DBExpr.CTX_NAME);
-            addSeparator = true;
-        }
-        // References
-        sql.append(") REFERENCES ");
-        targetTable.addSQL(sql, DBExpr.CTX_FULLNAME);
-        sql.append(" (");
-        // Target Names
-        addSeparator = false;
-        for (int i = 0; i < refs.length; i++)
-        {
-            sql.append((addSeparator) ? ", " : "");
-            refs[i].getTargetColumn().addSQL(sql, DBExpr.CTX_NAME);
-            addSeparator = true;
-        }
-        // done
-        sql.append(")");
-        script.addStmt(sql);
-    }
-
-    /**
-     * Creates an alter table dll statement for adding, modifiying or droping a column.
-     * @param col the column which to add, modify or drop
-     * @param type the type of operation to perform
-     * @param script to which to append the sql statement to
-     * @return true if the statement was successfully appended to the buffer
-     */
-    protected void alterTable(DBTableColumn col, DBCmdType type, DBSQLScript script)
-    {
-        StringBuilder sql = new StringBuilder();
-        sql.append("ALTER TABLE ");
-        col.getRowSet().addSQL(sql, DBExpr.CTX_FULLNAME);
-        switch(type)
-        {
-            case CREATE:
-                sql.append(" ADD ");
-                appendColumnDesc(col, sql);
-                break;
-            case ALTER:
-                sql.append(" MODIFY ");
-                appendColumnDesc(col, sql);
-                break;
-            case DROP:
-                sql.append(" DROP COLUMN ");
-                sql.append(col.getName());
-                break;
-        }
-        // done
-        script.addStmt(sql);
-    }
-
-    /**
-     * Returns true if the view has been created successfully.
-     * 
-     * @return true if the view has been created successfully
-     */
-    protected void createView(DBView v, DBSQLScript script)
-    {
-        // Create the Command
-        DBCommandExpr cmd = v.createCommand();
-        if (cmd==null)
-        {   // Check whether Error information is available
-            log.error("No command has been supplied for view " + v.getName());
-            throw new NotImplementedException(this, v.getName() + ".createCommand");
-        }
-        // Make sure there is no OrderBy
-        cmd.clearOrderBy();
-
-        // Build String
-        StringBuilder sql = new StringBuilder();
-        sql.append( "CREATE VIEW ");
-        v.addSQL(sql, DBExpr.CTX_FULLNAME);
-        sql.append( " (" );
-        boolean addSeparator = false;
-        for(DBColumn c : v.getColumns())
-        {
-            if (addSeparator)
-                sql.append(", ");
-            // Add Column name
-            c.addSQL(sql, DBExpr.CTX_NAME);
-            // next
-            addSeparator = true;
-        }
-        sql.append(")\r\nAS\r\n");
-        cmd.addSQL( sql, DBExpr.CTX_DEFAULT);
-        // done
-        script.addStmt(sql.toString());
-    }
-    
-    /**
-     * Returns true if the object has been dropped successfully.
-     * 
-     * @return true if the object has been dropped successfully
+     * @see DBDatabaseDriver#getDDLScript(DBCmdType, DBObject, DBSQLScript)  
      */
-    protected void dropObject(String name, String objType, DBSQLScript script)
+    @Override
+    public void getDDLScript(DBCmdType type, DBObject dbo, DBSQLScript script)
     {
-        if (name == null || name.length() == 0)
-            throw new InvalidArgumentException("name", name);
-        // Create Drop Statement
-        StringBuilder sql = new StringBuilder();
-        sql.append("DROP ");
-        sql.append(objType);
-        sql.append(" ");
-        appendElementName(sql, name);
-        script.addStmt(sql);
+        if (ddlGenerator==null)
+            ddlGenerator = new MySQLDDLGenerator(this);
+        // forward request
+        ddlGenerator.getDDLScript(type, dbo, script); 
     }
 
 }

Added: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/MySQLDDLGenerator.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/MySQLDDLGenerator.java?rev=1180729&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/MySQLDDLGenerator.java (added)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/MySQLDDLGenerator.java Sun Oct  9 22:21:31 2011
@@ -0,0 +1,159 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.empire.db.mysql;
+
+import java.util.Iterator;
+
+import org.apache.empire.commons.StringUtils;
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBColumn;
+import org.apache.empire.db.DBDDLGenerator;
+import org.apache.empire.db.DBDatabase;
+import org.apache.empire.db.DBExpr;
+import org.apache.empire.db.DBIndex;
+import org.apache.empire.db.DBSQLScript;
+import org.apache.empire.db.DBTable;
+import org.apache.empire.db.DBTableColumn;
+import org.apache.empire.db.DBDatabaseDriver.DBSeqTable;
+
+public class MySQLDDLGenerator extends DBDDLGenerator<DBDatabaseDriverMySQL>
+{
+    public MySQLDDLGenerator(DBDatabaseDriverMySQL driver)
+    {
+        super(driver);
+        // Alter Column Phrase
+        alterColumnPhrase  = " MODIFY ";
+        // set Oracle specific data types
+        initDataTypes();
+    }
+
+    /**
+     * sets Oracle specific data types
+     * @param driver
+     */
+    private void initDataTypes()
+    {   // Override data types
+        DATATYPE_TIMESTAMP  = "DATETIME";
+        DATATYPE_CLOB       = "LONGTEXT";
+    }
+
+    @Override
+    protected boolean appendColumnDataType(DataType type, double size, DBTableColumn c, StringBuilder sql)
+    {
+        switch (type)
+        {
+            case AUTOINC:
+            { // Auto increment
+                super.appendColumnDataType(type, size, c, sql);
+                if (driver.isUseSequenceTable()==false)
+                    sql.append(" AUTO_INCREMENT");
+                break;
+            }    
+           default:
+                // use default
+                return super.appendColumnDataType(type, size, c, sql);
+        }
+        return true;
+    }
+     
+    @Override
+    protected void createDatabase(DBDatabase db, DBSQLScript script)
+    {
+        // create Database if database name is supplied
+        String databaseName = driver.getDatabaseName();
+        if (StringUtils.isNotEmpty(databaseName))
+        { 
+            // Create Database
+            script.addStmt("CREATE DATABASE IF NOT EXISTS " + databaseName + " CHARACTER SET " + driver.getCharacterSet());
+            script.addStmt("USE " + databaseName);
+            // appendDDLStmt(db, "SET DATEFORMAT ymd", buf);
+            // Sequence Table
+            if (driver.isUseSequenceTable() && db.getTable(driver.getSequenceTableName())==null)
+                new DBSeqTable(driver.getSequenceTableName(), db);
+        }
+        // default processing
+        super.createDatabase(db, script);
+    }
+
+    /**
+     * Appends the DDL-Script for creating the given table to an SQL-Script 
+     * @param t the table to create
+     * @param script the sql script to which to append the dll command(s)
+     */
+    @Override
+    protected void createTable(DBTable t, DBSQLScript script)
+    {
+        StringBuilder sql = new StringBuilder();
+        sql.append("-- creating table ");
+        sql.append(t.getName());
+        sql.append(" --\r\n");
+        sql.append("CREATE TABLE ");
+        t.addSQL(sql, DBExpr.CTX_FULLNAME);
+        sql.append(" (");
+        boolean addSeparator = false;
+        Iterator<DBColumn> columns = t.getColumns().iterator();
+        while (columns.hasNext())
+        {
+            DBTableColumn c = (DBTableColumn) columns.next();
+            // Append column
+            sql.append((addSeparator) ? ",\r\n   " : "\r\n   ");
+            appendColumnDesc(c, false, sql);
+            addSeparator = true;
+        }
+        // Primary Key
+        DBIndex pk = t.getPrimaryKey();
+        if (pk != null)
+        { // add the primary key
+            sql.append(",\r\n PRIMARY KEY (");
+            addSeparator = false;
+            // columns
+            DBColumn[] keyColumns = pk.getColumns();
+            for (int i = 0; i < keyColumns.length; i++)
+            {
+                sql.append((addSeparator) ? ", " : "");
+                keyColumns[i].addSQL(sql, DBExpr.CTX_NAME);
+                addSeparator = true;
+            }
+            sql.append(")");
+        }
+        sql.append(")");
+        // Engine
+        addSeparator = false;
+        if (StringUtils.isNotEmpty(driver.getEngine())) 
+        {   // Set the table engine
+            sql.append("\r\n ENGINE = ");
+            sql.append(driver.getEngine());
+            addSeparator = true;
+        }
+        // Comment?
+        if (StringUtils.isNotEmpty(t.getComment()))
+        {   // Add the table comment
+            if (addSeparator)
+                sql.append(",");
+            sql.append("\r\n COMMENT = '");
+            sql.append(t.getComment());
+            sql.append("'");
+        }
+        // Create the table
+        addCreateTableStmt(t, sql, script);
+        // Create all Indexes
+        createTableIndexes(t, pk, script);        
+    }
+    
+}

Propchange: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/MySQLDDLGenerator.java
------------------------------------------------------------------------------
    eol-style = native

Propchange: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/MySQLDDLGenerator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/oracle/OracleDDLGenerator.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/oracle/OracleDDLGenerator.java?rev=1180729&r1=1180728&r2=1180729&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/oracle/OracleDDLGenerator.java (original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/oracle/OracleDDLGenerator.java Sun Oct  9 22:21:31 2011
@@ -35,6 +35,8 @@ public class OracleDDLGenerator extends 
     public OracleDDLGenerator(DBDatabaseDriverOracle driver)
     {
         super(driver);
+        // Alter Column Phrase
+        alterColumnPhrase  = " MODIFY ";
         // set Oracle specific data types
         initDataTypes(driver);
     }



Mime
View raw message