|
Build Generalized Database Utilities |
Sheerpower gives developers full access to database table metadata. This makes it easy to write generalized utilities, table browsers, report generators, validation tools, and other programs that inspect tables dynamically at runtime.
The ASK TABLE statement is used to inspect a table.
The SET TABLE statement is used to change a table's
current state, identity, or position.
The ASK TABLE statement retrieves information about an open table. It can return table-level information, such as the number of fields, and field-level information, such as field names, data types, descriptions, headings, masks, and key status.
Table-level options ask about the table as a whole. Field-level options ask about a specific field in the table.
A field can be specified by field number, field name, or another valid field expression.
| Option | Returns | Description |
|---|---|---|
fields |
Numeric | Returns the number of fields in the table. |
id |
String |
Returns a table ID that can later be used with
SET TABLE table_name: ID str_expr.
|
| Option | Returns | Description |
|---|---|---|
name |
String | Returns the name of the specified field. If the field expression is invalid, an empty string is returned. |
number |
Numeric |
Returns the field number of the specified field. Fields are
numbered sequentially. If the field does not exist, Sheerpower
returns 0. This is a simple way to validate that a
field expression is valid.
|
datatype |
String |
Returns the field data type, such as CH for character
or IN for integer.
|
attributes |
String |
Returns the Sheerpower field semantics for the specified field,
such as NUM for number or UC for
upper-case.
|
length |
Numeric | Returns the length of the specified field. |
position |
Numeric | Returns the starting position of the specified field. |
description |
String | Returns the description for the specified field. |
prompt |
String | Returns the prompt for the specified field. |
heading |
String | Returns the report column heading for the specified field. |
printmask |
String | Returns the print mask for the specified field. |
screenmask |
String | Returns the screen mask for the specified field. |
help |
String | Returns the help text for the specified field. |
keyed |
Numeric Boolean |
Returns TRUE if the specified field is a key field.
Otherwise, returns FALSE.
|
changeable |
Numeric Boolean |
Returns TRUE if the specified field can be changed.
Otherwise, returns FALSE.
|
This example opens the sample client table, asks how many
fields it contains, and then loops through each field to display its
metadata.
The SET TABLE statement changes the current state, identity, or position of an open table. It can find a record from a partial key, attach a table variable to a stored table ID, move to a specific extracted record, or reset the extracted-record count.
| Option | Description |
|---|---|
field field_expr: partial key str_expr |
Retrieves the first record matching the partial key in
str_expr.
|
id str_expr |
Sets a table to a table ID that was previously stored in a string
variable using ASK TABLE: ID. This allows generalized
routines to work with a table whose identity is not known until
runtime.
|
pointer num_expr |
Sets the table to the nth record in the current extracted
collection. If the pointer is valid, _EXTRACTED is
set to 1. Otherwise, _EXTRACTED is set
to 0.
|
extracted 0 |
Resets the extracted-record count to zero so a new collection can
be started. This is used with
EXTRACT TABLE table_name: APPEND.
|
PARTIAL KEY retrieves the first record matching the
partial key in str_expr.
This is useful when the user enters only the beginning of a keyed value, such as the first few letters of a last name.
SET TABLE: ID attaches a table variable to a table ID
that was previously retrieved with ASK TABLE: ID.
This is useful for generalized routines because the routine can work with a table passed to it at runtime, instead of requiring the table name to be hard-coded.
SET TABLE: POINTER sets the table to the nth record in
the current extracted collection.
This is useful after an EXTRACT TABLE operation because
it provides random access to any record in the extracted set.
If the pointer value is valid, _EXTRACTED is set to
1. If there are no records extracted, or if the number is
out of range, no error message is generated and _EXTRACTED
is set to 0.
SET TABLE: EXTRACTED 0 sets the number of extracted
records to zero. This causes a new extracted collection to be started.
This statement is used with
EXTRACT TABLE table_name: APPEND.
Problem: Generalized table utilities often need to work with tables whose names, fields, keys, and current records are not known until runtime.
Solution: ASK TABLE lets a program inspect table
metadata, while SET TABLE lets the program reposition,
reassign, or reset table access at runtime.
Efficiency: One routine can work across many tables without hard-coding table layouts, duplicating table-access logic, or adding extra abstraction layers.
Design insight: In many environments, generalized routines require separate database catalog queries, object reflection, ORM metadata, validation rules, and user-interface metadata. Sheerpower brings much of that information directly into the table system itself.
Takeaway: ASK TABLE answers questions about the
table. SET TABLE controls how the program is attached
to, positioned within, or collecting records from the table. Together,
they make Sheerpower tables self-describing and dynamically
accessible.
|
Hide Description
|
|
|
Enter or modify the code below, and then click on RUN |
|
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |