You can use sqlpassthroughobjects in a derived association when you need an OCL expression to retrieve MDriven objects through an SQL query; this is for modelers who need the query to resolve to existing objects of a class.
Use sqlpassthroughobjects in a derived association
sqlpassthroughobjects is an OCL operator that you call on a class, not on an object. The SQL expression identifies the database rows that correspond to objects of that class.
A common use is a derived association whose expression returns one related object. Add ->first when the query is expected to identify a single object.
- In MDriven Designer, select the derived association.
- Enter an OCL expression that starts with the target class.
- Build the SQL
selectexpression from values available in the OCL context. - Use
->firstif the association is single-valued.
Examples
Retrieve an object when you already have its primary key
If self.FakturaReferensnummer contains the primary key of a Kundfaktura, select that value and take the first returned object:
Kundfaktura.sqlpassthroughobjects(
'select ' + self.FakturaReferensnummer.toString
)->firstThe expression starts with Kundfaktura, the class of the object to retrieve. self is the object on which the derived association is evaluated.
Retrieve an object by looking up a field
If you need to locate the invoice by its FAKTURA_ID field, select the primary-key field in the query:
Kundfaktura.sqlpassthroughobjects(
'select FAKTURA_ID from tbl_FAKTUROR where FAKTURA_ID = ' +
self.FakturaReferensnummer.toString
)->firstIn this example, the SQL query returns FAKTURA_ID for the row in tbl_FAKTUROR whose value matches self.FakturaReferensnummer.
Pass values to the SQL expression
Use an automatically available variable
You can use an @variable in the SQL text when that variable is available at the point where the function is called. For example, @aNyttLosenord is an automatically available variable in the sqlpassthrough stored-procedure example.
Quote literal string values
SQL string values must be enclosed in quotes. When you construct the SQL text in OCL, use \' for an escaped single quote in the OCL string.
For example, the following pattern surrounds the value of self.Anvandarnamn with SQL single quotes:
'\'' + self.Anvandarnamn + '\''Use .toString when concatenating a non-string value such as an identifier into the SQL text, as shown in the invoice examples.
sqlpassthroughobjects is for SQL that resolves to objects. Use sqlpassthrough when you need the SQL passthrough operator described for stored procedures or tuple results. The PSEval operator uses OCL instead of SQL.
