secureswitools/swisistools/source/interpretsislib/dbhelper.cpp
branchRCL_3
changeset 26 8b7f4e561641
parent 25 7333d7932ef7
equal deleted inserted replaced
25:7333d7932ef7 26:8b7f4e561641
     1 /*
     1 /*
     2 * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    29 #include <sstream>
    29 #include <sstream>
    30 #include <stdexcept>
    30 #include <stdexcept>
    31 
    31 
    32 const int DbHelper::Absent = -1;
    32 const int DbHelper::Absent = -1;
    33 
    33 
    34 DbHelper::~DbHelper()
       
    35 	{
       
    36 	delete iScrDbHandler;
       
    37 	}
       
    38 
       
    39 DbHelper::DbHelper( const std::wstring& aDrivePath, bool aIsSystemDrive)
    34 DbHelper::DbHelper( const std::wstring& aDrivePath, bool aIsSystemDrive)
    40 	{
    35 	{
    41 
    36 	std::string dllName = "sqlite3.dll";
    42   #ifdef __LINUX__
       
    43   std::string dllName = "sqlite-3.6.1.so"; 	  	  	 
       
    44   #else 	  	  	 
       
    45   std::string dllName = "sqlite3.dll"; 
       
    46   #endif		
       
    47 	std::string dbName; 
    37 	std::string dbName; 
    48 	
    38 	
    49 	if (aIsSystemDrive)
    39 	if (aIsSystemDrive)
    50 	#ifdef __TOOLS2_LINUX__
       
    51 		dbName = wstring2string(aDrivePath) + "/sys/install/scr/scr.db";
       
    52 	#else
       
    53 		dbName = wstring2string(aDrivePath) + "\\sys\\install\\scr\\scr.db";
    40 		dbName = wstring2string(aDrivePath) + "\\sys\\install\\scr\\scr.db";
    54 	#endif
       
    55 	else 
    41 	else 
    56 	#ifdef __TOOLS2_LINUX__
       
    57 		dbName = wstring2string(aDrivePath) + "/sys/install/scr/provisioned/scr.db";
       
    58 	#else
       
    59 		dbName = wstring2string(aDrivePath) + "\\sys\\install\\scr\\provisioned\\scr.db";
    42 		dbName = wstring2string(aDrivePath) + "\\sys\\install\\scr\\provisioned\\scr.db";
    60 	#endif
       
    61 
       
    62 	std::wstring wdbname(string2wstring(dbName));
       
    63 	//Create the SCR DB if not present in the System Drive
       
    64 	if(!FileExists(wdbname))
       
    65 		{
       
    66 		const char* epocRoot = getenv("EPOCROOT");
       
    67 		if(NULL == epocRoot)
       
    68 			{
       
    69 				std::string emsg(" EPOCROOT environment variable not specified.");
       
    70 				int retCode = ExceptionCodes::EEnvNotSpecified;
       
    71 				throw InterpretSisError(emsg,retCode);
       
    72 			}
       
    73 		std::string epocRootStr(epocRoot); 
       
    74 
       
    75 		#ifndef __TOOLS2_LINUX__
       
    76 			std::wstring swEnvInfo = string2wstring(epocRootStr) + L"epoc32\\tools\\create_db.xml";
       
    77 		#else
       
    78 			std::wstring swEnvInfo = string2wstring(epocRootStr) + L"epoc32/tools/create_db.xml";
       
    79 		#endif
       
    80 
       
    81 		if(FileExists(swEnvInfo))
       
    82 			{
       
    83 			#ifdef __TOOLS2_LINUX__
       
    84 				std::string executable = "scrtool";
       
    85 			#else
       
    86 				std::string executable = "scrtool.exe";
       
    87 			#endif
       
    88 			std::string command;
       
    89 			
       
    90 			command = executable + " -c " + dbName + " -f " + wstring2string(swEnvInfo);
       
    91 			
       
    92 			std::cout << "Creating DB : " << command << std::endl;
       
    93 			
       
    94 			int error = system(command.c_str());
       
    95 			if(error != 0)
       
    96 				{
       
    97 					std::string err = "Scrtool failed to create the database.";
       
    98 					throw InterpretSisError(err, DATABASE_UPDATE_FAILED);
       
    99 				}
       
   100 			}
       
   101 		else
       
   102 			{
       
   103 				LERROR(L"Failed to create the database.");
       
   104 				std::string emessage(" XML file /epoc32/tools/create_db.xml which contains software environment information is not found.");
       
   105 				int returnCode = ExceptionCodes::EFileNotPresent;
       
   106 				throw InterpretSisError(emessage,returnCode);
       
   107 			}
       
   108 		}
       
   109 
    43 
   110 	try
    44 	try
   111 		{
    45 		{
   112 		iScrDbHandler = new CDbProcessor(dllName, dbName);		
    46 		iScrDbHandler = new CDbProcessor(dllName, dbName);		
   113 		}
    47 		}
   114 	catch(CException& aException)
    48 	catch(CException& aException)
   115 		{
    49 		{
   116 		int returnCode = aException.GetCode();
    50 		int returnCode = aException.GetCode();
   117 		std::wstring emessage = string2wstring( aException.GetMessageA() );
    51 		std::wstring emessage = Utf8ToUcs2( aException.GetMessageA() );
   118 
    52 
   119 		returnCode = (returnCode == ExceptionCodes::ELibraryLoadError) ? LIBRARYLOAD_ERROR : returnCode;
    53 		returnCode = (returnCode == ExceptionCodes::ELibraryLoadError) ? LIBRARYLOAD_ERROR : returnCode;
   120 		throw InterpretSisError(emessage,returnCode);		
    54 		throw InterpretSisError(emessage,returnCode);		
   121 		}
    55 		}
   122 	}
    56 	}
   131 	if(!stmtUid->ProcessNextRow())
    65 	if(!stmtUid->ProcessNextRow())
   132 		return false;
    66 		return false;
   133 		
    67 		
   134 	TInt32 uid = stmtUid->IntColumn(0);
    68 	TInt32 uid = stmtUid->IntColumn(0);
   135 	return (uid == aUid);
    69 	return (uid == aUid);
   136 	}
       
   137 
       
   138 TBool DbHelper::IsAppUidInstalled(TUint32 aAppUid) const
       
   139 	{
       
   140 	std::string selectUid("SELECT AppFile FROM AppRegistrationInfo WHERE AppUid=?;");
       
   141 	std::auto_ptr<CStatement> stmtUid(iScrDbHandler->PrepareStatement(selectUid));
       
   142 		
       
   143 	stmtUid->BindInt(1, aAppUid);
       
   144 	
       
   145 	if(!stmtUid->ProcessNextRow())
       
   146 		return false;
       
   147 		
       
   148 	return true;
       
   149 	}
    70 	}
   150 
    71 
   151 TInt32 DbHelper::GetComponentId(TInt32 aUid, const std::wstring aPackageName, const std::wstring aVendorName ) const
    72 TInt32 DbHelper::GetComponentId(TInt32 aUid, const std::wstring aPackageName, const std::wstring aVendorName ) const
   152 	{
    73 	{
   153 	std::string selectUid("SELECT ComponentId from ComponentProperties where Name='CompUid' and IntValue=? and ComponentId IN (SELECT ComponentId FROM \
    74 	std::string selectUid("SELECT ComponentId from ComponentProperties where Name='CompUid' and IntValue=? and ComponentId IN (SELECT ComponentId FROM \
   283 TUint32 DbHelper::GetUidFromFileName( const std::wstring& aFileName ) const
   204 TUint32 DbHelper::GetUidFromFileName( const std::wstring& aFileName ) const
   284 	{
   205 	{
   285 	std::string selCompId("SELECT ComponentId FROM ComponentsFiles WHERE Location=?;");
   206 	std::string selCompId("SELECT ComponentId FROM ComponentsFiles WHERE Location=?;");
   286 	std::auto_ptr<CStatement> stmtCompId(iScrDbHandler->PrepareStatement(selCompId));
   207 	std::auto_ptr<CStatement> stmtCompId(iScrDbHandler->PrepareStatement(selCompId));
   287 		
   208 		
   288 	stmtCompId->BindStr(1, aFileName.c_str());
   209 	stmtCompId->BindStr(1, aFileName);
   289 	
   210 	
   290 	TInt32 componentId = 0;
   211 	TInt32 componentId = 0;
   291 	if(stmtCompId->ProcessNextRow())
   212 	if(stmtCompId->ProcessNextRow())
   292 		{
   213 		{
   293 		componentId = stmtCompId->IntColumn(0);
   214 		componentId = stmtCompId->IntColumn(0);
   295 		}
   216 		}
   296 
   217 
   297 	std::string selCompPropsId("SELECT ComponentId FROM ComponentProperties WHERE StrValue=? and Name like 'WCFile%';");
   218 	std::string selCompPropsId("SELECT ComponentId FROM ComponentProperties WHERE StrValue=? and Name like 'WCFile%';");
   298 	std::auto_ptr<CStatement> stmtCompPropsId(iScrDbHandler->PrepareStatement(selCompPropsId));
   219 	std::auto_ptr<CStatement> stmtCompPropsId(iScrDbHandler->PrepareStatement(selCompPropsId));
   299 		
   220 		
   300 	stmtCompPropsId->BindStr(1, aFileName.c_str());
   221 	stmtCompPropsId->BindStr(1, aFileName);
   301 	
   222 	
   302 	if(stmtCompPropsId->ProcessNextRow())
   223 	if(stmtCompPropsId->ProcessNextRow())
   303 		{
   224 		{
   304 		componentId = stmtCompPropsId->IntColumn(0);
   225 		componentId = stmtCompPropsId->IntColumn(0);
   305 		return GetUid(componentId);
   226 		return GetUid(componentId);
   327 	return GetComponentPropertyIntValue(aComponentId,DbConstants::CompIndex);
   248 	return GetComponentPropertyIntValue(aComponentId,DbConstants::CompIndex);
   328 	}
   249 	}
   329 
   250 
   330 void DbHelper::RemoveEntry( TInt32 aComponentId ) const
   251 void DbHelper::RemoveEntry( TInt32 aComponentId ) const
   331 	{
   252 	{
   332 
       
   333 	std::vector<TInt32> appUidList = GetAppRegistrationInfoAppUid(aComponentId);
       
   334 	std::vector<TInt32> ::iterator appUidIter;
       
   335 	for( appUidIter = appUidList.begin(); appUidIter != appUidList.end() ; ++appUidIter )
       
   336 	{
       
   337 		// delete FileOwnershipInfo
       
   338 		std::string delFileOwnerInfo("DELETE FROM FileOwnershipInfo WHERE AppUid=?;");
       
   339 		std::auto_ptr<CStatement> stmtdelFileOwnerInfo(iScrDbHandler->PrepareStatement(delFileOwnerInfo));
       
   340 		stmtdelFileOwnerInfo->BindInt(1, *appUidIter);
       
   341 		stmtdelFileOwnerInfo->ExecuteStatement();
       
   342 
       
   343 		// delete AppProperties
       
   344 		std::string delAppProperties("DELETE FROM AppProperties WHERE AppUid=?;");
       
   345 		std::auto_ptr<CStatement> stmtAppProperties(iScrDbHandler->PrepareStatement(delAppProperties));
       
   346 		stmtAppProperties->BindInt(1, *appUidIter);
       
   347 		stmtAppProperties->ExecuteStatement();
       
   348 
       
   349 		std::vector<TInt32> serviceIdList = GetServiceInfoServiceID(*appUidIter);
       
   350 		
       
   351 		std::vector<TInt32> ::iterator serviceIdIter;
       
   352 		for( serviceIdIter = serviceIdList.begin(); serviceIdIter != serviceIdList.end() ; ++serviceIdIter )
       
   353 		{
       
   354 			// delete DataType
       
   355 			std::string delDataType("DELETE FROM DataType WHERE ServiceId=?;");
       
   356 			std::auto_ptr<CStatement> stmtDataType(iScrDbHandler->PrepareStatement(delDataType));
       
   357 			stmtDataType->BindInt(1, *serviceIdIter);
       
   358 			stmtDataType->ExecuteStatement();
       
   359 		}
       
   360 
       
   361 		// delete ServiceInfo
       
   362 		std::string delServiceInfo("DELETE FROM ServiceInfo WHERE AppUid=?;");
       
   363 		std::auto_ptr<CStatement> stmtServiceInfo(iScrDbHandler->PrepareStatement(delServiceInfo));
       
   364 		stmtServiceInfo->BindInt(1, *appUidIter);
       
   365 		stmtServiceInfo->ExecuteStatement();
       
   366 
       
   367 		std::vector<TInt32> localAppInfoIdList = GetLocalAppInfoID(*appUidIter);
       
   368 		
       
   369 		std::vector<TInt32> ::iterator localAppInfoIdIter;
       
   370 		for( localAppInfoIdIter = localAppInfoIdList.begin(); localAppInfoIdIter != localAppInfoIdList.end() ; ++localAppInfoIdIter )
       
   371 		{
       
   372 			std::vector<TInt32> viewDataCaptionIdList = GetViewDataCaptionAndIconID(*localAppInfoIdIter);
       
   373 			
       
   374 			std::vector<TInt32> ::iterator viewDataCaptionIdIter;
       
   375 			for( viewDataCaptionIdIter = viewDataCaptionIdList.begin(); viewDataCaptionIdIter != viewDataCaptionIdList.end() ; ++viewDataCaptionIdIter )
       
   376 			{
       
   377 				// delete CaptionAndIconInfo
       
   378 				std::string delCaptionInfo("DELETE FROM CaptionAndIconInfo WHERE CaptionAndIconId=?;");
       
   379 				std::auto_ptr<CStatement> stmtCaptionInfo(iScrDbHandler->PrepareStatement(delCaptionInfo));
       
   380 				stmtCaptionInfo->BindInt(1, *viewDataCaptionIdIter);
       
   381 				stmtCaptionInfo->ExecuteStatement();
       
   382 			}
       
   383 
       
   384 			// delete ViewData
       
   385 			std::string delViewData("DELETE FROM ViewData WHERE LocalAppInfoId=?;");
       
   386 			std::auto_ptr<CStatement> stmtViewData(iScrDbHandler->PrepareStatement(delViewData));
       
   387 			stmtViewData->BindInt(1, *localAppInfoIdIter);
       
   388 			stmtViewData->ExecuteStatement();
       
   389 		}
       
   390 
       
   391 		std::vector<TInt32> LocalCaptionIdList = GetLocalCaptionAndIconID(*appUidIter);
       
   392 		std::vector<TInt32> ::iterator LocalCaptionIdIter;
       
   393 		for( LocalCaptionIdIter = LocalCaptionIdList.begin(); LocalCaptionIdIter != LocalCaptionIdList.end() ; ++LocalCaptionIdIter )
       
   394 		{
       
   395 			// delete CaptionAndIconInfo
       
   396 			std::string delCaptionInfo("DELETE FROM CaptionAndIconInfo WHERE CaptionAndIconId=?;");
       
   397 			std::auto_ptr<CStatement> stmtCaptionInfo(iScrDbHandler->PrepareStatement(delCaptionInfo));
       
   398 			stmtCaptionInfo->BindInt(1, *LocalCaptionIdIter);
       
   399 			stmtCaptionInfo->ExecuteStatement();
       
   400 		}
       
   401 
       
   402 		// delete LocalizableAppInfo
       
   403 		std::string delLocalizableAppInfo("DELETE FROM LocalizableAppInfo WHERE AppUid=?;");
       
   404 		std::auto_ptr<CStatement> stmtLocalizableAppInfo(iScrDbHandler->PrepareStatement(delLocalizableAppInfo));
       
   405 		stmtLocalizableAppInfo->BindInt(1, *appUidIter);
       
   406 		stmtLocalizableAppInfo->ExecuteStatement();
       
   407 
       
   408 	}
       
   409 
       
   410 	// delete AppRegistrationInfo
       
   411 	std::string delAppRegistrationInfo("DELETE FROM AppRegistrationInfo WHERE ComponentId=?;");
       
   412 	std::auto_ptr<CStatement> stmtAppRegistrationInfo(iScrDbHandler->PrepareStatement(delAppRegistrationInfo));
       
   413 	stmtAppRegistrationInfo->BindInt(1, aComponentId);
       
   414 	stmtAppRegistrationInfo->ExecuteStatement();
       
   415 
       
   416 	
       
   417 	// delete component properties
   253 	// delete component properties
   418 	std::string delCompProps("DELETE FROM ComponentProperties WHERE ComponentId=?;");
   254 	std::string delCompProps("DELETE FROM ComponentProperties WHERE ComponentId=?;");
   419 	std::auto_ptr<CStatement> stmtDelCompProps(iScrDbHandler->PrepareStatement(delCompProps));
   255 	std::auto_ptr<CStatement> stmtDelCompProps(iScrDbHandler->PrepareStatement(delCompProps));
   420 	stmtDelCompProps->BindInt(1, aComponentId);
   256 	stmtDelCompProps->BindInt(1, aComponentId);
   421 	stmtDelCompProps->ExecuteStatement();
   257 	stmtDelCompProps->ExecuteStatement();
  1006 		return aDefaultValue;
   842 		return aDefaultValue;
  1007 
   843 
  1008 	return stmtCompProperty->IntColumn(0);
   844 	return stmtCompProperty->IntColumn(0);
  1009 	}
   845 	}
  1010 
   846 
  1011 std::vector<TInt32> DbHelper::GetServiceInfoServiceID(TInt32 aAppUid) const
       
  1012 {
       
  1013 	std::vector<TInt32> serviceIdList;
       
  1014 	std::string selectServiceId("SELECT ServiceId FROM ServiceInfo WHERE AppUid=?;");
       
  1015 	std::auto_ptr<CStatement> stmtServiceId(iScrDbHandler->PrepareStatement(selectServiceId));
       
  1016 	
       
  1017 	stmtServiceId->BindInt(1, aAppUid);
       
  1018 	while(stmtServiceId->ProcessNextRow())
       
  1019 	{
       
  1020 		serviceIdList.push_back(stmtServiceId->IntColumn(0));
       
  1021 	}
       
  1022 	
       
  1023 	return serviceIdList;
       
  1024 }
       
  1025 
       
  1026 
       
  1027 std::vector<TInt32> DbHelper::GetLocalAppInfoID(TInt32 aAppUid) const
       
  1028 {
       
  1029 	std::vector<TInt32> localAppInfoIdList;
       
  1030 	std::string selectLocalAppInfoId("SELECT LocalAppInfoId FROM LocalizableAppInfo WHERE AppUid=?;");
       
  1031 	std::auto_ptr<CStatement> stmtLocalAppInfoId(iScrDbHandler->PrepareStatement(selectLocalAppInfoId));
       
  1032 	
       
  1033 	stmtLocalAppInfoId->BindInt(1, aAppUid);
       
  1034 	while(stmtLocalAppInfoId->ProcessNextRow())
       
  1035 	{
       
  1036 		localAppInfoIdList.push_back(stmtLocalAppInfoId->IntColumn(0));
       
  1037 	}
       
  1038 	
       
  1039 	return localAppInfoIdList;
       
  1040 }
       
  1041 
       
  1042 
       
  1043 std::vector<TInt32> DbHelper::GetLocalCaptionAndIconID(TInt32 aAppUid) const
       
  1044 {
       
  1045 	std::vector<TInt32> localCaptionAndIconIdList;
       
  1046 	std::string selectLocalCaptionAndIconId("SELECT CaptionAndIconId FROM LocalizableAppInfo WHERE AppUid=?;");
       
  1047 	std::auto_ptr<CStatement> stmtLocalCaptionAndIconId(iScrDbHandler->PrepareStatement(selectLocalCaptionAndIconId));
       
  1048 	
       
  1049 	stmtLocalCaptionAndIconId->BindInt(1, aAppUid);
       
  1050 	while(stmtLocalCaptionAndIconId->ProcessNextRow())
       
  1051 	{
       
  1052 		localCaptionAndIconIdList.push_back(stmtLocalCaptionAndIconId->IntColumn(0));
       
  1053 	}
       
  1054 	
       
  1055 	return localCaptionAndIconIdList;
       
  1056 }
       
  1057 
       
  1058 
       
  1059 std::vector<TInt32> DbHelper::GetViewDataCaptionAndIconID(TInt32 aLocalAppInfoID) const
       
  1060 {
       
  1061 	std::vector<TInt32> viewCaptionIdList;
       
  1062 	std::string selectViewCaptionId("SELECT CaptionAndIconId FROM ViewData WHERE LocalAppInfoId=?;");
       
  1063 	std::auto_ptr<CStatement> stmtViewCaptionId(iScrDbHandler->PrepareStatement(selectViewCaptionId));
       
  1064 	
       
  1065 	stmtViewCaptionId->BindInt(1, aLocalAppInfoID);
       
  1066 	while(stmtViewCaptionId->ProcessNextRow())
       
  1067 	{
       
  1068 		viewCaptionIdList.push_back(stmtViewCaptionId->IntColumn(0));
       
  1069 	}
       
  1070 	
       
  1071 	return viewCaptionIdList;
       
  1072 }
       
  1073 
       
  1074 
       
  1075 std::vector<TInt32> DbHelper::GetAppRegistrationInfoAppUid(TInt32 aComponentId) const
       
  1076 {
       
  1077 	std::vector<TInt32> appUidList;
       
  1078 	std::string selectAppUid("SELECT AppUid FROM AppRegistrationInfo WHERE ComponentId=?;");
       
  1079 	std::auto_ptr<CStatement> stmtAppUid(iScrDbHandler->PrepareStatement(selectAppUid));
       
  1080 
       
  1081 	stmtAppUid->BindInt(1, aComponentId);
       
  1082 	while(stmtAppUid->ProcessNextRow())
       
  1083 	{
       
  1084 		appUidList.push_back(stmtAppUid->IntColumn(0));
       
  1085 	}
       
  1086 
       
  1087 	return appUidList;
       
  1088 }
       
  1089 
       
  1090 TInt32 DbHelper::GetComponentPropertyIntFormattedValue(TInt32 aComponentId, std::wstring aComponentPropertyName, TInt32 aIntFormatter) const
   847 TInt32 DbHelper::GetComponentPropertyIntFormattedValue(TInt32 aComponentId, std::wstring aComponentPropertyName, TInt32 aIntFormatter) const
  1091 	{
   848 	{
  1092 	TInt32 propertyValue = 0;
   849 	TInt32 propertyValue = 0;
  1093 	
   850 	
  1094 	std::string selectIntProperty("SELECT IntValue FROM ComponentProperties WHERE ComponentId=? and Name=?;");
   851 	std::string selectIntProperty("SELECT IntValue FROM ComponentProperties WHERE ComponentId=? and Name=?;");