Exemplo n.º 1
0
sqlqueryresultlist sqlquery::search(
						tStr searchstr,
						sqlquery::en_queryType querytype,
						bool exactmatch,
						tStr filterstr)
{
	sqlqueryresultlist result;
	int rc;
	bool twoTerms = true;
	result.result_type = sqlqueryresultlist::sqlresultERROR;
	if ((m_db == NULL)||(searchstr.empty())||(m_basepath.empty())) return result;
	tStr sqlqry, srchterm, filterterm;
	sqlqueryresultlist::en_resultType resultType = sqlqueryresultlist::sqlresultFULL;
	if (exactmatch && (querytype == sqlresultFUNCSINFILE)) {searchstr.insert(0, "%");}
	srchterm = process_searchterm(searchstr.c_str(), exactmatch);
	if (filterstr.empty()) {filterterm = "%";}
	else {filterterm = process_searchterm(filterstr.c_str(), false);}
	switch (querytype)
	{
		case sqlquerySYMBOL:
			sqlqry = exactmatch ? SQL_EM_SYM : SQL_SYM;
			break;
		case sqlresultFUNC_MACRO:
			sqlqry = exactmatch ? SQL_EM_FUNC_MACRO : SQL_FUNC_MACRO;
			break;
		case sqlresultCLASS_STRUCT:
			sqlqry = exactmatch ? SQL_EM_CLASS_STRUCT : SQL_CLASS_STRUCT;
			break;
		case sqlresultINCLUDE:
			sqlqry = exactmatch ? SQL_EM_INCLUDE : SQL_INCLUDE;
			resultType = sqlqueryresultlist::sqlresultFILE_LINE;
			twoTerms = false;
			break;
		case sqlresultFILEPATH:
			sqlqry = exactmatch ? SQL_EM_FILEPATH : SQL_FILEPATH;
			resultType = sqlqueryresultlist::sqlresultFILE_ONLY;
			twoTerms = false;
			break;
		case sqlresultCALLINGFUNC:
			sqlqry = exactmatch ? SQL_EM_CALLINGFUNC : SQL_CALLINGFUNC;
			break;
		case sqlresultCALLEDFUNC:
			sqlqry = exactmatch ? SQL_EM_CALLEDFUNC : SQL_CALLEDFUNC;
			break;
		case sqlresultCALLSOFFUNC:
			sqlqry = exactmatch ? SQL_EM_CALLS_OF_FUNC : SQL_CALLS_OF_FUNC;
			break;
		case sqlresultMEMBERS:
			sqlqry = exactmatch ? SQL_EM_MEMBERS : SQL_MEMBERS;
			break;
		case sqlresultOWNERCLASS:
			sqlqry = exactmatch ? SQL_EM_OWNERCLASS : SQL_OWNERCLASS;
			break;
		case sqlresultPARENTCLASS:
			sqlqry = exactmatch ? SQL_EM_PARENTCLASS : SQL_PARENTCLASS;
			break;
		case sqlresultCHILDCLASS:
			sqlqry = exactmatch ? SQL_EM_CHILDCLASS : SQL_CHILDCLASS;
			break;
		case sqlresultFUNCSINFILE:
			sqlqry = SQL_FUNCSINFILE;
			twoTerms = false;
			break;
		case sqlresultAUTOCOMPLETE:
			resultType = sqlqueryresultlist::sqlresultSYM_ONLY;
			twoTerms = false;
			break;
		default:
			result.sqlerrmsg = "Unknown search type";
			return result;
	}

	if (m_searchstmt.qry.compare(sqlqry) != 0)
	{
		sqlite3_finalize(m_searchstmt.get());
		rc = sqlite3_prepare_v2(m_db, sqlqry.c_str(),
					sqlqry.size(),
					&(m_searchstmt.m_stmt), NULL);
		m_searchstmt.qry = (rc == SQLITE_OK) ? sqlqry : "";
	}
	else
	{
		rc = sqlite3_reset(m_searchstmt.get());
	}
	if (rc != SQLITE_OK) {result.sqlerrmsg = sqlite3_errmsg(m_db); return result;}
	rc = sqlite3_bind_text(m_searchstmt.get(), 1, srchterm.c_str(), srchterm.size(), SQLITE_TRANSIENT);
	if (rc != SQLITE_OK) {result.sqlerrmsg = sqlite3_errmsg(m_db); return result;}
	if (twoTerms)
	{
		rc = sqlite3_bind_text(m_searchstmt.get(), 2, filterterm.c_str(), filterterm.size(), SQLITE_TRANSIENT);
		if (rc != SQLITE_OK) {result.sqlerrmsg = sqlite3_errmsg(m_db); return result;}
	}
	if (resultType == sqlqueryresultlist::sqlresultFULL) result = search_full(m_searchstmt.get());
	else if (resultType == sqlqueryresultlist::sqlresultFILE_LINE) result = search_file_line(m_searchstmt.get());
	else if (resultType == sqlqueryresultlist::sqlresultFILE_ONLY) result = search_file_only(m_searchstmt.get());
	return result;
}	
Exemplo n.º 2
0
sqlqueryresultlist sqlquery::search(
    tStr searchstr,
    sqlquery::en_queryType querytype,
    bool exactmatch,
    tStr filterstr)
{
    sqlqueryresultlist result;
    int rc;
    bool twoTerms = true;
    int count;
    /*
    if (exactmatch)
        printf("Exact\n");
    else
        printf("Fuzzy\n");
    */
    result.result_type = sqlqueryresultlist::sqlresultERROR;
    if ((m_db == NULL)||(searchstr.empty())||(m_basepath.empty())) return result;
    tStr sqlqry="", srchterm, filterterm;
    sqlqueryresultlist::en_resultType resultType = sqlqueryresultlist::sqlresultFULL;
    if (exactmatch && (querytype == sqlresultFUNCSINFILE))
    {
        searchstr.insert(0, "%");
    }
    srchterm = process_searchterm(searchstr.c_str(),
                                  (querytype==sqlquerySTRING?false:exactmatch));
    if (filterstr.empty())
    {
        filterterm = "%";
    }
    else
    {
        filterterm = process_searchterm(filterstr.c_str(), false);
    }
    switch (querytype)
    {
    case sqlquerySYMBOL:
        sqlqry = exactmatch ? SQL_EM_SYM : SQL_SYM;
        break;
    case sqlquerySYMBOL_ID:
        sqlqry = exactmatch ? SQL_EM_SYM_ID : SQL_SYM_ID;
        twoTerms = false;
        break;
    case sqlquerySTRING:
        sqlqry = exactmatch ? SQL_STRING : SQL_STRING; /* Fuzzy in both cases*/
        break;
    case sqlresultFUNC_MACRO:
        sqlqry = exactmatch ? SQL_EM_FUNC_MACRO : SQL_FUNC_MACRO;
        break;
    case sqlresultCLASS_STRUCT:
        sqlqry = exactmatch ? SQL_EM_CLASS_STRUCT : SQL_CLASS_STRUCT;
        break;
    case sqlresultINCLUDE:
        sqlqry = exactmatch ? SQL_EM_INCLUDE : SQL_INCLUDE;
        resultType = sqlqueryresultlist::sqlresultFILE_LINE;
        twoTerms = false;
        break;
    case sqlresultFILEPATH:
        sqlqry = exactmatch ? SQL_EM_FILEPATH : SQL_FILEPATH;
        resultType = sqlqueryresultlist::sqlresultFILE_ONLY;
        twoTerms = false;
        break;
    case sqlresultCALLINGFUNC:
        sqlqry = exactmatch ? SQL_EM_CALLINGFUNC : SQL_CALLINGFUNC;
        break;
    case sqlresultCALLEDFUNC:
        sqlqry = exactmatch ? SQL_EM_CALLEDFUNC : SQL_CALLEDFUNC;
        break;
    case sqlresultCALLSOFFUNC:
        sqlqry = exactmatch ? SQL_EM_CALLS_OF_FUNC : SQL_CALLS_OF_FUNC;
        break;
    case sqlresultMEMBERS:
        sqlqry = exactmatch ? SQL_EM_MEMBERS : SQL_MEMBERS;
        break;
    case sqlresultOWNERCLASS:
        sqlqry = exactmatch ? SQL_EM_OWNERCLASS : SQL_OWNERCLASS;
        break;
    case sqlresultPARENTCLASS:
        sqlqry = exactmatch ? SQL_EM_PARENTCLASS : SQL_PARENTCLASS;
        break;
    case sqlresultCHILDCLASS:
        sqlqry = exactmatch ? SQL_EM_CHILDCLASS : SQL_CHILDCLASS;
        break;
    case sqlresultFUNCSINFILE:
        sqlqry = SQL_FUNCSINFILE;
        twoTerms = false;
        break;
    case sqlresultAUTOCOMPLETE:
        resultType = sqlqueryresultlist::sqlresultSYM_ONLY;
        twoTerms = false;
        break;
    default:
        result.sqlerrmsg = "Unknown search type";
        return result;
    }

    if (m_searchstmt.qry.compare(sqlqry) != 0)
    {
        sqlite3_finalize(m_searchstmt.get());
        //printf("Q: %s\n",sqlqry.c_str());
        rc = sqlite3_prepare_v2(m_db, sqlqry.c_str(),
                                sqlqry.size(),
                                &(m_searchstmt.m_stmt), NULL);
        m_searchstmt.qry = (rc == SQLITE_OK) ? sqlqry : "";
    }
    else
    {
        rc = sqlite3_reset(m_searchstmt.get());
    }
    if (rc != SQLITE_OK)
    {
        result.sqlerrmsg = sqlite3_errmsg(m_db);
        return result;
    }
    count = 1;
    // Insert search term twice for STRING
    for (int i=0;i<((querytype == sqlquerySTRING)?1:1); i++)
    {
        rc = sqlite3_bind_text(m_searchstmt.get(), count, srchterm.c_str(), srchterm.size(), SQLITE_TRANSIENT);
        //printf("Term: %s rc=%d\n",srchterm.c_str(),rc);
        if (rc != SQLITE_OK)
        {
            result.sqlerrmsg = sqlite3_errmsg(m_db);
            return result;
        }
        count++;
        // Usual
        //printf("Count=%d\n",count);
        if (twoTerms)
        {
            rc = sqlite3_bind_text(m_searchstmt.get(), count, filterterm.c_str(), filterterm.size(), SQLITE_TRANSIENT);
            //printf("Filter: %s rc=%d\n",filterterm.c_str(),rc);
            if (rc != SQLITE_OK)
            {
                result.sqlerrmsg = sqlite3_errmsg(m_db);
                //printf("ERROR\n");
                return result;
            }
        }
    }
    try {
    if (resultType == sqlqueryresultlist::sqlresultFULL) result = search_full(m_searchstmt.get());
    else if (resultType == sqlqueryresultlist::sqlresultFILE_LINE) result = search_file_line(m_searchstmt.get());
    else if (resultType == sqlqueryresultlist::sqlresultFILE_ONLY) result = search_file_only(m_searchstmt.get());
    }
    catch (const std::exception&e)
    {
        //printf("Something bad happened %s\n", m_searchstmt.get());
    }
    return result;
}