* This class is used to generate an SQL query string from a search expression, * with a syntax very similar to that used by the Google search engine. * * The code herein is based upon the SearchQueryParser python class written by * Kovid Goyal as part of the Calibre eBook manager (https://calibre-ebook.com) * * Grammar: * prog ::= or_expression * or_expression ::= and_expression [ 'or'
| 45 | * selectQuery.exec(); |
| 46 | */ |
| 47 | class QueryParser |
| 48 | { |
| 49 | public: |
| 50 | struct TreeNode { |
| 51 | std::string t; |
| 52 | std::vector<TreeNode> children; |
| 53 | std::string expOperator; |
| 54 | |
| 55 | explicit TreeNode(std::string t, std::vector<TreeNode> children, std::string expOperator = "") |
| 56 | : t(t), children(children), expOperator(expOperator) |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | int buildSqlString(std::string &sqlString, int bindPosition = 0) const; |
| 61 | int bindValues(QSqlQuery &selectQuery, int bindPosition = 0) const; |
| 62 | }; |
| 63 | |
| 64 | explicit QueryParser(); |
| 65 | TreeNode parse(const std::string &expr); |
| 66 | |
| 67 | private: |
| 68 | static std::string toLower(const std::string &string); |
| 69 | |
| 70 | std::string token(bool advance = false); |
| 71 | std::string lcaseToken(bool advance = false); |
| 72 | Token::Type tokenType(); |
| 73 | bool isEof() const; |
| 74 | void advance(); |
| 75 | |
| 76 | QueryLexer lexer = QueryLexer(""); |
| 77 | Token currentToken = Token(Token::Type::undefined); |
| 78 | |
| 79 | template<typename T> |
| 80 | static bool isIn(const T &e, const std::list<T> &v) |
| 81 | { |
| 82 | return std::find(v.begin(), v.end(), e) != v.end(); |
| 83 | } |
| 84 | |
| 85 | bool isOperatorToken(Token::Type type); |
| 86 | |
| 87 | enum class FieldType { unknown, |
| 88 | numeric, |
| 89 | text, |
| 90 | boolean, |
| 91 | date, |
| 92 | dateFolder, |
| 93 | folder, |
| 94 | booleanFolder, |
| 95 | filename, |
| 96 | enumField, |
| 97 | enumFieldFolder }; |
| 98 | static FieldType fieldType(const std::string &str); |
| 99 | |
| 100 | static std::string join(const QStringList &strings, const std::string &delim); |
| 101 | static QStringList split(const std::string &string, char delim); |
| 102 | |
| 103 | TreeNode orExpression(); |
| 104 | TreeNode andExpression(); |
nothing calls this directly
no test coverage detected