home *** CD-ROM | disk | FTP | other *** search
/ Total C++ 2 / TOTALCTWO.iso / borland / 32snipit.pak / CR8TXTBL.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  12KB  |  318 lines

  1. // BDE32 3.x - (C) Copyright 1996 by Borland International
  2.  
  3. // cr8txtbl.c
  4. #include "snipit.h"
  5.  
  6. static const char szTblName[] = "texttbl";
  7. static const char szTblType[] = szASCII;
  8.  
  9. // Field descriptor used in creating a table.
  10. static SNIPFAR FLDDesc fldDesc[] = {
  11.               { // Field 1 - CHARACTER
  12.                 1,            // Field number
  13.                 "CHARACTER",  // Field name
  14.                 fldZSTRING,   // Field type
  15.                 fldUNKNOWN,   // Field subtype
  16.                 10,           // Field size ( 1 or 0, except
  17.                               //     BLOB, CHAR fields, Text FLOAT,
  18.                               //     NUMERIC fields )
  19.                 0,            // Decimal places ( 0 )
  20.                               //     computed
  21.                 0,            // Offset in record ( 0 )
  22.                 0,            // Length in bytes  ( 0 )
  23.                 0,            // Null offset    ( 0 )
  24.                 fldvNOCHECKS, // Validity checks   ( 0 )
  25.                 fldrREADWRITE // Rights
  26.               },
  27.               { // Field 2 - SHORT
  28.                 2, "SHORT", fldINT16, fldUNKNOWN,
  29.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  30.               },
  31.               { // Field 3 - NUMERIC
  32.                 3, "NUMERIC", fldFLOAT, fldUNKNOWN,
  33.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  34.               },
  35.               { // Field 4 - DATE
  36.                 4, "DATE", fldDATE, fldUNKNOWN,
  37.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  38.               },
  39.               { // Field 5 - LOGICAL
  40.                 5, "LOGICAL", fldBOOL, fldUNKNOWN,
  41.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  42.               },
  43.               { // Field 6 - TIME
  44.                 6, "TIME", fldTIME, fldUNKNOWN,
  45.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  46.               },
  47.               { // Field 7 - TIMESTAMP
  48.                 7, "TIMESTAMP", fldTIMESTAMP, fldUNKNOWN,
  49.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  50.               },
  51.               { // Field 8 - LONG
  52.                 8, "LONG", fldINT32, fldUNKNOWN,
  53.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  54.               },
  55.               { // Field 9 - MONEY
  56.                 9, "MONEY", fldFLOAT, fldstMONEY,
  57.                 0, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  58.               }
  59.             };  // Array of field descriptors
  60.  
  61. // The number of fields in the table.
  62. static const UINT16 uNumFields = sizeof(fldDesc) / sizeof (fldDesc[0]);
  63.  
  64. static DBIResult FillTextTable(hDBIDb hDb, pCHAR szTblName,
  65.                                pCHAR szTblType, FLOAT NumRecs,
  66.                                pFLDDesc pfldDesc, UINT16 uNumFields);
  67.  
  68. //=====================================================================
  69. //  Function:
  70. //          CreateAndFillSampleTXT();
  71. //
  72. //  Description:
  73. //          This sample code will create a text table, insert
  74. //          10 records into the table, then delete the table.
  75. //
  76. //          Note that the fields of a text table are set using the
  77. //          DbiSetFieldMap function.  It is required that the field
  78. //          descriptor used when setting the fields of a text table
  79. //          contains the physical field types.
  80. //=====================================================================
  81. void
  82. CreateAndFillSampleTXT (void)
  83. {
  84.     hDBIDb      hDb;                // Handle to the database
  85.     hDBICur     hCur;               // Handle to the table
  86.     CRTblDesc   TblDesc;            // Create table descriptor
  87.     UINT16      uDispNumRecs = 10;  // Number of records to add and
  88.                                     // display.
  89.     DBIResult   rslt;               // Return value from IDAPI functions
  90.     pFLDDesc    pTXTFlds;           // Contains the physical field mappings
  91.  
  92.     Screen("*** Create/Open/Fill Text Table Example ***\r\n");
  93.  
  94.     BREAK_IN_DEBUGGER();
  95.  
  96.     Screen("    Initializing IDAPI...");
  97.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  98.     {
  99.         Screen("\r\n*** End of Example ***");
  100.         return;
  101.     }
  102.  
  103.     Screen("    Setting the database directory...");
  104.     rslt = DbiSetDirectory(hDb, (pCHAR)szTblDirectory);
  105.     ChkRslt(rslt, "SetDirectory");
  106.  
  107.     Screen("    Initializing the table descriptor...");
  108.     memset((void *) &TblDesc , 0, sizeof(CRTblDesc));
  109.     lstrcpy(TblDesc.szTblName, szTblName);
  110.     lstrcpy(TblDesc.szTblType, szTblType);
  111.  
  112.     Screen("    Creating the text table...");
  113.     rslt = DbiCreateTable(hDb, TRUE, &TblDesc);
  114.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  115.     {
  116.         CloseDbAndExit(&hDb);
  117.         Screen("\r\n*** End of Example ***");
  118.         return;
  119.     }
  120.  
  121.     // Note that the table type has to be specified with the separator.
  122.     Screen("    Fill the table with Random Data...");
  123.     FillTextTable(hDb, (pCHAR)szTblName, "ASCIIDRV-\"-,", uDispNumRecs,
  124.                   fldDesc, uNumFields);
  125.  
  126.  
  127.     // Note that the table type has to be specified with the delimiter
  128.     //   and separator as we are opening the tables as a delimited text
  129.     //   file.
  130.     Screen("    Open the %s table....", szTblName);
  131.     rslt = DbiOpenTable(hDb, (pCHAR)szTblName, "ASCIIDRV-\"-,",
  132.                         NULL, NULL, 0, dbiREADWRITE,
  133.                         dbiOPENSHARED, xltFIELD, FALSE, NULL, &hCur);
  134.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  135.     {
  136.         CloseDbAndExit(&hDb);
  137.         Screen("\r\n*** End of Example ***");
  138.         return;
  139.     }
  140.  
  141.     pTXTFlds = (pFLDDesc) malloc(uNumFields * sizeof(FLDDesc));
  142.     if (pTXTFlds == NULL)
  143.     {
  144.         CloseDbAndExit(&hDb);
  145.         Screen("\r\n*** End of Example ***");
  146.         return;
  147.     }
  148.  
  149.     // Convert the record structure to the text table format (physical).
  150.     rslt = DbiTranslateRecordStructure(NULL, uNumFields, fldDesc,
  151.                                        szASCII, NULL, pTXTFlds, FALSE);
  152.     ChkRslt(rslt, "TranslateRecordStruct");
  153.  
  154.     Screen("    Use a field map to specify the fields of a"
  155.            " Text table...");
  156.     rslt = DbiSetFieldMap(hCur, uNumFields, pTXTFlds);
  157.     if (ChkRslt(rslt, "SetFieldMap") != DBIERR_NONE)
  158.     {
  159.         rslt = DbiCloseCursor(&hCur);
  160.         ChkRslt(rslt, "CloseCursor");
  161.         free(pTXTFlds);
  162.         CloseDbAndExit(&hDb);
  163.         Screen("\r\n*** End of Example ***");
  164.         return;
  165.     }
  166.  
  167.     Screen("    Display the %s table which we just created...", szTblName);
  168.     DisplayInMemoryTable(hCur, uDispNumRecs);
  169.  
  170.     Screen("\r\n    Close the %s table...", szTblName);
  171.     rslt = DbiCloseCursor(&hCur);
  172.     ChkRslt(rslt, "CloseCursor");
  173.  
  174.     Screen("    Close the database and exit IDAPI...");
  175.     CloseDbAndExit(&hDb);
  176.  
  177.     free(pTXTFlds);
  178.  
  179.     Screen("\r\n*** End of Example ***");
  180. }
  181.  
  182. //=====================================================================
  183. //  Function:
  184. //          FillTextTable(hDb, szTblName, szTblType, NumRecs, fldDesc);
  185. //
  186. //  Input:  hDb                 - The database handle
  187. //          szTblName           - Name of the table to fill
  188. //          szTblTyps           - Type of the table to fill
  189. //          NumRecs             - The number of records to insert
  190. //          fldDesc             - Field descriptor for the table
  191. //          UINT16 uNumFields   - Number of fields in the Field Desc
  192. //
  193. //  Return: DBIResult - success?
  194. //
  195. //  Description:
  196. //          This function adds the specified number of records to
  197. //          the table containing random data.
  198. //=====================================================================
  199. DBIResult
  200. FillTextTable (hDBIDb hDb, pCHAR szTblName, pCHAR szTblType, FLOAT NumRecs,
  201.                pFLDDesc pfldDesc, UINT16 uNumFields)
  202. {
  203.     DBIResult   rslt;           // Return value from IDAPI functions
  204.     FLOAT       fRecCount;      // Loop variable = count of records
  205.     UINT16      FldCntr;        // Field counter
  206.     pBYTE       pRecBuf;        // Pointer to the record buffer
  207.     CURProps    TblProps;       // Table descriptor
  208.     hDBICur     hCur;           // Handle to the table
  209.     pFLDDesc    pTXTFlds;       // Contains the physical field mappings
  210.     pFLDDesc    pLogFlds;       // Descriptor for the logical field mappings
  211.  
  212.     Screen("    Inserting %.0f records into the table...", NumRecs);
  213.  
  214.     rslt = DbiOpenTable(hDb, szTblName, szTblType, NULL,
  215.                         NULL, 0, dbiREADWRITE,
  216.                         dbiOPENSHARED, xltFIELD, FALSE,
  217.                         NULL, &hCur);
  218.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  219.     {
  220.         return rslt;
  221.     }
  222.  
  223.     pTXTFlds = (pFLDDesc) malloc(uNumFields * sizeof(FLDDesc));
  224.     if (pTXTFlds == NULL)
  225.     {                                                        
  226.         rslt = DbiCloseCursor(&hCur);
  227.         ChkRslt(rslt, "CloseCursor");
  228.         return DBIERR_NOMEMORY;
  229.     }
  230.  
  231.     // Field map has to be set using a field descriptor containing
  232.     //   physical field types.
  233.     rslt = DbiTranslateRecordStructure(NULL, uNumFields, pfldDesc,
  234.                                        szASCII, NULL, pTXTFlds, FALSE);
  235.     if (ChkRslt(rslt, "TranslateRecordStruct") != DBIERR_NONE)
  236.     {
  237.         free(pTXTFlds);
  238.         rslt = DbiCloseCursor(&hCur);
  239.         ChkRslt(rslt, "CloseCursor");
  240.         return rslt;
  241.     }
  242.  
  243.     // Use a field map to specify the fields of a text table.
  244.     rslt = DbiSetFieldMap(hCur, uNumFields, pTXTFlds);
  245.     if (ChkRslt(rslt, "SetFieldMap") == DBIERR_NONE);
  246.     {
  247.         //  Allocate a record buffer
  248.         rslt = DbiGetCursorProps(hCur, &TblProps);
  249.         ChkRslt(rslt, "GetCursorProps");
  250.  
  251.         pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE));
  252.         pLogFlds = (pFLDDesc) malloc(TblProps.iFields * sizeof(FLDDesc));
  253.         if ((pRecBuf == NULL) || (pLogFlds == NULL))
  254.  
  255.         {
  256.             if (pRecBuf) free(pRecBuf);
  257.             if (pLogFlds) free(pLogFlds);
  258.             free(pTXTFlds);
  259.             Screen("    Error - Out of memory.");
  260.             rslt = DbiCloseCursor(&hCur);
  261.             ChkRslt(rslt, "CloseCursor");
  262.             return DBIERR_NOMEMORY;
  263.         }
  264.  
  265.         rslt = DbiGetFieldDescs(hCur, pLogFlds);
  266.         if (ChkRslt(rslt, "GetFieldDescs") != DBIERR_NONE)
  267.         {
  268.             free(pRecBuf);
  269.             free(pTXTFlds);
  270.             free(pLogFlds);
  271.             rslt = DbiCloseCursor(&hCur);
  272.             ChkRslt(rslt, "CloseCursor");
  273.             return rslt;
  274.         }
  275.  
  276.         for (fRecCount = 0; fRecCount < NumRecs; fRecCount++)
  277.         {
  278.             //  Make sure we're starting with a clean record buffer
  279.             rslt = DbiInitRecord(hCur, pRecBuf);
  280.             ChkRslt(rslt, "InitRecord");
  281.  
  282.             // This loop will drive what type of data is inserted into
  283.             //   the table.
  284.             for (FldCntr = 0; FldCntr < uNumFields; FldCntr++)
  285.             {
  286.                 // Put field data into the record buffer.
  287.                 PutFieldSample(hCur, pRecBuf, (UINT16)(FldCntr+1), &pLogFlds[FldCntr]);
  288.             }
  289.  
  290.             // All fields have been inserted into the record buffer.
  291.             //   Append the record to the table.
  292.             rslt = DbiAppendRecord(hCur, pRecBuf);
  293.             ChkRslt(rslt, "InsertRecord");
  294.  
  295.             for (FldCntr = 0; FldCntr < uNumFields; FldCntr++)
  296.             {
  297.                 if (pLogFlds[FldCntr].iFldType == fldBLOB)
  298.                 {
  299.                     rslt = DbiFreeBlob(hCur, pRecBuf, FldCntr);
  300.                     ChkRslt(rslt, "FreeBlob");
  301.                 }
  302.             }
  303.         }
  304.  
  305.         free(pRecBuf);
  306.         free(pLogFlds);
  307.     }
  308.  
  309.     rslt = DbiCloseCursor(&hCur);
  310.     ChkRslt(rslt, "CloseCursor");
  311.  
  312.     free(pTXTFlds);
  313.  
  314.     return DBIERR_NONE;
  315. }
  316.  
  317.  
  318.