landmarks/locationlandmarks/tsrc/LandmarkTestModule/src/FT_DatabaseUtility.cpp
branchRCL_3
changeset 44 2b4ea9893b66
equal deleted inserted replaced
42:02ba3f1733c6 44:2b4ea9893b66
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *   ?description_line
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <f32file.h>
       
    22 #include "FT_DatabaseUtility.h"
       
    23 
       
    24 #include <D32DBMS.H>
       
    25 
       
    26 #include <EPos_Landmarks.h>
       
    27 // CONSTANTS
       
    28 
       
    29 // ============================ MEMBER FUNCTIONS ===============================
       
    30 
       
    31 // Code below taken from EPos_PosLmDatabaseManager.cpp
       
    32 // -----------------------------------------------------------------------------
       
    33 // FTDatabaseUtility::CreateDatabaseL
       
    34 // 
       
    35 // (other items were commented in a header).
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 void FTDatabaseUtility::CreateDatabaseL(const TDesC& aDbPath, TBool aReplaceDatabase, TBool aWrongId, TBool aCreateInvalidIndex)
       
    39     {
       
    40     RDbs dbSession;
       
    41     CleanupClosePushL(dbSession);
       
    42 
       
    43     User::LeaveIfError(dbSession.Connect());
       
    44 
       
    45     TPtrC dbPath(aDbPath);
       
    46     
       
    47 
       
    48     RDbNamedDatabase db;
       
    49     CleanupClosePushL(db);
       
    50 
       
    51     
       
    52     TUid KPosSecureUidWrongId = { 0x10204058 };
       
    53     _LIT(KWrongIdString, "secure[10204058]");
       
    54     
       
    55     if (aReplaceDatabase)
       
    56         {
       
    57         TInt err =KErrNone;
       
    58         
       
    59         if (!aWrongId) err = dbSession.DeleteDatabase(dbPath, KPosLmDbSecureUid);
       
    60         else err = dbSession.DeleteDatabase(dbPath, KPosSecureUidWrongId);
       
    61         
       
    62         if (err == KErrNone || err == KErrNotFound)
       
    63             {
       
    64             if (!aWrongId) User::LeaveIfError(db.Create(dbSession, dbPath, KPosLmDbSecureFormat));
       
    65             else User::LeaveIfError(db.Create(dbSession, dbPath, KWrongIdString));
       
    66             }
       
    67         else
       
    68             {
       
    69             User::Leave(err);
       
    70             }
       
    71         }
       
    72     else
       
    73         {
       
    74         if (!aWrongId) 
       
    75         {
       
    76 		User::LeaveIfError(db.Create(dbSession, dbPath, KPosLmDbSecureFormat));
       
    77 		
       
    78         }
       
    79         else User::LeaveIfError(db.Create(dbSession, dbPath, KWrongIdString));
       
    80         }
       
    81         
       
    82     CreateTablesL(db, aCreateInvalidIndex);
       
    83     
       
    84     CreateIndexesL(db);
       
    85     
       
    86     SetCounterToOneL(db, KPosLmLandmarkTable);
       
    87     SetCounterToOneL(db, KPosLmCategoryTable);
       
    88 
       
    89     // The settings table is created last so that a database can be validated 
       
    90     // when opening it (there will be a check for language there which is a 
       
    91     // part of the settings table).
       
    92     CreateSettingsTableL(db);
       
    93 
       
    94     CleanupStack::PopAndDestroy(2, &dbSession); // &db
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // FTDatabaseUtility::CreateTablesL
       
    99 // 
       
   100 // (other items were commented in a header).
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 void FTDatabaseUtility::CreateTablesL(
       
   104     RDbNamedDatabase& aDb, TBool aCreateInvalidIndex)
       
   105     {
       
   106     CreateLandmarkTableL(aDb, aCreateInvalidIndex);
       
   107     CreateLandmarkFieldTableL(aDb);
       
   108     CreateCategoryTableL(aDb, aCreateInvalidIndex);
       
   109     CreateLmCategoryTableL(aDb);
       
   110     CreateIconTableL(aDb);
       
   111 
       
   112     // Do not create settings table here. This is created in the end so that the
       
   113     // database can be validated when opening it.
       
   114     }
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // FTDatabaseUtility::CreateLandmarkTableL
       
   118 // 
       
   119 // (other items were commented in a header).
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 void FTDatabaseUtility::CreateLandmarkTableL(
       
   123     RDbNamedDatabase& aDb, TBool aCreateInvalidIndex)
       
   124     {
       
   125     CDbColSet* columns = CDbColSet::NewLC();
       
   126 
       
   127     TDbCol col(KPosLmLandmarkIdCol, EDbColUint32);
       
   128     col.iAttributes = col.EAutoIncrement;
       
   129     columns->AddL(col);
       
   130 
       
   131     columns->AddL(TDbCol(KPosLmLatitudeCol, EDbColReal64));
       
   132     columns->AddL(TDbCol(KPosLmLongitudeCol, EDbColReal64));
       
   133     columns->AddL(TDbCol(KPosLmAltitudeCol, EDbColReal32));
       
   134         	
       
   135     // Dont create the following tables when creating invalid table
       
   136     if (!aCreateInvalidIndex)
       
   137     	{
       
   138     	columns->AddL(TDbCol(KPosLmHorizAccCol, EDbColReal32));
       
   139         columns->AddL(TDbCol(KPosLmVertAccCol, EDbColReal32));
       
   140         columns->AddL(TDbCol(KPosLmCoverageRadiusCol, EDbColReal32));
       
   141         columns->AddL(TDbCol(KPosLmIconIdCol, EDbColUint32));
       
   142 		columns->AddL(TDbCol(KPosLmIconPicNrCol, EDbColInt32));
       
   143         columns->AddL(TDbCol(KPosLmMaskPicNrCol, EDbColInt32));
       
   144 	    columns->AddL(TDbCol(KPosLmNameCol, EDbColLongText, 
       
   145         	KPosLmMaxTextFieldLength));
       
   146     	}
       
   147 	
       
   148     columns->AddL(TDbCol(KPosLmNDescCol, EDbColLongText, 
       
   149         	KPosLmMaxDescriptionLength));
       
   150 
       
   151     User::LeaveIfError(aDb.CreateTable(KPosLmLandmarkTable, *columns));
       
   152     CleanupStack::PopAndDestroy(columns);
       
   153     }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // FTDatabaseUtility::CreateLandmarkFieldTableL
       
   157 // 
       
   158 // (other items were commented in a header).
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 void FTDatabaseUtility::CreateLandmarkFieldTableL(
       
   162     RDbNamedDatabase& aDb)
       
   163     {
       
   164     CDbColSet* columns = CDbColSet::NewLC();
       
   165 
       
   166     TDbCol col(KPosLmLandmarkIdCol, EDbColUint32);
       
   167     col.iAttributes = col.ENotNull;
       
   168     columns->AddL(TDbCol(col));
       
   169 
       
   170     TDbCol col2(KPosLmFieldTypeCol, EDbColUint16);
       
   171     col2.iAttributes = col2.ENotNull;
       
   172     columns->AddL(col2);
       
   173 
       
   174     columns->AddL(TDbCol(KPosLmFieldStringCol, EDbColLongText,
       
   175         KPosLmMaxTextFieldLength));
       
   176 
       
   177     User::LeaveIfError(aDb.CreateTable(KPosLmLandmarkFieldTable, *columns));
       
   178     CleanupStack::PopAndDestroy(columns);
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // FTDatabaseUtility::CreateCategoryTableL
       
   183 // 
       
   184 // (other items were commented in a header).
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 void FTDatabaseUtility::CreateCategoryTableL(
       
   188     RDbNamedDatabase& aDb, TBool aCreateInvalidIndex)
       
   189     {
       
   190     CDbColSet* columns = CDbColSet::NewLC();
       
   191     
       
   192     TDbCol col(KPosLmCategoryIdCol, EDbColUint32);
       
   193     col.iAttributes = col.EAutoIncrement;
       
   194     columns->AddL(col);
       
   195 
       
   196     columns->AddL(TDbCol(KPosLmGlobalCategoryIdCol, EDbColUint16));
       
   197     columns->AddL(TDbCol(KPosLmIconIdCol, EDbColUint32));
       
   198     columns->AddL(TDbCol(KPosLmIconPicNrCol, EDbColInt32));
       
   199     columns->AddL(TDbCol(KPosLmMaskPicNrCol, EDbColInt32));
       
   200     columns->AddL(TDbCol(KPosLmCategoryNameCol, EDbColText, 
       
   201         KPosLmMaxCategoryNameLength));
       
   202 
       
   203     // Dont create the following tables when creating invalid table
       
   204     if (!aCreateInvalidIndex)
       
   205     	{
       
   206         columns->AddL(TDbCol(KPosLmCategoryNameModifiedCol, EDbColBit));
       
   207         }
       
   208 
       
   209     User::LeaveIfError(aDb.CreateTable(KPosLmCategoryTable, *columns));
       
   210     CleanupStack::PopAndDestroy(columns);
       
   211     }
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // FTDatabaseUtility::CreateLmCategoryTableL
       
   215 // 
       
   216 // (other items were commented in a header).
       
   217 // -----------------------------------------------------------------------------
       
   218 //
       
   219 void FTDatabaseUtility::CreateLmCategoryTableL(
       
   220     RDbNamedDatabase& aDb)
       
   221     {
       
   222     CDbColSet* columns = CDbColSet::NewLC();
       
   223 
       
   224     TDbCol col(KPosLmLandmarkIdCol, EDbColUint32);
       
   225     col.iAttributes = col.ENotNull;
       
   226     columns->AddL(col);
       
   227 
       
   228     TDbCol col2(KPosLmCategoryIdCol, EDbColUint32);
       
   229     col2.iAttributes = col2.ENotNull;
       
   230     columns->AddL(col2);
       
   231 
       
   232     User::LeaveIfError(aDb.CreateTable(KPosLmLandmarkCategoryTable, *columns));
       
   233     CleanupStack::PopAndDestroy(columns);
       
   234     }
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // FTDatabaseUtility::CreateIconTableL
       
   238 // 
       
   239 // (other items were commented in a header).
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 void FTDatabaseUtility::CreateIconTableL(
       
   243     RDbNamedDatabase& aDb)
       
   244     {
       
   245     CDbColSet* columns = CDbColSet::NewLC();
       
   246 
       
   247     TDbCol col(KPosLmIconIdCol, EDbColUint32);
       
   248     col.iAttributes = col.EAutoIncrement;
       
   249     columns->AddL(col);
       
   250 
       
   251     columns->AddL(TDbCol(KPosLmIconPathCol, EDbColLongText, KMaxFileName));
       
   252 
       
   253     User::LeaveIfError(aDb.CreateTable(KPosLmIconTable, *columns));
       
   254     CleanupStack::PopAndDestroy(columns);
       
   255     }
       
   256 
       
   257 // -----------------------------------------------------------------------------
       
   258 // FTDatabaseUtility::CreateSettingsTableL
       
   259 // 
       
   260 // (other items were commented in a header).
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 void FTDatabaseUtility::CreateSettingsTableL(
       
   264     RDbNamedDatabase& aDb)
       
   265     {
       
   266     CDbColSet* columns = CDbColSet::NewLC();
       
   267 
       
   268     columns->AddL(TDbCol(KPosLmLanguageCol, EDbColInt32));
       
   269     columns->AddL(TDbCol(KPosLmInitFlagCol, EDbColInt32));
       
   270 
       
   271     User::LeaveIfError(aDb.CreateTable(KPosLmSettingsTable, *columns));
       
   272     CleanupStack::PopAndDestroy(columns);
       
   273     }
       
   274 
       
   275 // -----------------------------------------------------------------------------
       
   276 // FTDatabaseUtility::CreateIndexesL
       
   277 // 
       
   278 // (other items were commented in a header).
       
   279 // -----------------------------------------------------------------------------
       
   280 //
       
   281 void FTDatabaseUtility::CreateIndexesL(
       
   282     RDbNamedDatabase& aDb)
       
   283     {
       
   284     CreateIndexL(aDb, KPosLmLandmarkIdIndex, KPosLmLandmarkTable, 
       
   285         KPosLmLandmarkIdCol, ETrue);
       
   286     CreateIndexL(aDb, KPosLmCategoryIdIndex, KPosLmCategoryTable, 
       
   287         KPosLmCategoryIdCol, ETrue);
       
   288     CreateIndexL(aDb, KPosLmGlobalCategoryIdIndex, KPosLmCategoryTable, 
       
   289         KPosLmGlobalCategoryIdCol, EFalse);
       
   290     CreateIndexL(aDb, KPosLmCategoryNameIndex, KPosLmCategoryTable, 
       
   291         KPosLmCategoryNameCol, ETrue);
       
   292     CreateIndexL(aDb, KPosLmIconIdIndex, KPosLmIconTable, 
       
   293         KPosLmIconIdCol, ETrue);
       
   294     }
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // FTDatabaseUtility::CreateIndexL
       
   298 // 
       
   299 // (other items were commented in a header).
       
   300 // -----------------------------------------------------------------------------
       
   301 //
       
   302 void FTDatabaseUtility::CreateIndexL(
       
   303     RDbNamedDatabase& aDb,
       
   304     const TDesC& aIndex, 
       
   305     const TDesC& aTable,
       
   306     const TDesC& aColumn,
       
   307     TBool aUnique)
       
   308     {
       
   309     CDbKey* keys = CDbKey::NewLC();
       
   310     keys->AddL(TDbKeyCol(aColumn));
       
   311 
       
   312     if (aUnique)
       
   313         {
       
   314         keys->MakeUnique();
       
   315         }
       
   316 
       
   317     User::LeaveIfError(aDb.CreateIndex(aIndex, aTable, *keys));
       
   318     CleanupStack::PopAndDestroy(keys);
       
   319     }
       
   320 
       
   321 // -----------------------------------------------------------------------------
       
   322 // FTDatabaseUtility::SetCounterToOneL
       
   323 // 
       
   324 // (other items were commented in a header).
       
   325 // -----------------------------------------------------------------------------
       
   326 //
       
   327 void FTDatabaseUtility::SetCounterToOneL(
       
   328     RDbNamedDatabase& aDb, 
       
   329     const TDesC& aTable)
       
   330     {
       
   331     HBufC* sql = HBufC::NewLC(KPosLmSqlStatementMaxLen);
       
   332     sql->Des().Format(KPosLmSqlSelect, &KPosLmSqlAll, &aTable);
       
   333 
       
   334     RDbView view;
       
   335     CleanupClosePushL(view);
       
   336     User::LeaveIfError(view.Prepare(aDb, TDbQuery(*sql), 
       
   337         TDbWindow::EUnlimited));
       
   338 
       
   339     view.InsertL();
       
   340     view.PutL();
       
   341 
       
   342     if (!view.FirstL())
       
   343         {
       
   344         User::Leave(KErrGeneral);
       
   345         }
       
   346 
       
   347     view.DeleteL();
       
   348 
       
   349     CleanupStack::PopAndDestroy(2, sql); //&view
       
   350     }
       
   351 
       
   352 
       
   353 //  End of File