home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Total C++ 2
/
TOTALCTWO.iso
/
borland
/
32snipit.pak
/
IDXDBASE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1997-05-06
|
16KB
|
430 lines
// BDE32 3.x - (C) Copyright 1996 by Borland International
// idxdbase.c
#include "snipit.h"
#define NAMELEN 10 // Length of the name field.
#define PLACELEN 20 // Length of the POB field.
#define DATELEN 11 // Display length of a date field: mm\dd\yyyy
static const char szTblName[] = "people";
static const char szTblType[] = szDBASE;
// Field descriptor used in creating a table.
static SNIPFAR FLDDesc fldDesc[] = {
{ // Field 1 - First Name
1, // Field number
"FirstName", // Field name
fldZSTRING, // Field type
fldUNKNOWN, // Field subtype
NAMELEN, // Field size ( 1 or 0, except
// BLOb or CHAR field )
0, // Decimal places ( 0 )
// computed
0, // Offset in record ( 0 )
0, // Length in bytes ( 0 )
0, // For Null bits ( 0 )
fldvNOCHECKS, // Validity checks ( 0 )
fldrREADWRITE // Rights
},
{ // Field 2 - Middle Name
2, "MidName", fldZSTRING, fldUNKNOWN,
NAMELEN, 0, 0, 0, 0,
fldvNOCHECKS, fldrREADWRITE
},
{ // Field 3 - Last Name
3, "LName", fldZSTRING, fldUNKNOWN,
NAMELEN, 0, 0, 0, 0,
fldvNOCHECKS, fldrREADWRITE
},
{ // Field 4 - Date of Birth
4, "DOB", fldDATE, fldUNKNOWN,
0, 0, 0, 0, 0,
fldvNOCHECKS, fldrREADWRITE
},
{ // Field 5 - Place of Birth
5, "POB", fldZSTRING, fldUNKNOWN,
PLACELEN, 0, 0, 0, 0,
fldvNOCHECKS, fldrREADWRITE
}}; // Array of field descriptors
// Index descriptor - describes the index associated with the
// table.
static SNIPFAR IDXDesc idxDesc[] = {
{ // Composite Production Index
"", // Name
NULL, // Number
{"FNAME"}, // Tag Name ( for dBASE )
{ NULL }, // Optional Format ( BTREE,
// HASH, etc )
FALSE, // Primary?
FALSE, // Unique?
FALSE, // Descending?
TRUE, // Maintained?
FALSE, // SubSet?
TRUE, // Expression index?
NULL, // for QBE only
3, // Fields in key
1, // Length in bytes
FALSE, // Index out of date?
0, // Key type of expression
{ 1,2,3 }, // Array of field numbers
{"FirstName + MidName + LName" },// Key expression
{ 0 }, // Key condition
FALSE, // Case insensitive
0, // Block size in bytes
0 // Restructure number
},
{ // Single-field production index
"", NULL, {"LNAME"}, { NULL }, FALSE, FALSE,
FALSE, TRUE, FALSE, FALSE, NULL, 1, 1, FALSE,
0, { 3 }, { 0 }, { 0 }, FALSE, 0, 0
},
{ // Single-field production index
"", NULL, {"POB"}, { NULL }, FALSE, FALSE, FALSE,
TRUE, FALSE, FALSE, NULL, 1, 1, FALSE, 0, { 5 },
{ 0 }, { 0 }, FALSE, 0, 0
}};
static SNIPFAR IDXDesc idxDesc1[] = {
{ // Expression index, non-maintained .NDX style index
"FULLNAME.NDX", NULL, { NULL }, { NULL }, FALSE,
FALSE, FALSE, FALSE, FALSE, TRUE, NULL, 2, 1,
FALSE, 0, { 1,3 }, {"FirstName + LName"}, { 0 },
FALSE, 0, 0
}};
static DBIResult InitTable(phDBIDb);
static DBIResult AddRecord(phDBICur, pCHAR, pCHAR, pCHAR, UINT16, UINT16, UINT16,
pCHAR);
//=====================================================================
// Function:
// IndexDBase();
//
// Description:
// This example shows how to set up a dBASE table and dBASE
// indexes. This includes expression indexes (multiple field
// indexes) and the particular needs of the common dBASE index.
//=====================================================================
void
IndexDBase (void)
{
hDBIDb hDb = 0; // Database handle
hDBICur hCur = 0; // Cursor handle
DBIResult rslt; // Return value from IDAPI functions
IDXDesc *MyDesc; // Index descriptor
Screen("*** dBASE Index Example ***\r\n");
BREAK_IN_DEBUGGER();
Screen(" Initializing IDAPI...");
if (InitAndConnect(&hDb) != DBIERR_NONE)
{
Screen("\r\n*** End of Example ***");
return;
}
Screen(" Setting the database directory...");
rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
ChkRslt(rslt, "SetDirectory");
// Create the table and add four records.
if (InitTable(&hDb))
{
CloseDbAndExit(&hDb);
Screen("\r\n*** End of Example ***");
return;
}
Screen(" Open the table which we just created in exclusive"
" mode\r\n (required in order to add an index to a"
" dBASE table)...");
rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
NULL, NULL, NULL, dbiREADWRITE, dbiOPENEXCL,
xltFIELD, FALSE, NULL, &hCur);
if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
{
CloseDbAndExit(&hDb);
Screen("\r\n*** End of Example ***");
return;
}
// Now add the new index to the table. To add an index we must have
// an exclusive lock on the table which we acquired when opening the
// table.
Screen(" Adding a non-maintained index to the table...");
rslt = DbiAddIndex(hDb, hCur,(pCHAR) szTblName, (pCHAR) szTblType,
idxDesc1, NULL);
ChkRslt(rslt, "AddIndex");
// DbiAddIndex() adds the index and automatically opens it
// Close the index. In dBASE, after closing a non-maintained
// index will not be kept up-to-date.
Screen(" Closing the non-maintained index we just added...");
rslt = DbiCloseIndex(hCur, "FULLNAME.NDX", NULL);
ChkRslt(rslt, "CloseIndex");
// Now add a record to the table to make the index out of date.
Screen(" Adding a record to invalidate the index...");
rslt = AddRecord(&hCur, "Charlie", "Joel", "Waternon", 5, 21, 1983,
"California");
ChkRslt(rslt, "AddRecord");
// Open the added index using DbiOpenIndex, which is dBASE-
// specific.
Screen(" Open the new index to show that it is out of date...");
rslt = DbiOpenIndex(hCur, "FULLNAME.NDX", NULL);
ChkRslt(rslt, "OpenIndex");
// Opening the index does not make it the current index. To make the
// index current we need to switch to it. For a non maintained .NDX
// index, we need to supply the index name.
Screen(" Switch to the newly created index that is out of date...");
rslt = DbiSwitchToIndex(&hCur, "FULLNAME.NDX", NULL, NULL, FALSE);
ChkRslt(rslt, "SwitchToIndex");
Screen(" Display the table...");
Screen("\r\n Note that only 4 entries are displayed even"
" though there are are 5\r\n entries in the table"
" because the index is out of date...");
DisplayTable(hCur, 0);
// Switch to default order. We cannot regen the index if it is the
// currently active index on the cursor.
Screen("\r\n Switching to default order...");
rslt = DbiSwitchToIndex(&hCur, NULL, NULL, NULL, FALSE);
ChkRslt(rslt, "SwitchToIndex");
Screen("\r\n Regenerating the index...");
rslt = DbiRegenIndex(hDb, hCur, (pCHAR) szTblName, (pCHAR) szTblType,
"FULLNAME.NDX", NULL, NULL);
ChkRslt(rslt, "RegenIndex");
// Switch back to the non-maintained index. It should now be up-to-date.
Screen(" Switch to the newly regenerated index...");
rslt = DbiSwitchToIndex(&hCur, "FULLNAME.NDX", NULL, NULL, FALSE);
ChkRslt(rslt, "SwitchToIndex");
Screen(" Display the table according to the new index...");
DisplayTable(hCur, 0);
// Switch to default order. The default order for a dBASE table is
// the order in which the records were inserted.
Screen("\r\n Switching index to show record order...");
rslt = DbiSwitchToIndex(&hCur, NULL, NULL, NULL, FALSE);
ChkRslt(rslt, "SwitchToIndex");
Screen(" This is the order of the table with no index...");
DisplayTable(hCur, 0);
MyDesc = (IDXDesc *) malloc(sizeof(IDXDesc));
if (MyDesc == NULL)
{
CloseDbAndExit(&hDb);
Screen("\r\n*** End of Example ***");
return;
}
// Close the non-maintained index as we are done with this
// part of the example.
Screen("\r\n Closing the non-maintained index.");
rslt = DbiCloseIndex(hCur, "FULLNAME.NDX", NULL);
ChkRslt(rslt, "CloseIndex");
// The only indexes that are open now are maintained indexes since we
// have already closed the non-maintained index.
// Get any index descriptor (in this case the first one).
rslt = DbiGetIndexDesc(hCur, 1, MyDesc);
ChkRslt(rslt, "GetIndexDesc");
Screen(" Switching to an index in the production index... ");
rslt = DbiSwitchToIndex(&hCur, MyDesc->szName, MyDesc->szTagName, NULL,
FALSE);
ChkRslt(rslt, "SwitchToIndex");
Screen(" This is the order of the table using a tag inside the"
" production\r\n index called %s...", MyDesc->szTagName);
DisplayTable(hCur, 0);
// Delete the table and its related indexes and files from disk. To
// do this we first need to delete the NDX and then we close the
// table and delete the table. In this way IDAPI will delete the
// MDX file for us.
Screen("\r\n Delete the FULLNAME.NDX index. This must be done separately"
" as it is not\r\n a part of the production index...");
rslt = DbiDeleteIndex(hDb, hCur, (pCHAR) szTblName, (pCHAR) szTblType,
"FULLNAME.NDX", NULL, NULL);
ChkRslt(rslt, "DeleteIndex");
Screen(" Close the %s table...", szTblName);
rslt = DbiCloseCursor(&hCur);
ChkRslt(rslt, "CloseCursor");
Screen(" Delete the %s table...", szTblName);
rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
ChkRslt(rslt, "DeleteTable");
free(MyDesc);
Screen(" Close the database and exit IDAPI...");
CloseDbAndExit(&hDb);
Screen("\r\n*** End of Example ***");
}
//=====================================================================
// Function:
// InitTable();
//
// Input: pointer to the database handle.
//
// Return: result of the table initialization.
//
// Description:
// This function will create a table and fill it
// with a number of records. The function uses
// another function called AddRecord.
//=====================================================================
DBIResult
InitTable (phDBIDb phDb)
{
DBIResult rslt; // Value returned from IDAPI functions
hDBICur hCur; // Cursor handle for the table that is
// created
CRTblDesc crTblDsc; // Table descriptor
BOOL bOverWrite = TRUE; // Overwrite, yes/no flag
// The number of fields in the table - note that fldDesc is defined
// globally
const UINT16 uNumFields = sizeof(fldDesc) / sizeof (fldDesc[0]);
// Number of indexes to be created when the table is created - note
// that idxDesc is defined globally.
const UINT16 uNumIndexes = sizeof(idxDesc) / sizeof(idxDesc[0]);
memset(&crTblDsc, 0, sizeof(CRTblDesc)); // Clear the buffer.
// Set the name and the type of the table
strcpy(crTblDsc.szTblName, szTblName) ; // name of the table
strcpy(crTblDsc.szTblType, szTblType) ; // Type of table
// Set the field information for the table
crTblDsc.iFldCount = uNumFields ; // number of fields
crTblDsc.pfldDesc = fldDesc ; // Field descriptor
// Set the index information for the table
crTblDsc.iIdxCount = uNumIndexes ; // Number of indexes
crTblDsc.pidxDesc = idxDesc ; // Index descriptor
// Create the table using information supplied in the table
// descriptor above
Screen(" Creating table and indexes...");
rslt = DbiCreateTable(*phDb, bOverWrite, &crTblDsc);
if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
{
return rslt;
}
rslt = DbiOpenTable(*phDb, (pCHAR) szTblName, (pCHAR) szTblType,
NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
xltFIELD, FALSE, NULL, &hCur);
ChkRslt(rslt, "OpenTable");
// Add records to the table
Screen(" Adding Records to the table...");
AddRecord(&hCur, "Klaus", "John", "Lockwood", 7, 28, 1968,
"Chicago");
AddRecord(&hCur, "Ann", "Tracy", "Brown", 12, 27, 1969,
"Hermosa Beach");
AddRecord(&hCur, "John", "Boy", "Krull", 2, 7, 1954,
"Ohmaha");
AddRecord(&hCur, "Goliath", "Joel", "Raccah", 4, 13, 1970,
"Tel Aviv");
// Close the table so it can be reopened from the calling function.
rslt = DbiCloseCursor(&hCur);
ChkRslt(rslt, "CloseCursor");
return rslt;
}
//=====================================================================
// Function:
// AddRecord();
//
// Input: pointer to the cursor handle, first name (pCHAR), middle
// name (pCHAR), last name (pCHAR), Month of Birth (UINT16), Day
// of Birth (UINT16), Year of Birth (UINT16), Place of birth
// (pCHAR).
//
// Return: Result of adding the record to the table
//
// Description:
// This function will add a record to the given table.
//=====================================================================
DBIResult
AddRecord (phDBICur hCur, pCHAR pszFirst, pCHAR pszMiddle, pCHAR pszLast,
UINT16 uMonth, UINT16 uDay, UINT16 uYear, pCHAR pszPOB)
{
DBIDATE Date; // Date structure
DBIResult rslt; // Value returned from IDAPI functions
CURProps TblProps; // Table properties
pBYTE pRecBuf; // Record buffer
// Allocate a record buffer
rslt = DbiGetCursorProps(*hCur, &TblProps);
ChkRslt(rslt, "GetCursorProps");
pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE));
if (pRecBuf == NULL)
{
return DBIERR_NOMEMORY;
}
// Make sure we're starting with a clean record buffer
rslt = DbiInitRecord(*hCur, pRecBuf);
ChkRslt(rslt, "InitRecord");
// First Name
rslt = DbiPutField(*hCur, 1, pRecBuf, (pBYTE) pszFirst);
ChkRslt(rslt, "PutField");
// Middle Name
rslt = DbiPutField(*hCur, 2, pRecBuf, (pBYTE) pszMiddle);
ChkRslt(rslt, "PutField");
// Last Name
rslt = DbiPutField(*hCur, 3, pRecBuf, (pBYTE) pszLast);
ChkRslt(rslt, "PutField");
// DOB
rslt = DbiDateEncode(uMonth, uDay, uYear, &Date);
ChkRslt(rslt, "DateEncode");
rslt = DbiPutField(*hCur, 4, pRecBuf, (pBYTE) &Date);
ChkRslt(rslt, "PutField");
// Place of Birth
rslt = DbiPutField(*hCur, 5, pRecBuf, (pBYTE) pszPOB);
ChkRslt(rslt, "PutField");
rslt = DbiInsertRecord(*hCur, dbiNOLOCK, pRecBuf),
ChkRslt(rslt, "InsertRecord");
free(pRecBuf);
return rslt;
}