From 8efb9912ee07200e84bf91601d1d2336152bdb02 Mon Sep 17 00:00:00 2001 From: Iain Benson Date: Thu, 24 Oct 2019 20:39:43 +0100 Subject: [PATCH] Use concatenation, rather than ostringstream --- YACReaderLibrary/db/query_parser.cpp | 46 +++++++++++----------------- YACReaderLibrary/db/query_parser.h | 4 +-- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/YACReaderLibrary/db/query_parser.cpp b/YACReaderLibrary/db/query_parser.cpp index 802c11b8..f08d0872 100644 --- a/YACReaderLibrary/db/query_parser.cpp +++ b/YACReaderLibrary/db/query_parser.cpp @@ -1,7 +1,6 @@ #include "query_parser.h" #include -#include #include #include @@ -18,24 +17,22 @@ int QueryParser::TreeNode::buildSqlString(std::string &sqlString, int bindPositi { if (t == "token") { ++bindPosition; - std::ostringstream oss; if (toLower(children[0].t) == "all") { - oss << "("; + sqlString += "("; for (const auto &field : fieldNames.at(FieldType::text)) { - oss << "UPPER(ci." << field << ") LIKE UPPER(:bindPosition" << bindPosition << ") OR "; + sqlString += "UPPER(ci." + field + ") LIKE UPPER(:bindPosition" + std::to_string(bindPosition) + ") OR "; } - oss << "UPPER(c.filename) LIKE UPPER(:bindPosition" << bindPosition << ") OR "; - oss << "UPPER(f.name) LIKE UPPER(:bindPosition" << bindPosition << ")) "; + sqlString += "UPPER(c.filename) LIKE UPPER(:bindPosition" + std::to_string(bindPosition) + ") OR "; + sqlString += "UPPER(f.name) LIKE UPPER(:bindPosition" + std::to_string(bindPosition) + ")) "; } else if (isIn(fieldType(children[0].t), { FieldType::numeric, FieldType::boolean })) { - oss << "ci." << children[0].t << " = :bindPosition" << bindPosition << " "; + sqlString += "ci." + children[0].t + " = :bindPosition" + std::to_string(bindPosition) + " "; } else if (fieldType(children[0].t) == FieldType::filename) { - oss << "(UPPER(c." << children[0].t << ") LIKE UPPER(:bindPosition" << bindPosition << ")) "; + sqlString += "(UPPER(c." + children[0].t + ") LIKE UPPER(:bindPosition" + std::to_string(bindPosition) + ")) "; } else if (fieldType(children[0].t) == FieldType::folder) { - oss << "(UPPER(f.name) LIKE UPPER(:bindPosition" << bindPosition << ")) "; + sqlString += "(UPPER(f.name) LIKE UPPER(:bindPosition" + std::to_string(bindPosition) + ")) "; } else { - oss << "(UPPER(ci." << children[0].t << ") LIKE UPPER(:bindPosition" << bindPosition << ")) "; + sqlString += "(UPPER(ci." + children[0].t + ") LIKE UPPER(:bindPosition" + std::to_string(bindPosition) + ")) "; } - sqlString += oss.str(); } else if (t == "not") { sqlString += "(NOT "; bindPosition = children[0].buildSqlString(sqlString, bindPosition); @@ -54,12 +51,11 @@ int QueryParser::TreeNode::buildSqlString(std::string &sqlString, int bindPositi int QueryParser::TreeNode::bindValues(QSqlQuery &selectQuery, int bindPosition) const { if (t == "token") { - std::ostringstream oss; - oss << ":bindPosition" << ++bindPosition; + std::string bind_string(":bindPosition" + std::to_string(++bindPosition)); if (isIn(fieldType(children[0].t), { FieldType::numeric, FieldType::boolean })) { - selectQuery.bindValue(oss.str().c_str(), std::stoi(children[1].t)); + selectQuery.bindValue(bind_string.c_str(), std::stoi(children[1].t)); } else { - selectQuery.bindValue(oss.str().c_str(), ("%%" + children[1].t + "%%").c_str()); + selectQuery.bindValue(bind_string.c_str(), ("%%" + children[1].t + "%%").c_str()); } } else if (t == "not") { bindPosition = children[0].bindValues(selectQuery, bindPosition); @@ -163,23 +159,17 @@ void QueryParser::tokenize(const std::string &expr) iter = lexertl::siterator(expr.begin(), expr.end(), sm); } -std::string QueryParser::join(const std::vector &strings, const std::string &delim) +std::string QueryParser::join(const QStringList &strings, const std::string &delim) { return std::accumulate(strings.begin(), strings.end(), std::string(), - [&delim](const std::string &a, const std::string &b) -> std::string { - return a + (a.length() > 0 && b.length() > 0 ? delim : "") + b; + [&delim](const std::string &a, const QString &b) -> std::string { + return a + (a.length() > 0 && b.length() > 0 ? delim : "") + b.toStdString(); }); } -std::vector QueryParser::split(const std::string &string, char delim) +QStringList QueryParser::split(const std::string &string, char delim) { - std::istringstream iss(string); - std::vector words; - while (iss) { - std::string substr; - std::getline(iss, substr, delim); - words.push_back(substr); - } + auto words = QString(string.c_str()).split(delim); return words; } @@ -241,8 +231,8 @@ QueryParser::TreeNode QueryParser::baseToken() auto words(split(token(true), ':')); - if (words.size() > 1 && fieldType(words[0]) != FieldType::unknown) { - auto loc(toLower(words[0])); + if (words.size() > 1 && fieldType(words[0].toStdString()) != FieldType::unknown) { + auto loc(toLower(words[0].toStdString())); words.erase(words.begin()); if (words.size() == 1 && tokenType() == TokenType::quotedWord) { return { "token", { { loc, {} }, { token(true), {} } } }; diff --git a/YACReaderLibrary/db/query_parser.h b/YACReaderLibrary/db/query_parser.h index 0d032036..f23a7470 100644 --- a/YACReaderLibrary/db/query_parser.h +++ b/YACReaderLibrary/db/query_parser.h @@ -84,8 +84,8 @@ private: static FieldType fieldType(const std::string &str); void tokenize(const std::string &expr); - static std::string join(const std::vector &strings, const std::string &delim); - static std::vector split(const std::string &string, char delim); + static std::string join(const QStringList &strings, const std::string &delim); + static QStringList split(const std::string &string, char delim); TreeNode orExpression(); TreeNode andExpression();