GARBA.ORG

SmartDB

SmartDB is very small library for simple and quick Java-persistence but with handy capabilities such as memory-cache, structure management and simple join support. It doesn't need any kind of complex XML configuration. You can start using it in a few minutes just by looking at the examples. It is distributed under the GNU Lesser General Public License.

Main Features

Example

Consider the following scenario:

employees table

employee_id role_id name
1 1 Canova
2 5 Iacobellis
3 6 Lotzy
4 4 Albenga
5 3 Dino
6 2 Fietta
    roles table

role_id role_pid description
1 0 President
2 1 CEO
3 1 CTO
4 1 Administration
5 3 Servlet programming
6 3 EJB Programming

Each employee has a name and a role, in turn each role has a description and is hierarchically related to another role (the president is on top of the structure). The role_pid field denotes the parent role.

		President
		  |
		  |- CEO
		  |
		  |- CTO
		  |   |
		  |   |-Servlet programming
		  |   |-EJB programming
                  |
		  |- Administration

1) Write your POJOs (or recycle existing classes)


employee

01 package org.garba.smartdb.example;
02 
03 import org.garba.smartdb.*;
04 
05 public class Employee implements Table {
06 
07   private Integer employeeId;
08   private String name;
09   private Integer roleId;
10   private String roleDescription;
11   
12   public String getTableName() { return "employees"}
13 
14   public Integer getEmployeeId() { return employeeId;  }
15   public FieldInfo getEmployeeIdInfo() { return new PrimaryKey()}
16   public void setEmployeeId(Integer employeeId) {  this.employeeId = employeeId; }
17   
18   public String getName() { return name; }
19   public void setName(String name) { this.name = name; }
20   
21   public Integer getRoleId() { return roleId;}
22   public FieldInfo getRoleIdInfo() { return new ForeignKey(new Role().getTableName())}
23   public void setRoleId(Integer roleId) { this.roleId = roleId; }
24   
25   public String getRoleDescription() { return roleDescription; }
26   public FieldInfo getRoleDescriptionInfo() { 
27     return new ForeignField("description"new Role().getTableName());
28   }
29   public void setRoleDescription(String roleDescription) { this.roleDescription = roleDescription; 
30 }
Employee.java

Fields intended to be mapped to table columns must follow the standard getter/setter approach. There's just little information you need to specify in order to have a working SmartDB class:


role

01 package org.garba.smartdb.example;
02 
03 import org.garba.smartdb.*;
04 
05 public class Role implements Table, ArrayCache {
06 
07   private int roleId;
08   private int rolePid;
09   private String description;
10   
11   public String getTableName() { return "roles"}
12 
13   public int getRoleId() { return roleId; }
14   public FieldInfo getRoleIdInfo() { return new PrimaryKey()}
15   public void setRoleId(int roleId) { this.roleId = roleId; }
16   
17   public int getRolePid() { return rolePid; }
18   public FieldInfo getRolePidInfo() {  return new   ParentKey()}
19   public void setRolePid(int rolePid) { this.rolePid = rolePid; }
20 
21   public String getDescription() { return description; }
22   public void setDescription(String description) { this.description = description; }
23   
24 }
Role.java

This table implements ArrayCache. You can also use MapCache. ArrayCache is slightly faster (10%) as the data is internally stored as an array but the drawback is that you may find null values when there are missing numbers in the sequence of primary keys.


2) Start your engine


1     DBConfig config = new DBConfig(  "com.mysql.jdbc.Driver",
2                     "jdbc:mysql://localhost/example",
3                     "root",
4                     "");
5     DBEngine engine = new DBEngine(config);  

That's all what you have to do to initialize the engine. (You only need to import the org.garba.smartdb.* package). You can optionally use a datasource as an initialization parameter if you want to use your container's pool services.


3) Show all employees (with role description)


1     Iterator i = engine.loadAll(Employee.class).iterator();
2     
3     while (i.hasNext()) {
4       Employee e = (Employee)i.next();
5       System.out.println(e.getName() " - " + e.getRoleDescription());
6     }
	Canova     - President
	Iacobellis - Servlet programmer
	Lotzy      - EJB programmer
	Albenga    - Administration
	Dino       - CTO
	Fietta     - CEO

4) Perform common operations


01     // Create 
02 
03     Employee e = new Employee();
04     e.setName("Antonio");
05     e.setRoleId(new Integer(4));
06     e = (Employee)engine.createAndGetObject(e);
07     
08     // Update 
09 
10     e.setName("Pigro");
11     engine.update(e);
12     
13     // Delete
14     
15     engine.delete(Employee.class, e.getEmployeeId());

5) Use some of the tree functions (display roles under the CTO)


1     int children[] = engine.getChildrenFromTree(Role.class, new Integer(3));
2     
3     for (int i = ; i < children.length ; i++) {
4       Role r = (Role)engine.load(Role.class, children[i]);
5       System.out.println(r.getDescription());
6     }
	Servlet programmer
	EJB programmer

Rules & Limitations

Download

Note: SmartDB is still in alpha state


Colored Java sources by Java2html
© Ernesto Garbarino Top