// BDE - (C) Copyright 1995 by Borland International // Block.c #include "snipit.h" static const char szTblName[] = "customer"; static const char szCopyTblName[] = "custcopy"; static const char szTblType[] = szPARADOX; #define FIELDLEN 30 static DBIResult CreateTable(hDBIDb hDb, hDBICur hCur, pCHAR szDestTblName, pCHAR szTblType, phDBICur phCurTemp); //===================================================================== // Function: // Block(); // // Description: // This example shows how to read and write records to a table // in blocks. Moving records in blocks is the preferred method // of reading and writing records to a table because it // enhances the performance of the application. //===================================================================== void Block (void) { DBIResult rslt; // Return value from IDAPI functions hDBIDb hDb; // Handle to the database hDBICur hCur; // Handle to the table hDBICur hCurCopy; // Handle to a temporary table CURProps CurProps; // Properties of the cursor pBYTE pRecBuf; // Pointer to the record buffer UINT32 uNumBuf = 10; // Number of records to buffer UINT32 uNumRecs = 10; // Number of records to display, 0=all UINT16 uRecNum; // Loop counter - current record number UINT16 uRecOffset; // Offset into the block of records CHAR DestBuf[34]; // New field value to write to the // table CHAR TempBuf[34]; // Field value read in from the table BOOL bBlank; // Used to determine if a field is // blank Screen("*** Block maniplulation 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(" Open the %s table (source)...", szTblName); rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType, NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL, &hCur); if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE) { CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } // Create a temporary table with the same field structure as the // source table. if (CreateTable(hDb, hCur, (pCHAR) szCopyTblName, (pCHAR) szTblType, &hCurCopy) != DBIERR_NONE) { rslt = DbiCloseCursor(&hCur); ChkRslt(rslt, "CloseCursor"); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } // Get the size of the record buffer. rslt = DbiGetCursorProps(hCur, &CurProps); ChkRslt(rslt, "GetCursorProps"); // Allocate space for the record buffer. pRecBuf = (pBYTE) malloc((UINT16) CurProps.iRecBufSize * (UINT16) sizeof(BYTE) * (UINT16) uNumBuf); if (pRecBuf == NULL) { Screen(" Error - Out of memory"); CloseDbAndExit(&hDb); return; } Screen(" Go to the beginning of the %s table...", szTblName); rslt = DbiSetToBegin(hCur); ChkRslt(rslt, "SetToBegin"); Screen(" Display the %s table...", szTblName); DisplayTable(hCur, uNumRecs); Screen("\r\n Go to the beginning of the %s table...", szTblName); rslt = DbiSetToBegin(hCur); ChkRslt(rslt, "SetToBegin"); Screen(" Read %lu records from the %s table...", uNumBuf, szTblName); rslt = DbiReadBlock(hCur, &uNumBuf, pRecBuf); // Getting an EOF error is all right - means that an attempt was // made to read more records than are currently in the table. if ((rslt != DBIERR_EOF) && (rslt != DBIERR_NONE)) { ChkRslt(rslt, "ReadBlock"); } for (uRecNum = 0; uRecNum < uNumBuf; uRecNum++) { // Calculate the record offset. uRecOffset = (uRecNum * CurProps.iRecBufSize), // Get the value in the second field for each record. rslt = DbiGetField(hCur, 2, (pBYTE) &pRecBuf[uRecOffset], (pBYTE) TempBuf, &bBlank); ChkRslt(rslt, "GetField"); Screen(" Modify the record: add \"%u \" to %s", uRecNum + 1, TempBuf); // Put the record number at the start of the field. sprintf(DestBuf, "%u ", uRecNum + 1); // Add the record number to the head of the field. strncat(DestBuf, TempBuf, (FIELDLEN - strlen(DestBuf))); // Update the buffer value. rslt = DbiPutField(hCur, 2, (pBYTE) &pRecBuf[uRecOffset], (pBYTE) DestBuf); ChkRslt(rslt, "GetField"); } // Write uNumBuf records to the table. Screen("\r\n Write %lu records to the %s table...", uNumBuf, szCopyTblName); rslt = DbiWriteBlock(hCurCopy, &uNumBuf, pRecBuf); ChkRslt(rslt, "WriteBlock"); // Go to the beginning of the table. Screen("\r\n Go to the beginning of the %s table...", szCopyTblName); rslt = DbiSetToBegin(hCurCopy); ChkRslt(rslt, "SetToBegin"); Screen(" Display the %s table...", szCopyTblName); DisplayTable(hCurCopy, uNumRecs); // Clean up. free(pRecBuf); Screen("\r\n Close the %s table...", szTblName); rslt = DbiCloseCursor(&hCur); ChkRslt(rslt, "CloseCursor"); Screen("\r\n Close the %s table...", szCopyTblName); rslt = DbiCloseCursor(&hCurCopy); ChkRslt(rslt, "CloseCursor"); Screen(" Deleting the %s table...", szCopyTblName); rslt = DbiDeleteTable(hDb, (pCHAR) szCopyTblName, (pCHAR) szTblType); ChkRslt(rslt, "DeleteTable"); Screen(" Close the database and exit IDAPI..."); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); } //===================================================================== // Function: // CreateTable(hDb, hCur, szDestTblName, szTblType, phCurTemp); // // Input: hDb - Handle to the database // hCur - Handle to the source table // szDestTblName - Name of the destination table // szTblType - Type of the table // phCurTemp - Cursor returned on the temporary table // // Return: DBIResult - Success of the operation, or what went // wrong // // Description: // This function is used to create a table and return a cursor // to that table. //===================================================================== DBIResult CreateTable (hDBIDb hDb, hDBICur hCur, pCHAR szDestTblName, pCHAR szTblType, phDBICur phCurTemp) { DBIResult rslt; // Return value from IDAPI functions CRTblDesc crTblDesc; // Table descriptor pFLDDesc pfldDescs; // Field descriptor CURProps curProps; // Cursor properties // Allocate memory for the field descriptor. rslt = DbiGetCursorProps(hCur, &curProps); ChkRslt(rslt, "GetCursorProps"); pfldDescs = (pFLDDesc)malloc(curProps.iFields * sizeof(FLDDesc)); // Get information on the fields of the table. rslt = DbiGetFieldDescs(hCur, pfldDescs); ChkRslt(rslt, "GetFieldDescs"); // Initialize create table descriptor. memset((void *)&crTblDesc, 0, sizeof(crTblDesc)); strcpy(crTblDesc.szTblName, szDestTblName); strcpy(crTblDesc.szTblType, szTblType); crTblDesc.iFldCount = curProps.iFields; crTblDesc.pfldDesc = pfldDescs; // Create & open the table. rslt = DbiCreateTable(hDb, TRUE, &crTblDesc); if (ChkRslt(rslt, " CreateTable") != DBIERR_NONE) { free(pfldDescs); return rslt; } rslt = DbiOpenTable(hDb, crTblDesc.szTblName, crTblDesc.szTblType, NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL, phCurTemp); if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE) { free(pfldDescs); return rslt; } free(pfldDescs); return rslt; }