CTDatabase::NextTable() retrieves the name and path of the next table in a database. CTDatabase::NextTable() returns NO (false) when no more tables exist for the current database.
// Display all tables in a database
void DisplayTables(CTDatabase &ADatabase)
{
CTString Name;
CTString Path;
if (ADatabase.FirstTable(Name, Path))
do
{
printf("Table: %s Path: %s\n", Name.c_str(), Path.c_str());
}
while (ADatabase.NextTable(Name, Path);
}
CTDatabase::FindTable() locates a specific table given the table name and, if the table exists, retrieves the table path. If a table cannot be found, CTDatabase::FindTable() returns NO (false).
// return YES if table exist or NO if table does not exit
CTBOOL TableExist(CTDatabase &ADatabase, CTString& tblName)
{
CTString tblPath;
return ADatabase.FindTable(TblName, tblPath);
}
CTDatabase::GetFirstActive() retrieves the table object pointer of the first active table. If the database contains no active tables, CTDatabase::GetFirstActive() returns NULL.
CTDatabase::GetNextActive() retrieves the table object pointer of the next active table. When no more active tables exist, CTDatabase::GetNextActive() returns NULL.
// Display all active tables
void DisplayActiveTables(CTDatabase &ADatabase)
{
VRLEN hScan;
CTTable *pTable;
if ((pTable = ADatabase.GetFirstActive(&hScan)) != NULL)
{
do
{
printf("Table: %s Path: %s\n", pTable->GetName().c_str(),
pTable->GetPath().c_str());
pTable = ADatabase.GetNextActive(&hScan);
}
while (pTable != NULL;
}
}
CTDatabase::FindActive() locates a specific active table and returns the table object pointer. If the table is not active, CTDatabase::FindActive() returns NULL.
// Check if table is active
CTBOOL IsTableActive(CTDatabase& ADatabase, CTString& tblName)
{
return (ADatabase.FindActive(tblName) != NULL) ? YES : NO;
}
The function above is shown for example purposes only as the c-treeDB method CTTable::IsActive() provides a more efficient way to check if a table is active.
When a table is created or added to a database, an automatic and unique identifier (UID) is associated with the table. A table UID is unique within the database that the table is associated with.
A table UID is an unsigned long value that can be used as an alternative method to operate on tables once the table is created or added to a database.
The overloaded method CTDatabase::FindTable() locates a table in the database given the table UID and retrieves the table name and path. The following example shows how to implement a table open function using the table UID instead of the table name.
// open table using UID
CTTable* OpenByUID(CTDatabase& ADatabase, ULONG uid, CTOPEN_MODE OpenMode)
{
CTString tblName;
CTString tblPath;
CTTable* Retval;
// locate the table in the database by uid
if (ADatabase.FindTable(uid, tblName, tblPath))
{
Retval = new CTTable(ADatabase);
if (!Retval)
throw CTException(CTDBRET_NOMEMORY);
try
{
Retval->Open(tblName, OpenMode);
}
catch (CTException& err)
{
delete Retval;
throw;
}
}
else
{
// table not found
throw CTException(INOT_ERR);
}
return Retval;
}
The overloaded method CTDatabase::FindActive() locates an active table given its UID number and returns the active table object pointer. The following example shows how to check if a table is active using its UID number.
// check if atable is active, by UID
CTBOOL IsTableActive(CTDatabase& ADatabase, ULONG uid)
{
return (ADatabase. FindActive(uid) != NULL) ? YES : NO;
}