ExecuteQueryPlan lets a ViewModel evaluate OCL that reaches derived attributes and associations while loading missing data in bulk, for developers building complex ViewModels.
Purpose
Traditional SQL query planners use static statistics to choose a database access path before the query runs. MDriven ViewModels can use code injectionâincluding derived attributes and associationsâso the full set of data needed may not be known from the initial OCL expression alone.
ExecuteQueryPlan is the execution harness for this situation. It starts evaluating the OCL logic and observes the data the logic actually reaches. When evaluation needs data that is not loaded, execution pauses at that data fault, fetches the required data in bulk from the database, and then continues.
This approach is the core strategy that prevents complex ViewModels from turning data access into many small individual queries, commonly called the N+1 problem.
Syntax
selfVM.ExecuteQueryPlanThe expression is invoked on selfVM, the ViewModel execution context available to the OCL expression.
How execution proceeds
When you use selfVM.ExecuteQueryPlan, the execution flow is:
- Start evaluating the ViewModel's OCL logic.
- Reach an attribute, association, or derived value that requires data which is not currently available.
- Pause at that fault rather than continuing with an individual database request for that one access.
- Fetch the needed data in bulk.
- Resume the same OCL evaluation.
- Repeat as needed until the expression can complete.
The important distinction is that the data requirement is discovered while the logic runs. This is necessary when derived logic determines which associations or attributes are reached.
Example: avoiding repeated association loads
Consider a ViewModel that evaluates a collection of objects and, for each object, reaches an association whose data has not yet been loaded. Without a coordinated execution strategy, evaluating each item can cause a separate database request. With selfVM.ExecuteQueryPlan, evaluation identifies the missing association data as it is reached, requests the required data in bulk, and then continues evaluating the collection.
The OCL entry point is:
selfVM.ExecuteQueryPlanThe result is that ViewModel logic can follow derived attributes and associations without requiring the initial query to predict every later data need.
When to use it
Use ExecuteQueryPlan when your ViewModel's OCL can reach data indirectly, especially through:
- Derived attributes whose calculation reaches additional model data.
- Associations reached while evaluating a collection.
- Code-injected logic where the required data cannot be fully determined before execution begins.
It is intended for executing ViewModel OCL with coordinated data loading. It is not a SQL-expression mechanism. If you need to invoke SQL directly, see sqlpassthrough.
Terms
| Term | Meaning in this context |
|---|---|
| Code injection | ViewModel behavior supplied by model logic, including derived attributes and associations, which can make later data requirements depend on runtime evaluation. |
| Data fault | The point at which OCL evaluation reaches data that has not been loaded and must be fetched before evaluation can continue. |
| N+1 problem | A pattern where an initial data request is followed by many additional individual requests, often one per object while processing a collection. |
| Bulk fetch | Fetching the data required by the current execution step together instead of making a separate small request for each reached object. |
