db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgor...@mwt.net
Subject torque-gen from Java
Date Wed, 17 Nov 2004 21:56:27 GMT
[Long but useful if you are interested in the topic.]

I have performed generation of SQL and invocation  of
that SQL from a Java application by direct use of the 
Torque API and the Ant API. I have NOT done the gen of Java source, 
though the principle is the same.

What follows is an excerpt from one of my source files.
Both SQL generation is performed and then SQL execution.
These steps correspond, respectively, to TorqueSQLTask
and TorqueSQLExec. There methods map, more or less,
to the ant targets in build.xml. There are analogous
targets for source generation and the corresponding
Torque method. Once you ID the correct method, 
the trick is then to map the
properties in the build.xml into the appropriate
setters on that method.

You will note some comments that point to various 
locations in the file system. Basically you have to
tell methods where some input files exists and where
you want output to go (Duh!). Anyway, this ran from a
servlet and I stored the appropriate input files (e.g
datasource properties and db schema in /webapps/appName/conf...
I think) If anybody is really interested in any more detail 
I can share some more. 

THere is one of my own objects named below, DataSourceProperties.
Basically, this is populated from a props file that has parameterized
values in it. Based on context, I resolve those values into
concrete values. The properties defined are those that torque
expects when characterizing a datasource, eg. driver, url.
Anyway, the method to populate the DatasourceProperties is
at the end. It is one of the better commented methods
of my career. :)

(This is beginning to sound like a "How-To") Dear me.

import org.apache.torque.Torque;
import org.apache.torque.task.TorqueSQLTask;
import org.apache.torque.task.TorqueSQLExec;
import org.apache.torque.engine.database.model.AppData;
import org.apache.tools.ant.taskdefs.SQLExec;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;
import org.apache.velocity.context.Context;

      * Generate the SQL to create a database for an event or race
      * @param emsConfig Global configuration properties for EMS
      * @param appRoot The application context root directory
      * @param path Directory where event/race resides. This is an absolute path.
      * @param dbName Name of database
      * @param dsp Properties describing datasource for this database
      * @param schemaDir Absolute path of database schema directory.
  public static void generateDatabaseCreationSql(
                Configuration emsConfig, 
                String appRoot, 
                String path, String dbName, 
                DatasourceProperties dsp, 
                String schemaDir) throws Exception {
      TorqueSQLTask dbTask = new TorqueSQLTask();
      Project p = new Project();
      p.setBaseDir(new File(appRoot));
      dbTask.setOutputDirectory(new File(path + "/sql"));
        // "Target database" is type of database, e.g. mysql, oracle, etc.
        // See templates/sql/base
      FileSet fileset = new FileSet();
      fileset.setDir(new File(schemaDir));

      * Execute SQL generated by <code>generateTableCreationSql</code>.
      * @param appRoot Absolute path up to /event directory.
      * @param path Absolute path to sql directory created by SQL generation
step. See <code>generateTableCreationSql</code>.
      * @param relSqlPath Path to sql directory relative to
<code>appRoot</code>. E.g., event/<event-name> or event/<event-name>/<race-name>
      * @param dbName Name of database in which tables are to be created.
      * @param dsProp Configuration properties describing datasource of database
    public static void executeTableCreationSql(
               String appRoot, 
               String path, 
               String relSqlPath, 
               String dbName,  
               Configuration dsProp) throws Exception {
      String driver = (String)
      String user = (String) dsProp.getProperty(TorqueSupport.makeUserKey(dbName));
      String password = (String)
      TorqueSQLExec exec = new TorqueSQLExec();
      Project p = new Project();
      p.setBaseDir(new File(appRoot));
      exec.setUrl((String) dsProp.getProperty(TorqueSupport.makeUrlKey(dbName)));
      exec.setSqlDbMap(relSqlPath + "/sql/sqldb.map");
      exec.setSrcDir(path + "/sql");

      * Given data source values for a particular race in the <code>dsp</code>
argument and templates for Torque
      * runtime property values that describe a datasource in the
<code>dsConfig</code> argument, generate actual
      * datasource definition properties as they would appear in the
torque-runtime.properties file.
      * The <code>dsConfig</code> argument must contain the following keys:
      * <ul>
      * <li>adapter.<adapter-name></li>
      * <li><adapter-name>connection.driver</li>
      * <li><adapter-name>connection.url</li>
      * <li><adapter-name>connection.createUrl</li>
      * <li><adapter-name>connection.user</li>
      * <li><adapter-name>connection.password</li>
      * </ul>
      * <br>
      * This method takes the property values in <code>dsp</code> argument and
substitutes those values in
      * the paramterized values of the <code>dsConfig</code> object.
      * <br>
      * For example, an entry of the form :
      * <li>mysql.connection.url=jdbc:mysql://${host}/${databasename}
      * <br>
      * will be turned into:
      * <li>torque.dsfactory.circuit2004.connection.url =
      * where the values of <code>dsp.getHost()</code> and
<code>dsp.getDatabaseName()</code> are
      * localhost and circuit2004, respectively. Generally, a variable in a
      * value will be replaced with the value of the corresponding property in
the <code>dsp</code>
      * object.
  static PropertiesConfiguration
generateDatasourceProperties(DatasourceProperties dsp, Configuration dsConfig)
throws Exception {
    if (dsConfig == null) {
      throw new Exception("Datasource property templates must first be defined.");
    PropertiesConfiguration returnConfig = new PropertiesConfiguration();
    String adapter = dsp.getAdapter();
    String adapterValue;
    if (adapter == null) {
      // XXX log using default adapter
      adapterValue = DEFAULT_TORQUE_ADAPTER;
    } else {
      adapterValue = (String) dsConfig.getProperty("adapter." + adapter);

    Iterator dsit = dsConfig.getKeys();
    Configuration pc = dsConfig.subset(adapterValue);
    Iterator keyIterator = pc.getKeys();
    returnConfig.setProperty("torque.database." + dsp.getDatabaseName() +
".adapter", adapterValue);
    while (keyIterator.hasNext()) {
      String key = (String) keyIterator.next();
      String value = (String) pc.getProperty(key);
      String newValue = PropertiesSupport.resolveParameters(dsp, value, "$");
      returnConfig.setProperty(TORQUE_DSFACTORY_PROPERTY_KEY_PREFIX +    
dsp.getDatabaseName() + "." + key, newValue);
    return returnConfig;

This message was sent using Midwest Tel Net Web Based Mail.

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

View raw message