version 1.5
You can create a context either by using a context definition dialog box or by executing a set of procedural statements. If you use the dialog box, 4D ODBC generates the context definition statements for you.
Creating a Context Using a Dialog Box
You can use the Edit a Context dialog box to define a context that selects data on the data source server and relates it to data elements in 4th Dimension.
To open the Edit a Context dialog box:
1. Execute the OC Create context dialog function.
The Edit a Context dialog box appears:
Using the dialog box, you can do the following:
Define binds for the context,
Add clauses and options to the context,
Copy the code for the context definition to the Clipboard,
Save the context definition to a file,
Load the context definition from a file.
Defining a Bind
The Edit a Context dialog box allows you to select a data source column or expression and a 4th Dimension field, variable, or array, and bind the two together.
You can create multiple binds to associate multiple data source objects with multiple 4th Dimension objects. Each bind you create is listed in the Current Binds area.
To define a bind between a data source object and a 4th Dimension object using the Edit a Context dialog box, follow these steps:
1. Select a column or expression from the list below the ODBC Table pop-up menu.
To display columns from a different table, choose the name of the table from the ODBC Table pop-up menu.
2. Select a 4th Dimension field from the list below the 4th Dimension Files pop-up menu.
To display fields from a different file, choose the name of the file from the 4th Dimension Files pop-up menu.
You have now selected the ODBC object and the 4th Dimension object that you want to bind together. Before adding the bind to the context, you can select from among three options for the bind.
3. Click the Bind button.
You have now defined a bind in the context. The bind is not activated until you activate the context.
4. Repeat steps 13 to define additional binds for the context.
All binds are listed in the Current Binds area with a reminder of the options you set for the bind (Updatable, Sorted, or Primary Key). In the following figure, the CLONE.fAlpha column is linked to the [CLONE]fAlpha field.
At any time, you can remove a bind by selecting it in the Current Binds area and clicking the Delete button.
Selecting Options for the Context
Before activating the context, you may want to specify options or clauses that affect the behavior of the context. To select options for the context:
1. Choose the option(s) for the context from the Select Options area.
Distinct option: This option specifies that only distinct values should be returned. This prevents duplicate values from being returned.
For Update option: This option prepares the selected rows to be updated by locking them to all other users.
No Wait option: This option specifies that 4D ODBC should not wait for the rows selected by the SELECT statement to become unlocked. If the rows are locked, the query is cancelled. You must select the For Update option before you can select the No Wait option.
Adding Clauses to the Context
To add a clause to the context:
1. Click the Select Clauses button.
The Edit an SQL Clause dialog box is displayed, allowing you to create clauses such as WHERE, GROUP BY, or CONNECT BY.
You can select the type of clause to create from the Clause pop-up menu.
To construct the clause, type it into the text area. To simplify the creation of a clause, select keywords, column names, and functions from the lists at the bottom of the dialog box.
To display columns from a different table in the center list, use the pop-up list and choose a table name from the pop-up menu that appears, or, scroll through the list of tables using the arrows on either side of the title bar.
Copying the Context Creation Statements
Typically, contexts are defined graphically as a developer tool. To facilitate rapid application development, you can request that 4D ODBC generate the 4th Dimension statements equivalent to the context definition. Instead of redefining the context each time you need to use it, you can place the 4D ODBC statements in a 4th Dimension method.
To copy these statements to the Clipboard before activating the context:
1. Click the Copy Code button.
When you click the button, 4D ODBC asks you to name a 4th Dimension variable that it will use for storing the context ID.
2. Enter a variable name and click the OK button.
The statements are copied to the Clipboard. These statements can then be pasted into a 4th Dimension method.
Saving the Context Definition to a File
4D ODBC allows you to save contexts to files. A context saved to a file can be loaded into the Edit a Context dialog box at a later time. Also, a context saved to a file can be loaded into memory using the OC Open context file function.
To save the context you have created to a file:
1. Click the Save As button.
A Save File dialog box appears, allowing you to specify a name and location for the file.
2. Click the Save button.
The file is saved.
Loading a Context from a File
To load a context previously saved to a file:
1. Click the Open button in the Edit a Context dialog box.
An Open File dialog box appears.
2. Select the file and then click the Open button.
The context is loaded and displayed in the Edit a Context dialog box.
You can also load a context procedurally using the OC Open context file function.
Creating a Context Using Procedural Statements
If you do not want to use the Edit a Context dialog box, you can define a context procedurally using 4D ODBC commands and functions.
The basic steps for creating a context procedurally are the following:
1. Initialize the context and retrieve the context identifier by executing the OC Create context function.
The context identifier is used to identify the context in all other context commands.
2. Define the context by executing a series of OC ADD TO CONTEXT statements.
The OC ADD TO CONTEXT command allows you to bind data source objects with 4th Dimension objects and to add the equivalents of the UPDATABLE, SORT, and PRIMARY KEY options.
At this point, the SELECT statement being built by 4D ODBC might be the following:
SELECT empno, ename, FROM emp
Add clauses to the context definition by executing a series of OC SET CLAUSE IN CONTEXT statements.
If you wish to see only employees from department 20, you could use the following statement:
OC SET CLAUSE IN CONTEXT(Context_ID;2;"DEPTNO=20")
Adding this line modifies the query as follows:
SELECT empno, ename, FROM emp WHERE DEPTNO=20
The "WHERE" keyword has been automatically added by 4D ODBC.
You can later retrieve the clauses set for a context by executing a series of OC Get clause in context statements.