Using Database Object Structures to Access Direct Variables
When using direct variables, you need to declare the variables that are used by the Logic program. The variables have to be declared in VAR blocks. This can be time-consuming if the Logic program needs to reference the same set of properties for many different items of the same type.
ClearSCADA provides a structure for referencing multiple fields of a single database item. The structure is called a database object structure, and it allows you to group a set of fields for a type of item. The structure is included at the top of the Logic program and can be referenced within the program. This means that you can reference the name of the structure instead of referencing multiple fields several times in the same program.
For example, if your ST program references the CurrentValueFormatted, CurrentState, and CurrentStateDesc of 30 points, you would have to enter the variables for each separate point. This means you would have to define the CurrentValueFormatted, CurrentState, and CurrentStateDesc variables 30 times (each field for each point).
By using a database object structure, you can create a single reference to the CurrentValueFormatted, CurrentState, and CurrentStateDesc fields. The database object structure includes the specified fields in the relevant class (in this case, the CDBPoint class). This means that the same database object structure can be used to reference the specified fields for any item in the class—it can be used for any point.
Example:
A Logic program needs to reference the CurrentValueFormatted, HighLimit, CurrentState and CurrentStateDesc fields of 20 different points. If the database object structure is not used, you will have to add these fields to the program (for each of the 20 points).
The following database object structure is used:
- TYPE
- PointValues : DATABASE_OBJECT(CDBPOINT)
- CurrentValueFormatted: STRING;
- CurrentState: BYTE;
- CurrentStateDesc: STRING;
- END_DATABASE_OBJECT;
- END_TYPE
By using the database object structure, the fields are grouped at the top of the program. The database object structure is allocated a name (PointValues).
Where the program needs to refer to the fields, the name of the database object structure and the specific point is used. This means that the database object structure can be used to refer to the same specified fields for multiple points.
In the program, references to the point values use this format:
- <name of variable> AT %O(<path of item>) : <name of DATABASE_OBJECT structure>;
For example:
- VAR
- InputVal AT %O(Group 1.LimitPoint) : PointValues;
- END_VAR
- VAR
- PointFields :STRING;
- END_VAR
- PointFields:=InputVal.CurrentValueFormatted;
Where InputVal
is the name of the variable, and AT %O
indicates that the value is a write-only value. (Group 1.LimitPoint)
specifies the location of the point that is being referenced (the 'LimitPoint' that is stored in the 'Group 1' Group). PointValues
is the name of the database object structure, and .CurrentValueFormatted
is the specific field that needs to be referenced.
This means that each point value can be referenced by a single line. The VAR list only needs to contain the database object structure and one set of the fields that are to be included—there is no need to reference each field for each item.
When you create a database object structure, you need to adhere to these rules:
- The database object structure has to be specified within a TYPE definition.
- The TYPE definition has to be positioned at the top of the ST program, above the PROGRAM information.
- The name of the database object structure is the first part of the definition. It is followed by DATABASE_OBJECT (<class name>) where <class name> is the name of the class of database items.
You can view the classes of database items by using the ClearSCADA database schema (see Working with the Database Schema in the ClearSCADA Guide to the Database).
- The list of referenced fields has to include field names that are identical to the field names in the database. Each field name has to be followed by its data type.
NOTE: When using an SQL Query with a DATABASE_OBJECT structure, you have to include the Id column as the first column in the SQL Query. This is because DATABASE_OBJECT structures have an Id field, although this field is not referenced in the DATABASE_OBJECT definition.
- To end the list of fields, you have to enter END_DATABASE_OBJECT; To end the TYPE definition, you need to enter END_TYPE.
- The Logic program has to be entered after END_TYPE .
NOTE: You cannot access database aggregates via DATABASE_OBJECT structures. You need to use standard read/write declarations to access database aggregates.
Further Information