//--------------------------------------------------------------------------//
//	CRURefreshTaskExecutor::VerifyForceTblListCorrectness()
//
// Check that the user did not refered to a un-existing base table
//--------------------------------------------------------------------------//
void CRURefreshTaskExecutor::VerifyForceTblListCorrectness()
{
	const CRUMVForceOptions &forceOption =
		*GetRefreshTask()->GetRootMV().GetMVForceOption();

	const CRUTableForceOptionsList &tblList = forceOption.GetTableForceList();

	DSListPosition pos = tblList.GetHeadPosition();

	while (NULL != pos) 
	{
		CRUTableForceOptions* forceOpt = tblList.GetNext(pos);
		
		CRUTbl *pTbl = GetRefreshTask()->GetRootMV().GetUsedTableByName(forceOpt->GetFullName());

		if(NULL == pTbl)
		{
			CRUException ex;
			ex.SetError(IDS_RU_FORCE_FILE_TABLE_NOT_EXISTS);
			ex.AddArgument(GetRefreshTask()->GetRootMV().GetFullName());
			ex.AddArgument(forceOpt->GetFullName());
			throw ex;
		}
	}
}
void CRUCache::FetchMVsInGroup(CDDSchema *pddSch, const CDSString& mvgName)
{
	CDDMVGroup *pddMVG = GetDDMVGroupByName(pddSch, mvgName);

	if (TRUE == pddMVG->IsEmpty()) 
	{
		// The source MVGROUP is empty
		CRUException e;
		e.SetError(IDS_RU_MVGROUP_EMPTY);
		e.AddArgument(pddMVG->GetFullName());
		throw e;
	}

	CDDUIDTripleList& uidList = pddMVG->GetAllMVs();
	DSListPosition pos = uidList.GetHeadPosition();

	while (NULL != pos) 
	{
		// Retrieve the DD object
		CDDUIDTriple uidt = uidList.GetNext(pos);

		CDDMV *pddMV = GetDDMVByUID(uidt);

		// Setup the CRUMV wrapper object
		FetchSingleInvolvedMV(pddMV);
	}
}
CDDMVGroup *CRUCache::GetDDMVGroupByName(CDDSchema *pddSch, const CDSString& mvgName)
{
	CDDMVGroup *pddMVG = pddSch->GetMVGroup(mvgName);

	if (NULL == pddMVG) // try public schema if necessary
  {
    CDDSchema *pddSchP = GetPublicSchema();
    if (pddSchP)
    {
      pddMVG = pddSchP->GetMVGroup(mvgName);
      if (pddMVG)
      { // if can be found in the public schema
        // update the catalog and schema
        CRUOptions &options = CRUGlobals::GetInstance()->GetOptions();  	  
        options.SetCatalogName(pddSchP->GetCatName());
        options.SetSchemaName(pddSchP->GetName());
      }
    }
  }

	if (NULL == pddMVG)
	{
		// Source MV group does not exist
		CRUException e;
		
		e.SetError(IDS_RU_INVALID_MVGROUP_NAME);
		CDSString fname = pddSch->GetFullName() + "." + mvgName;
		e.AddArgument(fname.c_string());
		throw e;	
	}

	return pddMVG;
}
CDDSchema *CRUCache::GetDDSchemaByName(const CDSString& catName,
									   const CDSString& schName)
{
  // check for DEFAULT_SCHEMA_ACCESS_ONLY
  // get default catalog & schema
  CRUOptions &options = CRUGlobals::GetInstance()->GetOptions();  	  
  CDSString curCatSch = catName + "." + schName;
  if (CDDControlQueryDefault::ViolateDefaultSchemaAccessOnly(
        options.GetDefaultSchema(), curCatSch))
  {
    CRUException e;
    e.SetError(IDS_RU_SCHEMA_ACCESS_VIOLATION);
    throw e;
  }

  CDDCatalog *pddCat = sqlNode_.GetCatalog(catName);
	if (NULL == pddCat) 
	{
		// Source catalog does not exist
		CRUException e;
						
		e.SetError(IDS_RU_INVALID_CATALOG_NAME);
		e.AddArgument(catName.c_string());
		throw e;
	}
	
	CDDSchema *pddSch = pddCat->GetSchema(schName, TRUE);

	if (NULL == pddSch) 
	{
		// Source schema does not exist
		CRUException e;
						
		e.SetError(IDS_RU_INVALID_SCHEMA_NAME);
		CDSString fname = catName + "." + schName;
		e.AddArgument(fname.c_string());
		throw e;	
	}

	return pddSch;
}
void CRUCache::FetchCascadedMVs(CDDSchema *pddSch, const CDSString& mvName)
{
	CDDMV *pddRootMV = GetDDMVByName(pddSch, mvName);

	if (CDDMV::eON_STATEMENT == pddRootMV->GetRefreshType())
	{
		// The CASCADE option is not allowed for ON STATEMENT MVs
		CRUException e;
		e.SetError(IDS_RU_ILLEGAL_CASCADE);
		e.AddArgument(pddRootMV->GetFullName());
		throw e;
	}

	// Start the recursive seacrh
	RecursiveFetchCascadedMVs(pddRootMV);
}
void CRURefreshTaskExecutor::CheckSingleDeltaRestriction()
{
	CRUMV &rootMV = GetRootMV();
	CRUTblList &tblList = rootMV.GetFullySyncTablesUsedByMe();

	DSListPosition pos = tblList.GetHeadPosition();
	while (NULL != pos)
	{
		CRUTbl *pTbl = tblList.GetNext(pos);
		RUASSERT (TRUE == pTbl->IsFullySynchronized());

		if (TRUE == pTbl->IsEmptyDeltaNeeded()
			&&
			TRUE == rootMV.IsDeltaNonEmpty(*pTbl))
		{
			CRUException errDesc;
			errDesc.SetError(IDS_RU_INCONSISTENT_JOIN);
			errDesc.AddArgument(rootMVName_);
			throw errDesc;
		}
	}
}