Low Level Database Access: Difference between revisions

From Arianne
Jump to navigation Jump to search
imported>Hendrik Brummermann
No edit summary
imported>Hendrik Brummermann
No edit summary
(No difference)

Revision as of 17:25, 26 February 2010



Database
Marauroa
Stendhal


This article describes how Marauroa accesses the database internally and how you can add support for your own tables. The table structure of the Marauroa database is explained in Marauroa Database Structure. You might want to have a look at High Level Database Access first. It explains the high level API to access the database from your program code.


Database abstraction

The level code database access code is encapsulated in DAO classes. Only these classes use JDBC to send SQL queries to the database.

You start a transaction by obtaining a DBTransaction object from the TransactionPool. The you use the methods of the DBTransaction object to talk to the database. Note that many methods expect a parameter map as second parameter. This allows you to use [variables] in your SQL statement without having to worry about escaping input parameters to prevent SQL injection attacks.

DBTransaction, however, does not execute the SQL statements itself. It internally forwards them to a subclass of DatabaseAdapter. This is a very tiny abstraction layer of to hide SQL dialects used by different database systems.



Writing your own DAO

Extending a provided DAO

DAO classes should never be instantiated directly. Instead you should (and marauroa does) use the DAORegistry. This allows you to write a subclass of a DAO provided by marauroa and register it instead. If you are familiar with Spring, this is a similar concept. But without all the bulk of xml configuration files, parameter injection and interfaces with only one single implementation.

Imagine you want to subclass the CharacterDAO with your class SomeGameCharacterDAO: <source lang="java">

    public class SomeGameCharacterDAO extends CharacterDAO {
    ...

</source>

You simply register it as <source lang="java">

    DAORegistry.get().register(CharacterDAO.class, new SomeGameCharacterDAO());

</source> Note: In the register call the first parameter is the parent class you want to replace.

Adding support for another database system

Marauroa tries to stick closely to the SQL standard. It is, however, not possible to write SQL statements that work on all common database systems because each database system has its own dialect. Luckily there are only minor differences. For example MySQL requires "create table" statements to end in "TYPE=InnoDB". Marauroa uses an interface DatabaseAdapter to hide those differences.

There is already a default implementation provided for this interface called AbstractDatabaseAdapter (source). And we provide a MySQLDatabaseAdapter (source) and a H2DatabaseAdapter (source), too.

Just have a look at those java files. It should be very easy to add more adapters for other database systems. Note: We are very interested in accepting those adapters and adding them to the main code base.

Updating the database structure