Documentation - How-to use Druide DB

Imagine you have an application that needs to store the name and the job of some people.

You could design a dataBase like this one : Base MyBase

Table Person : 'first name', 'last name', 'job' --- Primary key is 'first name' 'last name'

Table Job : 'id', 'Description' --- Primary key is an auto-increment ID

You now can do the following operations :

Examples of data :

Job
id Description
0 Developper
1 Database Master
2 Analyst
3 Architect
Person
First name Last name Job
Paul Johnson 0
John Moore 0
Andrew Smith 3
Scott Tiger 1


Configuration


You just have to create a propertie file : 5 ultra fast steps are required.And then you can create your DAO and start coding.

Our example file example.properties :

1- Precise your sources files location. This path can be
relative or absolute. Always use '/' it's easier than '\\', and never
use a single '\' for your path. You can forgot the trailing slash :

filedb.gen.source = /home/toto/dev/myProject/src

or :

filedb.gen.source = C:/dev/myProject/src

 

2- Give your package name. I will automatically add a '.dao' suffix :

filedb.gen.package = ledruide.tests.db 

 

3- Add a version for your database, put whatever you want. This
version is something you will probably need if one day you want to
develop an automatic database upgrade for your application :

filedb.gen.version = 1.5 

 

4- Enter a backup level (will be implemented before the 1.0 release). If set to :

  • 0 or less, there will be no backup of your database (unsecured but max performance).
  • 1, your database will be backuped everytime you save something to it (secured but less performance),
  • 2, your database will be backuped every 2 times you you save something to it,
  • 23, your database will be backuped every 23 times you you save something to it,
  • And so on...
filedb.gen.save.level = 5 

 

5- Finally you can describe your database :

Give it a name :

filedb.gen.nom = MyBase 

And describe your tables :

  • + : used to define an auto-increment primary key
  • * : used to define a multi-column primary key
filedb.gen.table.job  = id+, description
filedb.gen.table.person = firstname*, lastname*, jobid 

And that's it, your database is almost ready, one final step is to generate all the DAO and access files.

Now you just have to call in a command line where your property file is (adapt the JARs path of the classpath) :

java -classpath ../lib/druideDB-X.X.X.jar;../lib/castor-xml.jar;../lib/xerces.jar com.ledruide.druidedb.gen.Generator example.properties 

And then all the DAO source files and mapping files are ready to use in your application.

NOTHING MORE you're ready to develop your application !


Insert / Update
In our example to insert a new data you just have to do that :

Job someJob = new Job();
someJob.setDescription("Senior architect");
someJob.save();
Person someOne = new Person();



someOne.setFirstname("Suzan");



someOne.setLastname("Anderson");



someOne.setJobid(someJob.getId());



someOne.save();

Now you have just added a new Job (whose id will be auto-incremented
from the last entry), and a new person who is a Senior Architect.



Find a record

In our example to find a line you just have to do that :

Person someOne = new Person();
someOne.setFirstname("Andrew");
List results = PersonPeer.find(someOne);

And we have a List of all the people in the database whose first name is Andrew ...

Find by primary Key

You have the PK let's have the result fast : :

Job aJob = JobPeer.findByPK("4"); 

And you have the Job... Hey, that's easy!

Find all records

List people = PersonPeer.findAll();

Then you can iterate over the list containing Person object... simple no ?

 



Update
Let's find a job :

Job aJob = JobPeer.findByPK("4");

Let's update this job :

aJob.setDescription("Really cool senior architect");
aJob.save();

Now you have just updated an existing Job.


Delete a record
In our example to delete a line you just have to do that :

Job aJob = new Job();
aJob.setId("2");
if (JobPeer.delete(aJob)) {
System.out.println("Job deleted...");
} else {
System.out.println("Job not deleted...");
}

This line will delete the record where the Id (wich is the PK of this table) is '2'.

This method returns a boolean true if everything happens correctly, all the jobs in the List will be deleted...
The delete() méthod can also have the commit argument in order to wait before commiting modifications.
For instance we want to delete the person "3" AND Job "2" and if one fails we just cancel modifications :

try {
Person someOne = PersonPeer.findByPK("3");
PersonPeer.delete(someOne, false);
Job aJob = new Job();
aJob.setId("2");
JobPeer.delete(aJob, true);
} 
catch (Exception ex) {
// we can do the rollback on any Peer we want....
JobPeer.rollback();
}

Delete some records

List someJobs = new ArrayList(2);
Job myJob1 = new Job();
myJob1.setId("2");
someJobs.add(myJob1);

Job myJob2 = new Job();
myJob2.setId("8");
someJobs.add(myJob2);
JobPeer.delete(someJobs);

This method returns a boolean true if everything happens correctly, all the jobs in the List will be deleted...
The delete() méthod can also have the commit argument in order to wait before commiting modifications as we saw before.

Delete all records

JobPeer.deleteAll();

This method returns a boolean true if everything is all the Jobs were deleted correctly...

Once you have understood these simple concepts, you know almost everything about DruideDB !
The rest you have to know is that DruideDB construct an XML database structure with the help of Castor


Transactions
By the way, if you want to use transactions it's easy.
For instance let's imagine you need to do several operations at once, and if one fails we want to cancel modifications :

try {
BankAccount accountA = new BankAccount();
accountA.setAccount("00354645A45BT");
accountA.setAmount("100");
accountA.save(false); // Change is only in memory
BankAccount accountB = new BankAccount();
accountB.setAccount("RT5674YH76544");
accountB.setAmount("-600");
accountB.save(false); // Change is only in memory
BankAccount accountC = new BankAccount();
accountC.setAccount("12345678JHGF56");
accountC.setAmount("3490643");
accountC.save(true); // Everything's OK : we save the data
}
catch (BankException be) {
// Here you want a rollback of all this changes
BankAccountPeer.rollback(); // back to previous sate
}

Aren't transations easy ?