// BDE - (C) Copyright 1995 by Borland International // batmove.c #include "snipit.h" #define NAMELEN 10 // Length of the name fields. #define PLACELEN 20 // Length of the POB field. #define DATELEN 11 // Display length for a date field: mm\dd\yyyy. static const char szDBTblName[] = "DBPeople"; static const char szDBTblType[] = szDBASE; // Field descriptor used in creating a table. static SNIPFAR FLDDesc DBfldDesc[] = { { // Field 1 - First Name 1, // Field number "Name", // 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 - Place of Birth 2, "POB", fldZSTRING, fldUNKNOWN, PLACELEN, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE }, { // Field 3 - Date of Birth 3, "DOB", fldZSTRING, fldUNKNOWN, DATELEN, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE } }; // Array of field descriptors // Index descriptor - describes the index associated with the // table. static SNIPFAR IDXDesc DBidxDesc[] = { { // Primary Index - Full Name "Name", // Name 1, // Number {"NAME"}, // Tag name ( for dBase ) { NULL }, // Optional format ( BTREE, // HASH, etc ) FALSE, // Primary? FALSE, // Unique? FALSE, // Descending? TRUE, // Maintained? FALSE, // SubSet? FALSE, // Expression index? NULL, // For QBE only 1, // Fields in key 1, // Length in bytes FALSE, // Index out of date? 0, // Key type of expression { 1 }, // Array of field numbers { 0 }, // Key expression { 0 }, // Key condition FALSE, // Case insensitive 0, // Block size in bytes 0 // Restructure number }, { // Secondary index 1 - Single field "POB", 2, {"POB1"}, { NULL }, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, NULL, 1, 1, FALSE, 0, { 2 }, { 0 }, { 0 }, FALSE, 0, 0 }, { // Secondary index 2 - Single field "DOB", 3, {"DOB1"}, { NULL }, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, NULL, 1, 1, FALSE, 0, { 3 }, { 0 }, { 0 }, FALSE, 0, 0 } }; static const char szPXTblName[] = "PXPeople"; static const char szPXTblType[] = szPARADOX; // Field descriptor used in creating a table. static SNIPFAR FLDDesc PXfldDesc[] = { { // Field 1 - First Name 1, // Field number "First Name", // 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, "Middle Name", fldZSTRING, fldUNKNOWN, NAMELEN, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE }, { // Field 3 - Last Name 3, "Last Name", 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 PXidxDesc[] = { { // Primary Index - Full Name "Full Name", // Name 1, // Number { NULL }, // Tag name ( for dBase ) { NULL }, // Optional format ( BTREE, // HASH, etc ) TRUE, // Primary? TRUE, // Unique? FALSE, // Descending? TRUE, // Maintained? FALSE, // Subset? FALSE, // 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 { 0 }, // Key expression { 0 }, // Key condition FALSE, // Case insensitive 0, // Block size in bytes 0 // Restructure number }, { // Secondary Index 1 - Single field - Maintained, // Case insensitive "Last Name", 2, { NULL }, { NULL }, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, NULL, 1, 1, FALSE, 0, { 3 }, { 0 }, { 0 }, TRUE, 0, 0 }, { // Secondary Index 2 - Single field - Not maintained "POB", 3, { NULL }, { NULL }, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NULL, 1, 1, FALSE, 0, { 5 }, { 0 }, { 0 }, FALSE, 0, 0 } }; static DBIResult AddRecord(phDBICur hCur, pCHAR pszFirst, pCHAR pszMiddle, pCHAR pszLast, UINT16 iMonth, UINT16 iDay, UINT16 iYear, pCHAR pszPOB); static DBIResult AddRecordDB(phDBICur hCur, pCHAR pszFirst, pCHAR pszPOB, pCHAR pszDate); static DBIResult CreateDBTable(phDBIDb phDb); static DBIResult CreatePXTable(phDBIDb phDb); //===================================================================== // Function: // BatchMove(); // // Description: // This example shows how to use the DbiBatchMove function to // copy data from a Paradox table to a dBASE table. //===================================================================== void BatchMove (void) { hDBIDb hDb; // Handle to the database hDBICur hCurDB; // dBASE cursor handle hDBICur hCurPX; // PARADOX cursor handle UINT32 ulNumRecs = 0; // Number of records moved in BatchMove UINT32 ulChangedRecs = 0; // Number of records changed in BatchMove UINT32 ulKeyViolRecs = 0; // Number of key violations in BatchMove UINT32 ulProbRecs = 0; // Number of problem recs in BatchMove UINT16 uaSrcRecs[3] = {1,5,4}; // Fields to use in the source table DBIResult rslt; // Return value from IDAPI functions DBIPATH szKeyViol = "KEYVIOL"; DBIPATH szProblems = "PROBLEMS"; DBIPATH szChanged = "CHANGED"; // Names of temporary tables Screen("*** Table Copy 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(" Create the %s dBASE table...", szDBTblName); if (CreateDBTable(&hDb) != DBIERR_NONE) { CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } Screen(" Open the %s table we just created...", szDBTblName); rslt = DbiOpenTable(hDb, (pCHAR) szDBTblName, (pCHAR) szDBTblType, NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL, &hCurDB); if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE) { CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } rslt = DbiSetToBegin(hCurDB); ChkRslt(rslt, "SetToBegin"); Screen(" Displaying the %s table...", szDBTblName); DisplayTable(hCurDB, 0); Screen("\r\n Create the %s PARADOX table...", szPXTblName); if (CreatePXTable(&hDb) != DBIERR_NONE) { CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } Screen(" Open the %s table which we just created...", szPXTblName); rslt = DbiOpenTable(hDb, (pCHAR) szPXTblName, (pCHAR) szPXTblType, NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL, &hCurPX); if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE) { CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } rslt = DbiSetToBegin(hCurPX); ChkRslt(rslt, "SetToBegin"); Screen(" Displaying the %s table...", szPXTblName); DisplayTable(hCurPX, 0); rslt = DbiSetToBegin(hCurPX); ChkRslt(rslt, "SetToBegin"); Screen("\r\n Appending the %s table to the %s table...", szPXTblName, szDBTblName); rslt = DbiBatchMove(NULL, hCurPX, NULL, hCurDB, batAPPEND, 3, uaSrcRecs, 0, 0, 0, szKeyViol, szProblems, szChanged, &ulProbRecs, &ulKeyViolRecs, &ulChangedRecs, FALSE, FALSE, &ulNumRecs, TRUE); ChkRslt(rslt, "Batch Move"); rslt = DbiSetToBegin(hCurDB); ChkRslt(rslt, "SetToBegin"); Screen(" Displaying the updated %s table...", szDBTblName); DisplayTable(hCurDB, 0); Screen("\r\n Close the %s table...", szPXTblName); rslt = DbiCloseCursor(&hCurPX); ChkRslt(rslt, "CloseCur"); Screen(" Delete the %s table...", szPXTblName); rslt = DbiDeleteTable(hDb,(pCHAR) szPXTblName, (pCHAR) szPXTblType); ChkRslt(rslt, "DeleteTable"); Screen(" Close the %s table...", szDBTblName); rslt = DbiCloseCursor(&hCurDB); ChkRslt(rslt, "CloseCur"); Screen(" Delete the %s table...", szDBTblName); rslt = DbiDeleteTable(hDb,(pCHAR) szDBTblName, (pCHAR) szDBTblType); ChkRslt(rslt, "DeleteTable"); Screen(" Close the database and exit IDAPI..."); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); } //===================================================================== // Function: // CreateDBTable(phDb); // // Input: phDb - Pointer to the database handle // // Return: Result of the table initialization // // Description: // This function creates a dBASE table and fills the table // with a few records. //===================================================================== DBIResult CreateDBTable (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 DBfldDesc is defined // globally. const unsigned uNumFields = sizeof(DBfldDesc) / sizeof (DBfldDesc[0]); // Number of indexes to be created when the table is created. const unsigned uNumIndexes = sizeof(DBidxDesc) / sizeof(DBidxDesc[0]); // Initialize the table create descriptor. memset(&crTblDsc, 0, sizeof(CRTblDesc)); strcpy(crTblDsc.szTblName, szDBTblName); strcpy(crTblDsc.szTblType, szDBTblType); crTblDsc.iFldCount = uNumFields; crTblDsc.pfldDesc = DBfldDesc; crTblDsc.iIdxCount = uNumIndexes ; crTblDsc.pidxDesc = DBidxDesc ; // Create the table using information supplied in the table // descriptor above. rslt = DbiCreateTable(*phDb, bOverWrite, &crTblDsc); if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE) { return rslt; } // Open the table. rslt = DbiOpenTable(*phDb, (pCHAR) szDBTblName, (pCHAR) szDBTblType, NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL, &hCur); if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE) { return rslt; } // Add records to the table. AddRecordDB(&hCur, "Jeffery", "San Jose", "1/28/1967"); AddRecordDB(&hCur, "William", "New York", "8/12/1970"); AddRecordDB(&hCur, "Jimmy", "San Francisco", "3/10/1924"); AddRecordDB(&hCur, "Larry", "Germany", "12/22/1933"); rslt = DbiCloseCursor(&hCur); ChkRslt(rslt, "CloseCursor"); return rslt; } //===================================================================== // Function: // CreatePXTable(phDb); // // Input: phDb - Pointer to the database handle // // Return: Result of the table initialization // // Description: // This function will create a Paradox table and fill the // table with a few records. //===================================================================== DBIResult CreatePXTable (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. const unsigned uNumFields = sizeof(PXfldDesc) / sizeof (PXfldDesc[0]); // Number of indexes to be created when the table is created. const unsigned uNumIndexes = sizeof(PXidxDesc) / sizeof(PXidxDesc[0]); // Initialize the table create descriptor. memset(&crTblDsc, 0, sizeof(CRTblDesc)); strcpy(crTblDsc.szTblName, szPXTblName); strcpy(crTblDsc.szTblType, szPXTblType); crTblDsc.iFldCount = uNumFields; crTblDsc.pfldDesc = PXfldDesc; crTblDsc.iIdxCount = uNumIndexes; crTblDsc.pidxDesc = PXidxDesc; // Create the table using information supplied in the table // descriptor above. rslt = DbiCreateTable(*phDb, bOverWrite, &crTblDsc); if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE) { return rslt; } rslt = DbiOpenTable(*phDb, (pCHAR) szPXTblName, (pCHAR) szPXTblType, NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL, &hCur); if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE) { return rslt; } AddRecord(&hCur, "David", "John", "Smith", 7, 28, 1968, "Chicago"); AddRecord(&hCur, "Tracy", "Joy", "Thomas", 12, 27, 1969, "Hermosa Beach"); AddRecord(&hCur, "John", "Boy", "Doe", 2, 7, 1954, "Ohmaha"); AddRecord(&hCur, "Klaus", "Epstein", "Stern", 4, 13, 1970, "Tel Aviv"); rslt = DbiCloseCursor(&hCur); ChkRslt(rslt, "CloseCursor"); return rslt; } //===================================================================== // Function: // AddRecordDB(hCur, pszName, pszPOB, pszDate); // // Input: hCur - Cursor handle // pszName - Value for the name field // pszDate - Value for the date field // pszPOB - Value for the "Place of Birth" field // // Return: Result of adding the record to the table // // Description: // This function will add a record to the given dBASE table. //===================================================================== DBIResult AddRecordDB (phDBICur hCur, pCHAR pszName, pCHAR pszPOB, pCHAR pszDate) { 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, "GetProps"); pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE)); if (pRecBuf == NULL) { Screen(" Error - Out of memory"); return DBIERR_NOMEMORY; } // Clear, fill, and insert the record. rslt = DbiInitRecord(*hCur, pRecBuf); ChkRslt(rslt, "InitRecord"); rslt = DbiPutField(*hCur, 1, pRecBuf, (pBYTE) pszName); ChkRslt(rslt, "PutField"); rslt = DbiPutField(*hCur, 2, pRecBuf, (pBYTE) pszPOB); ChkRslt(rslt, "PutField"); rslt = DbiPutField(*hCur, 3, pRecBuf, (pBYTE) pszDate); ChkRslt(rslt, "PutField"); rslt = DbiInsertRecord(*hCur, dbiNOLOCK, pRecBuf); ChkRslt(rslt, "InsertRec"); free(pRecBuf); return rslt; } //===================================================================== // Function: // AddRecord(hCur, pszFirst, pszMiddle, pszLast, iMonth, iDay, // iYear, pszPOB); // // Input: hCur - Pointer to the cursor handle // pszFirst - First name // pszMiddle - Middle name // pszLast - Last name // iMonth - Month of Birth // iDay - Day of Birth // iYear - Year of Birth // pszPOB - Place of birth // // 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 iMonth, UINT16 iDay, UINT16 iYear, 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"); // Date of Birth. rslt = DbiDateEncode(iMonth, iDay, iYear, &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; }