org.garret.perst
Class Database

java.lang.Object
  extended byorg.garret.perst.Database

public class Database
extends java.lang.Object

This class emulates relational database on top of Perst storage It maintain class extends, associated indices, prepare queries.


Constructor Summary
Database(Storage storage)
          Constructor of database.
 
Method Summary
 boolean addRecord(java.lang.Class table, IPersistent record)
          Add new record to the specified table.
 boolean addRecord(IPersistent record)
          Add new record to the table.
 boolean createIndex(java.lang.Class table, java.lang.String key, boolean unique)
          Add new index to the table.
 boolean createTable(java.lang.Class table)
          Create table for the specified class.
 boolean deleteRecord(java.lang.Class table, IPersistent record)
          Delete record from the specified table.
 boolean deleteRecord(IPersistent record)
          Delete record from the table.
 boolean dropIndex(java.lang.Class table, java.lang.String key)
          Drop index for the specified table and key.
 boolean dropTable(java.lang.Class table)
          Drop table associated with this class.
 boolean excludeFromIndex(java.lang.Class table, IPersistent record, java.lang.String key)
          Exclude record from specified index.
 boolean excludeFromIndex(IPersistent record, java.lang.String key)
          Exclude record from specified index.
 java.util.Iterator getRecords(java.lang.Class table)
          Get iterator through all table records
 Storage getStorage()
          Get storage associated with this database
 boolean includeInIndex(java.lang.Class table, IPersistent record, java.lang.String key)
          Include record in the specified index.
 boolean includeInIndex(IPersistent record, java.lang.String key)
          Include record in the specified index.
 Query prepare(java.lang.Class table, java.lang.String predicate)
          Prepare JSQL query.
 java.util.Iterator select(java.lang.Class table, java.lang.String predicate)
          Select record from specified table
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Database

public Database(Storage storage)
Constructor of database. This method initialize database if it not initialized yet.

