//------------------------------------------------------------------------------
bool CvDatabaseUtility::PopulateArrayByExistence(int*& pArray, const char* szTypeTableName, const char* szDataTableName, const char* szTypeColumn, const char* szFilterColumn, const char* szFilterValue)
{
	InitializeArray(pArray, MaxRows(szTypeTableName), -1);

	std::string strKey = "_PABE_";
	strKey.append(szTypeTableName);
	strKey.append(szDataTableName);
	strKey.append(szFilterColumn);

	Database::Results* pResults = GetResults(strKey);
	if(pResults == NULL)
	{
		char szSQL[512];
		sprintf_s(szSQL, "select %s.ID from %s inner join %s on %s = %s.Type where %s = ?", szTypeTableName, szDataTableName, szTypeTableName, szTypeColumn, szTypeTableName, szFilterColumn);
		pResults = PrepareResults(strKey, szSQL);
		if(pResults == NULL)
			return false;
	}

	if(!pResults->Bind(1, szFilterValue, false))
	{
		CvAssertMsg(false, GetErrorMessage());
		return false;
	}

	int idx = 0;
	while(pResults->Step())
	{
		pArray[idx++] = pResults->GetInt(0);
	}

	pResults->Reset();

	return true;
}
//------------------------------------------------------------------------------
bool CvDatabaseUtility::PopulateArrayByValue(int*& pArray, const char* szTypeTableName, const char* szDataTableName, const char* szTypeColumn, const char* szFilterColumn, const char* szFilterValue, const char* szValueColumn, int iDefaultValue /* = 0 */, int iMinArraySize /* = 0 */)
{
	int iSize = MaxRows(szTypeTableName);
	InitializeArray(pArray, (iSize<iMinArraySize)?iMinArraySize:iSize, iDefaultValue);

	std::string strKey = "_PABV_";
	strKey.append(szTypeTableName);
	strKey.append(szDataTableName);
	strKey.append(szFilterColumn);
	strKey.append(szValueColumn);

	Database::Results* pResults = GetResults(strKey);
	if(pResults == NULL)
	{
		char szSQL[512];
		sprintf_s(szSQL, "select %s.ID, %s from %s inner join %s on %s = %s.Type where %s = ?", szTypeTableName, szValueColumn, szDataTableName, szTypeTableName, szTypeColumn, szTypeTableName, szFilterColumn);
		pResults = PrepareResults(strKey, szSQL);
		if(pResults == NULL)
			return false;
	}

	if(!pResults->Bind(1, szFilterValue, false))
	{
		CvAssertMsg(false, GetErrorMessage());
		return false;
	}
	while(pResults->Step())
	{
		const int idx = pResults->GetInt(0);
		const int value = pResults->GetInt(1);
		pArray[idx] = value;
	}

	pResults->Reset();

	return true;
}
Exemple #3
0
bool CIndex::Search(CSearchObject& SearchObject) {
    
    Trace(tagSearch, levVerbose, ("CIndex::Search - entering."));
    
    bool Result = false;
    SearchObject.m_SearchOptions.m_DateAfterValid = false;
    SearchObject.m_SearchOptions.m_DateBeforeValid = false;
    
    PrepareKeywords(SearchObject);
    
    // cout << "find:[" << SearchObject.m_SearchData.m_Words << "]" << endl;

    for (int n = 0; n < (int) SearchObject.m_SearchData.m_Words.GetSize(); n++) {
        Trace(tagSearch, levInfo, ("CIndex::Search - [%s]", SearchObject.m_SearchData.m_Words[n].GetBuffer()));
    }

    SearchObject.m_HasNext = 0;
	SearchObject.m_IndexSize = m_Searcher.GetAlreadyIndexedPages().CountBits(true);

    if (SearchObject.m_SearchData.m_Words.GetSize()) {
        
        Result = m_Searcher.Find(SearchObject.m_SearchData);   
        
        if (SearchObject.m_SearchData.m_Results.GetSize() != 0) {
            PrepareResults(SearchObject);
            SortResults(SearchObject, SearchObject.m_SortType);
            SearchObject.m_HasNext = SearchObject.m_SearchData.m_Results.GetSize();
            
            if (SearchObject.m_Quant == -1) {
                SearchObject.m_SearchResults.SetDim(SearchObject.m_SearchData.m_Results.GetSize());
            } else {
                SearchObject.m_SearchResults.SetDim(BASE_MAX(BASE_MIN(100, SearchObject.m_Quant), 10000));
            }

            Trace(tagSearch, levInfo, ("CIndex::Search - redim of displayable search results [%d]", SearchObject.m_SearchResults.GetDim()));

            time_t time_t_time; time(&time_t_time); struct tm tm_time;
            base_localtime(time_t_time, tm_time);
            int curDayCount = CDate::DayCount(tm_time.tm_mon, tm_time.tm_mday, tm_time.tm_year);
            /* structure passed for values */
            CIndexMapV IndexMapStructure;
            /* Index parent object */
            IndexMapStructure.m_Parent = SearchObject.m_Parent;
            /* get the string to map to */
            CString MapString = SearchObject.m_Parent->GetValue("MAP");
            /* get the map to use for the date */
            IndexMapStructure.m_DateMap = SearchObject.m_Parent->GetValue("DATE");
            struct tm fDate;
            /* get the ForceQuote option */
            IndexMapStructure.m_ForceQuote = CString::StrToInt(SearchObject.m_Parent->GetValue("QUOTE"));
            
            for (int l=SearchObject.m_Start; 
                 (l<(int) SearchObject.m_SearchData.m_Results.GetSize())&&
                 ((l<(SearchObject.m_Start+SearchObject.m_Quant) || (SearchObject.m_Quant < 0))
                 ); l++) {
                
                // Trace(tagSearch, levInfo, ("CIndex::Search - mapping result [%d]", l));
                
                int lResult = SearchObject.m_SearchData.m_Results[l];

                m_INFManager.StartReading();            
                int INFManagerSize = (int) m_INFManager.GetSize();
                m_INFManager.StopReading();
                
                m_URLManager.StartReading();
                int URLManagerSize = (int) m_URLManager.GetUrlTree().GetSize();
                m_URLManager.StopReading();
                
                if ((INFManagerSize > lResult)&&
                    (URLManagerSize > lResult)) {
                    
                    /* URL / Search Index */
                    IndexMapStructure.m_SearchData = &SearchObject.m_SearchData;
                    IndexMapStructure.m_ResultPosition = SearchObject.m_SearchData.m_ResultsPositions[l];
                    IndexMapStructure.m_Url = GetURLLink(lResult);

					// domain sort will attempt to patch the url to the topmost one
					if (SearchObject.m_SortType.Same("DOMAIN")) {
						CUrl DomainUrl(IndexMapStructure.m_Url);
						IndexMapStructure.m_Url = DomainUrl.GetHttpServer();
						IndexMapStructure.m_HashInfo.SetSize(6);
						IndexMapStructure.m_HashInfo[1] = "0";						
					} else {					
						m_INFManager.StartReading();
						IndexMapStructure.m_HashInfo = m_INFManager[lResult];
						m_INFManager.StopReading();
					}
					
                    /* replace a portion of the URL by something else if set from parent */
                    SearchObject.m_Parent->ProcessResultsURL(IndexMapStructure.m_Url);

                    Trace(tagSearch, levInfo, ("CIndex::Search - url [%s]", IndexMapStructure.m_Url.GetBuffer()));
                    
                    if (IndexMapStructure.m_HashInfo.GetSize() >= 6) {
    
                        /* results index */                        
                        IndexMapStructure.m_Index = l + 1;
                    
                        CreateDateObjects(
                            * SearchObject.m_Parent, 
                            IndexMapStructure,
                            fDate,
                            curDayCount);
                        
                        SearchObject.m_SearchResults += GetMapTerm(MapString, IndexMapStructure);                        
                    }
                }
            }
        }
    }
    
    Trace(tagSearch, levVerbose, ("CIndex::Search - leaving."));
    
    return Result;
}