|
The QMSelectLeft() and QMSelectRight() functions traverse an alternate key index, creating a select list from the entry to the left or right of the last entry processed. They are analogous to the QMBasic SELECTLEFT and SELECTRIGHT statements.
The function arguments are:
| Var | is the variable to receive the index key value associated with the returned list. |
| FileNo | is the file number returned by a previous QMOpen() call. |
| IndexName | is the name of the alternate key index to be used. |
| ListNo | is the select list number (0 to 10). |
The QMSelectLeft() and QMSelectRight() functions construct a select list from the alternate key index entry to the left or right of the one most recently returned by QMSelectIndex(), QMSelectLeft() or QMSelectRight(). The position of the scan can be set at the extreme left using QMSetLeft() or at the extreme right using QMSetRight().
These operations allow a program to find a specific value and then walk through successive values in the sorted data structure that makes up an alternate key index. The function returns the indexed data value associated with the index entry found. The select list identified by ListNo is set to contain the records ids of the records that have this data value.
If QMSelectIndex() is used to locate a value that does not exist in the index, QMSelectLeft() will return a list of records for the value immediately before the non-existent one and QMSelectRight() will return a list of records for the value immediately after the non-existent one.
The QMStatus() function returns zero if the operation is successful, non-zero if it fails. Although other errors may occur, two useful status values are
3019
|
ER$AKNF
|
Specified index does not exist
|
3030
|
ER$EOF
|
No further items at end of index
|
The number of items in the select list can be established by using QMGetVar() to retrieve the @SELECTED variable immediately after the QMSelectIndex.
The example program fragments below set the scan position for an index on the CODE field to the leftmost item and walk through successive entries, processing each record.
char * QMSelectLeft(int FileNo, char * IndexName, int ListNo)
char * QMSelectRight(int FileNo, char * IndexName, int ListNo)
QMSetLeft(fData, "CODE");
while(1)
{
Key = QMSelectRight(fData, "CODE", 1);
if (QMStatus() != 0) break;
while((Id = QMReadNext(1)) != NULL)
{
if (Id == NULL) break;
Rec = QMRead(fData, Id, &Err);
if (Err == 0)
{
...process record...
QMFree(Rec);
}
QMFree(Id);
}
QMFree(Key);
}
The returned pointer references a dynamically allocated memory area that must be released using QMFree() when no longer needed. In this example, there are other dynamic memory areas that must be released in the same way.
|
QMSelectLeft(ByVal FileNo as Integer, ByVal IndexName as String, ByVal ListNo as Integer) as String
QMSelectRight(ByVal FileNo as Integer, ByVal IndexName as String, ByVal ListNo as Integer) as String
QMSetLeft(fData, "CODE")
Do
Key = QMSelectRight(fData, "CODE", 1)
If QMStatus() <> 0 Then Exit Do
Do
Id = QMReadNext(1)
If Id = Nothing Then Exit Do
Rec = QMRead(fData, Id, Err)
If Err = 0 Then
...process record...
End If
Loop
Loop
|
SelectLeft(FileNo, IndexName, ListNo)
SelectRight(FileNo, IndexName, ListNo)
session->SetLeft(fData, "CODE")
loop
Key = session->SelectRight(fData, "CODE", 1)
until session->ServerStatus
loop
Id = session->ReadNext(1)
until Id = ""
Rec = session->Read(fData, Id, Err)
if Err = 0 then
...process record...
end
repeat
repeat
|
String SelectLeft(int FileNo, String IndexName, int ListNo)
String SelectRight(int FileNo, String IndexName, int ListNo)
qm.SetLeft(fData, "CODE");
while(true)
{
Key = qm.SelectRight(fData, "CODE", 1);
if (qm.ServerStatus != 0) break;
while(true)
{
Id = qm.ReadNext(1);
if (qm.ServerError != SV$OK) break;
Rec = qm.Read(fData, Id);
if (qm.ServerError == 0)
{
...process record...
}
}
}
|
SelectLeft(FileNo, IndexName, ListNo)
SelectRight(FileNo, IndexName, ListNo)
qm.SetLeft(fData, "CODE")
while True:
Key = qm.SelectRight(fData, "CODE", 1)
if qm.Status() != 0: break
while True:
Id = qm.ReadNext(1)
if Id == "": break
Rec, Err = qm.Read(fData, Id)
if Err == 0:
...process record...
|
|