In the Torque Runtime, all information about a specific Database is gathered in a so-called Database adapter class. So if you want to support a new database in the runtime, you need to provide a Database Adapter class for this database.
If you are adding support for a new RDBMS, then you will probably also want to support the database in the Torque generator. To do this, you need to create a set of Velocity templates--used by Torque to generate a SQL schema for your RDBMS--in the templates component. The recommend method for doing this is to copy an existing set of templates and adapt them to your RDBMS as needed. This is not elaborated further here.
A database adapter class is a class that implements
org.apache.torque.adapter.DB
and encapsulates access
to a specific RDBMS implementation. Database adapter classes already
found in Torque include DBOracle, DBMM, DBSybase, etc.
These classes allow Torque to gain access to a wide range of databases
in a uniform manner. This allows you to easily swap between databases
without any modification to Torque or the application built
on top of Torque.
Why is this necessary if Java already offers uniform database access in the form of JDBC? Unfortunately, underlying databases still use different SQL implementations and conventions. For example, the use of single and double quotes varies. The use of database adapter classes in Torque endeavors to overcome this problem.
To add a new database adapter class to Torque you must follow these steps:
org.apache.torque.adapter.DB
(where dbname is the name of
the database or database driver you wish to add to Torque). An abstract
implementation, org.apache.torque.adapter.AbstractDBAdapter
is
provided which sets some defaults so that you only need to implement a
number of methods.
org.apache.torque.adapter.IDMethod.AUTO_INCREMENT
,
org.apache.torque.adapter.IDMethod.SEQUENCE
, and
org.apache.torque.adapter.IDMethod.NO_ID_METHOD
.
obj
is the name of the sequence.
Databases that use auto increment fields should return the last
generated id; obj
is the name of the table in this case.
Databases that do not support this must return null.
The adapter you wrote does not need to be compiled into Torque but it can be referenced in the configuration. Just use a new short name for the adapter and provide the class name in the configuration file. See the following example:
torque.database.mydatabase.adapter=myadapter torque.database.mydatabase.adapter.myadapter.className=com.acme.DBMyAdapter