AutoCloseablepublic interface OrmContext extends AutoCloseable
OrmObject.
Applications can use this interface to create, retrieve, update, and delete database entities.
OrmObjectSystemCapability.Data#DATA_APPDATAMGR| 限定符和类型 | 方法 | 说明 |
|---|---|---|
Double |
avg(OrmPredicates predicates,
String field) |
Queries data that meets the conditions specified in the
OrmPredicates instance and returns the average
value of the matched data in a specified column. |
boolean |
backup(String destPath) |
Backs up the current database to a specified path.
|
boolean |
backup(String destPath,
byte[] destEncryptKey) |
Backs up a database in a specified path using a specified encryption key.
|
void |
beginTransaction() |
Begins a transaction in EXCLUSIVE mode.
|
void |
changeEncryptKey(byte[] newEncryptKey) |
Changes the encryption key of an encrypted object-relational mapping (ORM) database that is opened.
|
void |
close() |
Clears the memory data and releases the store.
|
void |
commit() |
Commits a transaction.
|
Long |
count(OrmPredicates predicates) |
Queries data that meets the conditions specified by the
OrmPredicates instance and returns the number
of the matched data records in a specified column. |
int |
delete(OrmPredicates predicates) |
Deletes
OrmObject instances that meet the conditions specified by the OrmPredicates instance. |
<T extends OrmObject> |
delete(T object) |
Deletes an object from the database.
|
boolean |
flush() |
Writes unsaved data changes into the database.
|
String |
getAlias() |
Obtains the storage alias related to the current database context.
|
<T extends OrmObject> |
insert(T object) |
Inserts an object to the database.
|
boolean |
isInTransaction() |
Specifies whether the object is in a transaction.
|
Double |
max(OrmPredicates predicates,
String field) |
Queries data that meets the conditions specified by the
OrmPredicates instance and returns the maximum
value of the matched data in a specified column. |
Double |
min(OrmPredicates predicates,
String field) |
Queries data that meets the conditions specified in the
OrmPredicates instance and returns the minimum
value of the matched data in a specified column. |
<T extends OrmObject> |
query(OrmPredicates predicates) |
Queries
OrmObject instances that meet the conditions specified by the OrmPredicates instance. |
ResultSet |
query(OrmPredicates predicates,
String[] columns) |
Queries results that meet the conditions specified by the
OrmPredicates instance and returns the results
based on specified columns. |
void |
registerContextObserver(OrmContext watchedContext,
OrmObjectObserver observer) |
Registers an
OrmObjectObserver object to listen for changes of a specified OrmContext object. |
void |
registerEntityObserver(String entityName,
OrmObjectObserver observer) |
Registers an
OrmObjectObserver object to listen for changes of a specified Entity object. |
void |
registerObjectObserver(OrmObject ormObject,
OrmObjectObserver observer) |
Registers an
OrmObjectObserver object to listen for changes of a specified OrmObject instance. |
void |
registerStoreObserver(String alias,
OrmObjectObserver observer) |
Registers an
OrmObjectObserver object to listen for changes in the storage specified by its alias. |
boolean |
restore(String srcPath) |
Restores data from a specified database file.
|
boolean |
restore(String srcPath,
byte[] srcEncryptKey,
byte[] destEncryptKey) |
Restores a database from a specified encrypted or unencrypted database file.
|
void |
rollback() |
Rolls back a transaction.
|
Double |
sum(OrmPredicates predicates,
String field) |
Queries data that meets the conditions specified in the
OrmPredicates instance and returns the sum of
the matched data in a specified column. |
void |
unregisterContextObserver(OrmContext watchedContext,
OrmObjectObserver observer) |
Unregisters the
OrmObjectObserver object used to listen for changes of a specified OrmContext
object. |
void |
unregisterEntityObserver(String entityName,
OrmObjectObserver observer) |
Unregisters the
OrmObjectObserver object used to listen for changes of a specified Entity object. |
void |
unregisterObjectObserver(OrmObject ormObject,
OrmObjectObserver observer) |
Unregisters the
OrmObjectObserver object used to listen for changes of a specified OrmObject
instance. |
void |
unregisterStoreObserver(String alias,
OrmObjectObserver observer) |
Unregisters the
OrmObjectObserver object used to listen for changes in the storage specified by its
alias. |
int |
update(OrmPredicates predicates,
ValuesBucket value) |
Updates information in
ValuesBucket to a specified OrmObject instance based
on the conditions specified by the OrmPredicates instance. |
<T extends OrmObject> |
update(T object) |
Updates an object in the database.
|
<T extends OrmObject> |
where(Class<T> clz) |
Creates an
OrmPredicates instance. |
<T extends OrmObject> boolean insert(T object)
After this method is executed, data changes are stored in the memory. You need to use OrmContext#flush
to save the data changes in a database file.
T - Indicates the object type, which is restricted to subclasses of OrmObject.object - Indicates the object to be inserted.true if the object is inserted; returns false otherwise.<T extends OrmObject> boolean update(T object)
After this method is executed, data changes are stored in the memory. You need to use OrmContext#flush
to save the data changes in a database file.
T - Restricts the results that belong to the specified OrmObject.object - Indicates the object to be updated.true if the operation is successful; returns false otherwise.<T extends OrmObject> boolean delete(T object)
After this method is executed, data changes are stored in the memory. You need to use OrmContext#flush
to save the data changes in a database file.
T - Restricts the results that belong to the specified OrmObject.object - Indicates the object to be deleted.true if the operation is successful; returns false otherwise.<T extends OrmObject> OrmPredicates where(Class<T> clz)
OrmPredicates instance.
You can use this method to restrict the class of objects to be operated. To perform more specific database
operations, use OrmPredicates.
T - Indicates the type of clz, which is restricted to subclasses of OrmObject.clz - Indicates the class of the objects to search.OrmPredicates instance.OrmPredicates<T extends OrmObject> List<T> query(OrmPredicates predicates)
OrmObject instances that meet the conditions specified by the OrmPredicates instance.
You can use OrmPredicates to limit the operation range. Conditions contained in the
OrmPredicates instance must be compliant with SQL statement logic.
T - Indicates the type of elements in the returned list, which is restricted to subclasses
of OrmObject.predicates - Indicates the predicates that contain the match conditions.OrmPredicatesResultSet query(OrmPredicates predicates, String[] columns)
OrmPredicates instance and returns the results
based on specified columns.
This method supports cross-process query.
predicates - Indicates predicates that contain the match conditions.columns - Indicates the database column names.ResultSet instance if no data is matched.int delete(OrmPredicates predicates)
OrmObject instances that meet the conditions specified by the OrmPredicates instance.
You can use OrmPredicates to limit the operation range. Conditions contained in the
OrmPredicates instance must be compliant with SQL statement logic.
predicates - Indicates predicates that contain the match conditions.0 if no data is matched.OrmPredicatesint update(OrmPredicates predicates, ValuesBucket value)
ValuesBucket to a specified OrmObject instance based
on the conditions specified by the OrmPredicates instance.
You can use OrmPredicates to limit the operation range. Conditions contained in the
OrmPredicates instance must be compliant with SQL statement logic.
predicates - Indicates predicates that contain the match conditions.value - Indicates the map that caches a row of data. The keys should be the column names and the mapping
values.0 if no data is matched.OrmPredicatesLong count(OrmPredicates predicates)
OrmPredicates instance and returns the number
of the matched data records in a specified column.predicates - Indicates predicates that contain the match conditions.0 if no data is matched.OrmPredicatesDouble max(OrmPredicates predicates, String field)
OrmPredicates instance and returns the maximum
value of the matched data in a specified column.
If no data meets the conditions, this method will return 0. Conditions contained in
the OrmPredicates instance must be compliant with SQL statement logic.
predicates - Indicates predicates that contain the match conditions.field - Indicates the column name.0 if no data
is matched.OrmPredicatesDouble min(OrmPredicates predicates, String field)
OrmPredicates instance and returns the minimum
value of the matched data in a specified column.
If no data meets the conditions, this method will return 0. Conditions contained in the
OrmPredicates instance must be compliant with SQL statement logic.
predicates - Indicates predicates that contain the match conditions.field - Indicates the column name.0 if no data is
matched.OrmPredicatesDouble avg(OrmPredicates predicates, String field)
OrmPredicates instance and returns the average
value of the matched data in a specified column.
If no data meets the conditions, this method will return 0. Conditions contained in the
OrmPredicates instance must be compliant with SQL statement logic.
predicates - Indicates the predicates that contain the match conditions.field - Indicates the column name.0 if no data
is matched.OrmPredicatesDouble sum(OrmPredicates predicates, String field)
OrmPredicates instance and returns the sum of
the matched data in a specified column.
If no data meets the conditions, this method will return 0. Conditions contained in the
OrmPredicates instance must be compliant with SQL statement logic.
predicates - Indicates the predicates that contain the {code sum} conditions.field - Indicates the column name.0 if no data is matched.OrmPredicatesboolean flush()
This method is generally used after OrmContext#insert, OrmContext#update, and
OrmContext#delete to write memory data into a database file.
true if the memory data is written into the database; returns false otherwise.void beginTransaction()
Here is the standard idiom for transactions: ormContext.beginTransaction(); try { ... ormContext.commit(); } catch (RdbException e) { ormContext.rollback(); }
void commit()
See beginTransaction() for notes about how to use this and when transactions are committed and
rolled back.
boolean isInTransaction()
void rollback()
See beginTransaction() for notes about how to use this and when transactions are committed and
rolled back.
String getAlias()
void changeEncryptKey(byte[] newEncryptKey)
This method applies only to an encrypted ORM database and cannot be used to change it to an unencrypted one.
Before calling this method, ensure that the database is opened and that no transaction is using the database
connection. For the maximum size of the encryption key, see OrmConfig#MAX_ENCRYPT_KEY_SIZE.
newEncryptKey - Indicates the new encryption key of the ORM database.IllegalArgumentException - Throws this exception if the new encryption key is null or an empty string.IllegalStateException - Throws this exception if the database is not opened or not encrypted, or a
transaction is using the database connection.RdbException - Throws this exception if changing the encryption key of the encrypted
ORM database fails.void close()
close 在接口中 AutoCloseableboolean backup(String destPath)
destPath - Indicates the path for backing up the database.true if the database backup is successful; returns false otherwise.boolean restore(String srcPath)
srcPath - Indicates the path where the database file is stored.true if the data is restored successfully; returns false otherwise.boolean backup(String destPath, byte[] destEncryptKey)
The generated backup is encrypted. If destEncryptKey is null, the generated backup is unencrypted.
In that case, this method is equivalent to the OrmContext#backup(String) method.
For the maximum size of the encryption key, see OrmConfig#MAX_ENCRYPT_KEY_SIZE.
destPath - Indicates the path that saves the database backup.
Ensure that your application has write permissions to this path.destEncryptKey - Indicates the encryption key of the database to back up.
It can be different from that of the current database.true if the database is backed up successfully; returns false otherwise.boolean restore(String srcPath, byte[] srcEncryptKey, byte[] destEncryptKey)
When being called, this method deletes the original data and generates an encrypted or unencrypted database,
as defined by your application. If both srcEncryptKey and destEncryptKey are null,
this method is equivalent to the OrmContext#restore(String) method.
For the maximum size of the encryption key, see OrmConfig#MAX_ENCRYPT_KEY_SIZE.
srcPath - Indicates the path that stores the source database file.
Ensure that your application has write permissions to this path.srcEncryptKey - Indicates the encryption key of the source database.
If the database is unencrypted, set the value of this parameter to null.destEncryptKey - Indicates the encryption key of the restored database.If you want
the database to be unencrypted, set the value of this parameter to null.true if the database is restored successfully; returns false otherwise.void registerStoreObserver(String alias, OrmObjectObserver observer)
OrmObjectObserver object to listen for changes in the storage specified by its alias.alias - Indicates the alias of the storage to be listened.observer - Indicates the storage observer.void registerContextObserver(OrmContext watchedContext, OrmObjectObserver observer)
OrmObjectObserver object to listen for changes of a specified OrmContext object.watchedContext - Indicates the OrmContext object to be listened.observer - Indicates the observer of the OrmContext object.void registerEntityObserver(String entityName, OrmObjectObserver observer)
OrmObjectObserver object to listen for changes of a specified Entity object.entityName - Indicates the name of the database entity to be listened.observer - Indicates the observer of the Entity object.void registerObjectObserver(OrmObject ormObject, OrmObjectObserver observer)
OrmObjectObserver object to listen for changes of a specified OrmObject instance.ormObject - Indicates the OrmObject instance to be listened.observer - Indicates the observer of the OrmObject instance.void unregisterStoreObserver(String alias, OrmObjectObserver observer)
OrmObjectObserver object used to listen for changes in the storage specified by its
alias.alias - Indicates the alias of the storage being listened.observer - Indicates the OrmObjectObserver object to unregister.void unregisterContextObserver(OrmContext watchedContext, OrmObjectObserver observer)
OrmObjectObserver object used to listen for changes of a specified OrmContext
object.watchedContext - Indicates the OrmContext object being listened.observer - Indicates the OrmObjectObserver object to unregister.void unregisterEntityObserver(String entityName, OrmObjectObserver observer)
OrmObjectObserver object used to listen for changes of a specified Entity object.entityName - Indicates the name of the database entity being listened.observer - Indicates the OrmObjectObserver object to unregister.void unregisterObjectObserver(OrmObject ormObject, OrmObjectObserver observer)
OrmObjectObserver object used to listen for changes of a specified OrmObject
instance.ormObject - Indicates the OrmObject instance that has been listened.observer - Indicates the OrmObjectObserver object to unregister.