Database Adapters

データベースアダプタ

A database adapter class is a class that extends 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.

データベースアダプタクラスとはorg.apache.torque.adapter.DBを継承するクラスで、特定のRDBMSの実装へのアクセスをカプセル化します。 データベースアダプタクラスは既にTorqueにあり、DBOracle、DBMM、DBSybaseなどを含んでいます。 これらのクラスは、Torqueが同一の方法で広範囲のデータベースへアクセスすることを可能にします。 これは、Torqueや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.

Javaは既にJDBCという形態で統一されたデータベースアクセスを提供しているのに、これはなぜ必要なのでしょうか? 不運なことに、根底のデータベースはまだ異なるSQLの実装および仕様となっています。 例えば、シングルおよびダブルクォートの用途が異なります。 Torqueのデータベースアダプタクラスを利用することで、この問題を克服します。

To add a new database adapter class to Torque you must follow these steps:

Torqueに新しいデータベースアダプタクラスを追加するには、以下の手順を踏む必要があります:

  • Create a new class DB<dbname> that extends org.apache.torque.adapter.DB (where dbname is the name of the database or database driver you wish to add to Torque). DB is an abstract class, so you need to implement a number of methods.
  • org.apache.torque.adapter.DBを継承する新しいクラス DB<dbname> を作成してください(Torqueに追加したい場合、dbnameはデータベースかデータベースドライバの名前にしてください)。 DBは抽象的なクラスであり、多くのメソッドを実装する必要があります。
  • Implement getStringDelimiter(). This method has to return the character that the specific database implementation uses to indicate string literals in SQL. This will usually be a single qoute (').
  • getStringDelimiter()を実装してください。 このメソッドは、特定のデータベースの実装で使用するSQLの文字列リテラルを示す文字を返さなければなりません。 これは通常はシングルクォート(')になるでしょう。
  • Implement String getIdSqlForAutoIncrement(). Databases like MySQL that make use of auto increment fields will implement this method. For MySQL it returns "SELECT LAST_INSERT_ID()". Databases that do not support this must return null.
  • String getIdSqlForAutoIncrement()を実装してください。 オートインクリメントフィールドを利用するMySQLのようなデータベースはこのメソッドを実装できるでしょう。 MySQLの場合、"SELECT LAST_INSERT_ID()"を返します。 これをサポートしないデータベースはnullを返さなければいけません。
  • Implement String getSequenceSql(Object obj). Databases like Oracle that support the concept of unique id generators (called sequences) will implement this method. Databases that do not support this must return null.
  • String getSequenceSql(Object obj)を実装してください。 ユニークidジェネレーター(シーケンスと呼ばれる)の概念をサポートする、 Oracleのようなデータベースは、このメソッドを実装できるでしょう。 これをサポートしないデータベースはnullを返さなければいけません。
  • Implement void lockTable(Connection con, String table). This method should place a lock on a database table. Databases that only support table level locking obviously do not require this method. Databases that support row level locking must implement this method to avoid synchronization problems.
  • void lockTable(Connection con, String table)を実装してください。 このメソッドはデータベーステーブルにロックをかけます。 テーブル単位のロックしかサポートしていないデータベースは、明らかにこのメソッドを必要としません。 行単位のロックをサポートするデータベースは、同期問題を回避するためにこのメソッドを実装しなければなりません。
  • Implement void unlockTable(Connection con, String table). This method should release the table lock acquired by the above-mentioned method.
  • void unlockTable(Connection con, String table)を実装してください。 このメソッドは前述のメソッドで獲得したテーブルロックを解放します。
  • Implement String ignoreCase(String in). This method should implement a mechanism for case insensitive comparisons. Usually this converts the string to Uppercase. Example UPPER (<in>). If such a mechanism does not exist in your database simple return the in parameter without any modification. DO NOT return in.toUpper().
  • String ignoreCase(String in)を実装してください。 このメソッドは、大文字/小文字の区別を無視して比較するためのメカニズムを実装するべきです。 通常、これは文字列を大文字に変換します。 例としては UPPER (<in>) です。 そのようなメカニズムがあなたのデータベースに存在しない場合、変更を加えずに単にパラメータをリターンしてください。 return in.toUpper()としてはいけません。
  • Some databases (for example Interbase) do not support the function returned by their implementation of ignoreCase() in the ORDER BY clause of SELECT statements. If your database falls into this category you should override the default implementation of String ignoreCaseInOrderBy(String in). It is NOT required to override this method for other databases--the default implementation calls ignoreCase() and respectCase().
  • いくつかのデータベース(例えばInterbase)は、SELECTステートメントのORDER BY句の中ではignoreCase()の実装によって返されるファンクションをサポートしません。 利用するデータベースがこのカテゴリに分類される場合、String ignoreCaseInOrderBy(String in)のデフォルトの実装をオーバーライドするべきです。 他のデータベースではこのメソッドをオーバーライドする必要はありません--デフォルトの実装ではignoreCase()およびrespectCaseを()を呼び出します。

If you are adding support for a new RDBMS, then you will probably also need to create a set of Velocity templates--used by Torque to generate a SQL schema for your RDBMS--in the directory conf/torque/templates/sql/base/<dbname>. The recommend method for doing this is to copy an existing set of templates and adapt them to your RDBMS as needed.

新しいRDBMSをサポートするならば、ディレクトリ conf/torque/templates/sql/base/<dbname> に新しいVelocityテンプレートのセット(TorqueがRDBMSからSQLスキーマを生成するのに使用します)を作成する必要があるでしょう。 おすすめの方法としては、既存のテンプレートのセットをコピーし、そのRDBMSにそれらを適応させることです。