Chapter 3. An Explanation of the Tutorial Code

Table of Contents

3.1. Define the Application Object
3.2. Interactions with the Database
3.2.1. Connecting to the Database
3.2.2. Creating a Gamma Class from a Database Table
3.2.3. Querying Rows from the Database
3.2.4. Inserting Rows into a Database
3.2.5. Updating Existing Rows in a Database
3.2.6. Creating a Database Table
3.3. Set up Event Handlers
3.4. Shut Down

The DataHub interacts with relational databases through ODBC (Open Data Base Connectivity). Every database acts a little differently, and each person's requirements are specific to the their own application. Consequently, the DataHub offers its ODBC support through scripts, written in the Gamma language.

A DataHub script consists of four parts:

3.1. Define the Application Object

A DataHub script defines an object called an Application object. This object is a class derived from the class Application. All of the functions that you define should be methods of this class. Variables should be members of this class wherever possible.

class MyODBCScript Application
{
  env;    // store the ODBC environment here
  conn;   // store the ODBC connection here
}

This defines the application object.

The application object requires a constructor. The constructor acts as the main line of your program. To start your program, you just create an instance of the application class, causing the constructor to run.

method MyODBCScript.constructor ()
{
  // This is the program main line
}

The job of the constructor is to define event handlers. An event handler is program code attached to a particular event. An event can be one of:

    A change in an DataHub point value

    A timer

    A Microsoft Windows event

The constructor runs to completion. Once the constructor completes, the script engine begins processing events, and executes the program code attached to those events.

The script engine will continue to process events until the application object is destroyed. You may optionally provide a destructor for the object to clean up any specific data or open any files or timers that your application has created.

method MyODBCScript.destructor ()
{
  // This is where you clean up open files and connections
}

You can add other methods to the application object as you need them. Only the constructor is necessary.