5. Working with a Database : How You Can Access a Database with DataStream Objects : How You Can Use Query Objects : Required Operations
 
Share this page                  
Required Operations
The following operations are required to create a query object:
1. Provide the parameters for building the select clause:
a. Load the name of each database column into the ColumnName attribute of DataStream's Columns array attribute.
b. Supply the name of the target field or variable to the Expression attribute of the Targets attribute of the Columns array attribute (for every column selected from a database).
c. Set the value of IsSelectTarget to TRUE for every column selected from the database.
d. Set the value of QueryParm's IsUpdateTarget to TRUE for every database column that allows updates.
e. Set the value of QueryParm's IsInsertTarget to TRUE for every database column that allows insertions.
2. Provide the parameters for building the from clause:
a. Specify the name of the table or tables being accessed in the QueryTable's TableName attribute.
b. Supply tables to the FromTable array attribute of the Columns array attribute (required only if a column appears in more than one table).
3. Provide the parameters for building the where clause (if desired):
a. For update operations, set the value of QueryParm's IsUpdateWhere to TRUE for every column that is to be part of the where clause.
b. For delete operations, set the value of QueryParm's IsDeleteWhere to TRUE for every column (usually PrimaryKey columns) that is to be part of the where clause.
c. For select operations, provide values for QueryObject's RunTimeWhere attribute (if desired).
4. Provide the parameters for building the order by clause (if desired):
a. Set the value of the Columns array attribute's OrderBy attribute to an appropriate sequence number; that is, set the primary column to 1 and the secondary column to 2, and so on. If the sort order for a particular column should be descending, then the sequence number's sign should be changed to negative (for example, -1 or -2).
b. Supply the name or names of database columns to the AsName attribute of the Columns array attribute for all columns to be used in the order by clause.
5. Provide the parameters for building the group by clause (if desired):
a. Set the value of the Columns array attribute's GroupBy attribute to an appropriate sequence number, such as 1 for the primary column and 2 for the secondary column.
b. Supply the name or names of database columns to the AsName attribute of the Columns array attribute for all columns to be used in the group by clause.
6. Provide the parameters for building the having clause by entering an appropriate string into QueryObject's HavingClause attribute (if desired).
To execute the query and manipulate the data, you use most of the same DataStream methods used in running SQLSelect objects. However, because you do not specify the select query, you cannot use the SetCols method in building a query object.
The relevant DataStream methods are:
Open
Opens the query in the specified mode
FetchRow
(Valid only for cache mode.) Moves to a specified row in the cache and puts its data into internal buffers
NextRow
Retrieves the next row of data from the cache into internal buffers
PrevRow
(Valid only for cache mode.) Backs up to the previous row in the cached sequence, positioning that row to be loaded into fields or variables
Load
Moves one row of data from internal buffers into target variables, objects, or fields on the form
Close
Closes the query
In addition to using methods defined for the DataStream system class, using a query object for database inserts, updates, and deletions requires methods defined for the QueryObject system class. These methods are:
CommitToCache
Keeps data in the cache consistent with data in the database when insert, update, or delete operations are performed on cached data
DBDelete
Performs deletions on database tables with columns mapped to variables or fields specified for deletion
DBInsert
Performs insertions on database tables with columns mapped to variables or fields specified for insertion
DBUpdate
Performs updates on database tables with columns mapped to variables or fields specified for update
When running in cache mode, the cached data set is frozen to the state of the data at the time the query was executed. If you make changes to the data in the database tables, you must also update the cache to keep data synchronized. To update the cache after changing data in the database, use the CommitToCache method.