Working with databases
Lesson 6: Access data using local buffer
Following is a demonstration of using a local file buffer.
- Create a basic Windows project (a project without a user interface)…
- From within the DataFlex Studio select the ‘Create New’ icon from the top toolbar.
- Select ‘Basic Project.’ Select OK.
- Enter “LocalBuffer” as the ‘File Name.’ Select OK.
- Create a business process…
- A business process is an object in a Windows application that has a Data Dictionary structure to facilitate certain things and to show a status dialogue.
- Again, select the ‘Create New’ icon from the top toolbar.
- Select the ‘Other’ tab.
- Select ‘Business Process.’ Select OK.
- Enter “LocalBuffer” as the ‘Object Name.’ Select OK.
- The business process contains an object of the class “BusinessProcess,” and an “OnProcess” event that will contain the logic of what needs to be done. A business process always does something.
- Add a line of code by selecting the object, ‘oLocalBuffer,’ from the ‘Code Explorer’ on the left.
- From the ‘Properties’ panel on the right change ‘Display_Error_State’ to “True,” so that any errors along the way are shown.
- Select VIEW > DDO Explorer from the top menu to open the DDO Explorer panel in the Studio. It will appear on the right side of the Studio.
- Note: the DDO Explorer shows the DDO structure. For each table, a Data Dictionary (DD) class is automatically created. The DDO structure is a tree of objects that are created for each table to be used in the application.
- Select the ‘Add DDO’ icon from the top of the panel.
- Select ‘cRecipeDataDictionary’ form the list. Select ‘Select.’
- That DD is added to the DDO Explorer as is the ‘cKitchenDataDictionary,’ which is a parent table.
- Expanding ‘cRecipeDataDictionary’ shows that the parent is a server of the Recipe table.
- The source now includes both DDs, and that the two objects have been created, ‘oKitchen_DD’ and ‘oRecipe_DD.’
- This is a relational model in an object structure.
- The main DD of the business process is ‘oRecipe_DD.’ This is typically where most finds are performed if there are several DDs.
- Build a find loop using the DDS
Procedure OnProcess
Send Clear of oRecipe DD
Send Find of oRecipe_DD FIRST_RECORD 2
While (Found)
Showln (SFormat(“Recipe %1 (Kitchen: %2)”, Field_Current_Value(oRecipe_DD, RefTable(Recipe.Name)), Field_Current_Value(oKitchen_DD, RefTable(Kitchen.Name))))
Send Find of oRecipe_DD NEXT_RECORD 2
Loop
End_Procedure
- Before running the sample, the business process needs to be added to the application.
- Click over to ‘LocalBuffer.src.
- Add a use statement:
Use LocalBuffer.bp
Send DoProcess of oLocalBuffer
- Select the ‘Run’ icon from the top toolbar. An Output Window appears that now shows the list of recipes.
- To add a DD for the ingredients, close the output window, click over to ‘LocalBuffer.bp’ and select the ‘Add DDO’ icon from the top of the DDO Explorer.
- Select ‘cIngredientsDataDictionary’ from the list. Select ‘Select.’
- That DD is added to the DDO Explorer. Expanding it shows the relation to the server, ‘Recipe.’
- Constrains are also added to ‘Recipe’ because this is the main table in the DDO structure, and it is assumed that if child tables are added that they need to be restrained as is the ‘cKitchenDataDictionary,’ which is a parent table.
- Expanding ‘cRecipeDataDictionary’ shows that the parent is a server of the Recipe table.
- Build a nested find loop to find all the ingredients in the recipe…
Send Clear of oRecipe_DD
Send Find of oIngredients_DD FIRST RECORD 2
While (Found)
Showln (SFormat(“ - %1 (Kitchen: %2)”, Field_Current_Value(oRecipe_DD, RefTable(Recipe.Name)), Field_Current_Value(oKitchen_DD, RefTable(Kitchen.Name))))
Send Find of oIngredients_DD FIRST_RECORD 3
While (Found)
Showln (SFormat(“ - %1 (Amount: %2)”, Field_Current_Value(oIngredients_DD, RefTalbe(Ingredients.Name)), Field_Current_Value(oIngredients_DD, RefTable(Ingredients.Amount))))
Send Find of oIngredients_DD NEXT_RECORD 3
Loop
Send Find of oRecipe_DD NEXT RECORD 2
Loop
- Select the ‘Run’ icon from the top toolbar. An Output Window appears that now shows the list of recipes and all the ingredients for each recipe that is sorted alphabetically.
- To conclude: The difference between using the local and global file buffers is the message interface. Another advantage is that in a Windows application with multiple views the local file buffers for the views will always maintain their states. If the views are switched there is no interference between the views.