Short answers to common tasks. The examples use a TCustomer class with an Orders association and a TOrder
class with a Customer end; replace them with the classes of your own model. BoldSystem is the TBoldSystem of
your application, for example BoldSystemHandle1.System.
// With the generated class Customer := TCustomer.Create(BoldSystem); // By the name of the model class, when the class is only known at run time Customer := BoldSystem.CreateNewObjectByExpressionName('Customer') as TCustomer;
BoldSystem.UpdateDatabase; // or BoldSystemHandle1.UpdateDatabaseUpdateDatabase writes every new, changed and deleted object in one database transaction: either all of it is
saved or, on an error, none of it.
Customer.Delete; // deletes it in the object space BoldSystem.UpdateDatabase; // removes it from the database
if BoldSystem.BoldDirty then ShowMessage(Format('%d unsaved objects', [BoldSystem.DirtyObjects.Count]));
BoldSystem.Discard;
EvaluateExpressionAsNewElement returns a new element that the caller owns and must free. The other
EvaluateExpressionAs... functions return a plain value. See OCL for the language.
// The class extent: a list owned by the system, do not free it Customers := BoldSystem.ClassByExpressionName['Customer']; // The same through OCL: a new list, free it when done Customers := BoldSystem.EvaluateExpressionAsNewElement('Customer.allInstances') as TBoldObjectList;
Active := BoldSystem.EvaluateExpressionAsNewElement( 'Customer.allInstances->select(active)') as TBoldObjectList; WithOrders := BoldSystem.EvaluateExpressionAsNewElement( 'Customer.allInstances->select(orders->notEmpty)') as TBoldObjectList;
ByName := BoldSystem.EvaluateExpressionAsNewElement( 'Customer.allInstances->orderBy(name)') as TBoldObjectList; NewestFirst := BoldSystem.EvaluateExpressionAsNewElement( 'Customer.allInstances->orderDescending(createdDate)') as TBoldObjectList;
Total := BoldSystem.EvaluateExpressionAsFloat('Order.allInstances.total->sum'); Count := Customer.EvaluateExpressionAsInteger('orders->size');
// One-to-many: either end Customer.Orders.Add(Order); Order.Customer := Customer; // the same link // Many-to-many: either end Student.Courses.Add(Course); Course.Students.Add(Student); // the same link
Bold keeps both ends consistent: after either line of a pair, both ends show the link.
Customer.Orders.Remove(Order); Order.Customer := nil; // the same
var Order: TOrder; begin for Order in Customer.Orders do // Delphi: generated lists have typed enumerators ProcessOrder(Order); Customer := Order.Customer; // the single end end;
Under Free Pascal the generated lists have no typed enumerator; use an index loop over Customer.Orders[i].
Each attribute has an M_ property that gives the attribute object itself:
if Customer.M_Email.IsNull then ShowMessage('No email address');
Customer.M_Email.SetToNull;
StartTransaction, CommitTransaction and RollbackTransaction work on the object space, not on the database. A
rollback restores every object to the values it had when the transaction started, without a database round trip, so
a group of in-memory changes becomes all-or-nothing. Saving does not need a Bold transaction: UpdateDatabase
always writes in one database transaction.
Object Space: Transactions has the code pattern and how nested transactions behave, and the Transaction example shows a batch that is committed or rolled back as a whole.
BoldListHandle1.RootHandle := BoldSystemHandle1;
BoldListHandle1.Expression := 'Customer.allInstances';
BoldGrid1.BoldHandle := BoldListHandle1;BoldEdit1.BoldHandle := CustomerHandle;
BoldEdit1.BoldProperties.Expression := 'name';The Bold-aware controls are VCL controls. In FireMonkey and in the LCL, bind a TBoldDataSet to the handle and use
LiveBindings or the LCL's data-aware controls.
- Check that each handle's
RootHandlechain ends at the system handle, and that the system is active. - Check the expression: an OCL error leaves the handle empty.
- Check that the object is in the same object space as the handle.
// Fetch the orders of all customers in the list at once BoldSystem.FetchLinksWithObjects(CustomerList, 'orders'); // Fetch all objects of a list that are not loaded yet CustomerList.EnsureObjects;
- OCL is evaluated in memory by default, so
Customer.allInstances->select(...)loads every customer first. PassEvaluateInPS = TruetoEvaluateExpressionAsNewElementto translate the expression to SQL instead, or useTBoldSQLHandlefor a hand-writtenWHEREclause. - Fetch links in batches, as above, instead of one object at a time.
- Add database indexes on the columns you filter on.
TBoldUnloaderHandle (palette page Bold Handles) unloads objects that have not been used for a while. Unloaded
objects are fetched again when they are next needed.
The code assigns a derived attribute. A derived attribute is computed from its derivation, so it cannot be set, unless the model marks it reverse-derived and the class implements the reverse derivation (see the Reverse-Derived Attributes example).
The whole update is rolled back, and the objects stay dirty in the object space. Check the exception message for the SQL error, typically a constraint violation, a missing table or column (the schema is older than the model), or a lost connection.
- Check that the class and member names match the names in the model.
- Check the navigation path one step at a time, for example in the
OCL Workbench example
or with the OCL editor of a handle's
Expressionproperty.
In the IDE, File → Save and Generate All in the model editor offers to check the database for schema changes
and evolve it. In code, TBoldDataBaseEvolutor (unit BoldDbEvolutor) compares the model with the database while
the persistence handle is inactive:
Evolutor := TBoldDataBaseEvolutor.Create(BoldPersistenceHandleDB1); try Evolutor.CalculateScript; if Evolutor.HasSchemaChanges then Evolutor.ExecuteScript; // or GenerateScript(DbScript, MappingScript) to review the SQL first finally Evolutor.Free; end;
The Model Evolution example shows a model change that keeps the existing data.
Yes, in two ways:
- Let Bold own a new database and import the old data into it. The tagged values
TableNameandColumnNamelet the model choose the table and column names. - Keep the external database and map it by hand with external persistence, as the External Persistence example does.