3.2.2. Creating a Gamma Class from a Database Table

[Note]

The tutorial code connects to an existing database table. If you would like the script create a table for you, please refer to Section 3.2.6, “Creating a Database Table” below.

ODBC tables are mapped as classes into the Gamma engine. This means that most interactions with the database will be through convenient method calls rather than SQL queries. The Gamma engine is able to automatically determine the structure of a table in your database and to create a class from it:

tableclass = .conn.ClassFromTable (#ClassName, nil, "table_name");

This statement will look up the table named table_name in your database, and create a class whose name is ClassName from it. It will also return the class definition into the variable tableclass.

The Gamma engine attempts to determine the primary key field from your table. Some databases, such as Microsoft Access, do not provide a facility to do this. In that case, you need to assign the primary key for the table:

.conn.SetPrimaryKey (tableclass, "id");

The resulting class will have a number of special data members, like __table, the name of the table, as well as a data member for each column of the database table.