Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

MyDb.cpp

00001 #include "MyDb.hpp"
00002 
00003 // File: MyDb.cpp
00004 
00005 // Class constructor. Requires a path to the location
00006 // where the database is located, and a database name
00007 MyDb::MyDb(std::string &path, std::string &dbName,
00008            bool isSecondary)
00009     : db_(NULL, 0),               // Instantiate Db object
00010       dbFileName_(path + dbName), // Database file name
00011       cFlags_(DB_CREATE)          // If the database doesn't yet exist,
00012                                   // allow it to be created.
00013 {
00014     try
00015     {
00016         // Redirect debugging information to std::cerr
00017         db_.set_error_stream(&std::cerr);
00018 
00019         // If this is a secondary database, support
00020         // sorted duplicates
00021         if (isSecondary)
00022             db_.set_flags(DB_DUPSORT);
00023 
00024         // Open the database
00025         db_.open(NULL, dbFileName_.c_str(), NULL, DB_BTREE, cFlags_, 0);
00026     }
00027     // DbException is not a subclass of std::exception, so we
00028     // need to catch them both.
00029     catch(DbException &e)
00030     {
00031         std::cerr << "Error opening database: " << dbFileName_ << "\n";
00032         std::cerr << e.what() << std::endl;
00033     }
00034     catch(std::exception &e)
00035     {
00036         std::cerr << "Error opening database: " << dbFileName_ << "\n";
00037         std::cerr << e.what() << std::endl;
00038     }
00039 }
00040 
00041 // Private member used to close a database. Called from the class
00042 // destructor.
00043 void
00044 MyDb::close()
00045 {
00046     // Close the db
00047     try
00048     {
00049         db_.close(0);
00050         std::cout << "Database " << dbFileName_
00051                   << " is closed." << std::endl;
00052     }
00053     catch(DbException &e)
00054     {
00055             std::cerr << "Error closing database: " << dbFileName_ << "\n";
00056             std::cerr << e.what() << std::endl;
00057     }
00058     catch(std::exception &e)
00059     {
00060         std::cerr << "Error closing database: " << dbFileName_ << "\n";
00061         std::cerr << e.what() << std::endl;
00062     }
00063 }

Generated on Sun Dec 25 12:14:25 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2