📄 passworddbms.cpp
字号:
#include <badesca.h> // CDesCArrayFlat
#include <s32file.h> // CFileStore & CPermanentFileStore
#include <bautils.h> // file helpers
#include <eikenv.h>
#include "PasswordDBMS.h"
const int KCustomSqlMaxLength = 256;
const int KArrayGranularity = 5; // for CDesCArrayFlat
CPasswordDb* CPasswordDb::NewL()
{
CPasswordDb* tmp = new (ELeave)CPasswordDb();
CleanupStack::PushL(tmp);
tmp->ConstructL();
CleanupStack::Pop();
return tmp;
}
// ---------------------------------------------------------------------------
// CBookDb::~CBookDb()
//
// Destructor of the Book database engine. Release resources.
// ---------------------------------------------------------------------------
CPasswordDb::~CPasswordDb()
{
Close(); // Just in case, if the user does not close this explicitely
iFsSession.Close();
}
// ---------------------------------------------------------------------------
// CBookDb::ConstructL()
//
// Second phase construction. Leaves, if RFs session cannot be created.
// ---------------------------------------------------------------------------
void CPasswordDb::ConstructL()
{
TInt err = iFsSession.Connect();
if(err)
User::Leave(err);
}
// ---------------------------------------------------------------------------
// CBookDb::CBookDb()
//
// Constructor
// ---------------------------------------------------------------------------
CPasswordDb::CPasswordDb()
{
iOpen = EFalse;
}
// ---------------------------------------------------------------------------
// CBookDb::OpenDbL()
//
// Open existing Book database for exclusive access.
// ---------------------------------------------------------------------------
TInt CPasswordDb::OpenDb(const TFileName& aExistingPasswordFile)
{
Close();
if(!BaflUtils::FileExists(iFsSession, aExistingPasswordFile))
{
return KErrNotFound;
}
TRAPD(error,
iFileStore = CPermanentFileStore::OpenL(iFsSession, aExistingPasswordFile,
EFileRead|EFileWrite);
iFileStore->SetTypeL(iFileStore->Layout());/* Set file store type*/
iPasswordDb.OpenL(iFileStore,iFileStore->Root())
);
if(error!=KErrNone)
{
return error;
}
iOpen = ETrue;
return KErrNone;
}
RDbStoreDatabase CPasswordDb::GetDb()
{
return iPasswordDb;
}
// ---------------------------------------------------------------------------
// CBookDb::CreateDbL()
//
// Create a new database. The database will be in exclusive access mode.
// ---------------------------------------------------------------------------
TInt CPasswordDb::CreateDb(const TFileName& aNewPasswordFile)
{
Close();
// Create empty database file.
TRAPD(error,
iFileStore = CPermanentFileStore::ReplaceL(iFsSession, aNewPasswordFile,
EFileRead|EFileWrite);
iFileStore->SetTypeL(iFileStore->Layout());// Set file store type
TStreamId id = iPasswordDb.CreateL(iFileStore);// Create stream object
iFileStore->SetRootL(id);// Keep database id as root of store
iFileStore->CommitL();// Complete creation by commiting
// Create Book tables and indexes
CreatePasswordTableL();
CreatePasswordIndexL();
);
if(error!=KErrNone)
{
return error;
}
iOpen = ETrue;
return KErrNone;
}
// ---------------------------------------------------------------------------
// CBookDb::RemoveDb()
//
// First remove the Books table. Then remove the database file.
// ---------------------------------------------------------------------------
TInt CPasswordDb::RemoveDb(const TFileName& aExistingPasswordFile)
{
Close();
if(!BaflUtils::FileExists(iFsSession, aExistingPasswordFile))
{
return KErrNotFound;
}
// It is enough to delete the database file directly. Because this example
// demonstrates DDL statements, it first opens and drops the Books table.
TInt error = OpenDb(aExistingPasswordFile);
if(error!=KErrNone)
{
return error;
}
DropPasswordTable();
Close();
iFsSession.Delete(aExistingPasswordFile);
return KErrNone;
}
// ---------------------------------------------------------------------------
// CBookDb::Close()
//
// Close the database.
// ---------------------------------------------------------------------------
TInt CPasswordDb::Close()
{
if(IsOpen())
{
iPasswordDb.Close();
}
if(iFileStore)
{
delete iFileStore;
iFileStore = NULL;
}
iOpen = EFalse;
return KErrNone;
}
// ---------------------------------------------------------------------------
// CBookDb::IsOpen()
//
// Return open status of the database.
// ---------------------------------------------------------------------------
TBool CPasswordDb::IsOpen() const
{
return iOpen;
}
// ---------------------------------------------------------------------------
// CBookDb::CreateBooksTableL()
//
// Creates Books table. Leaves, if the table cannot be created.
// ---------------------------------------------------------------------------
void CPasswordDb::CreatePasswordTableL()
{
// Specify columns for Books table
TDbCol IDCol(KPasswordIDCol, EDbColInt32);
TDbCol titleCol(KPasswordTitleCol, EDbColText, KTitleMaxLength);
TDbCol usernameCol(KPasswordUsernameCol, EDbColText,KTitleMaxLength); // Using default length
TDbCol passwordCol(KPasswordPWDCol, EDbColText,KTitleMaxLength); // Using default length
TDbCol descriptionCol(KPasswordDescriptionCol, EDbColLongText); // Using default length
TDbCol createDateCol(KPasswordCreateDateCol, EDbColDateTime); // Using default length
TDbCol modifiedDateCol(KPasswordModifiedDateCol, EDbColDateTime); // Using default length
IDCol.iAttributes = TDbCol::EAutoIncrement;
titleCol.iAttributes = TDbCol::ENotNull;
// Add the columns to column set
CDbColSet* passwordColSet = CDbColSet::NewLC();
passwordColSet->AddL(IDCol);
passwordColSet->AddL(titleCol);
passwordColSet->AddL(usernameCol);
passwordColSet->AddL(passwordCol);
passwordColSet->AddL(descriptionCol);
passwordColSet->AddL(createDateCol);
passwordColSet->AddL(modifiedDateCol);
// Create the Books table
User::LeaveIfError(iPasswordDb.CreateTable(KPasswordTable,
*passwordColSet));
CleanupStack::PopAndDestroy(passwordColSet);
}
// ---------------------------------------------------------------------------
// CBookDb::CreateBooksIndexL()
//
// Creates an index for Books table. Leaves, if the index cannot be created.
// ---------------------------------------------------------------------------
void CPasswordDb::CreatePasswordIndexL()
{
// Create index consisting of two columns
// TDbKeyCol authorCol(KBooksAuthorCol);
TDbKeyCol titleCol(KPasswordTitleCol);
CDbKey* index = CDbKey::NewLC(); // create index key set
index->AddL(titleCol);
// index->AddL(authorCol);
User::LeaveIfError(iPasswordDb.CreateIndex(
KPasswordIndexName, KPasswordTable, *index));
CleanupStack::PopAndDestroy(index);
}
// ---------------------------------------------------------------------------
// CBookDb::DropBooksTable()
//
// Drop the Books table incrementally. Uses RDbIncremental and DDL statement.
// ---------------------------------------------------------------------------
void CPasswordDb::DropPasswordTable()
{
_LIT(KDropTable, "DROP TABLE ");
// Sql: DROP TABLE Books
TBuf<KCustomSqlMaxLength> sqlStr;
sqlStr.Append(KDropTable);
sqlStr.Append(KPasswordTable);
RDbIncremental incOp;
TInt incStep = 0xFFFF;
// Initialise Execution
TInt incStat = incOp.Execute(iPasswordDb, sqlStr, incStep);
while (incStep>0 && incStat==KErrNone)
{
incStat = incOp.Next(incStep); // Do the work
}
incOp.Close();
}
// ---------------------------------------------------------------------------
// CBookDb::AddBookWithSqlL()
//
// Add a book to database using RDbView and SQL
// ---------------------------------------------------------------------------
TInt CPasswordDb::AddPasswordWithSql(const TDesC& aTitle,
const TDesC& aUsername,
const TDesC& aPassword,
const TDesC& aDescription)
{
if(aTitle.Length()==0)
{
return KErrGeneral;
}
_LIT(KSelect, "SELECT ");
_LIT(KFrom, " FROM ");
_LIT(KOrderBy, " ORDER BY ");
_LIT(KDot, ", ");
// Sql: SELECT Author, Title, Description FROM Books ORDER BY Title, Author
TBuf<KCustomSqlMaxLength> sqlStr;
sqlStr.Append(KSelect);
sqlStr.Append(KPasswordTitleCol);
sqlStr.Append(KDot);
sqlStr.Append(KPasswordUsernameCol);
sqlStr.Append(KDot);
sqlStr.Append(KPasswordPWDCol);
sqlStr.Append(KDot);
sqlStr.Append(KPasswordDescriptionCol);
sqlStr.Append(KFrom);
sqlStr.Append(KPasswordTable);
sqlStr.Append(KOrderBy);
sqlStr.Append(KPasswordTitleCol);
sqlStr.Append(KDot);
sqlStr.Append(KPasswordUsernameCol);
RDbView view; // Create a view on the database
TInt error;
error = view.Prepare(iPasswordDb, TDbQuery(sqlStr, EDbCompareFolded));
if(error!=KErrNone)
{
return error;
}
error = view.EvaluateAll();
if(error!=KErrNone)
{
return error;
}
RDbColWriteStream writeStream; // Use stream to insert the description
TRAP(error,
view.InsertL(); // Insert a row. Column order matches sql select statement
view.SetColL(2, aTitle);
view.SetColL(3, aUsername);
view.SetColL(4, aPassword);
writeStream.OpenL(view, 5);
writeStream.WriteL(aDescription);
);
if(error!=KErrNone)
{
return error;
}
writeStream.Close();
TRAP(error, view.PutL()); // Complete insertion
if(error!=KErrNone)
{
return error;
}
view.Close();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -