org.vrspace.server.db
Class SQLDB

java.lang.Object
  extended byorg.vrspace.server.DB
      extended byorg.vrspace.server.db.SQLDB
All Implemented Interfaces:
org.vrspace.server.db.Types

public class SQLDB
extends DB
implements org.vrspace.server.db.Types

SQLDB is based on ORM (object-relational mapping) logic. Unlike standard solutions for ORM, this DB doesn't have any xml class descriptors - it has dynamic class analysis and class description repository in DB itself. This way, adding objects of some new class to DB is transparent to developers - they just put objects of new class to DB - and DB will do everything else automaticaly... Underlying relational DB can be any RDBMS that have JDBC drivers. For now implementation is done for MySQL. HOWTO Start using VRSpace server with SQLDB and MySQL ====================================================== - if you don't have MySQL, you can download it from http://www.mysql.com - after installing MySQL, create new DB (just create 'vrspace' directory in mysql/data directory) - go to VRSpace dist directory, find vrspace.cfg file, and add next properties (or replace values for existing one): vrspace.db.class=org.vrspace.server.db.SQLDB vrspace.db.url=jdbc:mysql://localhost/vrspace - start MySQL, then start VRSpace server... HOWTO Implement another RDBMS into SQLDB ========================================= If you want to implement another RDBMS into SQLDB, you have to: - add JDBC driver for targeted RDBMS to /lib directory of vrspace project - create new 'dbname'.properties file in org.vrspace.server.db.config directory, copy all properties from some existing properties file (e.g. mysql.properties), and change them depending on targeted RDBMS - 'dbname' is name of db that appears in jdbc URL, example: for MySQL url is something like: "jdbc:mysql://localhost/vrspace", so valid 'dbname' is "mysql", not "MySQL" or "My SQL"!!! - add this name to 'DBNames' array below in code ...and that's it! now you can look at "HOWTO Start using VRSpace server with SQLDB and MySQL" and write some docs for your RDBMS implementation... General logic: ============== DB stores objects by storing all object's public fields. When storing object of class that is inherited from other class(es), every field is stored in table of class that declares that field. Tables for classes that don't have any public fields are not created. Requirements and restrictions: ============================== every object that will be stored in DB must inherit VRObject class stored can be any public field that is: - primitive - primitive wrapper object - java.lang.String - java.util.Date - object whose class is subclass of VRObject class - object whose class is not subclass of VRObject class, but whose class has: - public toString() method - constructor that accepts String as argument - array of all primitives/objects above - TODO: collection classes (Map, Set, Vector,...) public fields that don't follow above rules will not be stored DB internal structure: ====================== DB consists of "repository" tables and "object" tables: "repository" tables stores information about classes, objects and packages "object" tables store object's fields Description of "repository" tables: PACKAGES +--------------+--------------+ | Field | Type | +--------------+--------------+ | PACKAGE_ID | bigint(20) | | PACKAGE_NAME | varchar(255) | +--------------+--------------+ OBJECTS +-----------+------------+ | Field | Type | +-----------+------------+ | OBJECT_ID | bigint(20) | | CLASS_ID | bigint(20) | +-----------+------------+ CLASSES +------------------+--------------+ | Field | Type | +------------------+--------------+ | CLASS_ID | bigint(20) | | SUPERCLASS_ID | bigint(20) | | PACKAGE_ID | bigint(20) | | CLASS_NAME | varchar(255) | | CLASS_DESCRIPTOR | text | +------------------+--------------+ SUPERCLASS_ID in one CLASSES record must exist as CLASS_ID in another CLASSES record (for classes whose superclass is org.vrspace.server.VRObject, SUPERCLASS_ID is 0) CLASS_NAME is full class name CLASS_DESCRIPTOR is class info object stored as string Description of "object" tables: - names of tables are full class names with dots replaced by optional char ('_') (because DB's can't have dots in table names) - every table has DB_ID column that contains OBJECT_ID stored in OBJECTS repository table - names of columns are names of object public fields - types of columns depend on field type, see org.vrspace.server.Types - if field is array, it's stored in separate table: table name is full class name + field name every member of array is stored in separate row - if field if another VRObject object, it's stored in apropriate table, and his DB_ID is stored here as reference

Author:
eddie@vrspace.org

Field Summary
static int BOOLEAN
           
static java.lang.String BOOLEAN_NAME
           
static int BOOLEAN_OBJ
           
static java.lang.String BOOLEAN_OBJ_NAME
           
static int BYTE
           
static java.lang.String BYTE_NAME
           
static int BYTE_OBJ
           
static java.lang.String BYTE_OBJ_NAME
           
static int CHAR
           
static java.lang.String CHAR_NAME
           
static int CHAR_OBJ
           
static java.lang.String CHAR_OBJ_NAME
           
static int DATE
           
static java.lang.String DATE_NAME
           
static int DOUBLE
           
static java.lang.String DOUBLE_NAME
           
static int DOUBLE_OBJ
           
static java.lang.String DOUBLE_OBJ_NAME
           
static int FLOAT
           
static java.lang.String FLOAT_NAME
           
static int FLOAT_OBJ
           
static java.lang.String FLOAT_OBJ_NAME
           
static int INT
           
static java.lang.String INT_NAME
           
static int INT_OBJ
           
static java.lang.String INT_OBJ_NAME
           
static int LONG
           
static java.lang.String LONG_NAME
           
static int LONG_OBJ
           
static java.lang.String LONG_OBJ_NAME
           
static int OBJECT
           
static int SHORT
           
static java.lang.String SHORT_NAME
           
static int SHORT_OBJ
           
static java.lang.String SHORT_OBJ_NAME
           
static int STRING
           
static java.lang.String STRING_NAME
           
static int VROBJECT
           
 
Fields inherited from class org.vrspace.server.DB
cache
 
Constructor Summary
SQLDB()
           
 
Method Summary
 void commit()
          Commit changes
 void connect(java.lang.String name)
          Connect to the database
 java.lang.String create(java.lang.String name)
          not implemented
 void delete(java.lang.Object obj)
          Delete object from database.
 void disconnect()
          Close connection to the database
 java.lang.Object get(java.lang.Object obj)
          Retreives an object from the database
 java.lang.Object get(java.lang.String className, long db_id)
          Retreives an object from the database
 java.lang.Object get(java.lang.String className, java.lang.String field, java.lang.Object value)
          Get first object of class className, whose field field have value value
 java.lang.Object[] getAll(java.lang.String className)
          Get all objects of className from db
 java.lang.Object[] getAllObjects()
          return all objects in DB.
 java.lang.String[] getClasses()
          Returns all VRObject Classes stored in the database.
 java.lang.Object[] getRange(java.lang.Object o1, java.lang.Object o2)
          Get all objects that are "between" object o1 and o2.
 java.lang.Object[] getRange(java.lang.String className, java.lang.String field, java.lang.Object value)
          Get all objects of class className, whose field field have value value
 void put(java.lang.Object obj)
          Stores obj to the database.
 void update(Request r)
          Update one field in object.
 
Methods inherited from class org.vrspace.server.DB
load, load, load
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BOOLEAN

public static final int BOOLEAN
See Also:
Constant Field Values

BYTE

public static final int BYTE
See Also:
Constant Field Values

SHORT

public static final int SHORT
See Also:
Constant Field Values

INT

public static final int INT
See Also:
Constant Field Values

LONG

public static final int LONG
See Also:
Constant Field Values

FLOAT

public static final int FLOAT
See Also:
Constant Field Values

DOUBLE

public static final int DOUBLE
See Also:
Constant Field Values

CHAR

public static final int CHAR
See Also:
Constant Field Values

BOOLEAN_OBJ

public static final int BOOLEAN_OBJ
See Also:
Constant Field Values

BYTE_OBJ

public static final int BYTE_OBJ
See Also:
Constant Field Values

SHORT_OBJ

public static final int SHORT_OBJ
See Also:
Constant Field Values

INT_OBJ

public static final int INT_OBJ
See Also:
Constant Field Values

LONG_OBJ

public static final int LONG_OBJ
See Also:
Constant Field Values

FLOAT_OBJ

public static final int FLOAT_OBJ
See Also:
Constant Field Values

DOUBLE_OBJ

public static final int DOUBLE_OBJ
See Also:
Constant Field Values

CHAR_OBJ

public static final int CHAR_OBJ
See Also:
Constant Field Values

STRING

public static final int STRING
See Also:
Constant Field Values

DATE

public static final int DATE
See Also:
Constant Field Values

OBJECT

public static final int OBJECT
See Also:
Constant Field Values

VROBJECT

public static final int VROBJECT
See Also:
Constant Field Values

BOOLEAN_NAME

public static final java.lang.String BOOLEAN_NAME
See Also:
Constant Field Values

BYTE_NAME

public static final java.lang.String BYTE_NAME
See Also:
Constant Field Values

SHORT_NAME

public static final java.lang.String SHORT_NAME
See Also:
Constant Field Values

INT_NAME

public static final java.lang.String INT_NAME
See Also:
Constant Field Values

LONG_NAME

public static final java.lang.String LONG_NAME
See Also:
Constant Field Values

FLOAT_NAME

public static final java.lang.String FLOAT_NAME
See Also:
Constant Field Values

DOUBLE_NAME

public static final java.lang.String DOUBLE_NAME
See Also:
Constant Field Values

CHAR_NAME

public static final java.lang.String CHAR_NAME
See Also:
Constant Field Values

BOOLEAN_OBJ_NAME

public static final java.lang.String BOOLEAN_OBJ_NAME
See Also:
Constant Field Values

BYTE_OBJ_NAME

public static final java.lang.String BYTE_OBJ_NAME
See Also:
Constant Field Values

SHORT_OBJ_NAME

public static final java.lang.String SHORT_OBJ_NAME
See Also:
Constant Field Values

INT_OBJ_NAME

public static final java.lang.String INT_OBJ_NAME
See Also:
Constant Field Values

LONG_OBJ_NAME

public static final java.lang.String LONG_OBJ_NAME
See Also:
Constant Field Values

FLOAT_OBJ_NAME

public static final java.lang.String FLOAT_OBJ_NAME
See Also:
Constant Field Values

DOUBLE_OBJ_NAME

public static final java.lang.String DOUBLE_OBJ_NAME
See Also:
Constant Field Values

CHAR_OBJ_NAME

public static final java.lang.String CHAR_OBJ_NAME
See Also:
Constant Field Values

STRING_NAME

public static final java.lang.String STRING_NAME
See Also:
Constant Field Values

DATE_NAME

public static final java.lang.String DATE_NAME
See Also:
Constant Field Values
Constructor Detail

SQLDB

public SQLDB()
Method Detail

create

public java.lang.String create(java.lang.String name)
not implemented

Specified by:
create in class DB
Parameters:
name - Database name
Returns:
String to use as parameter to connect()
See Also:
DB.connect(java.lang.String)

connect

public void connect(java.lang.String name)
             throws java.lang.Exception
Connect to the database

Specified by:
connect in class DB
Parameters:
name - database URL
Throws:
java.lang.Exception

commit

public void commit()
Commit changes

Specified by:
commit in class DB

disconnect

public void disconnect()
Close connection to the database

Specified by:
disconnect in class DB

get

public java.lang.Object get(java.lang.Object obj)
                     throws java.lang.Exception
Retreives an object from the database

Specified by:
get in class DB
Throws:
java.lang.Exception

get

public java.lang.Object get(java.lang.String className,
                            long db_id)
                     throws java.lang.Exception
Retreives an object from the database

Specified by:
get in class DB
Throws:
java.lang.Exception

get

public java.lang.Object get(java.lang.String className,
                            java.lang.String field,
                            java.lang.Object value)
                     throws java.lang.Exception
Get first object of class className, whose field field have value value

Specified by:
get in class DB
Throws:
java.lang.Exception

getRange

public java.lang.Object[] getRange(java.lang.String className,
                                   java.lang.String field,
                                   java.lang.Object value)
                            throws java.lang.Exception
Get all objects of class className, whose field field have value value

Specified by:
getRange in class DB
Throws:
java.lang.Exception

getRange

public java.lang.Object[] getRange(java.lang.Object o1,
                                   java.lang.Object o2)
                            throws java.lang.Exception
Get all objects that are "between" object o1 and o2. Object o1 and o2 must be of same class, and must have implemented method compareTo(obj) (interface Comparable), else null is returned.

Specified by:
getRange in class DB
Throws:
java.lang.Exception

getAll

public java.lang.Object[] getAll(java.lang.String className)
                          throws java.lang.Exception
Get all objects of className from db

Specified by:
getAll in class DB
Throws:
java.lang.Exception

delete

public void delete(java.lang.Object obj)
            throws java.lang.Exception
Delete object from database.

Specified by:
delete in class DB
Throws:
java.lang.Exception

put

public void put(java.lang.Object obj)
         throws java.lang.Exception
Stores obj to the database.

Specified by:
put in class DB
Throws:
java.lang.Exception

update

public void update(Request r)
Update one field in object. Field and object are encapsulated inside request object. This method allows optimal database update. Updated are only public fields.

Specified by:
update in class DB

getAllObjects

public java.lang.Object[] getAllObjects()
                                 throws java.lang.Exception
return all objects in DB.

Throws:
java.lang.Exception

getClasses

public java.lang.String[] getClasses()
                              throws java.lang.Exception
Description copied from class: DB
Returns all VRObject Classes stored in the database.

Specified by:
getClasses in class DB
Throws:
java.lang.Exception