You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cayenne.apache.org by Tomi N/A <he...@gmail.com> on 2008/03/14 16:49:04 UTC

command line mapping generation

I would like to be able to generate database mappings from the command line.
I have glimpsed into DbLoader source as a first step (and have
questions about it), but would like to hear your opinions about how
this should be done (there was some talk of it on the users list).

The use case would be using the GUI to set up everything (classpaths,
DataSources etc.) and _then_ use the command line refering to existing
data sources.
Even better, it would be great if I could bypass it completely and
just specify the jdbc driver class and the database connection string
and generate a mapping with most of the parameters you can now set
from the GUI.

Cheers,
Tomislav

Re: command line mapping generation

Posted by Tomi N/A <he...@gmail.com>.
2008/3/14, Andrus Adamchik <an...@objectstyle.org>:
> You are on the right track. You can write a simple Java tool (or an
>  Ant task) based on DbLoader class. Then use
>  DataMap.encodeAsXML(XMLEncoder) to store the result in an XML file.

Thanks for the comment, Andrus.
This is what I have so far and it works as expected:

			dataSource = new PoolManager("org.postgresql.Driver",
			        "jdbc:postgresql://localhost:5432/mydb",
			        1,
			        5,
			        "myuser",
			        "mypassword");
			Connection connection = dataSource.getConnection();
			DbLoaderDelegate delegate = null;
			PostgresAdapter adapter = new PostgresAdapter();
			DbLoader dl = new DbLoader(connection, adapter, delegate);
			DataMap dataMap = new DataMap("datamap name");
			dl.loadDataMapFromDB("myschema", "%", dataMap);
			
			PrintWriter pw = new PrintWriter("/tmp/mapping.xml");
			dataMap.encodeAsXML(pw );

I would like to take this a couple of steps further:
1.) generalize it so that it isn't specific to Postgresql, reads
connection parameters from a file (the datanode XML file might be a
good choice) etc.
2.) see if there would be interest for code of that purpose in the
official cayenne source and integrate it there
3.) create a maven2 (ant? is one preferred?) task to run it from the CLI
4.) use a XML diff tool like VM Tools, 3DM, diffxml, xmlunit to merge
(what now might seem like) simpler differences (added/removed table
attribute, a type or name change, dropped table, added table etc.)


Once the project is set up, a part of our everyday coding routine
might look something like this:
1.) apply a change to the database model whichever way you do choose
2.) execute a maven/ant task (from the console, the IDE or which ever
way you're comfortable with) and voila: 9 out of 10 times, all the
artifacts that should reflect the change in the database have been
regenerated: the mapping (while preserving your manual changes via
xmldiff and merge), the classes and possibly other artifacts people
tend to generate via the velocity templates!

Opinions are obviuosly very welcome. :)

Cheers,
Tomislav

Re: command line mapping generation

Posted by Andrus Adamchik <an...@objectstyle.org>.
You are on the right track. You can write a simple Java tool (or an  
Ant task) based on DbLoader class. Then use  
DataMap.encodeAsXML(XMLEncoder) to store the result in an XML file.

Andrus


On Mar 14, 2008, at 5:49 PM, Tomi N/A wrote:

> I would like to be able to generate database mappings from the  
> command line.
> I have glimpsed into DbLoader source as a first step (and have
> questions about it), but would like to hear your opinions about how
> this should be done (there was some talk of it on the users list).
>
> The use case would be using the GUI to set up everything (classpaths,
> DataSources etc.) and _then_ use the command line refering to existing
> data sources.
> Even better, it would be great if I could bypass it completely and
> just specify the jdbc driver class and the database connection string
> and generate a mapping with most of the parameters you can now set
> from the GUI.
>
> Cheers,
> Tomislav
>