00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <f32file.h>
00032 #include <s32file.h>
00033 #include <d32dbms.h>
00034 #include <bautils.h>
00035 #include <eikmenub.h>
00036
00037 #include <StoreDatabase.rsg>
00038
00039 #include "StoreDatabaseAppUi.h"
00040 #include "StoreDatabaseMainView.h"
00041 #include "employee.h"
00042 #include "StoreDatabase.hrh"
00043
00044
00045 _LIT(KFileName, "c:\\data\\employee.db");
00046
00047 _LIT(KEmployeesTable, "EmployeesTable");
00048 _LIT(KEmployeesIndex, "EmployeesIndex");
00049
00050 _LIT(KIdentifier, "Identifier");
00051 _LIT(KName, "Name");
00052 _LIT(KPhoneNumber, "PhoneNumber");
00053
00054 const TInt KIdentifierIdx = 1;
00055 const TInt KNameIdx = 2;
00056 const TInt KPhoneNumberIdx = 3;
00057
00058 const TInt KMaxBuffer = 100;
00059
00060 _LIT(KName1, "John Doe");
00061 _LIT(KPhoneNumber1, "+1-222-333-4444");
00062
00063 _LIT(KName2, "Jane Roe");
00064 _LIT(KPhoneNumber2, "+1-234-567-8900");
00065
00066 const TChar KEndOfLine = '\f';
00067
00068
00069
00070
00071
00072
00073
00074 void CStoreDatabaseAppUi::ConstructL()
00075 {
00076
00077
00078 BaseConstructL(EAknEnableSkin);
00079
00080 iMainView = CStoreDatabaseMainView::NewL(ClientRect());
00081 iStatus = EStatusUninitialized;
00082 BaflUtils::EnsurePathExistsL(iCoeEnv->FsSession(), KFileName);
00083 }
00084
00085
00086
00087
00088 CStoreDatabaseAppUi::~CStoreDatabaseAppUi()
00089 {
00090 if (iMainView)
00091 {
00092 delete iMainView;
00093 iMainView = NULL;
00094 }
00095
00096 iDatabase.Close();
00097 delete iFileStore;
00098 iArray.ResetAndDestroy();
00099 }
00100
00101
00102
00103
00104 void CStoreDatabaseAppUi::HandleCommandL(TInt aCommand)
00105 {
00106 switch ( aCommand )
00107 {
00108
00109
00110 case EAknSoftkeyExit:
00111 case EEikCmdExit:
00112 {
00113 Exit();
00114 break;
00115 }
00116
00117 case EStoreDatabaseCreateDb:
00118 {
00119 DoCreateDbL();
00120 break;
00121 }
00122
00123 case EStoreDatabaseAddDataToDb:
00124 {
00125 DoAddDataToDbL();
00126 break;
00127 }
00128
00129 case EStoreDatabaseReadDataFromDb:
00130 {
00131 DoReadDataFromDbL();
00132 break;
00133 }
00134
00135 case EStoreDatabaseDeleteDataFromDb:
00136 {
00137
00138 DoDeleteDataFromDbL();
00139 break;
00140 }
00141
00142 default:
00143
00144 break;
00145 }
00146 }
00147
00148
00149
00150
00151
00152
00153 void CStoreDatabaseAppUi::HandleResourceChangeL(TInt aType)
00154 {
00155 CAknAppUi::HandleResourceChangeL(aType);
00156 iMainView->SetRect(ClientRect());
00157 }
00158
00159 void CStoreDatabaseAppUi::DoCreateDbL()
00160 {
00161 CreateDatabaseL(iCoeEnv->FsSession(), KFileName);
00162 CreateTableL(iDatabase);
00163 CreateIndexL(iDatabase);
00164 iStatus = EStatusCreated;
00165
00166
00167 HBufC* message = iCoeEnv->AllocReadResourceLC(
00168 R_STOREDATABASE_DBCREATED);
00169 iMainView->SetTextL(*message);
00170 CleanupStack::PopAndDestroy(message);
00171 }
00172
00173 void CStoreDatabaseAppUi::DoAddDataToDbL()
00174 {
00175
00176 if (EStatusUninitialized == iStatus)
00177 {
00178 iEikonEnv->InfoWinL(R_STOREDATABASE_CAPTION,
00179 R_STOREDATABASE_UNINITIALIZED);
00180 return;
00181 }
00182
00183
00184 if (EStatusAdded == iStatus)
00185 {
00186 return;
00187 }
00188
00189
00190 CEmployee* employee = new (ELeave) CEmployee();
00191 CleanupStack::PushL(employee);
00192
00193
00194 employee->SetName(KName1);
00195 employee->SetPhoneNumber(KPhoneNumber1);
00196 AddEmployeeToDatabaseUsingSqlL(iDatabase, *employee);
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 employee->SetName(KName2);
00207 employee->SetPhoneNumber(KPhoneNumber2);
00208 AddEmployeeToDatabaseUsingSqlL(iDatabase, *employee);
00209
00210 CleanupStack::PopAndDestroy(employee);
00211 iStatus = EStatusAdded;
00212
00213
00214 HBufC* message = iCoeEnv->AllocReadResourceLC(
00215 R_STOREDATABASE_DBADDED);
00216 iMainView->SetTextL(*message);
00217 CleanupStack::PopAndDestroy(message);
00218 }
00219
00220 void CStoreDatabaseAppUi::DoReadDataFromDbL()
00221 {
00222
00223 if (iStatus == EStatusUninitialized)
00224 {
00225 iEikonEnv->InfoWinL(R_STOREDATABASE_CAPTION,
00226 R_STOREDATABASE_UNINITIALIZED);
00227 return;
00228 }
00229
00230
00231 iArray.ResetAndDestroy();
00232 ReadEmployeesFromDatabaseL(iDatabase);
00233
00234
00235 RBuf buffer;
00236 buffer.CreateL(KMaxBuffer * iArray.Count());
00237 CleanupClosePushL(buffer);
00238 for (TInt i = 0; i < iArray.Count(); i++)
00239 {
00240 const CEmployee* employee = iArray[i];
00241 buffer.AppendNum(employee->Identifier());
00242 buffer.Append(KEndOfLine);
00243 buffer.Append(employee->Name());
00244 buffer.Append(KEndOfLine);
00245 buffer.Append(employee->PhoneNumber());
00246 buffer.Append(KEndOfLine);
00247 buffer.Append(KEndOfLine);
00248 }
00249 iMainView->SetTextL(buffer);
00250
00251 CleanupStack::PopAndDestroy(&buffer);
00252 }
00253
00254 void CStoreDatabaseAppUi::DoDeleteDataFromDbL()
00255 {
00256
00257 if (iStatus == EStatusUninitialized)
00258 {
00259 iEikonEnv->InfoWinL(R_STOREDATABASE_CAPTION,
00260 R_STOREDATABASE_UNINITIALIZED);
00261 return;
00262 }
00263
00264
00265
00266
00267 DeleteEmployeeFromDatabaseL(iDatabase, KName1);
00268 DeleteEmployeeFromDatabaseL(iDatabase, KName2);
00269 iStatus = EStatusCreated;
00270
00271
00272 HBufC* message = iCoeEnv->AllocReadResourceLC(
00273 R_STOREDATABASE_DBDELETED);
00274 iMainView->SetTextL(*message);
00275 CleanupStack::PopAndDestroy(message);
00276 }
00277
00278 void CStoreDatabaseAppUi::CreateDatabaseL(RFs& aFs, const TDesC& aFileName)
00279 {
00280
00281 delete iFileStore;
00282 iFileStore = 0;
00283 iFileStore = CPermanentFileStore::ReplaceL(
00284 aFs, aFileName, EFileRead | EFileWrite);
00285 iFileStore->SetTypeL(iFileStore->Layout());
00286
00287
00288 iDatabase.Close();
00289 TStreamId id = iDatabase.CreateL(iFileStore);
00290
00291
00292 iFileStore->SetRootL(id);
00293 iFileStore->CommitL();
00294 }
00295
00296 void CStoreDatabaseAppUi::CreateTableL(RDbDatabase& aDatabase)
00297 {
00298 CDbColSet* columns = CDbColSet::NewLC();
00299
00300
00301 TDbCol identifier(KIdentifier, EDbColUint32);
00302 identifier.iAttributes = TDbCol::EAutoIncrement;
00303 columns->AddL(identifier);
00304
00305
00306 TDbCol name(KName, EDbColText, KMaxEmployeeName);
00307 name.iAttributes = TDbCol::ENotNull;
00308 columns->AddL(name);
00309
00310
00311 columns->AddL(TDbCol(KPhoneNumber, EDbColText, KMaxEmployeeNumber));
00312
00313
00314 User::LeaveIfError(aDatabase.CreateTable(KEmployeesTable, *columns));
00315
00316 CleanupStack::PopAndDestroy(columns);
00317 }
00318
00319 void CStoreDatabaseAppUi::CreateIndexL(RDbDatabase& aDatabase)
00320 {
00321
00322 CDbKey* key = CDbKey::NewLC();
00323 key->AddL(TDbKeyCol(KName));
00324 key->AddL(TDbKeyCol(KIdentifier));
00325
00326
00327 User::LeaveIfError(aDatabase.CreateIndex(KEmployeesIndex, KEmployeesTable, *key));
00328
00329 CleanupStack::PopAndDestroy(key);
00330 }
00331
00332 void CStoreDatabaseAppUi::AddEmployeeToDatabaseL(RDbDatabase& aDatabase,
00333 const CEmployee& aEmployee)
00334 {
00335
00336 _LIT(KSqlStatement, "SELECT * FROM %S");
00337 TBuf<KMaxBuffer> sqlStatement;
00338 sqlStatement.AppendFormat(KSqlStatement, &KEmployeesTable);
00339
00340
00341 RDbView view;
00342 User::LeaveIfError(view.Prepare(
00343 aDatabase, TDbQuery(sqlStatement, EDbCompareFolded)));
00344 CleanupClosePushL(view);
00345 User::LeaveIfError(view.EvaluateAll());
00346
00347
00348 view.InsertL();
00349 view.SetColL(KNameIdx, aEmployee.Name());
00350 view.SetColL(KPhoneNumberIdx, aEmployee.PhoneNumber());
00351 view.PutL();
00352
00353
00354 CleanupStack::PopAndDestroy(&view);
00355 }
00356
00357 void CStoreDatabaseAppUi::AddEmployeeToDatabaseUsingSqlL(RDbDatabase& aDatabase,
00358 const CEmployee& aEmployee)
00359 {
00360
00361 _LIT(KSqlStatement, "INSERT INTO %S (%S, %S) VALUES ('%S', '%S')");
00362
00363 TBuf<KMaxBuffer> sqlStatement;
00364 TPtrC name = aEmployee.Name();
00365 TPtrC phoneNumber = aEmployee.PhoneNumber();
00366 sqlStatement.AppendFormat(KSqlStatement, &KEmployeesTable,
00367 &KName, &KPhoneNumber, &name, &phoneNumber);
00368
00369
00370 User::LeaveIfError(aDatabase.Execute(sqlStatement, EDbCompareFolded));
00371 }
00372
00373 void CStoreDatabaseAppUi::ReadEmployeesFromDatabaseL(RDbDatabase& aDatabase)
00374 {
00375
00376 _LIT(KSqlStatement, "SELECT * FROM %S ORDER BY %S");
00377 TBuf<KMaxBuffer> sqlStatement;
00378 sqlStatement.AppendFormat(KSqlStatement, &KEmployeesTable,
00379 &KName);
00380
00381 RDbView view;
00382 User::LeaveIfError(view.Prepare(
00383 aDatabase, TDbQuery(sqlStatement, EDbCompareFolded)));
00384 CleanupClosePushL(view);
00385 User::LeaveIfError(view.EvaluateAll());
00386
00387
00388 view.FirstL();
00389 while (view.AtRow())
00390 {
00391 view.GetL();
00392 TUint32 identifier = view.ColUint32(KIdentifierIdx);
00393 TPtrC name = view.ColDes(KNameIdx);
00394 TPtrC phoneNumber = view.ColDes(KPhoneNumberIdx);
00395 view.NextL();
00396
00397
00398
00399 CEmployee* employee = new (ELeave) CEmployee();
00400 employee->SetIdentifier(identifier);
00401 employee->SetName(name);
00402 employee->SetPhoneNumber(phoneNumber);
00403 iArray.Append(employee);
00404 }
00405
00406 CleanupStack::PopAndDestroy(&view);
00407 }
00408
00409 void CStoreDatabaseAppUi::DeleteEmployeeFromDatabaseL(RDbDatabase& aDatabase,
00410 const TDesC& aName)
00411 {
00412
00413 _LIT(KSqlStatement, "DELETE FROM %S WHERE %S='%S'");
00414
00415 TBuf<KMaxBuffer> sqlStatement;
00416 sqlStatement.AppendFormat(KSqlStatement, &KEmployeesTable,
00417 &KName, &aName);
00418
00419
00420 User::LeaveIfError(aDatabase.Execute(sqlStatement, EDbCompareFolded));
00421 }
00422
00423