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
00032
00033
00034 #include "CDatabase.h"
00035
00036
00037 const int KDelay = 1000000;
00038
00039
00040 const int KLastNum = 50;
00041
00045 CDatabase* CDatabase::NewL()
00046 {
00047 CDatabase* self=new(ELeave)CDatabase();
00048 CleanupStack::PushL(self);
00049 self->ConstructL();
00050 CleanupStack::Pop(self);
00051 return self;
00052 }
00053
00057 CDatabase::~CDatabase()
00058 {
00059 iFsSession.Close();
00060 delete iPeriodic;
00061 delete iConsole;
00062 #ifdef USE_SEMAPHORE
00063 iSemWrite.Close();
00064 #endif
00065 }
00066
00070 void CDatabase::ConstructL()
00071 {
00072 iPeriodic = CPeriodic::NewL(CActive::EPriorityUserInput);
00073
00074 _LIT(KDbCreateDb,"exampleDatabase.db");
00075
00076 iFsSession.Connect();
00077
00078
00079
00080 User::LeaveIfError(iFsSession.CreatePrivatePath(RFs::GetSystemDrive()));
00081
00082 User::LeaveIfError(iFsSession.PrivatePath(iDbname));
00083 iDbname.Append(KDbCreateDb);
00084
00085 #ifdef USE_SEMAPHORE
00086
00087 _LIT(KSemaphoreWrite, "WriteSemaphore");
00088 User::LeaveIfError(iSemWrite.OpenGlobal(KSemaphoreWrite, EOwnerProcess));
00089 #endif
00090 }
00091
00095 void CDatabase::CreateDatabaseL()
00096 {
00097
00098
00099 CFileStore* store = CPermanentFileStore::ReplaceLC(iFsSession, iDbname, EFileRead|EFileWrite);
00100
00101 store->SetTypeL(store->Layout());
00102
00103
00104 TStreamId id= iDatabase.CreateL(store);
00105
00106
00107 store->SetRootL(id);
00108
00109
00110 store->CommitL();
00111
00112
00113
00114 CDbColSet* columns=CDbColSet::NewLC();
00115
00116
00117 _LIT(KCol1, "Number1");
00118 _LIT(KCol2, "Number2");
00119 _LIT(KCol3, "Number3");
00120 columns->AddL(TDbCol(KCol1,EDbColInt32));
00121 columns->AddL(TDbCol(KCol2,EDbColInt32));
00122 columns->AddL(TDbCol(KCol3,EDbColInt32));
00123
00124
00125
00126 _LIT(KTable, "Numbers");
00127 User::LeaveIfError(iDatabase.CreateTable(KTable,*columns));
00128
00129 CDbKey* key=CDbKey::NewLC();
00130
00131
00132 TDbKeyCol number1(KCol1);
00133 key->AddL(number1);
00134 TDbKeyCol number2(KCol2);
00135 key->AddL(number2);
00136 TDbKeyCol number3(KCol3);
00137 key->AddL(number3);
00138 User::LeaveIfError(iDatabase.CreateIndex(KTable,KTable,*key));
00139
00140 iDatabase.Close();
00141
00142 CleanupStack::PopAndDestroy(key);
00143 CleanupStack::PopAndDestroy(columns);
00144 CleanupStack::PopAndDestroy(store);
00145 }
00146
00151 void CDatabase::WritetoDatabaseL()
00152 {
00153 iPeriodic->Start(0, KDelay, TCallBack(PeriodicWriteL, this));
00154 CActiveScheduler::Start();
00155 }
00156
00161 TInt CDatabase::PeriodicWriteL(TAny* aPtr)
00162 {
00163 CDatabase* ptr = static_cast<CDatabase*> (aPtr);
00164
00165 ptr->PeriodicWriteFuncL();
00166 return KErrNone;
00167 }
00168
00173 void CDatabase::PeriodicWriteFuncL()
00174 {
00175
00176 static int num = 0 ;
00177
00178
00179 _LIT(KSQLStatement,"select Number1, Number2, Number3 from Numbers order by Number1,Number2,Number3");
00180 CFileStore* store = CFileStore::OpenLC(iFsSession,iDbname,EFileRead|EFileWrite);
00181
00182
00183
00184 iDatabase.OpenL(store,store->Root());
00185
00186
00187 iDatabase.Begin();
00188
00189
00190 RDbView view;
00191 User::LeaveIfError(view.Prepare(iDatabase,TDbQuery(KSQLStatement,EDbCompareNormal)));
00192 User::LeaveIfError(view.EvaluateAll());
00193
00194
00195
00196 view.InsertL();
00197
00198
00199 view.SetColL(1, num++);
00200 view.SetColL(2, num++);
00201 view.SetColL(3, num++);
00202 view.PutL();
00203
00204
00205 view.Close();
00206
00207
00208 iDatabase.Commit();
00209
00210
00211 iDatabase.Close();
00212
00213
00214 CleanupStack::PopAndDestroy(store);
00215
00216
00217 if(num > KLastNum )
00218 {
00219 iPeriodic->Cancel();
00220 CActiveScheduler::Stop();
00221 }
00222
00223 #ifdef USE_SEMAPHORE
00224
00225
00226
00227 iSemWrite.Signal();
00228 #endif
00229 }
00230
00236 void CDatabase::ReadfromDatabaseL()
00237 {
00238 _LIT(KTextConsoleTitle, "ReaderThread");
00239 if(!iConsole)
00240 {
00241 iConsole = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,KConsFullScreen));
00242 _LIT(KReading, "Reading the database.\n");
00243 iConsole->Printf(KReading);
00244 }
00245 iPeriodic->Start(0, KDelay, TCallBack(PeriodicReadL,this));
00246 CActiveScheduler::Start();
00247 }
00248
00253 TInt CDatabase::PeriodicReadL(TAny* aPtr)
00254 {
00255 CDatabase* ptr = static_cast<CDatabase*> (aPtr);
00256 ptr->PeriodicReadFuncL();
00257 return KErrNone;
00258 }
00259
00264 void CDatabase::PeriodicReadFuncL()
00265 {
00266 #ifdef USE_SEMAPHORE
00267
00268 iSemWrite.Wait();
00269 #endif
00270
00271 CFileStore* store = CFileStore::OpenLC(iFsSession,iDbname,EFileRead);
00272
00273
00274 iDatabase.OpenL(store,store->Root());
00275
00276
00277 iDatabase.Begin();
00278
00279
00280 _LIT(KSQLStatement,"select Number1,Number2,Number3 from Numbers order by Number1,Number2, Number3");
00281
00282
00283 RDbView view;
00284 User::LeaveIfError(view.Prepare(iDatabase,TDbQuery(KSQLStatement,EDbCompareNormal)));
00285 User::LeaveIfError(view.EvaluateAll());
00286
00287
00288 static int count = 0;
00289 TInt iter = 0;
00290
00291
00292
00293
00294 view.FirstL();
00295 while( iter < count)
00296 {
00297 view.NextL();
00298 iter++;
00299 }
00300 count ++ ;
00301
00302
00303 view.GetL();
00304 TInt number1 = view.ColInt(1);
00305 TInt number2 = view.ColInt(2);
00306 TInt number3 = view.ColInt(3);
00307
00308 _LIT(KRowFormatter, "\n reading %d \t%d\t%d");
00309
00310 iConsole->Printf(KRowFormatter,number1,number2, number3);
00311
00312
00313 if(number3 >= KLastNum)
00314 {
00315 iPeriodic->Cancel();
00316 CActiveScheduler::Stop();
00317 _LIT(KAllRead, "\nAll the numbers in the database have been read\n");
00318 iConsole->Printf(KAllRead);
00319 _LIT(KExit, "Press a key to exit\n");
00320 iConsole->Printf(KExit);
00321 iConsole->Getch();
00322 }
00323
00324 view.Close();
00325
00326
00327 iDatabase.Commit();
00328
00329 iDatabase.Close();
00330 CleanupStack::PopAndDestroy(store);
00331 }