You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by Thomas Dudziak <to...@first.gmd.de> on 2004/01/21 15:00:16 UTC

New utility class for db creation

Hi folks,

I just added a new class that allows to create (e.g. "create
database") and initialize (e.g. "create table") a database from java using
torque and the torque schemas. Usage is something like this:

String               srcDir    = "resources/ojb";
String               schemas   = "*-schema.xml";
PersistenceBroker    broker    = 
PersistenceBrokerFactory.defaultPersistenceBroker();
ConnectionRepository connRep   = 
MetadataManager.getInstance().connectionRepository();
TorqueDBHandling     dbHandler = new 
TorqueDBHandling(connRep.getDescriptor(broker.getPBKey()), srcDir, 
schemas);

dbHandler.createDB();
dbHandler.createTables();

I figured such a class might be useful to further automate the unit tests
and eventually make them independent of each other (e.g. by creating
and populating the database prior to each test).
I didn't exactly know where to put the class but I thought the
broker.platforms subpackage is a good place as similar implementations
might be necessary for other platforms supported by OJB (e.g. MsAccess and
Informix are not supported by Torque so they would need specific
creation/initialization stuff) which then could be hidden by a
Platform.getDbHandler and a DbHandler interface where the actual
implementation is chosen by the PlatformImpls.

I'll probably add stuff to populate the db from strings or files (xml,
csv), as well.

One problem that remains is that for most jdbc drivers the jdbc url to
create a database is different from the url to manipulate an existing
db. E.g. when using the ant sql task with mysql you would do this to
create a db:

<?xml version="1.0" encoding="UTF-8"?>
<project default="main" basedir="." name="build-db">
    <path id="project.class.path">
        <fileset dir="lib" casesensitive="no">
          <include name="**/*.jar"/>
          <include name="**/*.zip"/>
        </fileset>
    </path>

    <target name="main">
        <sql classpathref="project.class.path"
             driver="com.mysql.jdbc.Driver"
             url="jdbc:mysql://localhost/"
             userid="root"
             password="">
        drop database if exists testdb;
        create database testdb;
        </sql>
    </target>
</project>

(note the missing database name in the url). As the database url is driver
specific (or at least platform specific), there is some testing necessary
to determine these urls for the torque-supported platforms.

Tom



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Re: New utility class for db creation

Posted by Brian McCallister <mc...@forthillcompany.com>.
This is great, I have been executing the torque generated SQL directly 
against in-memory hsqldb for unit tests. Will be very happy to go from 
the torque schema instead!

-Brian

On Jan 21, 2004, at 9:00 AM, Thomas Dudziak wrote:

> Hi folks,
>
> I just added a new class that allows to create (e.g. "create
> database") and initialize (e.g. "create table") a database from java 
> using
> torque and the torque schemas. Usage is something like this:
>
> String               srcDir    = "resources/ojb";
> String               schemas   = "*-schema.xml";
> PersistenceBroker    broker    =
> PersistenceBrokerFactory.defaultPersistenceBroker();
> ConnectionRepository connRep   =
> MetadataManager.getInstance().connectionRepository();
> TorqueDBHandling     dbHandler = new
> TorqueDBHandling(connRep.getDescriptor(broker.getPBKey()), srcDir,
> schemas);
>
> dbHandler.createDB();
> dbHandler.createTables();
>
> I figured such a class might be useful to further automate the unit 
> tests
> and eventually make them independent of each other (e.g. by creating
> and populating the database prior to each test).
> I didn't exactly know where to put the class but I thought the
> broker.platforms subpackage is a good place as similar implementations
> might be necessary for other platforms supported by OJB (e.g. MsAccess 
> and
> Informix are not supported by Torque so they would need specific
> creation/initialization stuff) which then could be hidden by a
> Platform.getDbHandler and a DbHandler interface where the actual
> implementation is chosen by the PlatformImpls.
>
> I'll probably add stuff to populate the db from strings or files (xml,
> csv), as well.
>
> One problem that remains is that for most jdbc drivers the jdbc url to
> create a database is different from the url to manipulate an existing
> db. E.g. when using the ant sql task with mysql you would do this to
> create a db:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <project default="main" basedir="." name="build-db">
>     <path id="project.class.path">
>         <fileset dir="lib" casesensitive="no">
>           <include name="**/*.jar"/>
>           <include name="**/*.zip"/>
>         </fileset>
>     </path>
>
>     <target name="main">
>         <sql classpathref="project.class.path"
>              driver="com.mysql.jdbc.Driver"
>              url="jdbc:mysql://localhost/"
>              userid="root"
>              password="">
>         drop database if exists testdb;
>         create database testdb;
>         </sql>
>     </target>
> </project>
>
> (note the missing database name in the url). As the database url is 
> driver
> specific (or at least platform specific), there is some testing 
> necessary
> to determine these urls for the torque-supported platforms.
>
> Tom
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-dev-help@db.apache.org
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org