| Previous | Contents | Index |
A string expression for the structure name can be used in an ASK STRUCTURE #string_expr or SET STRUCTURE #string_expr statement. This allows generalized code to be written to work for several structures.
ASK STRUCTURE struc_name : ENGINE str_var
|
| Example 15-41 ASK STRUCTURE: ENGINE |
|---|
open structure cl : name 'sheerpower:samples\vendor' ask structure cl : engine ename$ print 'DATABASE ENGINE is: '; ename$ end DATABASE ENGINE is: ARS |
The ASK STRUCTURE: ENGINE statement returns the name of the database engine (record management system) being used for the specified structure in str_var.
The SET STRUCTURE statement is used to change various device and structure characteristics from within your programs.
SET STRUCTURE struc_name: struc_option [num_var | str_var]
|
SET STRUCTURE struc_name: CURRENT str_expr
|
| Example 15-42 SET STRUCTURE: CURRENT |
|---|
dim a$(100)
let i = 0
open structure cl: name 'sheerpower:samples\client'
extract structure cl
end extract
for each cl
print cl(last); ', '; cl(first)
input 'Would you like to see this record (Y/N)': yn$
if yn$ = 'Y' then
let i = i + 1
ask structure cl: current a$(i)
end if
next cl
print
for j = 1 to i
set structure cl: current a$(j)
print cl(last); ','; cl(first), cl(state), cl(phone)
next j
end
Errant, Earl Would you like to see this record (Y/N)? Y
Abott, Al Would you like to see this record (Y/N)? Y
Brock, Bud Would you like to see this record (Y/N)? N
Cass, Cathy Would you like to see this record (Y/N)? N
Derringer, Dale Would you like to see this record (Y/N)? Y
Farmer, Fred Would you like to see this record (Y/N)? Y
Errant, Earl CA (408) 844-7676
Abott, Al NY (202) 566-9892
Derringer, Dale CA (818) 223-9014
Farmer, Fred FL (305) 552-7872
|
The CURRENT option sets the current record to that specified by the str_expr. The str_expr contains the information for the current record for the record management system being used. ASK STRUCTURE: CURRENT is used to store a current record value into a string variable.
When SheerPower executes a SET STRUCTURE: CURRENT statement, it uses the structure name and sets the current record to the value specified by the string variable. The structure must be open and str_expr must contain a value stored with the ASK STRUCTURE: CURRENT statement.
If a null string is used for the value, SheerPower sets the structure to no current record and sets _EXTRACTED to zero.
SET STRUCTURE struc_name: CURRENT ''
|
15.9.2 SET STRUCTURE, FIELD: KEY
SET STRUCTURE struc_name, FIELD field_expr: KEY str_expr
|
| Example 15-43 SET STRUCTURE, FIELD: KEY |
|---|
open structure cl: name 'sheerpower:samples\client'
line input 'Enter an ID': id$
set structure cl, field id: key id$
if _extracted = 0 then
message error: 'Not found'
else
print cl(id), cl(last)
end if
end
Enter an ID? 80561
80561 Derringer
|
The FIELD option allows for record retrieval using a key field in a structure. SETUP's "SHOW FIELDS" menu option (see Chapter 16, Database Setup) displays the field names. The FIELD option is currently used only with the KEY or PARTIAL KEY option. The KEY option specifies the key to look for. The key is contained in str_expr.
The above example shows how to look in the CLIENT structure for an ID.
_EXTRACTED contains the number of records extracted. If the operation fails, _EXTRACTED will be 0 and an error message will be displayed.
15.9.3 SET STRUCTURE, FIELD: PARTIAL KEY
SET STRUCTURE struc_name, FIELD field_expr: PARTIAL KEY str_expr
|
| Example 15-44 SET STRUCTURE, FIELD: PARTIAL KEY |
|---|
open structure cl: name 'sheerpower:samples\client' input 'Name': name$ set structure cl, field last: partial key name$ print cl(id); ' '; cl(last) end Name? D 80561 Derringer |
This statement retrieves the first record matching the partial key in str_expr.
SET STRUCTURE struc_name: ID str_expr
|
| Example 15-45 SET STRUCTURE: ID |
|---|
declare structure str
open structure cl: name 'sheerpower:samples\client'
ask structure cl: id cl_id$
set structure str: id cl_id$
extract structure str
end extract
for each str
print str(#1); ' '; str(#2)
next str
end
20000 Smith
20001 Jones
20002 Kent
23422 Johnson
32001 Waters
43223 Errant
80542 Brock
80543 Cass
80544 Porter
80561 Derringer
80573 Farmer
|
SET STRUCTURE: ID sets a structure to a structure ID that has been stored previously into a string variable with the ASK STRUCTURE: ID statement. Once the SET STRUCTURE: ID statement has been used, the structure with the new structure name (STR in the example) can be accessed. By using these statements, generalized routines can be written when the structure that will be accessed until runtime is not known.
SET STRUCTURE struc_name: POINTER num_expr
|
| Example 15-46 SET STRUCTURE: POINTER |
|---|
open structure cl: name 'sheerpower:samples\client' extract structure cl end extract set structure cl: pointer 3 print cl(id); ' ' ; cl(last) end 23422 Johnson |
This statement sets the structure to the nth record extracted. The statement is useful after an extract has been done because it provides random access to any record extracted. There is no error message if there are no records extracted, or if the number given is out of range. If the number is valid, _EXTRACTED is set to 1; otherwise, it is set to 0.
15.9.6 SET STRUCTURE: EXTRACTED 0
SET STRUCTURE struc_name: EXTRACTED 0
|
| Example 15-47 SET STRUCTURE: EXTRACTED 0 |
|---|
open structure vend: name 'sheerpower:samples\vendor' set structure vend: extracted 0 end |
Setting the number of records extracted to zero causes a new collection to be started. The SET STRUCTURE struc_name : EXTRACTED 0 statement is used in conjunction with the EXTRACT STRUCTURE struc_name: APPEND statement.
Below is an example of opening an existing structure, extracting a record from it and updating the information in the record.
| Example 15-48 Updating a Structure |
|---|
// Simple customer query
open structure cust: name 'sheerpower:\samples\customer', access outin
do
line input 'Customer number', default '12513': cust$
if _exit or _back or cust$ = '' then exit do // get out if nothing to do
set structure cust, field custnbr: key cust$ // do the search
if _extracted = 0 then
message error: 'Cannot find '; cust$
repeat do
end if
print cust(custnbr), cust(name)
line input 'New name', default cust(name): newname$
if _exit or _back then repeat do
if newname$ = cust(name) then repeat do // nothing to do
cust(name) = newname$ // update the name
print 'Name changed to: '; cust(name)
loop
end
|
| Previous | Next | Contents | Index |