| java.lang.Object org.garret.perst.Database
Database | public class Database (Code) | | This class emulates relational database on top of Perst storage
It maintain class extends, associated indices, prepare queries.
|
Inner Class :static class Table extends Persistent | |
Constructor Summary | |
public | Database(Storage storage, boolean multithreaded) Constructor of database. | public | Database(Storage storage, boolean multithreaded, boolean autoRegisterTables) Constructor of database. | public | Database(Storage storage) Constructor of single threaded database. |
Method Summary | |
public boolean | addRecord(T record) Add new record to the table. | public boolean | addRecord(Class table, T record) Add new record to the specified table. | public void | beginTransaction() | public void | commitTransaction() | public boolean | createIndex(Class table, String key, boolean unique) Add new index to the table. | public boolean | createIndex(Class table, String key, boolean unique, boolean caseInsensitive) Add new index to the table. | public boolean | createTable(Class table) Create table for the specified class. | public boolean | deleteRecord(T record) Delete record from the table. | public boolean | deleteRecord(Class table, T record) Delete record from the specified table. | public boolean | dropIndex(Class table, String key) Drop index for the specified table and key. | public boolean | dropTable(Class table) Drop table associated with this class. | public boolean | excludeFromIndex(IPersistent record, String key) Exclude record from specified index. | public boolean | excludeFromIndex(Class table, IPersistent record, String key) Exclude record from specified index. | public HashMap | getIndices(Class table) | public IterableIterator<T> | getRecords(Class table) | public IterableIterator<T> | getRecords(Class table, boolean forUpdate) Get iterator through all table records
Parameters: table - class corresponding to the table Parameters: forUpdate - true if records are selected for update - in this case exclusive lock is set for the table to avoid deadlock. | public Storage | getStorage() | public boolean | includeInIndex(IPersistent record, String key) Include record in the specified index. | public boolean | includeInIndex(Class table, IPersistent record, String key) Include record in the specified index. | public Query<T> | prepare(Class table, String predicate) Prepare JSQL query. | public Query<T> | prepare(Class table, String predicate, boolean forUpdate) Prepare JSQL query. | public void | rollbackTransaction() | public IterableIterator<T> | select(Class table, String predicate) Select record from specified table
Parameters: table - class corresponding to the table Parameters: predicate - search predicate exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to the specified class iterator through selected records. | public IterableIterator<T> | select(Class table, String predicate, boolean forUpdate) Select record from specified table
Parameters: table - class corresponding to the table Parameters: predicate - search predicate Parameters: forUpdate - true if records are selected for update - in this case exclusive lock is set for the table to avoid deadlock. exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to the specified class iterator through selected records. |
autoRegisterTables | boolean autoRegisterTables(Code) | | |
multithreaded | boolean multithreaded(Code) | | |
Database | public Database(Storage storage, boolean multithreaded)(Code) | | Constructor of database. This method initialize database if it not initialized yet.
Starting from 2.72 version of Perst.Net, it supports automatic
creation of table descriptors when Database class is used.
So now it is not necessary to explicitly create tables and indices -
the Database class will create them itself on demand.
Indexable attribute should be used to mark key fields for which index should be created.
Table descriptor is created when instance of the correspondent class is first time stored
in the database. Perst creates table descriptors for all derived classes up
to the root java.lang.Object class.
Parameters: storage - opened storage. Storage should be either empty (non-initialized, eitherpreviously initialized by the this method. It is not possible to open storage with root object other than table index created by this constructor. Parameters: multithreaded - true if database should support concurrent accessto the data from multiple threads. |
Database | public Database(Storage storage, boolean multithreaded, boolean autoRegisterTables)(Code) | | Constructor of database. This method initialize database if it not initialized yet.
Parameters: storage - opened storage. Storage should be either empty (non-initialized, eitherpreviously initialized by the this method. It is not possible to open storage with root object other than table index created by this constructor. Parameters: multithreaded - true if database should support concurrent accessto the data from multiple threads. Parameters: autoRegisterTables - automatically create tables descriptors for instances of new classes inserted in the database |
Database | public Database(Storage storage)(Code) | | Constructor of single threaded database. This method initialize database if it not initialized yet.
Parameters: storage - opened storage. Storage should be either empty (non-initialized, eitherpreviously initialized by the this method. It is not possible to open storage with root object other than table index created by this constructor. |
addRecord | public boolean addRecord(T record)(Code) | | 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 true if record was successfully added to the table, false if there is already such record (object with the same ID) in the table or there is some record with the same value of unique key field exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to record class |
addRecord | public boolean addRecord(Class table, T record)(Code) | | 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 Parameters: record - object to be inserted in the table true if record was successfully added to the table, false if there is already such record (object with the same ID) in the table or there is some record with the same value of unique key field exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to record class |
beginTransaction | public void beginTransaction()(Code) | | Begin transaction
|
commitTransaction | public void commitTransaction()(Code) | | Commit transaction
|
createIndex | public boolean createIndex(Class table, String key, boolean unique)(Code) | | Add new index to the table. If such index already exists this method does nothing.
Parameters: table - class corresponding to the table Parameters: key - field of the class to be indexed Parameters: unique - if index is unique or not exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to the specified class true if index is created, false if indexalready exists |
createIndex | public boolean createIndex(Class table, String key, boolean unique, boolean caseInsensitive)(Code) | | Add new index to the table. If such index already exists this method does nothing.
Parameters: table - class corresponding to the table Parameters: key - field of the class to be indexed Parameters: unique - if index is unique or not Parameters: caseInsensitive - if string index is case insensitive exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to the specified class true if index is created, false if indexalready exists |
createTable | public boolean createTable(Class table)(Code) | | Create table for the specified class.
This function does nothing if table for such class already exists
Parameters: table - class corresponding to the table true if table is created, false if table alreay exists |
deleteRecord | public boolean deleteRecord(T record)(Code) | | 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 true if record was successfully deleted from the table, false if there is not such record (object with the same ID) in the table exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to record class |
deleteRecord | public boolean deleteRecord(Class table, T record)(Code) | | 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 Parameters: record - object to be deleted from the table true if record was successfully deleted from the table, false if there is not such record (object with the same ID) in the table exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to specified class |
dropIndex | public boolean dropIndex(Class table, String key)(Code) | | Drop index for the specified table and key.
Does nothing if there is no such index.
Parameters: table - class corresponding to the table Parameters: key - field of the class to be indexed exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to the specified class true if index is deleted, false if index is not found |
dropTable | public boolean dropTable(Class table)(Code) | | Drop table associated with this class. Do nothing if there is no such table in the database.
Parameters: table - class corresponding to the table true if table is deleted, false if table is not found |
excludeFromIndex | public boolean excludeFromIndex(IPersistent record, String key)(Code) | | 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 Parameters: key - name of the indexed field exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to record class true if record is excluded from index, false if there is no such index |
excludeFromIndex | public boolean excludeFromIndex(Class table, IPersistent record, String key)(Code) | | 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 Parameters: record - object to be excluded from the specified index Parameters: key - name of the indexed field exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to the specified class true if record is excluded from index, false if there is no such index |
getIndices | public HashMap getIndices(Class table)(Code) | | Get indices for the specified table
Parameters: table - class corresponding to the table map of table indices |
getRecords | public IterableIterator<T> getRecords(Class table)(Code) | | Get iterator through all table records
Parameters: table - class corresponding to the table iterator through all table records. exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to the specified class |
getRecords | public IterableIterator<T> getRecords(Class table, boolean forUpdate)(Code) | | Get iterator through all table records
Parameters: table - class corresponding to the table Parameters: forUpdate - true if records are selected for update - in this case exclusive lock is set for the table to avoid deadlock. iterator through all table records. exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to the specified class |
getStorage | public Storage getStorage()(Code) | | Get storage associated with this database
underlying storage |
includeInIndex | public boolean includeInIndex(IPersistent record, String key)(Code) | | 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 Parameters: key - name of the indexed field exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to the specified class true if record is included in index, false if there is no such index |
includeInIndex | public boolean includeInIndex(Class table, IPersistent record, String key)(Code) | | 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 Parameters: record - object to be excluded from the specified index Parameters: key - name of the indexed field exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to the specified class true if record is included in index, false if there is no such index |
prepare | public Query<T> prepare(Class table, String predicate)(Code) | | 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 Parameters: predicate - search predicate exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to the specified class exception: CompileError - exception is thrown if predicate is not valid JSQL exception |
prepare | public Query<T> prepare(Class table, String predicate, boolean forUpdate)(Code) | | 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 Parameters: predicate - search predicate Parameters: forUpdate - true if records are selected for update - in this case exclusive lock is set for the table to avoid deadlock. exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to the specified class exception: CompileError - exception is thrown if predicate is not valid JSQL exception |
rollbackTransaction | public void rollbackTransaction()(Code) | | Rollback transaction
|
select | public IterableIterator<T> select(Class table, String predicate)(Code) | | Select record from specified table
Parameters: table - class corresponding to the table Parameters: predicate - search predicate exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to the specified class iterator through selected records. This iterator doesn't support remove() method exception: CompileError - exception is thrown if predicate is not valid JSQL exception exception: JSQLRuntimeException - exception is thrown if there is runtime error during query execution |
select | public IterableIterator<T> select(Class table, String predicate, boolean forUpdate)(Code) | | Select record from specified table
Parameters: table - class corresponding to the table Parameters: predicate - search predicate Parameters: forUpdate - true if records are selected for update - in this case exclusive lock is set for the table to avoid deadlock. exception: StorageError - (CLASS_NOT_FOUND) exception is thrown if there is no table corresponding to the specified class iterator through selected records. This iterator doesn't support remove() method exception: CompileError - exception is thrown if predicate is not valid JSQL exception exception: JSQLRuntimeException - exception is thrown if there is runtime error during query execution |
|
|