Parameters:
storage - opened storage. Storage should be either empty (non-initialized, either previously initialized by the this method. It is not possible to open storage with root object other than table index created by this constructor.
Method Detail

addRecord

public boolean addRecord(java.lang.Class table,
                         IPersistent record)
Add new record to the specified table. Record is inserted in table corresponding to the specified class. Record will be automatically added to all indices existed for this table.

Parameters:
table - class corresponding to the table
record - object to be inserted in the table
Returns:
true if record was successfully added to the table, false if there is already such record (object with the same ID) in the table
Throws:
StorageError(CLASS_NOT_FOUND) - exception is thrown if there is no table corresponding to record class

addRecord

public boolean addRecord(IPersistent record)
Add new record to the table. Record is inserted in table corresponding to the class of the object. Record will be automatically added to all indices existed for this table. If there is not table associated with class of this object, then database will search for table associated with superclass and so on...

Parameters:
record - object to be inserted in the table
Returns:
true if record was successfully added to the table, false if there is already such record (object with the same ID) in the table
Throws:
StorageError(CLASS_NOT_FOUND) - exception is thrown if there is no table corresponding to record class

createIndex

public boolean createIndex(java.lang.Class table,
                           java.lang.String key,
                           boolean unique)
Add new index to the table. If such index already exists this method does nothing.

Parameters:
table - class corresponding to the table
key - field of the class to be indexed
unique - if index is unique or not
Returns:
true if index is created, false if index already exists
Throws:
StorageError(CLASS_NOT_FOUND) - exception is thrown if there is no table corresponding to the specified class

createTable

public boolean createTable(java.lang.Class table)
Create table for the specified class. This function does nothing if table for such class already exists

Parameters:
table - class corresponding to the table
Returns:
true if table is created, false if table alreay exists

deleteRecord

public boolean deleteRecord(java.lang.Class table,
                            IPersistent record)
Delete record from the specified table. Record is removed from the table corresponding to the specified class. Record will be automatically added to all indices existed for this table. Object represented the record will be also deleted from the storage.

Parameters:
table - class corresponding to the table
record - object to be deleted from the table
Returns:
true if record was successfully deleted from the table, false if there is not such record (object with the same ID) in the table
Throws:
StorageError(CLASS_NOT_FOUND) - exception is thrown if there is no table corresponding to specified class

deleteRecord

public boolean deleteRecord(IPersistent record)
Delete record from the table. Record is removed from the table corresponding to the class of the object. Record will be automatically added to all indices existed for this table. If there is not table associated with class of this object, then database will search for table associated with superclass and so on... Object represented the record will be also deleted from the storage.

Parameters:
record - object to be deleted from the table
Returns:
true if record was successfully deleted from the table, false if there is not such record (object with the same ID) in the table
Throws:
StorageError(CLASS_NOT_FOUND) - exception is thrown if there is no table corresponding to record class

dropIndex

public boolean dropIndex(java.lang.Class table,
                         java.lang.String key)
Drop index for the specified table and key. Does nothing if there is no such index.

Parameters:
table - class corresponding to the table
key - field of the class to be indexed
Returns:
true if index is deleted, false if index is not found
Throws:
StorageError(CLASS_NOT_FOUND) - exception is thrown if there is no table corresponding to the specified class

dropTable

public boolean dropTable(java.lang.Class table)
Drop table associated with this class. Do nothing if there is no such table in the database.

Parameters:
table - class corresponding to the table
Returns:
true if table is deleted, false if table is not found

excludeFromIndex

public boolean excludeFromIndex(java.lang.Class table,
                                IPersistent record,
                                java.lang.String key)
Exclude record from specified index. This method is needed to perform update of indexed field (key). Before updating the record, it is necessary to exclude it from indices which keys are affected. After updating the field, record should be reinserted in these indices using includeInIndex method.

If there is not table associated with class of this object, then database will search for table associated with superclass and so on...

This method does nothing if there is no index for the specified field.

Parameters:
table - class corresponding to the table
record - object to be excluded from the specified index
key - name of the indexed field
Returns:
true if record is excluded from index, false if there is no such index
Throws:
StorageError(CLASS_NOT_FOUND) - exception is thrown if there is no table corresponding to the specified class

excludeFromIndex

public boolean excludeFromIndex(IPersistent record,
                                java.lang.String key)
Exclude record from specified index. This method is needed to perform update of indexed field (key). Before updating the record, it is necessary to exclude it from indices which keys are affected. After updating the field, record should be reinserted in these indices using includeInIndex method.

If there is not table associated with class of this object, then database will search for table associated with superclass and so on...

This method does nothing if there is no index for the specified field.

Parameters:
record - object to be excluded from the specified index
key - name of the indexed field
Returns:
true if record is excluded from index, false if there is no such index
Throws:
StorageError(CLASS_NOT_FOUND) - exception is thrown if there is no table corresponding to record class

getRecords

public java.util.Iterator getRecords(java.lang.Class table)
Get iterator through all table records

Parameters:
table - class corresponding to the table
Returns:
iterator through all table records.
Throws:
StorageError(CLASS_NOT_FOUND) - exception is thrown if there is no table corresponding to the specified class

getStorage

public Storage getStorage()
Get storage associated with this database

Returns:
underlying storage

includeInIndex

public boolean includeInIndex(java.lang.Class table,
                              IPersistent record,
                              java.lang.String key)
Include record in the specified index. This method is needed to perform update of indexed field (key). Before updating the record, it is necessary to exclude it from indices which keys are affected using excludeFromIndex method. After updating the field, record should be reinserted in these indices using this method.

If there is not table associated with class of this object, then database will search for table associated with superclass and so on...

This method does nothing if there is no index for the specified field.

Parameters:
table - class corresponding to the table
record - object to be excluded from the specified index
key - name of the indexed field
Returns:
true if record is included in index, false if there is no such index
Throws:
StorageError(CLASS_NOT_FOUND) - exception is thrown if there is no table corresponding to the specified class

includeInIndex

public boolean includeInIndex(IPersistent record,
                              java.lang.String key)
Include record in the specified index. This method is needed to perform update of indexed field (key). Before updating the record, it is necessary to exclude it from indices which keys are affected using excludeFromIndex method. After updating the field, record should be reinserted in these indices using this method.

If there is not table associated with class of this object, then database will search for table associated with superclass and so on...

This method does nothing if there is no index for the specified field.

Parameters:
record - object to be excluded from the specified index
key - name of the indexed field
Returns:
true if record is included in index, false if there is no such index
Throws:
StorageError(CLASS_NOT_FOUND) - exception is thrown if there is no table corresponding to the specified class

prepare

public Query prepare(java.lang.Class table,
                     java.lang.String predicate)
Prepare JSQL query. Prepare is needed for queries with parameters. Also preparing query can improve speed if query will be executed multiple times (using prepare, it is compiled only once).

To execute prepared query, you should use Query.execute(db.getRecords(XYZ.class)) method

Parameters:
table - class corresponding to the table
predicate - search predicate
Throws:
StorageError(CLASS_NOT_FOUND) - exception is thrown if there is no table corresponding to the specified class
CompileError - exception is thrown if predicate is not valid JSQL exception

select

public java.util.Iterator select(java.lang.Class table,
                                 java.lang.String predicate)
Select record from specified table

Parameters:
table - class corresponding to the table
predicate - search predicate
Returns:
iterator through selected records. This iterator doesn't support remove() method
Throws:
StorageError(CLASS_NOT_FOUND) - exception is thrown if there is no table corresponding to the specified class
CompileError - exception is thrown if predicate is not valid JSQL exception
JSQLRuntimeException - exception is thrown if there is runtime error during query execution