sqlquery::en_filereadstatus sqlquery::open_dbfile(tStr dbfn) { if (dbfn.empty()) return sqlfileOPENERROR; smartFILE fp; // Does the file exist? if (check_fileExists(dbfn.c_str()) == false) { return sqlfileOPENERROR; } // Try to open the file for reading fp = fopen(dbfn.c_str(), "r"); if (fp == NULL) { return sqlfileOPENERROR; } fp.close_file(); int rc = sqlite3_open_v2(dbfn.c_str(), &m_db, SQLITE_OPEN_READONLY, NULL); if ((rc != SQLITE_OK)||(m_db == NULL)) { close_dbfile(); return sqlfileOPENERROR; } tempstmt stmt; sqlite3_exec(m_db, /*"PRAGMA synchronous = OFF;" "PRAGMA journal_mode = OFF;" "PRAGMA locking_mode = EXCLUSIVE;" "PRAGMA automatic_index = FALSE;"*/ "PRAGMA cache_size = 20000;", NULL, 0, NULL); tStr majorver = read_configtbl("DB_MAJOR_VER", stmt.get()); tStr minorver = read_configtbl("DB_MINOR_VER", stmt.get()); if ((majorver.empty())||(minorver.empty())) { return sqlfileNOTCORRECTDB; } if (majorver.compare(tStr("0")) != 0) return sqlfileINCORRECTVER; if (minorver.compare(tStr("1")) != 0) return sqlfileINCORRECTVER; m_basepath = read_configtbl("DB_BASE_PATH", stmt.get()); if (m_basepath.empty()) { return sqlfileNOTCORRECTDB; } rc = sqlite3_prepare_v2(m_db, SQL_AUTOCOMPLETE, strlen(SQL_AUTOCOMPLETE), &(m_autocompstmt.m_stmt), NULL); if (rc != SQLITE_OK) { return sqlfileNOTCORRECTDB; } return sqlfileOK; }
// reverse string compare bool strrevcmp(tStr str, tStr cmpstr) { bool retval = (1 == 1); int n = str.length(); if (n != cmpstr.length()) {retval = (1 == 0);} else for (int i = (n - 1); i >= 0; i--) { if (str[i] != cmpstr[i]) { retval = (1 == 0); break; } } return retval; }
tStr limitcstr(int limitlen, tStr str) { if (limitlen <= 0) return str; else return str.substr(0,limitlen); }
void process_argwithopt(option_t* thisOpt, bool& err, tStr& fnstr, bool filemustexist) { if (thisOpt->argument != NULL) { fnstr = thisOpt->argument; if (filemustexist && (fileexists(fnstr.c_str()) == false)) { printf("Error: File %s doesn't exist.\n", fnstr.c_str()); err = true; } } else { printf("Error: -%c used without file option.\n", thisOpt->option); err = true; } }
int process_query(tStr sqfn, tStr term, tStr param, bool exact, bool debug) { if ((sqfn.empty())||(term.empty())||(param.empty())) return 1; int retVal = 0; std::auto_ptr<sqlquery> sq(new sqlquery); sqlquery::en_filereadstatus filestatus = sq->open_dbfile(sqfn); switch (filestatus) { case sqlquery::sqlfileOK: break; case sqlquery::sqlfileOPENERROR: printf("Error: File %s open error!\n", sqfn.c_str()); return 1; case sqlquery::sqlfileNOTCORRECTDB: printf("Error: File %s does not have correct database format!\n", sqfn.c_str()); return 1; case sqlquery::sqlfileINCORRECTVER: printf("Error: File %s has an unsupported database version number!\n", sqfn.c_str()); return 1; case sqlquery::sqlfileUNKNOWNERROR: printf("Error: Unknown Error!\n"); return 1; } int intParam = atoi(param.c_str()) - 1; if ((intParam < 0) || (intParam >= sqlquery::sqlresultAUTOCOMPLETE)) { printf("Error: Parameter is out of range!\n"); return 1; } sqlqueryresultlist resultlst; resultlst = sq->search(term, (sqlquery::en_queryType) intParam, exact); if (resultlst.result_type == sqlqueryresultlist::sqlresultERROR) { printf("Error: SQL Error! %s!\n", resultlst.sqlerrmsg.c_str()); return 1; } for(std::vector<sqlqueryresult>::iterator it = resultlst.resultlist.begin(); it != resultlst.resultlist.end(); it++) { switch(resultlst.result_type) { case sqlqueryresultlist::sqlresultFULL: printf("%s\t%s:%s\t%s\n", it->symname.c_str(), it->filename.c_str(), it->linenum.c_str(), it->linetext.c_str()); break; case sqlqueryresultlist::sqlresultFILE_LINE: printf("%s:%s\t%s\n", it->filename.c_str(), it->linenum.c_str(), it->linetext.c_str()); break; case sqlqueryresultlist::sqlresultFILE_ONLY: printf("%s\n", it->filepath.c_str()); break; } } return retVal; }
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; }
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; }