Low Level Database Access: Difference between revisions
imported>Hendrik Brummermann No edit summary |
imported>Hendrik Brummermann No edit summary |
||
| Line 11: | Line 11: | ||
== Database abstraction == |
== 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 [http://stendhal.game-host.org/hudson/job/marauroa_HEAD/javadoc/marauroa/server/db/TransactionPool.html TransactionPool]. The you use the methods of the [http://stendhal.game-host.org/hudson/job/marauroa_HEAD/javadoc/marauroa/server/db/DBTransaction.html DBTransaction] object to talk to the database. Note that many methods expect a parameter map as second parameter. This allows you to use <nowiki>[variables]</nowiki> 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. |
|||
[[Image:Classdiagram_database_access.png]] |
|||
== Writing your own DAO == |
== Writing your own DAO == |
||
Revision as of 17:08, 26 February 2010
- 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
Updating the database structure
TODO: Clean this up:
JDBC technology is an API that lets you access virtually any tabular data source from the Java programming language. It provides cross-DBMS connectivity to a wide range of relational databases. Unfortunatally it does not hide vendor specific stuff. Marauroa and Stendhal currently only work with MySQL because of that. Adding support for other database software would be very easy as the database specific code is concentrated in the classes JDBCDatabase (Marauroa) and StendhalPlayerDatabase (Stendhal). We have no need for that, so it was not done, yet. We will, however, accept patches for multi database system support, so if you need it, go ahead.
You need to download MySQL Connector/J in order to get it to run: http://www.mysql.com/downloads/api-jdbc-stable.html
To configure Marauroa to work with a JDBC source, run the appropriate GenerateINI program. For Stendhal this is games.stendhal.server.core.engine.GenerateINI.
Before using the application with the database, you need to create the database itself. So, with MySQL just run MySQL and enter:
create database stendhal; grant all on stendhal.* to stendhal_user@localhost identified by 'stendhal_passwd';
The rest of code is handled by the server itself, and it will create the tables if they don't exist.
