src/EquivalentNamesUploader/database/DBQuery.cpp
// Gets all documents in which the entity is in the second slot of a relation
// in relationsMain, and the first slot of the relation fills a role 
// in eventsSecondary
// Relations and events are specified as a vector of TYPE.Subtype strings

Gets all documents in which the entity is in the second slot of a relation
in relationsMain, and the first slot of the relation fills a role
in eventsSecondary
Relations and events are specified as a vector of TYPE.Subtype strings
Gets all documents in which the entity is in the second slot of a relation in relationsMain and the first slot of the relation fills a role in eventsSecondary Relations and events are specified as a vector of TYPE . Subtype strings
bool DBQuery::getDocumentsForNamesInRelationEventIntersection(ValueVector* names, ValueVector* relationsMain, ValueVector* eventsSecondary, ValueVector* sources, ValueVector* dates, ValueVector* ingestDates, ValueVector* locations, int max_results, ResultSet* results) {
    wstringstream query;
    if ((!names) || (names->getNValues() == 0)) { return false; }
    if ((!relationsMain) || (relationsMain->getNValues() == 0)) { return false; }
	if ((!eventsSecondary) || (eventsSecondary->getNValues() == 0)) { return false; }
    if (!results) { return false; }
    query << "select distinct top " << max_results << " d.brandyID, ec1.mentions_mention_cnt_ratio as score from ";
    query << "(select distinct docID, entityID from Entity_mentions where head_norm in (";
    for (size_t i = 0; i < names->getNValues(); i++) {
        if (i > 0) query << ",";
        query << _sqlQueryPhrasePrefix << names->getValueForSQL(i) << "'";
    }
    query << ")) as em1, ";
    query << "Entity_counts ec1, Relations r1, Events v, Event_Arguments va, Documents d, Sources s where ";
    query << "d.docID = em1.docID and d.docID = ec1.docID and d.docID = r1.docID and ";
	query << "d.docID = v.docID and d.docID = va.docID and ";
	query << "r1.entityID_right = em1.entityID and va.entityID = r1.entityID_left and ";
	query << "va.eventID = v.eventID and ";
    query << "em1.entityID = ec1.entityID and d.sourceid = s.sourceid AND ";
    query << "    (";
    for (size_t i = 0; i < relationsMain->getNValues(); i++) {
        wstring relation = relationsMain->getValueForSQL(i);
        size_t pos = relation.find(L'.');
        if (pos <= 0) { 
            SessionLogger::info("BRANDY") << "Could not parse relation " << relation << "\n"; 
            return false;
        }
        wstring type = relation.substr(0, pos);
        wstring subtype = relation.substr(pos+1);
        if (i > 0) { query << " OR "; }
        query << "(r1.relation_type = '" << type << "' AND r1.relation_subtype = '" << subtype << "')";
    }
    query << "    ) AND ";
	query << "    (";
    for (size_t i = 0; i < eventsSecondary->getNValues(); i++) {
        wstring ev = eventsSecondary->getValueForSQL(i);
        size_t pos = ev.find(L'.');
        if (pos <= 0) { 
            SessionLogger::info("BRANDY") << "Could not parse event " << ev << "\n"; 
            return false;
        }
        wstring type = ev.substr(0, pos);
        wstring subtype = ev.substr(pos+1);
        if (i > 0) { query << " OR "; }
        query << "(v.event_type = '" << type << "' AND v.event_subtype = '" << subtype << "')";
    }
    query << ")";
    query << getSourcesDatesLocationsSQL(sources, dates, ingestDates, locations);
    query << "order by score desc, brandyID asc";
    vector<string> column_names;
    vector<int> column_types;
    column_names.push_back("brandyID");
    column_types.push_back(CHAR_TYPE);
    column_names.push_back("score");
    column_types.push_back(DOUBLE_TYPE);
    SessionLogger::info("BRANDY") << L"Query:\n" << query.str().c_str() << L"\n";  
    printQuery(L"getDocumentsForNamesInRelationEventIntersection", query.str().c_str());
    bool success = getResults(query.str(), column_names, column_types, results);
    return success;
}
bool DBQuery :: getDocumentsForNamesInRelationEventIntersection ( ValueVector * names , ValueVector * relationsMain , ValueVector * eventsSecondary , ValueVector * sources , ValueVector * dates , ValueVector * ingestDates , ValueVector * locations , int max_results , ResultSet * results ) { wstringstream query ; if ( ( ! names ) || ( names -> getNValues ( ) == 0 ) ) { return false ; } if ( ( ! relationsMain ) || ( relationsMain -> getNValues ( ) == 0 ) ) { return false ; } if ( ( ! eventsSecondary ) || ( eventsSecondary -> getNValues ( ) == 0 ) ) { return false ; } if ( ! results ) { return false ; } query << " " << max_results << " " ; query << " " ; for ( size_t i = 0 ; i < names -> getNValues ( ) ; i ++ ) { if ( i > 0 ) query << " " ; query << _sqlQueryPhrasePrefix << names -> getValueForSQL ( i ) << " " ; } query << " " ; query << " " ; query << " " ; query << " " ; query << " " ; query << " " ; query << " " ; query << " " ; for ( size_t i = 0 ; i < relationsMain -> getNValues ( ) ; i ++ ) { wstring relation = relationsMain -> getValueForSQL ( i ) ; size_t pos = relation . find ( L' ' ) ; if ( pos <= 0 ) { SessionLogger :: info ( " " ) << " " << relation << " \n " ; return false ; } wstring type = relation . substr ( 0 , pos ) ; wstring subtype = relation . substr ( pos + 1 ) ; if ( i > 0 ) { query << " " ; } query << " " << type << " " << subtype << " " ; } query << " " ; query << " " ; for ( size_t i = 0 ; i < eventsSecondary -> getNValues ( ) ; i ++ ) { wstring ev = eventsSecondary -> getValueForSQL ( i ) ; size_t pos = ev . find ( L' ' ) ; if ( pos <= 0 ) { SessionLogger :: info ( " " ) << " " << ev << " \n " ; return false ; } wstring type = ev . substr ( 0 , pos ) ; wstring subtype = ev . substr ( pos + 1 ) ; if ( i > 0 ) { query << " " ; } query << " " << type << " " << subtype << " " ; } query << " " ; query << getSourcesDatesLocationsSQL ( sources , dates , ingestDates , locations ) ; query << " " ; vector < string > column_names ; vector < int > column_types ; column_names . push_back ( " " ) ; column_types . push_back ( CHAR_TYPE ) ; column_names . push_back ( " " ) ; column_types . push_back ( DOUBLE_TYPE ) ; SessionLogger :: info ( " " ) << L" \n " << query . str ( ) . c_str ( ) << L" \n " ; printQuery ( L" " , query . str ( ) . c_str ( ) ) ; bool success = getResults ( query . str ( ) , column_names , column_types , results ) ; return success ; }
Gets all documents in which the entity is in the second slot of a relation
in relationsMain, and the first slot of the relation fills a role
in eventsSecondary
Relations and events are specified as a vector of TYPE.Subtype strings
Gets all documents in which the entity is in the second slot of a relation in relationsMain and the first slot of the relation fills a role in eventsSecondary Relations and events are specified as a vector of TYPE . Subtype strings
//TimedSection ts("DBQuery::getDocumentsForNamesInRelations");
 /* Creates a query of the form:
	select distinct top 100 d.brandyID, ec1.mentions_mention_cnt_ratio as score from
    (select distinct docID, entityID from Entity_mentions where head_norm in ('Microsoft', 'Microsoft Inc.')) as em1,
    Entity_counts ec1, Relations r1, Events v, Event_Arguments va, Documents d, Sources s where
    d.docID = em1.docID and d.docID = ec1.docID and d.docID = r1.docID and
    d.docID = va.docID and d.docID = v.docID and em1.entityID = ec1.entityID and d.sourceid = s.sourceid and 
    r1.entityID_right = em1.entityID and and va.entityID = r1.entityID_left and va.eventID = v.eventID and 
	em1.entityID = ec1.entityID and d.sourceid = s.sourceid AND
    ((r1.relation_type = 'ORG-AFF' AND r1.relation_subtype = 'Employment') OR
    (r1.relation_type = 'ORG-AFF' AND r1.relation_subtype ='Membership'))
	and 
	((v.event_type = 'Movement' AND v.event_subtype = 'Transport'))
    AND CONTAINS(source_text, N'"Baghdad"')
    AND s.CODE IN ('AFE', 'APE')
    AND d.DOCDATE BETWEEN '1 January 2003' AND '27 January 2004'
    order by score desc, brandyID asc
    */ // Set up our query
 // Add in our equivalent names
 // Loop over our relationsMain and split them into type and subtype		
 // Add the type and subtype to our query
 // Loop over our relationsSecondary and split them into type and subtype		
 // Add the type and subtype to our query
 // Get our results
 // add column name 
 // add column data type
 // DEBUGGING - REMOVE ME
