home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / business / printcar / printcar.exe / src / DB / Database.cc < prev    next >
C/C++ Source or Header  |  2000-06-10  |  3KB  |  126 lines

  1. //
  2. //  $Id: Database.cc,v 1.4 2000/06/10 00:20:01 sergey Exp $
  3. //
  4.  
  5. #include <Pilot.h>
  6. #include "Database.h"
  7. #include "AppInfo.h"
  8. #include "Record.h"
  9. #include "DbError.h"
  10. #include "Util/MemBuffer.h"
  11. #include "Util/Assert.h"
  12.  
  13.  
  14. namespace DB
  15. {
  16.     Database::Database():
  17.         _cardNo(0),
  18.         _openRef(0)
  19.     {}
  20.  
  21.     Database::~Database()
  22.     {
  23.         close();
  24.     }
  25.  
  26.     // operations
  27.  
  28.     bool Database::open(int cardNo, const char* databaseName, int mode)
  29.     {
  30.         assert(!isOpened());
  31.  
  32.         _cardNo = cardNo;
  33.  
  34.         LocalID id = DmFindDatabase(cardNo, (const CharPtr)databaseName);
  35.         if (id == 0)
  36.         {
  37.             DbError::openError(__FILE__, __LINE__, databaseName, DmGetLastErr());
  38.             return false;
  39.         }
  40.  
  41.         _openRef = DmOpenDatabase(cardNo, id, mode);
  42.         if (_openRef == 0)
  43.         {
  44.             DbError::openError(__FILE__, __LINE__, databaseName, DmGetLastErr());
  45.             return false;
  46.         }
  47.  
  48.         return true;
  49.     }
  50.  
  51.     void Database::close()
  52.     {
  53.         if (_openRef != 0)
  54.         {
  55.             Err error = DmCloseDatabase(_openRef);
  56.             if (error != 0)
  57.                 DbError::closeError(__FILE__, __LINE__, error);
  58.  
  59.             _openRef = 0;
  60.         }
  61.     }
  62.  
  63.     bool Database::readAppInfo(AppInfo& appInfo) const
  64.     {
  65.         assert(isOpened());
  66.  
  67.         LocalID localID = DmGetAppInfoID(_openRef);
  68.         if (localID != 0)
  69.         {
  70.             void* buffer = MemLocalIDToLockedPtr(localID, _cardNo);
  71.  
  72.             bool rc = appInfo.restore(buffer);
  73.  
  74.             if (MemLocalIDKind(localID) == memIDHandle)
  75.                 MemPtrUnlock(buffer);
  76.  
  77.             if (rc)
  78.                 return true;
  79.         }
  80.  
  81.         DbError::readAppInfoError(__FILE__, __LINE__, DmGetLastErr());
  82.         return false;
  83.     }
  84.  
  85.     int Database::recordCount() const
  86.     {
  87.         return isOpened()? DmNumRecords(_openRef) : 0;
  88.     }
  89.  
  90.     bool Database::readRecord(int index, Record& record) const
  91.     {
  92.         assert(isOpened());
  93.  
  94.         VoidHand recordHandle = DmQueryRecord(_openRef, index);
  95.         if (recordHandle == 0)
  96.         {
  97.             DbError::readRecordError(__FILE__, __LINE__, DmGetLastErr());
  98.             return false;
  99.         }
  100.  
  101.         return record.restore(index, recordHandle);
  102.     }
  103.  
  104.     // attributes
  105.  
  106.     bool Database::isDeletedRecord(int index) const
  107.     {
  108.         if (isRecord(index))
  109.         {
  110.             unsigned short attributes;
  111.  
  112.             Err err = DmRecordInfo(_openRef, index, &attributes, 0, 0);
  113.             if (err != 0)
  114.             {
  115.                 DbError::getRecordInfoError(__FILE__, __LINE__, DmGetLastErr());
  116.                 return false;
  117.             }
  118.  
  119.             return (attributes & dmRecAttrDelete) != 0;
  120.         }
  121.  
  122.         return false;
  123.     }
  124. }
  125. // namespace DB
  126.