00001 /* 00002 * Copyright © 2008 Nokia Corporation. 00003 */ 00004 00005 #include <badesca.h> // CDesCArrayFlat 00006 #include <s32file.h> // CFileStore & CPermanentFileStore 00007 #include <bautils.h> // file helpers 00008 #include <eikenv.h> 00009 #include "DBMSEngine.h" 00010 00011 // Implementation specific constants 00012 const int KCustomSqlMaxLength = 256; 00013 const int KArrayGranularity = 5; // for CDesCArrayFlat 00014 00015 // --------------------------------------------------------------------------- 00016 // CBookDb::NewL() 00017 // 00018 // Create instance of the Book database engine. 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 // CBookDb::~CBookDb() 00031 // 00032 // Destructor of the Book database engine. Release resources. 00033 // --------------------------------------------------------------------------- 00034 CBookDb::~CBookDb() 00035 { 00036 Close(); // Just in case, if the user does not close this explicitely 00037 iFsSession.Close(); 00038 } 00039 00040 // --------------------------------------------------------------------------- 00041 // CBookDb::ConstructL() 00042 // 00043 // Second phase construction. Leaves, if RFs session cannot be created. 00044 // --------------------------------------------------------------------------- 00045 void CBookDb::ConstructL() 00046 { 00047 TInt err = iFsSession.Connect(); 00048 if(err) 00049 User::Leave(err); 00050 } 00051 00052 // --------------------------------------------------------------------------- 00053 // CBookDb::CBookDb() 00054 // 00055 // Constructor 00056 // --------------------------------------------------------------------------- 00057 CBookDb::CBookDb() 00058 { 00059 iOpen = EFalse; 00060 } 00061 00062 // --------------------------------------------------------------------------- 00063 // CBookDb::OpenDbL() 00064 // 00065 // Open existing Book database for exclusive access. 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());/* Set file store type*/ 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 // CBookDb::CreateDbL() 00093 // 00094 // Create a new database. The database will be in exclusive access mode. 00095 // --------------------------------------------------------------------------- 00096 TInt CBookDb::CreateDb(const TFileName& aNewBookFile) 00097 { 00098 Close(); 00099 00100 // Create empty database file. 00101 TRAPD(error, 00102 iFileStore = CPermanentFileStore::ReplaceL(iFsSession, aNewBookFile, 00103 EFileRead|EFileWrite); 00104 iFileStore->SetTypeL(iFileStore->Layout());// Set file store type 00105 TStreamId id = iBookDb.CreateL(iFileStore);// Create stream object 00106 iFileStore->SetRootL(id);// Keep database id as root of store 00107 iFileStore->CommitL();// Complete creation by commiting 00108 // Create Book tables and indexes 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 // CBookDb::RemoveDb() 00123 // 00124 // First remove the Books table. Then remove the database file. 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 // It is enough to delete the database file directly. Because this example 00136 // demonstrates DDL statements, it first opens and drops the Books table. 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 // CBookDb::Close() 00153 // 00154 // Close the database. 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 // CBookDb::IsOpen() 00170 // 00171 // Return open status of the database. 00172 // --------------------------------------------------------------------------- 00173 TBool CBookDb::IsOpen() const 00174 { 00175 return iOpen; 00176 } 00177 00178 // --------------------------------------------------------------------------- 00179 // CBookDb::CreateBooksTableL() 00180 // 00181 // Creates Books table. Leaves, if the table cannot be created. 00182 // --------------------------------------------------------------------------- 00183 void CBookDb::CreateBooksTableL() 00184 { 00185 00186 // Specify columns for Books table 00187 TDbCol authorCol(KBooksAuthorCol, EDbColText); // Using default length 00188 TDbCol titleCol(KBooksTitleCol, EDbColText, KTitleMaxLength); 00189 titleCol.iAttributes = TDbCol::ENotNull; 00190 TDbCol descriptionCol(KBooksDescriptionCol, EDbColLongText); // Stream Data 00191 00192 // Add the columns to column set 00193 CDbColSet* bookColSet = CDbColSet::NewLC(); 00194 bookColSet->AddL(authorCol); 00195 bookColSet->AddL(titleCol); 00196 bookColSet->AddL(descriptionCol); 00197 00198 // Create the Books table 00199 User::LeaveIfError(iBookDb.CreateTable(KBooksTable, 00200 *bookColSet)); 00201 CleanupStack::PopAndDestroy(bookColSet); 00202 } 00203 00204 // --------------------------------------------------------------------------- 00205 // CBookDb::CreateBooksIndexL() 00206 // 00207 // Creates an index for Books table. Leaves, if the index cannot be created. 00208 // --------------------------------------------------------------------------- 00209 void CBookDb::CreateBooksIndexL() 00210 { 00211 // Create index consisting of two columns 00212 TDbKeyCol authorCol(KBooksAuthorCol); 00213 TDbKeyCol titleCol(KBooksTitleCol); 00214 00215 CDbKey* index = CDbKey::NewLC(); // create index key set 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 // CBookDb::DropBooksTable() 00225 // 00226 // Drop the Books table incrementally. Uses RDbIncremental and DDL statement. 00227 // --------------------------------------------------------------------------- 00228 void CBookDb::DropBooksTable() 00229 { 00230 00231 _LIT(KDropTable, "DROP TABLE "); 00232 00233 // Sql: DROP TABLE Books 00234 TBuf<KCustomSqlMaxLength> sqlStr; 00235 sqlStr.Append(KDropTable); 00236 sqlStr.Append(KBooksTable); 00237 00238 RDbIncremental incOp; 00239 TInt incStep = 0xFFFF; 00240 // Initialise Execution 00241 TInt incStat = incOp.Execute(iBookDb, sqlStr, incStep); 00242 while (incStep>0 && incStat==KErrNone) 00243 { 00244 incStat = incOp.Next(incStep); // Do the work 00245 } 00246 incOp.Close(); 00247 } 00248 00249 // --------------------------------------------------------------------------- 00250 // CBookDb::AddBookWithSqlL() 00251 // 00252 // Add a book to database using RDbView and SQL 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 // Sql: SELECT Author, Title, Description FROM Books ORDER BY Title, Author 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; // Create a view on the database 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; // Use stream to insert the description 00298 00299 TRAP(error, 00300 view.InsertL(); // Insert a row. Column order matches sql select statement 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()); // Complete insertion 00313 if(error!=KErrNone) 00314 { 00315 return error; 00316 } 00317 view.Close(); 00318 return KErrNone; 00319 } 00320 00321 // --------------------------------------------------------------------------- 00322 // CBookDb::AddBookWithCppApiL() 00323 // 00324 // Add a book to database using RDbTable API 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 // Create an updateable database table object 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); // col = 1 00354 table.SetColL(booksColSet->ColNo(KBooksTitleCol), aTitle); // col = 2 00355 // Use a stream for the long text column 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()); // Complete changes (the insertion) 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 // CBookDb::GetAllBooksL() 00381 // 00382 // Get array of all books in database. Format of each array item is: 00383 // <Author>|<Title>|<Description> 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; // A stream object for long columns 00415 readStream.OpenLC(table,descrColNo); 00416 readStream.ReadL(description, table.ColLength(descrColNo)); 00417 readStream.Close(); 00418 CleanupStack::Pop(); //readStream 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); // Copy rowText to resultArray 00427 } 00428 CleanupStack::PopAndDestroy(colSet); 00429 CleanupStack::Pop(resultArray); 00430 table.Close(); 00431 00432 return resultArray; 00433 } 00434 00435 // --------------------------------------------------------------------------- 00436 // CBookDb::GetABookFast() 00437 // 00438 // Get a book using index. Format of the result is: 00439 // <Author>|<Title>|<Description> 00440 // --------------------------------------------------------------------------- 00441 TInt CBookDb::GetABookFast(const TDesC& aTitle, TDes& aResult) 00442 { 00443 TInt err = KErrNone; 00444 TBuf<KDescriptionMaxLength> description; // Only 128 first characters read 00445 RDbTable rowset; 00446 00447 TDbSeekKey seekKey(aTitle); // Initialize one-column seek key 00448 00449 // Open view to "Books" table. Use index to browse the table. 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 // Query colum numbers for author, title, and description 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 // Search the index for aTitle 00473 TBool isTitle = false; 00474 TRAP(err, isTitle = rowset.SeekL(seekKey)); 00475 if(isTitle) 00476 { 00477 RDbColReadStream readStream; // A stream object for long columns 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 // CBookDb::GetBooksByKeyL() 00510 // 00511 // Get array of books from database according to column name and a search 00512 // pattern. Format of each array item is: 00513 // <Author>|<Title>|<Description> 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 // Sql: SELECT Author, Title, Description FROM Books 00531 // WHERE "aColumnName LIKE aSearchString" 00532 // ORDER BY Title, Author 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 // Create a view on the database 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 // Append each result row to array 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; // A stream object for long columns 00578 readStream.OpenLC(view, descrColNo); 00579 readStream.ReadL(description, view.ColLength(descrColNo)); 00580 readStream.Close(); 00581 CleanupStack::Pop(); //readStream 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 // CBookDb::RemoveBooks() 00601 // 00602 // Delete a book using title pattern and RDbUpdate (DML) 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 // Sql: DELETE FROM Books WHERE Title LIKE 'aTitle' 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 // Initialize execution and perform the first step. 00624 // Note: Execute() returns 0 (=KErrNone), but it does not affect database 00625 // until Next() is called. 00626 TInt incStat = updOp.Execute(iBookDb, sqlStr, EDbCompareFolded); 00627 incStat = updOp.Next(); // This will leave, if Execute() failed. 00628 00629 while( incStat == 1 ) // Just in case, if the operation has more steps 00630 { 00631 incStat = updOp.Next(); 00632 } 00633 aResultCount = updOp.RowCount(); 00634 updOp.Close(); 00635 return incStat; // KErrNone or system wide error code 00636 } 00637 00638 // --------------------------------------------------------------------------- 00639 // CBookDb::RemoveAllBooks() 00640 // 00641 // Delete books using asynchronous API. (RDbUpdate and DML) 00642 // This implementation is still synchronous, because it uses 00643 // User::WaitForRequest. Normally asynchronous functionality should be hidden 00644 // into active object and client callback interfaces. 00645 // --------------------------------------------------------------------------- 00646 TInt CBookDb::RemoveAllBooks(TInt& aResultCount) 00647 { 00648 _LIT(KDeleteFrom, "DELETE FROM "); 00649 00650 // Sql: DELETE FROM Books 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); // Start async operation. It returns 00661 // immediately. 00662 User::WaitForRequest(incStat); // For simplicity wait completion here. 00663 } 00664 00665 aResultCount = updOp.RowCount(); 00666 updOp.Close(); 00667 00668 if(updStat!=KErrNone) 00669 return updStat; // System wide error code 00670 else 00671 return incStat.Int(); // KErrNone or system wide error code 00672 } 00673 00674 00675 // --------------------------------------------------------------------------- 00676 // CBookDb::UpdateBookTitle() 00677 // 00678 // Update book title using SQL UPDATE. 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 // CBookDb::ColumnNamesAndSizesL() 00701 // 00702 // Get array of column names and sizes of the Books table. 00703 // --------------------------------------------------------------------------- 00704 CDesCArrayFlat* CBookDb::ColumnNamesAndSizesL() 00705 { 00706 RDbTable booksTable; 00707 TBuf<32> columnNameAndSize; 00708 _LIT(KDelimiter, ": "); 00709 _LIT(KNoSize,"No size"); 00710 00711 // Open the Books table. 00712 TInt err = booksTable.Open(iBookDb, KBooksTable, booksTable.EReadOnly); 00713 User::LeaveIfError(err); 00714 00715 CleanupClosePushL(booksTable); // Remember to pop and close 00716 00717 CDesCArrayFlat* resultArray = 00718 new (ELeave)CDesC16ArrayFlat(KArrayGranularity); 00719 CleanupStack::PushL(resultArray); 00720 00721 // Iterate through the colums of Books table. Extract the column name and 00722 // column size (size only for text columns).´ 00723 // Note: Description column is long text. Database limits its size 00724 // only by hardware. If size is queried, it is -1 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 // Pop the booksTable from cleanup stack and close it. 00744 CleanupStack::PopAndDestroy(); 00745 00746 return resultArray; 00747 } 00748 00749 // --------------------------------------------------------------------------- 00750 // CBookDb::HasDateColumnL() 00751 // 00752 // Tests wheter the Books table has date column 00753 // --------------------------------------------------------------------------- 00754 TInt CBookDb::HasDateColumn(TBool& aReturnValue) 00755 { 00756 RDbTable booksTable; 00757 aReturnValue = EFalse; 00758 00759 // Open the Books table. 00760 TInt err = booksTable.Open(iBookDb, KBooksTable, booksTable.EReadOnly); 00761 if(err!=KErrNone) 00762 { 00763 return err; 00764 } 00765 00766 // Iterate through the colums of Books table. Check whether there is 00767 // a 'PublishDate' column 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) // 0 = equal 00779 { 00780 aReturnValue = ETrue; 00781 break; 00782 } 00783 colIter++; 00784 } 00785 00786 return KErrNone; 00787 } 00788 00789 // --------------------------------------------------------------------------- 00790 // CBookDb::AddDateColumn() 00791 // 00792 // Adds date column to Books table (DDL). 00793 // --------------------------------------------------------------------------- 00794 TInt CBookDb::AddDateColumn() 00795 { 00796 _LIT(KSqlAddDate, "ALTER TABLE Books ADD PublishDate DATE"); 00797 return iBookDb.Execute(KSqlAddDate); 00798 } 00799 00800 // --------------------------------------------------------------------------- 00801 // CBookDb::RemoveDateColumn() 00802 // 00803 // Removes date column from Books table (DDL). 00804 // --------------------------------------------------------------------------- 00805 TInt CBookDb::RemoveDateColumn() 00806 { 00807 _LIT(KSqlRemoveDate, "ALTER TABLE Books DROP PublishDate"); 00808 return iBookDb.Execute(KSqlRemoveDate); 00809 } 00810
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.