shared: sqlitesearch: reformat - no functional change

This commit is contained in:
Albert S. 2020-12-19 12:40:27 +01:00
parent a6f73d724a
commit 344eaa402e

View File

@ -9,198 +9,201 @@
SqliteSearch::SqliteSearch(QSqlDatabase &db) SqliteSearch::SqliteSearch(QSqlDatabase &db)
{ {
this->db = &db; this->db = &db;
} }
QString SqliteSearch::fieldToColumn(QueryField field) QString SqliteSearch::fieldToColumn(QueryField field)
{ {
if(field == FILE_MTIME) if(field == FILE_MTIME)
{ {
return "file.mtime"; return "file.mtime";
} }
else if(field == FILE_PATH) else if(field == FILE_PATH)
{ {
return "file.path"; return "file.path";
} }
else if(field == FILE_SIZE) else if(field == FILE_SIZE)
{ {
return "file.size"; return "file.size";
} }
else if(field == CONTENT_TEXT_PAGE) else if(field == CONTENT_TEXT_PAGE)
{ {
return "content.page"; return "content.page";
} }
else if(field == CONTENT_TEXT) else if(field == CONTENT_TEXT)
{ {
return "content.text"; return "content.text";
} }
return ""; return "";
} }
QString SqliteSearch::createSortSql(const QVector<SortCondition> sortConditions) QString SqliteSearch::createSortSql(const QVector<SortCondition> sortConditions)
{ {
QString sortsql; QString sortsql;
for(const SortCondition &sc : sortConditions) for(const SortCondition &sc : sortConditions)
{ {
QString order; QString order;
QString field = fieldToColumn(sc.field); QString field = fieldToColumn(sc.field);
if(field == "") if(field == "")
{ {
throw QSSGeneralException("Unknown sort field supplied"); throw QSSGeneralException("Unknown sort field supplied");
} }
if(sc.order == DESC) if(sc.order == DESC)
{ {
order = "DESC"; order = "DESC";
} }
else else
{ {
order = "ASC"; order = "ASC";
} }
sortsql += field + " " + order + ", "; sortsql += field + " " + order + ", ";
} }
sortsql.chop(2); sortsql.chop(2);
if(!sortsql.isEmpty()) if(!sortsql.isEmpty())
{ {
return " ORDER BY " + sortsql; return " ORDER BY " + sortsql;
} }
return ""; return "";
} }
QPair<QString, QVector<QString>> createNonArgPair(QString key) QPair<QString, QVector<QString>> createNonArgPair(QString key)
{ {
return { " " + key + " ", QVector<QString>() }; return {" " + key + " ", QVector<QString>()};
} }
QPair<QString, QVector<QString>> SqliteSearch::createSql(const Token &token) QPair<QString, QVector<QString>> SqliteSearch::createSql(const Token &token)
{ {
QPair<QString, QVector<QString>> result; QPair<QString, QVector<QString>> result;
QString value = token.value; QString value = token.value;
value = value.replace("'", "\\'"); value = value.replace("'", "\\'");
if(token.type == BOOL_AND) if(token.type == BOOL_AND)
{ {
return createNonArgPair("AND"); return createNonArgPair("AND");
} }
if(token.type == BOOL_OR) if(token.type == BOOL_OR)
{ {
return createNonArgPair("OR"); return createNonArgPair("OR");
} }
if(token.type == NEGATION) if(token.type == NEGATION)
{ {
return createNonArgPair("NOT"); return createNonArgPair("NOT");
} }
if(token.type == BRACKET_OPEN) if(token.type == BRACKET_OPEN)
{ {
return createNonArgPair("("); return createNonArgPair("(");
} }
if(token.type == BRACKET_CLOSE) if(token.type == BRACKET_CLOSE)
{ {
return createNonArgPair(")"); return createNonArgPair(")");
} }
if(token.type == FILTER_PATH_STARTS) if(token.type == FILTER_PATH_STARTS)
{ {
return { " file.path LIKE ? || '%' ", { value } }; return {" file.path LIKE ? || '%' ", {value}};
} }
if(token.type == FILTER_PATH_ENDS) if(token.type == FILTER_PATH_ENDS)
{ {
return { " file.path LIKE '%' || ? ", { value } }; return {" file.path LIKE '%' || ? ", {value}};
} }
if(token.type == FILTER_PATH_CONTAINS) if(token.type == FILTER_PATH_CONTAINS)
{ {
return { " file.path LIKE '%' || ? || '%' ", { value } } ; return {" file.path LIKE '%' || ? || '%' ", {value}};
} }
if(token.type == FILTER_CONTENT_PAGE) if(token.type == FILTER_CONTENT_PAGE)
{ {
return { " content.page = ?", { value } }; return {" content.page = ?", {value}};
} }
if(token.type == FILTER_CONTENT_CONTAINS) if(token.type == FILTER_CONTENT_CONTAINS)
{ {
return { " content.id IN (SELECT content_fts.ROWID FROM content_fts WHERE content_fts.content MATCH ?) " , { value } }; return {" content.id IN (SELECT content_fts.ROWID FROM content_fts WHERE content_fts.content MATCH ?) ",
} {value}};
throw QSSGeneralException("Unknown token passed (should not happen)"); }
throw QSSGeneralException("Unknown token passed (should not happen)");
} }
QSqlQuery SqliteSearch::makeSqlQuery(const QSSQuery &query) QSqlQuery SqliteSearch::makeSqlQuery(const QSSQuery &query)
{ {
QString whereSql; QString whereSql;
QVector<QString> bindValues; QVector<QString> bindValues;
bool isContentSearch = query.getTokensMask() & FILTER_CONTENT == FILTER_CONTENT; bool isContentSearch = query.getTokensMask() & FILTER_CONTENT == FILTER_CONTENT;
if(query.getTokens().isEmpty()) if(query.getTokens().isEmpty())
{ {
throw QSSGeneralException("Nothing to search for supplied"); throw QSSGeneralException("Nothing to search for supplied");
} }
for(const Token &token : query.getTokens()) for(const Token &token : query.getTokens())
{ {
auto sql = createSql(token); auto sql = createSql(token);
whereSql += sql.first; whereSql += sql.first;
bindValues.append(sql.second); bindValues.append(sql.second);
} }
QString prepSql; QString prepSql;
QString sortSql = createSortSql(query.getSortConditions()); QString sortSql = createSortSql(query.getSortConditions());
if(isContentSearch) if(isContentSearch)
{ {
if(sortSql.isEmpty()) if(sortSql.isEmpty())
{ {
sortSql = "ORDER BY file.mtime DESC, content.page ASC"; sortSql = "ORDER BY file.mtime DESC, content.page ASC";
} }
prepSql = "SELECT file.path AS path, group_concat(content.page) AS pages, file.mtime AS mtime, file.size AS size, file.filetype AS filetype FROM file INNER JOIN content ON file.id = content.fileid WHERE 1=1 AND " + whereSql + " GROUP BY file.path " + sortSql; prepSql =
} "SELECT file.path AS path, group_concat(content.page) AS pages, file.mtime AS mtime, file.size AS size, "
else "file.filetype AS filetype FROM file INNER JOIN content ON file.id = content.fileid WHERE 1=1 AND " +
{ whereSql + " GROUP BY file.path " + sortSql;
if(sortSql.isEmpty()) }
{ else
sortSql = "ORDER BY file.mtime DESC"; {
} if(sortSql.isEmpty())
prepSql = "SELECT file.path AS path, '0' as pages, file.mtime AS mtime, file.size AS size, file.filetype AS filetype FROM file WHERE 1=1 AND " + whereSql + " " + sortSql; {
} sortSql = "ORDER BY file.mtime DESC";
}
prepSql = "SELECT file.path AS path, '0' as pages, file.mtime AS mtime, file.size AS size, file.filetype AS "
"filetype FROM file WHERE 1=1 AND " +
whereSql + " " + sortSql;
}
QSqlQuery dbquery(*db); QSqlQuery dbquery(*db);
dbquery.prepare(prepSql); dbquery.prepare(prepSql);
for(const QString &value : bindValues) for(const QString &value : bindValues)
{ {
if(value != "") if(value != "")
{ {
dbquery.addBindValue(value); dbquery.addBindValue(value);
} }
} }
return dbquery; return dbquery;
} }
QVector<SearchResult> SqliteSearch::search(const QSSQuery &query) QVector<SearchResult> SqliteSearch::search(const QSSQuery &query)
{ {
QVector<SearchResult> results; QVector<SearchResult> results;
QSqlQuery dbQuery = makeSqlQuery(query); QSqlQuery dbQuery = makeSqlQuery(query);
bool success = dbQuery.exec(); bool success = dbQuery.exec();
if(!success) if(!success)
{ {
qDebug() << dbQuery.lastError(); qDebug() << dbQuery.lastError();
qDebug() << dbQuery.executedQuery(); qDebug() << dbQuery.executedQuery();
throw QSSGeneralException("SQL Error: " + dbQuery.lastError().text()); throw QSSGeneralException("SQL Error: " + dbQuery.lastError().text());
} }
while(dbQuery.next()) while(dbQuery.next())
{ {
SearchResult result; SearchResult result;
result.fileData.absPath = dbQuery.value("path").toString(); result.fileData.absPath = dbQuery.value("path").toString();
result.fileData.mtime = dbQuery.value("mtime").toUInt(); result.fileData.mtime = dbQuery.value("mtime").toUInt();
result.fileData.size = dbQuery.value("size").toUInt(); result.fileData.size = dbQuery.value("size").toUInt();
result.fileData.filetype = dbQuery.value("filetype").toChar(); result.fileData.filetype = dbQuery.value("filetype").toChar();
QString pages = dbQuery.value("pages").toString(); QString pages = dbQuery.value("pages").toString();
QStringList pagesList = pages.split(","); QStringList pagesList = pages.split(",");
for(QString &page : pagesList) for(QString &page : pagesList)
{ {
if(page != "") if(page != "")
{ {
result.pages.append(page.toUInt()); result.pages.append(page.toUInt());
} }
} }
results.append(result); results.append(result);
} }
return results; return results;
} }