## Peers

A Peer class contains the operations on a specific Database table. Peer classes are generated automatically when you generate the Torque classes.

Peer classes have static methods only, so you would never create objects of Peer classes. See the PeerImpl section below if you rather work with instances. Peer methods are thread safe.

For each table, two Peer classes are generated: Base<table-name>Peer and <table-name>Peer. The Base<table-name>Peer class contains all the functionality and should not be changed. The other class is empty but can be extended to add or change functionality. If you regenerate the classes, only the Base* class changes. This allows you to change the schema, but still keep your existing code.

## PeerImpl classes

The PeerImpl classes provide a way to exchange the Peer class implementation, e.g. for unit testing, and can be used if instances are desired rather than static access (the latter is usually less code). If you are happy with the Peer classes as they are, you need not bother about the PeerImpl classes.

PeerImpl classes are generated automatically when you generate the Torque classes.

For each table, two PeerImpl classes are generated: Base<table-name>PeerImpl and <table-name>PeerImpl. The Base<table-name>PeerImpl class contains all the functionality and should not be changed. The other class is empty but can be extended to add or change functionality. If you regenerate the classes, only the Base* class changes.

## Data Objects

A Data Object holds information about a single row of a specific table. They contain a field with getter and setter for each column of the table. Data Objects are generated automatically when you generate the Torque classes.

For each table, two Data Object classes are generated: Base<table-name> and <table-name>. As with the Peers, the Base<table-name> class contains all the functionality and should not be changed. The other class is empty but can be extended to add or change functionality. If you regenerate the classes, only the Base* classes will be overwritten.

Data Objects are used almost exclusively with their related Peer classes. Where peer classes "wrap around" around a database table, a Data Object "wrap around" individual rows of the table. The two always go together.

You normally use Data Objects in one of two ways. The most common way is to extract data after you called a doSelect on a Peer class. The doSelect method returns a List of Data Objects that holds the data of the resultset. Secondly you can create Data Objects and call their save methods to insert or update the related row into the database.

## Criteria

Criteria is an abstraction of the criteria of an sql query. We use criteria objects to specify the criteria of a sql statement. The database adaptor classes contains information on how this Criteria object will be translated to different flavours of sql.

Criteria is in effect a map of field names and values that forms the criteria of a query. By default the comparison is equals (=) but you can define any comparison operator (<, >, <=, > =, IN, etc.).

Criteria can also be used to do some other sql function like ORDER BY or DISTINCT. If Criteria is too limited for your purposes (which should not happen often) you are still free to use raw sql queries.

## Record Mappers

A Record Mapper maps data between JDBC metadata and Data Objects. RecordMapper classes are generated by Torque and used internally, there is usually no need to work with RecordMappers directly.

For each table, two RecordMapper classes are generated: Base<table-name>RecordMapper and <table-name>RecordMapper. If you regenerate the classes, only the Base* classes will be overwritten. If you want to change the RecordMapper code (which should not happen often), add your changes to the <table-name>RecordMapper class so that they do not get overwritten when regenerating the code.

## Database Maps

The Peers make use of a DatabaseMap class that holds internal data about the relational schema. You will seldom, if ever, need to work with the DatabaseMap class. It is used internally by Peers to discover information about the database at runtime.

There is exactly one DatabaseMap for each relational database that you connect to. You may wish to connect to more than one database in your application. You should then have one DatabaseMap for each of the databases.

DatabaseMaps are constructed by classes called MapBuilders. Torque generates MapBuilder classes for each of the tables in your schema. The MapBuilder for a table is called when the Peer class for the table is loaded.

All DatabaseMaps are instances of the class `org.apache.torque.map.DatabaseMap`. They are kept in the field databaseMap of the objects stored in the map `TorqueInstance.databases`. The Map for the database with the name key can be retrieved by the method Torque.getDatabaseMap(key).

## ID Broker

The ID Broker is used to automatically create unique primary keys for tables. It creates id's from a database table called id_table.

Of course Torque also supports using the ID generation provided by the underlying database system - just set the `idMethod` attributes as desired in your schema.

The ID Broker is used in the underlying Peer code. After you have generated your object model classes you need not worry about it anymore.

## Database adapters

Although all databases supported by Torque understand SQL, there are differences in the behavior of the databases which the Torque runtime needs to know about. For example, the standard (String) format of a date object in an Oracle9i database is different from a postgresql database. The adapter for a database provides the necessary methods to hide such differences from the user. For example, the adapter provides a method to create a String in the database's preferred format from a Date object.

Adapters are subclasses of the `org.apache.torque.adapter.AbstractAdapter` class. The adapters are stored in the field adapter of the objects kept in the map `TorqueInstance.databases`. The key of the map is the name of the database (e.g. "bookstore"). The adapter for a given key can be retrieved via the method Torque.getAdapter(key).

If your database is not yet supported, you can read the Support for new Databases docs on how to create a new adapter for your database.

## DataSourceFactories

To access a database, a connection must be made to the database. A DataSource is an object which can provide connections to the database. A DataSourceFactory is used to configure and provide one DataSource.

All DataSourceFactories used by Torque must implement the interface `org.apache.torque.dsfactory.DataSourceFactory`. The adapters are stored in the field dataSourceFactory of the objects kept in the map `TorqueInstance.databases`. The DataSourceFactory for a given key can not be retrieved by a public method; however, a connection from the DataSource for the DataSourceFactory for a given key can be obtained by Torque.getConnection(key);

## Other classes

The Torque runtime library contains more classes than described above. The classes in the packages org.apache.torque.sql and org.apache.torque.adapter are not considered public API of torque, i.e. you can use these classes but they cannot be expected to stay compatible between releases.

## Internal resources used by the Torque Runtime

### Default database name

Torque can be used with several databases at once. The resources for each database are usually kept in Maps where the key is the name of the database. To make things easier for people who use only one database, Torque supports the notion of a default database. This allows it to provide convenience methods like `Torque.getConnection()` where no database name must be specified. These methods refer to the default database, in contrast to e.g. `Torque.getConnection(String)` where the name of the database must be supplied explicitly.

The name of the default database can be retrieved by `Torque.getDefaultDB()`.