Data Access Visual Composition Sample

This sample demonstrates how Data Access classes can be used as visual parts, and built into an application by using the Visual Builder component of C++.

Objective
The objective of this sample is to demonstrate how to:

The sample database you will use must be set up before you work with the sample. See Preparation for Samples for information.

Description
This sample does the following:

  1. Presents a graphical interface for viewing and editing department and employee records.
  2. Connects to the database.
  3. Lets you list departments and employees from the database.
  4. Lets you edit fields for a selected department or employee.
  5. Disconnects from the database.

Generated Data Access Classes Used by this Sample
This sample uses the following generated classes and methods, as defined in the Data Access mapping, minisamp.dax:

Some of the methods listed are inherited from the Data Access class library.

Running the Sample
The executable for the sample is called VisSamp. Since Data Access Builder generates code for three access methods (CLI, ESQL, and ODBC), a VisSamp executable is located in each of three corresponding directories: db2cli, db2esql, and odbc (all of these are subdirectories of samples\database\visual).

Before running the sample in the DB2esql directory, it must be bound to the database by running "rebind" from the directory. If the connect fails when running "rebind", then edit "rebind.cmd" and replace USERID and PASSWORD with a valid user ID and password, and run "rebind" again.

To run the sample, enter the following on the command line of the appropriate directory:

vissamp

When the program is running, enter a valid UserID and password into the panel's fields and click Connect. Once connected, click the Get All button under Departments to display all records from the Department database. Select one of the the departments and click Display Selected to bring up a panel that lets you update the record.

Click on the Get All button under Employees to display all records from the Employee database. Select one of the the employees and click Display Selected to bring up a panel that lets you update the record.

Click Get From Selected Dept to display all employees that belong to the selected department.

The source code for the VisSamp executables is located in the samples\database\visual\source directory. If you make changes to the sample's source code, you must rebuild it.

The Data Access Builder Mapping
The Data Access samples use a complex mapping, which is provided for you in a file called visual.dax. One visual.dax mapping file is provided in each of the three directories: db2cli, db2esql, and odbc. If you want to see the mapping or generate the classes for it, import the sample into the IDE; in the Data Access Launch view, select the part and click Edit Part.

Hints on How to Create a Mapping for This Sample
Rather than using the mapping provided with the sample (visual.dax), you can create your own mapping. To create a mapping for this sample, follow these steps:

  1. In the IDE, create a new project, an executable target, and a new Data Access part. When you choose to add a Data Access part, the Data Access SmartGuide will start up.
  2. To create the schema mapping in the SmartGuide pages, do the following steps:
    1. On the overview page of the SmartGuide, click Next>> to go to the next page.
    2. If you are using DB2, select DB2 Driver and select the database SAMPLE; if you are using dBase, select ODBC Driver and select the database CPPASAMP40.
    3. Select By selecting database tables or views for the method to create the schema mapping. Click Next>> to go to the next page.
    4. If you are using DB2, select the desired database access method (DB2 CLI or DB2 ESQL). Click Next>> to go to the next page. If you are using ODBC, you will not see this page.
    5. Click Get Tables with Filter and when the list of tables is displayed, select DEPARTMENT for DB2 or DEPT for dBase.
    6. Click Finish. You will return to the IDE.
  3. Edit the new Data Access part by clicking the Edit Part button. This will launch the Data Access Builder main window.
  4. Select Map Schema from the File menu. This will launch the Data Access SmartGuide. Repeat the instructions in item 2, above, except in step e, select EMPLOYEE for DB2 or EMPL for dBase. When you click Finish, you will return to the Data Access Builder main window.
  5. Select Properties from the Department mapping icon's popup menu. Ensure the Class Name field contains "Department" and the File Stem field contains "Dept". Ensure the Enable Visual Builder notification checkbox is enabled.
  6. Select Properties from the Employee mapping icon's popup menu. Ensure the Class Name field contains "Employee" and the File Stem field contains "Empl". Ensure the Enable Visual Builder notification checkbox is enabled.
  7. Change the deptno attribute (in the Department Mapping) to be a data ID by doing the following steps:
    1. Select the Department mapping icon. Select Attributes from its popup menu.
    2. From the list of attributes, select the deptno row.
    3. Enable the Data Identifier check box (near the bottom of the dialog).
    4. Click OK to close the notebook.
  8. Change the empno attribute (in the Employee Mapping) to be a data ID by doing the following steps:
    1. Select the Employee mapping icon. Select Attributes from its popup menu.
    2. From the list of attributes, select the empno row.
    3. Enable the Data Identifier check box (near the bottom of the dialog).
    4. Click OK to close the notebook.
  9. Define a method for returning the number of employees in a given department by doing the following steps:
    1. Select Methods from the Employee mapping icon's popup menu.
    2. Click Add under the top list box.
    3. Enter the method name numEmpInDept.
    4. Enter the following SQL statement:
      select count(empno) from employee where workdept=?

      Click Validate.

    5. In the Parameters list, select the first parameter. Under Mapped To, select Parameter. In the Name field to the right of the selection you just made, enter department. Ensure IString is selected in the Type drop-down list. Click Modify.
    6. In the Parameters list, select the second parameter. Under Mapped To, select Return Value. From the Type drop-down list to the right of the selection you just made, select long. Click Modify.
    7. Click OK. In the Methods window, click OK again.
  10. Define a method to return a collection of employees in a given department by doing the following steps:
    1. Select Manager Methods from the Employee mapping icon's popup menu.
    2. Click Add under the top list box.
    3. Enter the method Name empInDept
    4. Enter the following SQL predicate:
      where workdept=?

      Click Validate.

    5. In the Parameters list, select the first parameter. In the Name field, enter department. Select IString from the Type drop-down list. Click Modify.
    6. Click OK. In the Manager Methods window, click OK again.
  11. In the Data Access Builder window, select Save from the File menu to save the mapping.
  12. Select the mapping icon and select Generate from its popup menu to generate the C++ classes. Exit Data Access Builder.
  13. Return to the IDE and build the part.

How to Re-build the Visual Sample

  1. Start the IDE.
  2. Open one of the following projects, depending on the access method you want to use:
    x:\cppbeta\samples\database\visual\VisSamp.icc
    x:\cppbeta\samples\database\visual\VisSamp.icc
    x:\cppbeta\samples\database\visual\VisSamp.icc
  3. Click No when the IDE asks if the project shoulds be built.
  4. Select the Data Access Builder Sessions tab on the Project VisSamp page.  Click mouse button two on the Data Access Builder Sessions pane and select Refresh View from the popup menu. Select the VISUAL.DAX file and click the Edit Session button. The Data Access Builder is then launched.
  5. Once Data Access Builder is launched, choose Save from the File menu, and then exit Data Access Builder.
  6. In the IDE, from the Project Workbook menu, select Build.

The generated program, VisSamp, can then be run either from the IDE or from the directory where it was built.



Data Access Builder
Data Access Builder Workflow


Data Access Class Library and Generated Class Reference
Data Access Builder Samples