home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Total C++ 2
/
TOTALCTWO.iso
/
borland
/
32snipit.pak
/
CR8TXTBL.C
< prev
next >
Wrap
C/C++ Source or Header
|
1997-05-06
|
12KB
|
318 lines
// BDE32 3.x - (C) Copyright 1996 by Borland International
// cr8txtbl.c
#include "snipit.h"
static const char szTblName[] = "texttbl";
static const char szTblType[] = szASCII;
// Field descriptor used in creating a table.
static SNIPFAR FLDDesc fldDesc[] = {
{ // Field 1 - CHARACTER
1, // Field number
"CHARACTER", // Field name
fldZSTRING, // Field type
fldUNKNOWN, // Field subtype
10, // Field size ( 1 or 0, except
// BLOB, CHAR fields, Text FLOAT,
// NUMERIC fields )
0, // Decimal places ( 0 )
// computed
0, // Offset in record ( 0 )
0, // Length in bytes ( 0 )
0, // Null offset ( 0 )
fldvNOCHECKS, // Validity checks ( 0 )
fldrREADWRITE // Rights
},
{ // Field 2 - SHORT
2, "SHORT", fldINT16, fldUNKNOWN,
0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
},
{ // Field 3 - NUMERIC
3, "NUMERIC", fldFLOAT, fldUNKNOWN,
0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
},
{ // Field 4 - DATE
4, "DATE", fldDATE, fldUNKNOWN,
0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
},
{ // Field 5 - LOGICAL
5, "LOGICAL", fldBOOL, fldUNKNOWN,
0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
},
{ // Field 6 - TIME
6, "TIME", fldTIME, fldUNKNOWN,
0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
},
{ // Field 7 - TIMESTAMP
7, "TIMESTAMP", fldTIMESTAMP, fldUNKNOWN,
0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
},
{ // Field 8 - LONG
8, "LONG", fldINT32, fldUNKNOWN,
0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
},
{ // Field 9 - MONEY
9, "MONEY", fldFLOAT, fldstMONEY,
0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
}
}; // Array of field descriptors
// The number of fields in the table.
static const UINT16 uNumFields = sizeof(fldDesc) / sizeof (fldDesc[0]);
static DBIResult FillTextTable(hDBIDb hDb, pCHAR szTblName,
pCHAR szTblType, FLOAT NumRecs,
pFLDDesc pfldDesc, UINT16 uNumFields);
//=====================================================================
// Function:
// CreateAndFillSampleTXT();
//
// Description:
// This sample code will create a text table, insert
// 10 records into the table, then delete the table.
//
// Note that the fields of a text table are set using the
// DbiSetFieldMap function. It is required that the field
// descriptor used when setting the fields of a text table
// contains the physical field types.
//=====================================================================
void
CreateAndFillSampleTXT (void)
{
hDBIDb hDb; // Handle to the database
hDBICur hCur; // Handle to the table
CRTblDesc TblDesc; // Create table descriptor
UINT16 uDispNumRecs = 10; // Number of records to add and
// display.
DBIResult rslt; // Return value from IDAPI functions
pFLDDesc pTXTFlds; // Contains the physical field mappings
Screen("*** Create/Open/Fill Text Table 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");
Screen(" Initializing the table descriptor...");
memset((void *) &TblDesc , 0, sizeof(CRTblDesc));
lstrcpy(TblDesc.szTblName, szTblName);
lstrcpy(TblDesc.szTblType, szTblType);
Screen(" Creating the text table...");
rslt = DbiCreateTable(hDb, TRUE, &TblDesc);
if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
{
CloseDbAndExit(&hDb);
Screen("\r\n*** End of Example ***");
return;
}
// Note that the table type has to be specified with the separator.
Screen(" Fill the table with Random Data...");
FillTextTable(hDb, (pCHAR)szTblName, "ASCIIDRV-\"-,", uDispNumRecs,
fldDesc, uNumFields);
// Note that the table type has to be specified with the delimiter
// and separator as we are opening the tables as a delimited text
// file.
Screen(" Open the %s table....", szTblName);
rslt = DbiOpenTable(hDb, (pCHAR)szTblName, "ASCIIDRV-\"-,",
NULL, NULL, 0, dbiREADWRITE,
dbiOPENSHARED, xltFIELD, FALSE, NULL, &hCur);
if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
{
CloseDbAndExit(&hDb);
Screen("\r\n*** End of Example ***");
return;
}
pTXTFlds = (pFLDDesc) malloc(uNumFields * sizeof(FLDDesc));
if (pTXTFlds == NULL)
{
CloseDbAndExit(&hDb);
Screen("\r\n*** End of Example ***");
return;
}
// Convert the record structure to the text table format (physical).
rslt = DbiTranslateRecordStructure(NULL, uNumFields, fldDesc,
szASCII, NULL, pTXTFlds, FALSE);
ChkRslt(rslt, "TranslateRecordStruct");
Screen(" Use a field map to specify the fields of a"
" Text table...");
rslt = DbiSetFieldMap(hCur, uNumFields, pTXTFlds);
if (ChkRslt(rslt, "SetFieldMap") != DBIERR_NONE)
{
rslt = DbiCloseCursor(&hCur);
ChkRslt(rslt, "CloseCursor");
free(pTXTFlds);
CloseDbAndExit(&hDb);
Screen("\r\n*** End of Example ***");
return;
}
Screen(" Display the %s table which we just created...", szTblName);
DisplayInMemoryTable(hCur, uDispNumRecs);
Screen("\r\n Close the %s table...", szTblName);
rslt = DbiCloseCursor(&hCur);
ChkRslt(rslt, "CloseCursor");
Screen(" Close the database and exit IDAPI...");
CloseDbAndExit(&hDb);
free(pTXTFlds);
Screen("\r\n*** End of Example ***");
}
//=====================================================================
// Function:
// FillTextTable(hDb, szTblName, szTblType, NumRecs, fldDesc);
//
// Input: hDb - The database handle
// szTblName - Name of the table to fill
// szTblTyps - Type of the table to fill
// NumRecs - The number of records to insert
// fldDesc - Field descriptor for the table
// UINT16 uNumFields - Number of fields in the Field Desc
//
// Return: DBIResult - success?
//
// Description:
// This function adds the specified number of records to
// the table containing random data.
//=====================================================================
DBIResult
FillTextTable (hDBIDb hDb, pCHAR szTblName, pCHAR szTblType, FLOAT NumRecs,
pFLDDesc pfldDesc, UINT16 uNumFields)
{
DBIResult rslt; // Return value from IDAPI functions
FLOAT fRecCount; // Loop variable = count of records
UINT16 FldCntr; // Field counter
pBYTE pRecBuf; // Pointer to the record buffer
CURProps TblProps; // Table descriptor
hDBICur hCur; // Handle to the table
pFLDDesc pTXTFlds; // Contains the physical field mappings
pFLDDesc pLogFlds; // Descriptor for the logical field mappings
Screen(" Inserting %.0f records into the table...", NumRecs);
rslt = DbiOpenTable(hDb, szTblName, szTblType, NULL,
NULL, 0, dbiREADWRITE,
dbiOPENSHARED, xltFIELD, FALSE,
NULL, &hCur);
if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
{
return rslt;
}
pTXTFlds = (pFLDDesc) malloc(uNumFields * sizeof(FLDDesc));
if (pTXTFlds == NULL)
{
rslt = DbiCloseCursor(&hCur);
ChkRslt(rslt, "CloseCursor");
return DBIERR_NOMEMORY;
}
// Field map has to be set using a field descriptor containing
// physical field types.
rslt = DbiTranslateRecordStructure(NULL, uNumFields, pfldDesc,
szASCII, NULL, pTXTFlds, FALSE);
if (ChkRslt(rslt, "TranslateRecordStruct") != DBIERR_NONE)
{
free(pTXTFlds);
rslt = DbiCloseCursor(&hCur);
ChkRslt(rslt, "CloseCursor");
return rslt;
}
// Use a field map to specify the fields of a text table.
rslt = DbiSetFieldMap(hCur, uNumFields, pTXTFlds);
if (ChkRslt(rslt, "SetFieldMap") == DBIERR_NONE);
{
// Allocate a record buffer
rslt = DbiGetCursorProps(hCur, &TblProps);
ChkRslt(rslt, "GetCursorProps");
pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE));
pLogFlds = (pFLDDesc) malloc(TblProps.iFields * sizeof(FLDDesc));
if ((pRecBuf == NULL) || (pLogFlds == NULL))
{
if (pRecBuf) free(pRecBuf);
if (pLogFlds) free(pLogFlds);
free(pTXTFlds);
Screen(" Error - Out of memory.");
rslt = DbiCloseCursor(&hCur);
ChkRslt(rslt, "CloseCursor");
return DBIERR_NOMEMORY;
}
rslt = DbiGetFieldDescs(hCur, pLogFlds);
if (ChkRslt(rslt, "GetFieldDescs") != DBIERR_NONE)
{
free(pRecBuf);
free(pTXTFlds);
free(pLogFlds);
rslt = DbiCloseCursor(&hCur);
ChkRslt(rslt, "CloseCursor");
return rslt;
}
for (fRecCount = 0; fRecCount < NumRecs; fRecCount++)
{
// Make sure we're starting with a clean record buffer
rslt = DbiInitRecord(hCur, pRecBuf);
ChkRslt(rslt, "InitRecord");
// This loop will drive what type of data is inserted into
// the table.
for (FldCntr = 0; FldCntr < uNumFields; FldCntr++)
{
// Put field data into the record buffer.
PutFieldSample(hCur, pRecBuf, (UINT16)(FldCntr+1), &pLogFlds[FldCntr]);
}
// All fields have been inserted into the record buffer.
// Append the record to the table.
rslt = DbiAppendRecord(hCur, pRecBuf);
ChkRslt(rslt, "InsertRecord");
for (FldCntr = 0; FldCntr < uNumFields; FldCntr++)
{
if (pLogFlds[FldCntr].iFldType == fldBLOB)
{
rslt = DbiFreeBlob(hCur, pRecBuf, FldCntr);
ChkRslt(rslt, "FreeBlob");
}
}
}
free(pRecBuf);
free(pLogFlds);
}
rslt = DbiCloseCursor(&hCur);
ChkRslt(rslt, "CloseCursor");
free(pTXTFlds);
return DBIERR_NONE;
}