home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / kdbf / bdatabas.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  12.8 KB  |  384 lines

  1. /**********************************************************************
  2. **
  3. **                          BDATABASE.H
  4. **
  5. ** This file defines the interface for the BDatabase class. This 
  6. ** class encapsulates the concept of a Database and defines
  7. ** database-level functions that operate on a defined collection 
  8. ** of persistent objects like tables, indexes, and so on. 
  9. **
  10. **********************************************************************/
  11.  
  12. // DBF - (C) Copyright 1994 by Borland International
  13.  
  14. #ifndef BDATABASE_H
  15. #define BDATABASE_H
  16.  
  17. #include "envdef.h"
  18. #include "bdbobjec.h"
  19. #include "blistcur.h"
  20.  
  21. class BDatabase : public BDbObject {
  22.  
  23.     friend class BEngine;
  24.     friend class BCursor;
  25.     friend class BStatement;
  26.  
  27. public:
  28.     Retcode lastError;
  29.     BOOL    isOpen;
  30.  
  31. public:
  32.  
  33.     // Constructor that initializes fields and opens the universal database.
  34.  
  35.     BDatabase(BEngine *eng,
  36.               DBIOpenMode openMode = dbiREADWRITE,
  37.               DBIShareMode shareMode = dbiOPENSHARED);
  38.  
  39.     // Constructor that initializes fields and opens the specified database.
  40.  
  41.     BDatabase(BEngine *eng,
  42.               const char *BDDriver,
  43.               const char *BDAlias = "",
  44.               const char *BDPassword  = "",
  45.               DBIOpenMode openMode = dbiREADWRITE,
  46.               DBIShareMode shareMode = dbiOPENSHARED);
  47.  
  48.     // Destructor that closes a database if open.
  49.  
  50.     virtual ~BDatabase();             
  51.  
  52.     // Open the Universal database.
  53.  
  54.     virtual Retcode open(DBIOpenMode openMode = dbiREADWRITE,
  55.                          DBIShareMode shareMode = dbiOPENSHARED);
  56.  
  57.     // Open a database for a given driver, alias and password.
  58.  
  59.     virtual Retcode open(const char *BDDriver,
  60.                          const char *BDAlias = "",
  61.                          const char *BDPassword = "",
  62.                          DBIOpenMode openMode = dbiREADWRITE,
  63.                          DBIShareMode shareMode = dbiOPENSHARED);
  64.  
  65.     // Close the database.
  66.  
  67.     virtual Retcode close(void);
  68.  
  69.     // See if a table exists in the database.
  70.  
  71.     BOOL tableExists(const char *tableName);
  72.  
  73.     // Create a table from the table descriptor(number of fields and the
  74.     // field descriptor array).
  75.  
  76.     Retcode createTable(const char *tableName,
  77.                         int numFields, const FieldDesc *desc,
  78.                         ValidityDesc *valDesc = NULL,
  79.                         RefDesc *refDesc = NULL);
  80.  
  81.     // Copy one table family to another.
  82.  
  83.     Retcode copyTable(const char *srcTable, const char *destTable,
  84.                       const char *destType = NULL);
  85.  
  86.     // Append records of the source table to the destination table.
  87.  
  88.     Retcode appendTable(const char *srcTableName,
  89.                         const char *destTableName,
  90.                         const char *destType = NULL,
  91.                         const BDatabase &db = NULL);
  92.  
  93.     // Rename a table and its family members.
  94.  
  95.     Retcode renameTable(const char *oldName, const char *newName);
  96.  
  97.     // Delete a table and its family.
  98.  
  99.     Retcode deleteTable(const char *tableName);
  100.  
  101.     // Encrypt a table based on a supplied password.
  102.  
  103.     Retcode encryptTable(const char *tableName, const char *password);
  104.  
  105.     // Decrypt a table using a password.
  106.  
  107.     Retcode decryptTable(const char *tableName);
  108.  
  109.     // Empty a table by deleting all its records.
  110.  
  111.     Retcode emptyTable(const char *tableName);
  112.  
  113.     // Upgrade the table from Paradox 3.5 to Paradox 4.0 format.
  114.  
  115.     Retcode upgradeTable(const char *tableName);
  116.  
  117.     // Restructure the table
  118.     
  119.     Retcode restructureTable(CRTblDesc &tblDesc,
  120.                              const char *tableName);
  121.  
  122.     // Find out if the table is protected; if it is, return TRUE.
  123.     // In all other cases, return FALSE.
  124.  
  125.     BOOL isProtected(const char *tableName);
  126.  
  127.     // Return the number of fields (columns) in the table. Return
  128.     // zero if not successful.
  129.  
  130.     int getFieldCount(const char *tableName);
  131.  
  132.     // Given a table, return the number of fields and an array of
  133.     // its field descriptors. The caller is responsible for
  134.     // deleting the free store memory allocated for the field
  135.     // descriptors. Descriptors are always allocated in the far
  136.     // heap, even under Windows medium memory model.
  137.  
  138.     Retcode getDescVector(char *tableName,
  139.                           int& numFields, FieldDesc *&desc);
  140.  
  141.     // Given a table, return the number of indexes and an array of
  142.     // its index descriptors. The caller is responsible for
  143.     // deleting the free store memory allocated for the index
  144.     // descriptors. Descriptors are always allocated in the far
  145.     // heap, even under Windows medium memory model.
  146.  
  147.     Retcode getIndexVector(char *tableName,
  148.                            int& numIndexes,
  149.                            IDXDesc *&desc);
  150.  
  151.     // Given an ordered list of fields in the table, create
  152.     // a composite index on those fields. Returns an
  153.     // indexID. Only valid for Paradox tables
  154.  
  155.     Retcode defineCompoundKey(const char *tableName,
  156.                               int numFields,
  157.                               const FIELDNUMBER *fldArray,
  158.                               const char *indexName,
  159.                               BOOL caseSen,
  160.                               FIELDNUMBER& outH);
  161.  
  162.     // Given an ordered list of fields in the table, create
  163.     // a composite index on those fields
  164.  
  165.     Retcode defineCompoundKey(const char *tableName,
  166.                               int numFields,
  167.                               const FIELDNUMBER *fldArray,
  168.                               const char *indexName,
  169.                               BOOL caseSen = TRUE);
  170.  
  171.     // Create a secondary index on the table. If the field handle
  172.     // passed is greater than 255, it must be a number obtained
  173.     // from the defineCompoundKey function.
  174.  
  175.     Retcode createSIndex(const char *tableName,
  176.                          FIELDNUMBER fieldH,
  177.                          PXKeyCrtMode keyMode);
  178.  
  179.     // Create a primary key on the table using the first numFields
  180.     // fields as the key.
  181.  
  182.     Retcode createPIndex(const char *tableName, int numFields);
  183.  
  184.     // Create an index using an index Descriptor
  185.     
  186.     Retcode createIndex(const char *tableName, IDXDesc &indexDesc);
  187.  
  188.     // Delete an index on the table. indexID must be zero for the
  189.     // primary key.
  190.  
  191.     Retcode dropIndex(const char *tableName, FIELDNUMBER indexID);
  192.  
  193.     // Delete an index on the table. Uses the indexName  to specify the
  194.     // index
  195.  
  196.     Retcode dropIndex(const char *tableName, const char *indexName);
  197.  
  198.     // Delete an index on the table. Uses the indexName and indexTagName
  199.     // to specify the index
  200.  
  201.     Retcode dropIndex(const char *tableName, const char *indexName,
  202.                       const char *indexTagName);
  203.  
  204.     // Return the number of primary key fields for the table; if a primary
  205.     // key does not exist or under all other circumstances, returns zero.
  206.  
  207.     int getNumPFields(const char *tableName);
  208.  
  209.     // Given the name of a composite secondary index, return information
  210.     // about it.
  211.  
  212.     Retcode getSKeyInfo(const char *indexName,
  213.                         char *fldName, int& numFields,
  214.                         BOOL& caseSen, FIELDNUMBER *fldArray,
  215.                         FIELDNUMBER& indexID);
  216.  
  217.     // Return information on a single field secondary index.
  218.  
  219.     Retcode getSKeyInfo(const char *indexName,
  220.                         char *fldName, BOOL& caseSen,
  221.                         FIELDNUMBER& indexID);
  222.  
  223.     // Acquire a file name lock on the network; File need not
  224.     // physically exist.
  225.  
  226.     Retcode lockNetFile(const char *fileName, PXLockMode lockMode);
  227.  
  228.     // Release a previously acquired lock on a file.
  229.  
  230.     Retcode unlockNetFile(const char *fileName, PXLockMode lockMode);
  231.  
  232.     // Write the changed record buffers associated with this
  233.     // Engine instance to be written to disk.
  234.  
  235.     Retcode forceWrite(void);
  236.  
  237.     // Return the name of the user causing a locking error.
  238.  
  239.     char  *getNetErrUser(void);
  240.  
  241.     // Perform a query on the table
  242.  
  243.     Retcode query(const char *qryString, BCursor &answer,
  244.                   DBIQryLang qryLang = qrylangSQL);
  245.  
  246.     // Get the type of the Database, i.e. dBASE, Paradox, Interbase, etc.
  247.  
  248.     const char *getDriver(void);
  249.  
  250.     // Start a transaction - Supported for SQL tables only
  251.     
  252.     Retcode beginTransaction(hDBIXact &tranHandle, eXILType tranType);
  253.  
  254.     // End a transaction - supported for SQL tables only
  255.     
  256.     Retcode endTransaction(eXEnd tranType);
  257.  
  258.     // Get information about a transaction - supported for SQL tables only
  259.     
  260.     Retcode getTransactionInfo(XInfo &tranInfo);
  261.  
  262.     // Set a property of the Database
  263.  
  264.     Retcode setProp(UINT32 prop, UINT32 value);
  265.  
  266.     // Get a property of the Database
  267.  
  268.     Retcode getProp(UINT32 prop, pVOID value, UINT16 maxLen,
  269.                     UINT16 &retLen);
  270.  
  271.     // Get the ID for the specified indexName
  272.  
  273.     Retcode getIndexId(const char *tableName,
  274.                        const char *indexName,
  275.                        UINT16 &indexID);
  276.  
  277.     // Get the ID for the specified indexName
  278.  
  279.     Retcode getIndexId(const char *tableName,
  280.                        const FIELDNUMBER *fldArray,
  281.                        UINT16 &indexID);
  282.  
  283.     // Sets the working directory for the Database - only
  284.     // supported for local tables
  285.                                    
  286.     Retcode setDirectory(const char *Directory);
  287.  
  288.     // Get the current working directory
  289.  
  290.     Retcode getDirectory(char *Directory);
  291.  
  292.     // Get the default directory
  293.  
  294.     Retcode getDefaultDirectory(char *Directory);
  295.  
  296.     // dBASE only. Pack the table. Regenerate indexes.
  297.  
  298.     Retcode packTable(const char *tableName, BOOL regenIndexes = TRUE);
  299.  
  300.     // Get the handle of the Database. WARNING: be carefull when
  301.     // using the DATABASEHANDLE outside the class.
  302.  
  303.     DATABASEHANDLE getHandle(void);
  304.     
  305.     // Return an array of field descriptors. numFields needs to be
  306.     // determined prior to calling this function. fldDescs needs to
  307.     // be allocated before calling this function.
  308.  
  309.     Retcode getDesc(const char *tableName, int& numFields,
  310.                     FieldDesc far *fldDescs);
  311.  
  312.     // Return an array of index Descriptors                            
  313.     Retcode getIndexDesc(const char *tableName, int& numIndexes,
  314.                          IDXDesc far *idxDescs);
  315.  
  316.     // Reset the list of valid tables for the database
  317.  
  318.     Retcode resetDBTableList();
  319.  
  320.     Retcode openTableList(BListCursor<TBLBaseDesc> &tableList,
  321.                           const char *wildCard = "*.*",
  322.                           BOOL system = TRUE);
  323.  
  324.     Retcode openTableList(BListCursor<TBLFullDesc> &tableList,
  325.                           const char *wildCard = "*.*",
  326.                           BOOL system = TRUE);
  327.  
  328.     Retcode openFileList(BListCursor<FILEDesc> &fileList,
  329.                          const char *wildCard = "*.*");
  330.  
  331.     Retcode openIndexList(BListCursor<IDXDesc> &indexList,
  332.                           const char *tableName,
  333.                           const char *tableType = NULL);
  334.  
  335.     Retcode openFieldList(BListCursor<FLDDesc> &fieldList,
  336.                           const char *tableName,
  337.                           const char *tableType = NULL,
  338.                           BOOL physicalTypes = FALSE);
  339.  
  340.     Retcode openFamilyList(BListCursor<FMLDesc> &familyList,
  341.                            const char *tableName,
  342.                            const char *tableType = NULL);
  343.  
  344.     Retcode openVchkList(BListCursor<VCHKDesc> &vchkList,
  345.                          const char *tableName,
  346.                          const char *tableType = NULL);
  347.  
  348.     Retcode openRintList(BListCursor<RINTDesc> &rintList,
  349.                          const char *tableName,
  350.                          const char *tableType = NULL);
  351.  
  352.     Retcode openSecurityList(BListCursor<SECDesc> &securityList,
  353.                              const char *tableName,
  354.                              const char *tableType = NULL);
  355.  
  356.     // Redefine pure virtuals from the BDbObject class.
  357.  
  358.     virtual char *nameOf() const;
  359.     virtual void printOn(ostream& os);
  360.  
  361. protected:
  362.     DATABASEHANDLE  hDb;
  363.     BListCursor<TBLFullDesc> TableList;
  364.     BOOL            isLocal;
  365.     char            userName[DBIMAXUSERNAMELEN+1];
  366.     char            Alias[DBIMAXNAMELEN+1];
  367.     char            Driver[DBIMAXNAMELEN+1];
  368.     char            Password[DBIMAXNAMELEN+1];
  369.  
  370.     // Don't allow the copy constructor to be called unless specifically
  371.     //   defined
  372.     BDatabase(BDatabase ©Cur);
  373.     // Don't allow the assignment opperator to be called unless specifically
  374.     //   defined
  375.  
  376.     BDatabase& operator=(BDatabase ©DB);
  377.  
  378. private:
  379.     void    *dbobj;             // Tracks objects of the database.
  380. };
  381.  
  382. #endif 
  383.  
  384.