You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@empire-db.apache.org by Christopher Richmond <cr...@referentia.com> on 2012/08/31 03:16:58 UTC

getting tables for completely dynamic databas

I have a database which starts with absolutely no schema when started.

I have made this simple class:

public class TestDB extends DBDatabase {

  public TestDB(){

  }

}

simply so that i may create tables completely on the fly for my embedded H2
database like so:

     TestDB db = new TestDB()

    config.init((args.length > 0 ? args[0] : "config.xml" ));
    Connection conn = getJDBCConnection();
    DBDatabaseDriver driver = getDatabaseDriver("h2");

    db.open(driver, conn);



    DBTable dbTable = new DBTable("MyNewTable", db);

    dbTable.addColumn("MyColumn", DataType.INTEGER, 32d, false);
    dbTable.addColumn("MyColumn2", DataType.INTEGER, 32d, false);
    dbTable.addColumn("MyColumn3", DataType.INTEGER, 1024d, false);
    dbTable.addColumn("MyColumn4", DataType.INTEGER, 1024d, false);


    DBSQLScript script = new DBSQLScript();

    db.getDriver().getDDLScript(DBCmdType.CREATE, db, script);

    System.err.println(script.toString());
    script.run(db.getDriver(), conn);

    db.close(conn);




This works fine, new tables are created in my H2 database.  My problem is
this. Since I have no static definition of the schema then things like

    db.getTable("TableNameLookingFor")

never returns any DBTables whatsoever.

When I create my db I need for it to find the tables that actually exist in
the database(all of which are created on the fly like above at runtime with
various names at various times, which I will never now when)



How can I go about this?  Or am I approaching this the wrong way?  Perhaps
I shouldn't be using a simple TestDB implementation of DBDatabase like
that?  Do I need to override some superclass methods to get it to load the
table schema from the on disk H2 database any time I construct it?  I am
not sure how to proceed.

Thanks