00001
00002
00003
00004
00005 #include <badesca.h>
00006 #include <s32file.h>
00007 #include <bautils.h>
00008 #include <eikenv.h>
00009 #include "DBMSEngine.h"
00010
00011
00012 const int KCustomSqlMaxLength = 256;
00013 const int KArrayGranularity = 5;
00014
00015
00016
00017
00018
00019
00020 CBookDb* CBookDb::NewL()
00021 {
00022 CBookDb* tmp = new (ELeave)CBookDb();
00023 CleanupStack::PushL(tmp);
00024 tmp->ConstructL();
00025 CleanupStack::Pop();
00026 return tmp;
00027 }
00028
00029
00030
00031
00032
00033
00034 CBookDb::~CBookDb()
00035 {
00036 Close();
00037 iFsSession.Close();
00038 }
00039
00040
00041
00042
00043
00044
00045 void CBookDb::ConstructL()
00046 {
00047 TInt err = iFsSession.Connect();
00048 if(err)
00049 User::Leave(err);
00050 }
00051
00052
00053
00054
00055
00056
00057 CBookDb::CBookDb()
00058 {
00059 iOpen = EFalse;
00060 }
00061
00062
00063
00064
00065
00066
00067 TInt CBookDb::OpenDb(const TFileName& aExistingBookFile)
00068 {
00069 Close();
00070
00071 if(!BaflUtils::FileExists(iFsSession, aExistingBookFile))
00072 {
00073 return KErrNotFound;
00074 }
00075
00076 TRAPD(error,
00077 iFileStore = CPermanentFileStore::OpenL(iFsSession, aExistingBookFile,
00078 EFileRead|EFileWrite);
00079 iFileStore->SetTypeL(iFileStore->Layout());
00080 iBookDb.OpenL(iFileStore,iFileStore->Root())
00081 );
00082 if(error!=KErrNone)
00083 {
00084 return error;
00085 }
00086
00087 iOpen = ETrue;
00088 return KErrNone;
00089 }
00090
00091
00092
00093
00094
00095
00096 TInt CBookDb::CreateDb(const TFileName& aNewBookFile)
00097 {
00098 Close();
00099
00100
00101 TRAPD(error,
00102 iFileStore = CPermanentFileStore::ReplaceL(iFsSession, aNewBookFile,
00103 EFileRead|EFileWrite);
00104 iFileStore->SetTypeL(iFileStore->Layout());
00105 TStreamId id = iBookDb.CreateL(iFileStore);
00106 iFileStore->SetRootL(id);
00107 iFileStore->CommitL();
00108
00109 CreateBooksTableL();
00110 CreateBooksIndexL();
00111 );
00112
00113 if(error!=KErrNone)
00114 {
00115 return error;
00116 }
00117 iOpen = ETrue;
00118 return KErrNone;
00119 }
00120
00121
00122
00123
00124
00125
00126 TInt CBookDb::RemoveDb(const TFileName& aExistingBookFile)
00127 {
00128 Close();
00129
00130 if(!BaflUtils::FileExists(iFsSession, aExistingBookFile))
00131 {
00132 return KErrNotFound;
00133 }
00134
00135
00136
00137
00138 TInt error = OpenDb(aExistingBookFile);
00139 if(error!=KErrNone)
00140 {
00141 return error;
00142 }
00143
00144 DropBooksTable();
00145 Close();
00146
00147 iFsSession.Delete(aExistingBookFile);
00148 return KErrNone;
00149 }
00150
00151
00152
00153
00154
00155
00156 TInt CBookDb::Close()
00157 {
00158 iBookDb.Close();
00159 if(iFileStore)
00160 {
00161 delete iFileStore;
00162 iFileStore = NULL;
00163 }
00164 iOpen = EFalse;
00165 return KErrNone;
00166 }
00167
00168
00169
00170
00171
00172
00173 TBool CBookDb::IsOpen() const
00174 {
00175 return iOpen;
00176 }
00177
00178
00179
00180
00181
00182
00183 void CBookDb::CreateBooksTableL()
00184 {
00185
00186
00187 TDbCol authorCol(KBooksAuthorCol, EDbColText);
00188 TDbCol titleCol(KBooksTitleCol, EDbColText, KTitleMaxLength);
00189 titleCol.iAttributes = TDbCol::ENotNull;
00190 TDbCol descriptionCol(KBooksDescriptionCol, EDbColLongText);
00191
00192
00193 CDbColSet* bookColSet = CDbColSet::NewLC();
00194 bookColSet->AddL(authorCol);
00195 bookColSet->AddL(titleCol);
00196 bookColSet->AddL(descriptionCol);
00197
00198
00199 User::LeaveIfError(iBookDb.CreateTable(KBooksTable,
00200 *bookColSet));
00201 CleanupStack::PopAndDestroy(bookColSet);
00202 }
00203
00204
00205
00206
00207
00208
00209 void CBookDb::CreateBooksIndexL()
00210 {
00211
00212 TDbKeyCol authorCol(KBooksAuthorCol);
00213 TDbKeyCol titleCol(KBooksTitleCol);
00214
00215 CDbKey* index = CDbKey::NewLC();
00216 index->AddL(titleCol);
00217 index->AddL(authorCol);
00218 User::LeaveIfError(iBookDb.CreateIndex(
00219 KBooksIndexName, KBooksTable, *index));
00220 CleanupStack::PopAndDestroy(index);
00221 }
00222
00223
00224
00225
00226
00227
00228 void CBookDb::DropBooksTable()
00229 {
00230
00231 _LIT(KDropTable, "DROP TABLE ");
00232
00233
00234 TBuf<KCustomSqlMaxLength> sqlStr;
00235 sqlStr.Append(KDropTable);
00236 sqlStr.Append(KBooksTable);
00237
00238 RDbIncremental incOp;
00239 TInt incStep = 0xFFFF;
00240
00241 TInt incStat = incOp.Execute(iBookDb, sqlStr, incStep);
00242 while (incStep>0 && incStat==KErrNone)
00243 {
00244 incStat = incOp.Next(incStep);
00245 }
00246 incOp.Close();
00247 }
00248
00249
00250
00251
00252
00253
00254 TInt CBookDb::AddBookWithSql(const TDesC& aAuthor,
00255 const TDesC& aTitle,
00256 const TDesC& aDescription)
00257 {
00258
00259 if(aAuthor.Length()==0 || aTitle.Length()==0 || aDescription.Length()==0)
00260 {
00261 return KErrGeneral;
00262 }
00263
00264 _LIT(KSelect, "SELECT ");
00265 _LIT(KFrom, " FROM ");
00266 _LIT(KOrderBy, " ORDER BY ");
00267 _LIT(KDot, ", ");
00268
00269
00270 TBuf<KCustomSqlMaxLength> sqlStr;
00271
00272 sqlStr.Append(KSelect);
00273 sqlStr.Append(KBooksAuthorCol);
00274 sqlStr.Append(KDot);
00275 sqlStr.Append(KBooksTitleCol);
00276 sqlStr.Append(KDot);
00277 sqlStr.Append(KBooksDescriptionCol);
00278 sqlStr.Append(KFrom);
00279 sqlStr.Append(KBooksTable);
00280 sqlStr.Append(KOrderBy);
00281 sqlStr.Append(KBooksTitleCol);
00282 sqlStr.Append(KDot);
00283 sqlStr.Append(KBooksAuthorCol);
00284
00285 RDbView view;
00286 TInt error;
00287 error = view.Prepare(iBookDb, TDbQuery(sqlStr, EDbCompareFolded));
00288 if(error!=KErrNone)
00289 {
00290 return error;
00291 }
00292 error = view.EvaluateAll();
00293 if(error!=KErrNone)
00294 {
00295 return error;
00296 }
00297 RDbColWriteStream writeStream;
00298
00299 TRAP(error,
00300 view.InsertL();
00301 view.SetColL(1, aAuthor);
00302 view.SetColL(2, aTitle);
00303 writeStream.OpenL(view, 3);
00304 writeStream.WriteL(aDescription);
00305
00306 );
00307 if(error!=KErrNone)
00308 {
00309 return error;
00310 }
00311 writeStream.Close();
00312 TRAP(error, view.PutL());
00313 if(error!=KErrNone)
00314 {
00315 return error;
00316 }
00317 view.Close();
00318 return KErrNone;
00319 }
00320
00321
00322
00323
00324
00325
00326 TInt CBookDb::AddBookWithCppApiL(const TDesC& aAuthor,
00327 const TDesC& aTitle,
00328 const TDesC& aDescription)
00329 {
00330
00331 if(aAuthor.Length()==0 || aTitle.Length()==0 || aDescription.Length()==0)
00332 {
00333 return KErrGeneral;
00334 }
00335
00336
00337 RDbTable table;
00338 TInt err = table.Open(iBookDb, KBooksTable, table.EUpdatable);
00339
00340 if(err!=KErrNone)
00341 {
00342 return err;
00343 }
00344
00345 CDbColSet* booksColSet = table.ColSetL();
00346 CleanupStack::PushL(booksColSet);
00347
00348 table.Reset();
00349 RDbColWriteStream writeStream;
00350
00351 TRAPD(error,
00352 table.InsertL();
00353 table.SetColL(booksColSet->ColNo(KBooksAuthorCol), aAuthor);
00354 table.SetColL(booksColSet->ColNo(KBooksTitleCol), aTitle);
00355
00356 writeStream.OpenL(table, booksColSet->ColNo(KBooksDescriptionCol));
00357 writeStream.WriteL(aDescription);
00358 );
00359
00360 if(error!=KErrNone)
00361 {
00362 return error;
00363 }
00364 writeStream.Close();
00365
00366 TRAP(err, table.PutL());
00367 if(err!=KErrNone)
00368 {
00369 return err;
00370 }
00371
00372 CleanupStack::PopAndDestroy(booksColSet);
00373 table.Close();
00374
00375 return KErrNone;
00376
00377 }
00378
00379
00380
00381
00382
00383
00384
00385 CDesCArrayFlat* CBookDb::GetAllBooksL()
00386 {
00387 TPtrC author, title;
00388 TBuf<KDescriptionMaxLength> description;
00389 TBuf<KBookItemMaxLength> rowText;
00390
00391 RDbTable table;
00392 TInt err = table.Open(iBookDb, KBooksTable, table.EReadOnly);
00393 User::LeaveIfError(err);
00394
00395 CDesCArrayFlat* resultArray =
00396 new (ELeave)CDesC16ArrayFlat(KArrayGranularity);
00397 CleanupStack::PushL(resultArray);
00398
00399 table.Reset();
00400 CDbColSet* colSet = table.ColSetL();
00401 CleanupStack::PushL(colSet);
00402
00403 for (table.FirstL(); table.AtRow(); table.NextL())
00404 {
00405 description.Zero();
00406 rowText.Zero();
00407
00408 table.GetL();
00409
00410 author.Set(table.ColDes(colSet->ColNo(KBooksAuthorCol)));
00411 title.Set(table.ColDes(colSet->ColNo(KBooksTitleCol)));
00412
00413 TDbColNo descrColNo = colSet->ColNo(KBooksDescriptionCol);
00414 RDbColReadStream readStream;
00415 readStream.OpenLC(table,descrColNo);
00416 readStream.ReadL(description, table.ColLength(descrColNo));
00417 readStream.Close();
00418 CleanupStack::Pop();
00419
00420 rowText.Append(author);
00421 rowText.Append(KSeparator);
00422 rowText.Append(title);
00423 rowText.Append(KSeparator);
00424 rowText.Append(description);
00425
00426 resultArray->AppendL(rowText);
00427 }
00428 CleanupStack::PopAndDestroy(colSet);
00429 CleanupStack::Pop(resultArray);
00430 table.Close();
00431
00432 return resultArray;
00433 }
00434
00435
00436
00437
00438
00439
00440
00441 TInt CBookDb::GetABookFast(const TDesC& aTitle, TDes& aResult)
00442 {
00443 TInt err = KErrNone;
00444 TBuf<KDescriptionMaxLength> description;
00445 RDbTable rowset;
00446
00447 TDbSeekKey seekKey(aTitle);
00448
00449
00450 err = rowset.Open(iBookDb, KBooksTable, rowset.EReadOnly);
00451 if(err!=KErrNone)
00452 {
00453 return err;
00454 }
00455 err = rowset.SetIndex(KBooksIndexName);
00456 if(err!=KErrNone)
00457 {
00458 return err;
00459 }
00460
00461 CDbColSet* colSet=NULL;
00462 TRAP(err, colSet = rowset.ColSetL());
00463 if(err!=KErrNone)
00464 {
00465 return err;
00466 }
00467
00468 TInt authorColumnNo = colSet->ColNo(KBooksAuthorCol);
00469 TInt titleColumnNo = colSet->ColNo(KBooksTitleCol);
00470 TInt descrColumnNo = colSet->ColNo(KBooksDescriptionCol);
00471
00472
00473 TBool isTitle = false;
00474 TRAP(err, isTitle = rowset.SeekL(seekKey));
00475 if(isTitle)
00476 {
00477 RDbColReadStream readStream;
00478 TRAPD(error,
00479 rowset.GetL();
00480 readStream.OpenL(rowset,descrColumnNo);
00481 readStream.ReadL(description, rowset.ColLength(descrColumnNo));
00482 );
00483 if(error!=KErrNone)
00484 {
00485 return error;
00486 }
00487
00488 readStream.Close();
00489
00490 aResult.Zero();
00491 aResult.Append(rowset.ColDes(authorColumnNo));
00492 aResult.Append(KSeparator);
00493 aResult.Append(rowset.ColDes(titleColumnNo));
00494 aResult.Append(KSeparator);
00495 aResult.Append(description);
00496
00497 err = KErrNone;
00498 }
00499 else
00500 {
00501 err = KErrNotFound;
00502 }
00503
00504 rowset.Close();
00505 return err;
00506 }
00507
00508
00509
00510
00511
00512
00513
00514
00515 CDesCArrayFlat* CBookDb::GetBooksByKeyL(const TDesC& aColumnName,
00516 const TDesC& aSearchString)
00517 {
00518
00519 TPtrC author, title;
00520 TBuf<KDescriptionMaxLength> description;
00521 TBuf<KBookItemMaxLength> rowText;
00522
00523 _LIT(KSelect, "SELECT ");
00524 _LIT(KFrom, " FROM ");
00525 _LIT(KWhere, " WHERE ");
00526 _LIT(KLike, " LIKE '");
00527 _LIT(KOrderBy, "' ORDER BY ");
00528 _LIT(KDot, ", ");
00529
00530
00531
00532
00533 TBuf<KCustomSqlMaxLength> sqlStr;
00534 sqlStr.Append(KSelect);
00535 sqlStr.Append(KBooksAuthorCol);
00536 sqlStr.Append(KDot);
00537 sqlStr.Append(KBooksTitleCol);
00538 sqlStr.Append(KDot);
00539 sqlStr.Append(KBooksDescriptionCol);
00540 sqlStr.Append(KFrom);
00541 sqlStr.Append(KBooksTable);
00542 sqlStr.Append(KWhere);
00543 sqlStr.Append(aColumnName);
00544 sqlStr.Append(KLike);
00545 sqlStr.Append(aSearchString);
00546 sqlStr.Append(KOrderBy);
00547 sqlStr.Append(KBooksTitleCol);
00548 sqlStr.Append(KDot);
00549 sqlStr.Append(KBooksAuthorCol);
00550
00551 CDesCArrayFlat* resultArray =
00552 new (ELeave)CDesC16ArrayFlat(KArrayGranularity);
00553 CleanupStack::PushL(resultArray);
00554
00555
00556 RDbView view;
00557 User::LeaveIfError(
00558 view.Prepare(iBookDb, TDbQuery(sqlStr), view.EReadOnly));
00559 User::LeaveIfError(view.EvaluateAll());
00560
00561 CDbColSet* colSet = view.ColSetL();
00562 CleanupStack::PushL(colSet);
00563
00564
00565 for (view.FirstL(); view.AtRow(); view.NextL())
00566 {
00567
00568 description.Zero();
00569 rowText.Zero();
00570
00571 view.GetL();
00572
00573 author.Set(view.ColDes(colSet->ColNo(KBooksAuthorCol)));
00574 title.Set(view.ColDes(colSet->ColNo(KBooksTitleCol)));
00575
00576 TDbColNo descrColNo = colSet->ColNo(KBooksDescriptionCol);
00577 RDbColReadStream readStream;
00578 readStream.OpenLC(view, descrColNo);
00579 readStream.ReadL(description, view.ColLength(descrColNo));
00580 readStream.Close();
00581 CleanupStack::Pop();
00582
00583 rowText.Append(author);
00584 rowText.Append(KSeparator);
00585 rowText.Append(title);
00586 rowText.Append(KSeparator);
00587 rowText.Append(description);
00588
00589 resultArray->AppendL(rowText);
00590 }
00591 CleanupStack::PopAndDestroy(colSet);
00592 view.Close();
00593 CleanupStack::Pop(resultArray);
00594
00595 return resultArray;
00596
00597 }
00598
00599
00600
00601
00602
00603
00604 TInt CBookDb::RemoveBooks(const TDesC& aTitle, TInt& aResultCount)
00605 {
00606 RDbUpdate updOp;
00607
00608 _LIT(KDeleteFrom, "DELETE FROM ");
00609 _LIT(KWhere, " WHERE ");
00610 _LIT(KLike, " LIKE '");
00611 _LIT(KDot, "'");
00612
00613
00614 TBuf<KCustomSqlMaxLength> sqlStr;
00615 sqlStr.Append(KDeleteFrom);
00616 sqlStr.Append(KBooksTable);
00617 sqlStr.Append(KWhere);
00618 sqlStr.Append(KBooksTitleCol);
00619 sqlStr.Append(KLike);
00620 sqlStr.Append(aTitle);
00621 sqlStr.Append(KDot);
00622
00623
00624
00625
00626 TInt incStat = updOp.Execute(iBookDb, sqlStr, EDbCompareFolded);
00627 incStat = updOp.Next();
00628
00629 while( incStat == 1 )
00630 {
00631 incStat = updOp.Next();
00632 }
00633 aResultCount = updOp.RowCount();
00634 updOp.Close();
00635 return incStat;
00636 }
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646 TInt CBookDb::RemoveAllBooks(TInt& aResultCount)
00647 {
00648 _LIT(KDeleteFrom, "DELETE FROM ");
00649
00650
00651 TBuf<KCustomSqlMaxLength> sqlStr;
00652 sqlStr.Append(KDeleteFrom);
00653 sqlStr.Append(KBooksTable);
00654
00655 RDbUpdate updOp;
00656 TRequestStatus incStat(1);
00657 TInt updStat = updOp.Execute(iBookDb, sqlStr, EDbCompareFolded);
00658 while (updStat==KErrNone && incStat ==1)
00659 {
00660 updOp.Next(incStat);
00661
00662 User::WaitForRequest(incStat);
00663 }
00664
00665 aResultCount = updOp.RowCount();
00666 updOp.Close();
00667
00668 if(updStat!=KErrNone)
00669 return updStat;
00670 else
00671 return incStat.Int();
00672 }
00673
00674
00675
00676
00677
00678
00679
00680
00681 TInt CBookDb::UpdateBookTitle(const TDesC& aOldTitleKey,
00682 const TDesC& aNewTitle)
00683 {
00684 _LIT(KSQLUpdateStart, "UPDATE Books SET Title = '");
00685 _LIT(KSQLUpdateMiddle, "' WHERE Title = '");
00686 _LIT(KSQLUpdateEnd, "'");
00687
00688 TBuf<KCustomSqlMaxLength> sqlStr;
00689 sqlStr.Append(KSQLUpdateStart);
00690 sqlStr.Append(aNewTitle);
00691 sqlStr.Append(KSQLUpdateMiddle);
00692 sqlStr.Append(aOldTitleKey);
00693 sqlStr.Append(KSQLUpdateEnd);
00694
00695 return iBookDb.Execute(sqlStr);
00696 }
00697
00698
00699
00700
00701
00702
00703
00704 CDesCArrayFlat* CBookDb::ColumnNamesAndSizesL()
00705 {
00706 RDbTable booksTable;
00707 TBuf<32> columnNameAndSize;
00708 _LIT(KDelimiter, ": ");
00709 _LIT(KNoSize,"No size");
00710
00711
00712 TInt err = booksTable.Open(iBookDb, KBooksTable, booksTable.EReadOnly);
00713 User::LeaveIfError(err);
00714
00715 CleanupClosePushL(booksTable);
00716
00717 CDesCArrayFlat* resultArray =
00718 new (ELeave)CDesC16ArrayFlat(KArrayGranularity);
00719 CleanupStack::PushL(resultArray);
00720
00721
00722
00723
00724
00725 CDbColSet* colSet = booksTable.ColSetL();
00726 CleanupStack::PushL(colSet);
00727 TDbColSetIter colIter(*colSet);
00728 while(colIter)
00729 {
00730 columnNameAndSize.Zero();
00731 columnNameAndSize.Append(colIter->iName);
00732 columnNameAndSize.Append(KDelimiter);
00733 if(colIter->iType == EDbColText)
00734 columnNameAndSize.AppendNum(colIter->iMaxLength);
00735 else
00736 columnNameAndSize.Append(KNoSize);
00737 resultArray->AppendL(columnNameAndSize);
00738 colIter++;
00739 }
00740 CleanupStack::PopAndDestroy(colSet);
00741 CleanupStack::Pop(resultArray);
00742
00743
00744 CleanupStack::PopAndDestroy();
00745
00746 return resultArray;
00747 }
00748
00749
00750
00751
00752
00753
00754 TInt CBookDb::HasDateColumn(TBool& aReturnValue)
00755 {
00756 RDbTable booksTable;
00757 aReturnValue = EFalse;
00758
00759
00760 TInt err = booksTable.Open(iBookDb, KBooksTable, booksTable.EReadOnly);
00761 if(err!=KErrNone)
00762 {
00763 return err;
00764 }
00765
00766
00767
00768 CDbColSet* colSet=NULL;
00769 TRAP(err, colSet = booksTable.ColSetL());
00770 if(err!=KErrNone)
00771 {
00772 return err;
00773 }
00774
00775 TDbColSetIter colIter(*colSet);
00776 while(colIter)
00777 {
00778 if( (colIter->iName).Compare(KBooksDateCol) == 0)
00779 {
00780 aReturnValue = ETrue;
00781 break;
00782 }
00783 colIter++;
00784 }
00785
00786 return KErrNone;
00787 }
00788
00789
00790
00791
00792
00793
00794 TInt CBookDb::AddDateColumn()
00795 {
00796 _LIT(KSqlAddDate, "ALTER TABLE Books ADD PublishDate DATE");
00797 return iBookDb.Execute(KSqlAddDate);
00798 }
00799
00800
00801
00802
00803
00804
00805 TInt CBookDb::RemoveDateColumn()
00806 {
00807 _LIT(KSqlRemoveDate, "ALTER TABLE Books DROP PublishDate");
00808 return iBookDb.Execute(KSqlRemoveDate);
00809 }
00810