// BDE - (C) Copyright 1995 by Borland International // Inmemtbl.C #include "snipit.h" static char szTblName[] = "INMEMTBL"; // Field Descriptor used in creating a table. static SNIPFAR FLDDesc fldDesc[] = { { // Field 1 - ALPHA 1, // Field number "ALPHA", // Field name fldZSTRING, // Field type fldUNKNOWN, // Field subtype 10, // Field size 0, // Decimal places ( 0 ) // computed 0, // Offset in record ( 0 ) 11, // Length in bytes ( 0 ) 0, // For Null bits ( 0 ) fldvNOCHECKS, // Validity checks ( 0 ) fldrREADWRITE // Rights }, { // Field 2 - NUMERIC 2, "NUMERIC", fldFLOAT, fldUNKNOWN, 0, 0, 0, 8, 0, fldvNOCHECKS, fldrREADWRITE }, { // Field 3 - MONEY 3, "MONEY", fldFLOAT, fldstMONEY, 0, 0, 0, 8, 0, fldvNOCHECKS, fldrREADWRITE }, { // Field 4 - DATE 4, "DATE", fldDATE, fldUNKNOWN, 0, 0, 0, 4, 0, fldvNOCHECKS, fldrREADWRITE }, { // Field 5 - SHORT 5, "SHORT", fldINT16, fldUNKNOWN, 0, 0, 0, 2, 0, fldvNOCHECKS, fldrREADWRITE }, { // Field 6 - TIME 6, "TIME", fldTIME, fldUNKNOWN, 0, 0, 0, 4, 0, fldvNOCHECKS, fldrREADWRITE }, { // Field 7 - TIMESTAMP 7, "TIMESTAMP", fldTIMESTAMP, fldUNKNOWN, 0, 0, 0, 8, 0, fldvNOCHECKS, fldrREADWRITE }, { // Field 8 - LONG 8, "LONG", fldINT32, fldUNKNOWN, 0, 0, 0, 4, 0, fldvNOCHECKS, fldrREADWRITE }, { // Field 9 - BOOL 9, "BOOL", fldBOOL, fldUNKNOWN, 0, 0, 0, 2, 0, fldvNOCHECKS, fldrREADWRITE }, { // Field 10 - BYTES 10, "BYTES", fldBYTES, fldUNKNOWN, 20, 0, 0, 20, 0, fldvNOCHECKS, fldrREADWRITE } }; // Array of field descriptors. // The number of fields in the table. static unsigned uNumFields = sizeof(fldDesc) / sizeof (fldDesc[0]); static DBIResult FillTable1(hDBICur hCur, DFLOAT NumRecs); //===================================================================== // Function: // // CreateAndFillInMemoryTbl(); // Description: // This sample code will create an in-memory table, insert // 10 records into the table, then close the table. // // There are the following limits for In-Memory table: // Fields: 1024 // Max Rec Size: 16K // Indexes: 0 (Cannot be indexed) // Table Size: 512M //===================================================================== void CreateAndFillInMemoryTbl (void) { hDBIDb hDb; // Handle to the database hDBICur hCur; // Handle to the table UINT16 uDispNumRecs = 10 ; // Number of records to add and // display DBIResult rslt; // Return value from IDAPI functions Screen("*** Create/Open/Fill In-Memory 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(" Creating the %s in-memory table...", szTblName); rslt = DbiCreateInMemTable(hDb, szTblName, uNumFields, fldDesc, &hCur); if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE) { CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } Screen(" Fill the %s table with random data...", szTblName); FillTable1(hCur, uDispNumRecs); rslt = DbiSetToBegin(hCur); ChkRslt(rslt, "SetToBegin"); Screen(" Display the %s table which we just created...", szTblName); DisplayInMemoryTable(hCur, uDispNumRecs); // You do not need to delete this table. IDAPI will delete the // table when it is closed. Screen("\r\n Close the %s table...", szTblName); rslt = DbiCloseCursor(&hCur); ChkRslt(rslt, "CloseCursor"); Screen(" Close the database and exit IDAPI..."); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); } //===================================================================== // Function: // FillTable1(hCur, NumRecs); // // Input: hCur - The table cursor // NumRecs - The number of records to insert // // Return: DBIResult - Success of the opperation // // Description: // This function adds the specified number of records to // the in-memory table. //===================================================================== DBIResult FillTable1 (hDBICur hCur, DFLOAT NumRecs) { DBIResult rslt; // Return value from IDAPI functions DFLOAT fRecCount; // Loop variable = count of records UINT16 FldCntr; // Field counter pBYTE pRecBuf = NULL; // Pointer to the record buffer CURProps TblProps; // Table descriptor Screen(" Inserting %.0f records into the table...", NumRecs); if (hCur) { // Allocate a record buffer. rslt = DbiGetCursorProps(hCur, &TblProps); ChkRslt(rslt, "GetCursorProps"); pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize); if (pRecBuf == NULL) { Screen(" Error - Out of memory"); return DBIERR_NOMEMORY; } // Loop until the specified number of records have been written // to the table. for (fRecCount = 0; fRecCount < NumRecs; fRecCount++) { // Make sure we're starting with a clean record buffer. rslt = DbiInitRecord(hCur, pRecBuf); ChkRslt(rslt, "InitRecord"); for (FldCntr = 0; FldCntr < uNumFields; FldCntr++) { // Put field data into the record buffer. PutFieldSample(hCur, pRecBuf, (FldCntr + 1), &fldDesc[FldCntr]); } // Append the record to the table.... rslt = DbiAppendRecord(hCur, pRecBuf); ChkRslt(rslt, "InsertRecord"); } free((pCHAR) pRecBuf); } return DBIERR_NONE; }