home *** CD-ROM | disk | FTP | other *** search
- //
- // $Id: Database.cc,v 1.4 2000/06/10 00:20:01 sergey Exp $
- //
-
- #include <Pilot.h>
- #include "Database.h"
- #include "AppInfo.h"
- #include "Record.h"
- #include "DbError.h"
- #include "Util/MemBuffer.h"
- #include "Util/Assert.h"
-
-
- namespace DB
- {
- Database::Database():
- _cardNo(0),
- _openRef(0)
- {}
-
- Database::~Database()
- {
- close();
- }
-
- // operations
-
- bool Database::open(int cardNo, const char* databaseName, int mode)
- {
- assert(!isOpened());
-
- _cardNo = cardNo;
-
- LocalID id = DmFindDatabase(cardNo, (const CharPtr)databaseName);
- if (id == 0)
- {
- DbError::openError(__FILE__, __LINE__, databaseName, DmGetLastErr());
- return false;
- }
-
- _openRef = DmOpenDatabase(cardNo, id, mode);
- if (_openRef == 0)
- {
- DbError::openError(__FILE__, __LINE__, databaseName, DmGetLastErr());
- return false;
- }
-
- return true;
- }
-
- void Database::close()
- {
- if (_openRef != 0)
- {
- Err error = DmCloseDatabase(_openRef);
- if (error != 0)
- DbError::closeError(__FILE__, __LINE__, error);
-
- _openRef = 0;
- }
- }
-
- bool Database::readAppInfo(AppInfo& appInfo) const
- {
- assert(isOpened());
-
- LocalID localID = DmGetAppInfoID(_openRef);
- if (localID != 0)
- {
- void* buffer = MemLocalIDToLockedPtr(localID, _cardNo);
-
- bool rc = appInfo.restore(buffer);
-
- if (MemLocalIDKind(localID) == memIDHandle)
- MemPtrUnlock(buffer);
-
- if (rc)
- return true;
- }
-
- DbError::readAppInfoError(__FILE__, __LINE__, DmGetLastErr());
- return false;
- }
-
- int Database::recordCount() const
- {
- return isOpened()? DmNumRecords(_openRef) : 0;
- }
-
- bool Database::readRecord(int index, Record& record) const
- {
- assert(isOpened());
-
- VoidHand recordHandle = DmQueryRecord(_openRef, index);
- if (recordHandle == 0)
- {
- DbError::readRecordError(__FILE__, __LINE__, DmGetLastErr());
- return false;
- }
-
- return record.restore(index, recordHandle);
- }
-
- // attributes
-
- bool Database::isDeletedRecord(int index) const
- {
- if (isRecord(index))
- {
- unsigned short attributes;
-
- Err err = DmRecordInfo(_openRef, index, &attributes, 0, 0);
- if (err != 0)
- {
- DbError::getRecordInfoError(__FILE__, __LINE__, DmGetLastErr());
- return false;
- }
-
- return (attributes & dmRecAttrDelete) != 0;
- }
-
- return false;
- }
- }
- // namespace DB
-