tomdz 2004/08/08 14:11:08 Modified: sql/src/java/org/apache/commons/sql/builder SqlBuilder.java PostgreSqlBuilder.java AxionBuilder.java SqlBuilderFactory.java Added: sql/src/java/org/apache/commons/sql/builder FirebirdBuilder.java Log: Added initial support for Firebird Revision Changes Path 1.22 +50 -17 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/SqlBuilder.java Index: SqlBuilder.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/SqlBuilder.java,v retrieving revision 1.21 retrieving revision 1.22 diff -u -r1.21 -r1.22 --- SqlBuilder.java 8 Aug 2004 11:23:44 -0000 1.21 +++ SqlBuilder.java 8 Aug 2004 21:11:08 -0000 1.22 @@ -354,20 +354,11 @@ { dropDatabase(database); } - - for (Iterator it = database.getTables().iterator(); it.hasNext(); ) - { - Table table = (Table)it.next(); - writeTableComment(table); - createTable(table); - } + createTables(database); // we're writing the external foreignkeys last to ensure that all referenced tables are already defined - for (Iterator it = database.getTables().iterator(); it.hasNext(); ) - { - createExternalForeignKeys((Table)it.next()); - } + createExternalForeignKeys(database); } /** @@ -549,6 +540,22 @@ } /** + * Outputs the DDL to create all tables of the given database model. + * + * @param database The database + */ + public void createTables(Database database) throws IOException + { + for (Iterator it = database.getTables().iterator(); it.hasNext(); ) + { + Table table = (Table)it.next(); + + writeTableComment(table); + createTable(table); + } + } + + /** * Outputs the DDL to create the table along with any non-external constraints as well * as with external primary keys and indices (but not foreign keys). * @@ -589,6 +596,19 @@ } /** + * Creates the external foreignkey creation statements for all tables in the database. + * + * @param database The database + */ + public void createExternalForeignKeys(Database database) throws IOException + { + for (Iterator it = database.getTables().iterator(); it.hasNext(); ) + { + createExternalForeignKeys((Table)it.next()); + } + } + + /** * Creates external foreignkey creation statements if necessary. * * @param table The table @@ -709,6 +729,18 @@ } } + /** + * Writes the column name. This method allows builder implementations to e.g. escape + * the name or similar. + * + * @param table The table of the column + * @param column The column + */ + protected void writeColumnName(Table table, Column column) throws IOException + { + print(column.getName()); + } + /** * Outputs the DDL for the specified column. * @@ -718,7 +750,7 @@ protected void writeColumn(Table table, Column column) throws IOException { //see comments in columnsDiffer about null/"" defaults - print(column.getName()); + writeColumnName(table, column); print(" "); print(getSqlType(column)); @@ -769,7 +801,7 @@ { writeTableAlterStmt(table); print("DROP COLUMN "); - print(column.getName()); + writeColumnName(table, column); printEndOfStatement(); } @@ -896,7 +928,7 @@ { println(","); printIndent(); - writePrimaryKeyStmt(primaryKeyColumns); + writePrimaryKeyStmt(table, primaryKeyColumns); } } @@ -917,7 +949,7 @@ print("ADD CONSTRAINT "); print(table.getName()); print("_PK "); - writePrimaryKeyStmt(primaryKeyColumns); + writePrimaryKeyStmt(table, primaryKeyColumns); printEndOfStatement(); } } @@ -946,14 +978,15 @@ /** * Writes a primary key statement for the given columns. * + * @param table The table * @param primaryKeyColumns The primary columns */ - protected void writePrimaryKeyStmt(List primaryKeyColumns) throws IOException + protected void writePrimaryKeyStmt(Table table, List primaryKeyColumns) throws IOException { print("PRIMARY KEY ("); for (Iterator it = primaryKeyColumns.iterator(); it.hasNext();) { - print(((Column)it.next()).getName()); + writeColumnName(table, (Column)it.next()); if (it.hasNext()) { print(", "); 1.12 +2 -2 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/PostgreSqlBuilder.java Index: PostgreSqlBuilder.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/PostgreSqlBuilder.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- PostgreSqlBuilder.java 8 Aug 2004 11:23:44 -0000 1.11 +++ PostgreSqlBuilder.java 8 Aug 2004 21:11:08 -0000 1.12 @@ -62,7 +62,7 @@ */ public void writeColumn(Table table, Column column) throws IOException { - print(column.getName()); + writeColumnName(table, column); print(" "); if (column.isAutoIncrement()) { 1.13 +1 -1 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/AxionBuilder.java Index: AxionBuilder.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/AxionBuilder.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- AxionBuilder.java 8 Aug 2004 11:23:44 -0000 1.12 +++ AxionBuilder.java 8 Aug 2004 21:11:08 -0000 1.13 @@ -74,7 +74,7 @@ * default from the Axion column builder. */ public void writeColumn(Table table, Column column) throws IOException { - print(column.getName()); + writeColumnName(table, column); print(" "); print(getSqlType(column)); print(" "); 1.10 +1 -0 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/SqlBuilderFactory.java Index: SqlBuilderFactory.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/SqlBuilderFactory.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- SqlBuilderFactory.java 1 Aug 2004 21:43:36 -0000 1.9 +++ SqlBuilderFactory.java 8 Aug 2004 21:11:08 -0000 1.10 @@ -91,6 +91,7 @@ { registerDatabase("axion", AxionBuilder.class); registerDatabase("db2", Db2Builder.class); + registerDatabase("firebird", FirebirdBuilder.class); registerDatabase("hsqldb", HsqlDbBuilder.class); registerDatabase("maxdb", MaxDbBuilder.class); registerDatabase("mckoi", MckoiSqlBuilder.class); 1.1 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/FirebirdBuilder.java Index: FirebirdBuilder.java =================================================================== package org.apache.commons.sql.builder; /* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed 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. */ import java.io.IOException; import java.sql.Types; import org.apache.commons.sql.model.Column; import org.apache.commons.sql.model.Database; import org.apache.commons.sql.model.ForeignKey; import org.apache.commons.sql.model.Table; /** * An SQL Builder for the Firebird database. * * @author <a href="mailto:[EMAIL PROTECTED]">Thomas Dudziak</a> */ public class FirebirdBuilder extends SqlBuilder { public FirebirdBuilder() { setPrimaryKeyEmbedded(true); setForeignKeysEmbedded(false); setCommentPrefix("/*"); setCommentSuffix("*/"); addNativeTypeMapping(Types.BIGINT, "DECIMAL(18,0)"); addNativeTypeMapping(Types.BINARY, "BLOB"); addNativeTypeMapping(Types.BIT, "DECIMAL(1,0)"); addNativeTypeMapping(Types.BOOLEAN, "DECIMAL(1,0)"); addNativeTypeMapping(Types.CLOB, "BLOB SUB_TYPE TEXT"); addNativeTypeMapping(Types.DOUBLE, "DOUBLE PRECISION"); addNativeTypeMapping(Types.LONGVARBINARY, "BLOB"); addNativeTypeMapping(Types.LONGVARCHAR, "BLOB SUB_TYPE TEXT"); addNativeTypeMapping(Types.REAL, "FLOAT"); addNativeTypeMapping(Types.TINYINT, "SMALLINT"); addNativeTypeMapping(Types.VARBINARY, "BLOB"); } /* (non-Javadoc) * @see org.apache.commons.sql.builder.SqlBuilder#getDatabaseName() */ public String getDatabaseName() { return "Firebird"; } /* (non-Javadoc) * @see org.apache.commons.sql.builder.SqlBuilder#dropDatabase(org.apache.commons.sql.model.Database) */ public void dropDatabase(Database database) throws IOException { super.dropDatabase(database); print("COMMIT"); printEndOfStatement(); } /* (non-Javadoc) * @see org.apache.commons.sql.builder.SqlBuilder#createTables(org.apache.commons.sql.model.Database) */ public void createTables(Database database) throws IOException { super.createTables(database); print("COMMIT"); printEndOfStatement(); } /* (non-Javadoc) * @see org.apache.commons.sql.builder.SqlBuilder#writeExternalForeignKeyCreateStmt(org.apache.commons.sql.model.Table, org.apache.commons.sql.model.ForeignKey, int) */ protected void writeExternalForeignKeyCreateStmt(Table table, ForeignKey key, int numKey) throws IOException { super.writeExternalForeignKeyCreateStmt(table, key, numKey); if (key.getForeignTable() != null) { print("COMMIT"); printEndOfStatement(); } } /* (non-Javadoc) * @see org.apache.commons.sql.builder.SqlBuilder#createTable(org.apache.commons.sql.model.Table) */ public void createTable(Table table) throws IOException { super.createTable(table); // creating generator and trigger for auto-increment Column column = table.getAutoIncrementColumn(); if (column != null) { print("CREATE GENERATOR gen_"); print(table.getName()); print("_"); print(column.getName()); printEndOfStatement(); print("CREATE TRIGGER trg_"); print(table.getName()); print("_"); print(column.getName()); print(" FOR "); println(table.getName()); println("ACTIVE BEFORE INSERT POSITION 0"); println("AS"); println("BEGIN"); print("IF (NEW."); print(column.getName()); println(" IS NULL) THEN"); print("NEW."); print(column.getName()); print(" = GEN_ID(gen_"); print(table.getName()); print("_"); print(column.getName()); println(", 1);"); print("END"); printEndOfStatement(); } } /* (non-Javadoc) * @see org.apache.commons.sql.builder.SqlBuilder#dropTable(org.apache.commons.sql.model.Table) */ public void dropTable(Table table) throws IOException { // dropping generator and trigger for auto-increment Column column = table.getAutoIncrementColumn(); if (column != null) { print("DROP TRIGGER trg_"); print(table.getName()); print("_"); print(column.getName()); printEndOfStatement(); print("DROP GENERATOR gen_"); print(table.getName()); print("_"); print(column.getName()); printEndOfStatement(); } super.dropTable(table); } /* (non-Javadoc) * @see org.apache.commons.sql.builder.SqlBuilder#printAutoIncrementColumn(Table,Column) */ protected void writeColumnAutoIncrementStmt(Table table, Column column) throws IOException { // we're using a generator } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]