APPLY operator
The APPLY operator creates an action that applies changes to the database.
Syntax
APPLY [NESTED [nestedPropertySelector] [CLASSES]] [SINGLE] [SERIALIZABLE] [action]
where nestedPropertySelector has one of the following forms:
LOCAL
(propertyId1, ..., propertyIdN)
Description
The APPLY operator creates an action that applies changes to the database. By specifying the keyword NESTED you can specify local properties whose changes are not dropped when applying the changes. This operator also includes an action to be executed before applying the changes to the database.
Parameters
-
NESTEDOptional keyword after which you can specify which local properties preserve their changes after the
APPLYoperator is executed. By itself, with neitherLOCALnor a property list, it has no effect on the operator. -
LOCALKeyword. If specified after
NESTED, all local properties preserve their changes after theAPPLYoperator is executed. -
propertyId1, ..., propertyIdNNon-empty list of local properties, specified after
NESTEDin parentheses. Each list element is a property ID. The local properties specified in the list will preserve their changes after the operator is executed. -
CLASSESKeyword. Can be written, but has no effect on the
APPLYoperator. -
SINGLEOptional keyword. Enables an apply-transaction optimization for cases where the applied action reads stored properties it also modifies: changes to such stored properties are flushed incrementally during the transaction instead of being batched at the end of the apply.
-
SERIALIZABLEA keyword that sets the transaction isolation level to "Serializable".
-
actionA context-dependent operator that describes an action to be executed before applying changes. It is executed in the same transaction as the application of changes, so the platform may run it more than once when retrying the transaction after an update conflict, deadlock, or timeout — see
APPLYparadigm for the implications.
Examples
CLASS Sku;
id = DATA INTEGER (Sku);
in = DATA LOCAL BOOLEAN (Sku);
locked = DATA BOOLEAN (Sku);
applyIn() {
in(Sku s) <- TRUE WHERE id(s) == 123;
APPLY NESTED (in[Sku]) {};
IF canceled() THEN
MESSAGE applyMessage();
FOR in(Sku s) DO
MESSAGE id(s); // shows '123'
}
calculateInTransaction() {
APPLY {
id(Sku s) <- (GROUP MAX id(Sku ss)) (+) 1; // putting down a new code inside the transaction
}
}
// bare apply — saves accumulated session changes
saveChanges() { APPLY; }
// SINGLE optimization combined with an applied action
recalcCost() {
APPLY SINGLE {
id(Sku s) <- (GROUP MAX id(Sku ss)) (+) 1;
}
}
// keep all local properties on apply
sendBatch() {
in(Sku s) <- TRUE;
APPLY NESTED LOCAL;
}
// SERIALIZABLE on a write to a stored property; applied action is a single context-dependent operator (no braces)
unlock (Sku s) {
APPLY SERIALIZABLE locked(s) <- NULL;
}