簡體   English   中英

無法使用boost :: spirit :: qi解析條件的SQL類型

[英]Unable to parse SQL type where condition using boost::spirit::qi

我可能問的是一個非常瑣碎的問題,但是我並沒有從大腦中脫穎而出。 嘗試使用boost :: spirit :: qi解析如下所示的SQL where子句,以生成向量對

std::string input = "book.author_id = '1234' and book.isbn = 'xy99' and book.type = 'abc' and book.lang = 'Eng'"

我已經通過下面的線程,但仍無法做到這一點:-(了Thread5 Thread4 Thread3 線程2 線程1

[Thread1][6]
[Thread2][7]
[Thread3][8]
[Thread4][9]
[Thread5][10]

我真誠地要求,請幫助我理解如何實現此目標...可能是我沒有完全給我100%的費用,但請保持友好....

這是完整的代碼(我想做一部分注釋),作為第一步,我只是檢查是否可以在Vector中獲取所有標記,然后解析每個Vector元素以生成另一個std :: pair向量。

#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <map>
#include <vector>

namespace qi    = boost::spirit::qi;
namespace phx   = boost::phoenix;

typedef std::string str_t;
typedef std::pair<str_t, str_t> pair_t;
typedef std::vector<pair_t> pairs_t;

typedef std::vector<str_t> strings_t;
//typedef std::map<std::string, std::string> map_t;
//typedef std::vector<map_t> maps_t;

template <typename It, typename Skipper = qi::space_type>
    //struct parser : qi::grammar<It, pairs_t(), Skipper>
    struct parser : qi::grammar<It, strings_t(), Skipper>
{
    parser() : parser::base_type(start)
    {
        using namespace qi;

        cond    = lexeme [ *(char_) ];
        conds   =  *(char_) >> cond % (lit("and"));

        //conds =  *(char_ - lit("and")) >>(cond % lit("and"));
        /*cond  = lexeme [ *(char_ - lit("and")) ];
        cond    = key >> "=" >> value;
        key     = *(char_ - "=");
        value   = ('\'' >> *(~char_('\'')) >> '\'');
        kv_pair = key >> value;*/
        start   = conds;
        //cond  = key >> "=" >> value;
        //key       = *(char_ - "=");
        //value = ('\'' >> *(~char_('\'')) >> '\'');
  //      kv_pair   = key >> value;
  //      start = kv_pair;
    }

  private:
    qi::rule<It, str_t(), Skipper> cond;
    qi::rule<It, strings_t(), Skipper> conds;
    //qi::rule<It, std::string(), Skipper> key, value;//, cond;
    //qi::rule<It, pair_t(), Skipper> kv_pair;
    //qi::rule<It, pairs_t(), Skipper> start;
    qi::rule<It, strings_t(), Skipper> start;
};

template <typename C, typename Skipper>
    bool doParse(const C& input, const Skipper& skipper)
{
    auto f(std::begin(input)), l(std::end(input));

    parser<decltype(f), Skipper> p;
    strings_t data;

    try
    {
        bool ok = qi::phrase_parse(f,l,p,skipper,data);
        if (ok)   
        {
            std::cout << "parse success\n";
            std::cout << "No Of Key-Value Pairs=  "<<data.size()<<"\n";
        }
        else    std::cerr << "parse failed: '" << std::string(f,l) << "'\n";
        return ok;
    } 
    catch(const qi::expectation_failure<decltype(f)>& e)
    {
        std::string frag(e.first, e.last);
        std::cerr << e.what() << "'" << frag << "'\n";
    }

    return false;
}

int main()
{
    std::cout<<"Pair Test \n";
    const std::string input = "book.author_id = '1234' and book.isbn = 'xy99' and book.type = 'abc' and book.lang = 'Eng'";
    bool ok = doParse(input, qi::space);
    std::cout<< input <<"\n";
    return ok? 0 : 255;
}

OUTPUT:

Pair Test
parse success
No Of Key-Value Pairs=  2
book.author_id = '1234' and book.isbn = 'xy99' and book.type = 'abc' and book.lang = 'Eng'

我期望4 ...因為有4個條件!

在此先感謝Vivek

一些可以在大腸桿菌上工作的例子

很抱歉把它給您打破,但是您的語法比您想像的要復雜得多。

    conds   =  *(char_) // ...

在這里,您基本上只是將所有輸入解析為單個字符串,而忽略了空格。 實際上,添加

    for (auto& el : data)
        std::cout << "'" << el << "'\n";

解析打印后:

Pair Test 
parse success
No Of Key-Value Pairs=  2
'book.author_id='1234'andbook.isbn='xy99'andbook.type='abc'andbook.lang='Eng''
''

正如你所看到的,第一個元素是字符串*char_解析,你會得到一個空元素免費由於這兩個condscond對空輸入匹配。

我強烈建議您從簡單開始。 我的意思是,要簡單得多。

從頭慢慢建立語法。 Spirit是解決測試驅動開發的一個非常好的工具(編譯時間除外,但是,您有更多的時間思考!)。

這是我剛剛完成的工作,從第一個構建塊, indent符開始思考,然后逐步發展到更高層次的元素:

// lexemes (no skipper)
ident     = +char_("a-zA-Z.");
op        = no_case [ lit("=") | "<>" | "LIKE" | "IS" ];
nulllit   = no_case [ "NULL" ];
and_      = no_case [ "AND" ];
stringlit = "'" >> *~char_("'") >> "'";

// other productions
field     = ident;
value     = stringlit | nulllit;
condition = field >> op >> value;

conjunction = condition % and_;
start       = conjunction;

這些接近於我認為可以解析您的語法的最簡單的東西(左右有一些創造性的注釋,在這些注釋中似乎不太令人討厭)。

更新所以這是我在20分鍾內到達的地方:

我總是從映射我希望規則公開的類型開始:

namespace ast
{
    enum op { op_equal, op_inequal, op_like, op_is };

    struct null { };

    typedef boost::variant<null, std::string> value;

    struct condition
    {
        std::string _field;
        op _op;
        value _value;
    };

    typedef std::vector<condition> conditions;
}

如果沒有適應,則condition不能在Spirit語法中“自然地”使用:

BOOST_FUSION_ADAPT_STRUCT(ast::condition, (std::string,_field)(ast::op,_op)(ast::value,_value))

現在是語法本身:

    // lexemes (no skipper)
    ident       = +char_("a-zA-Z._");
    op_token.add
        ("=",    ast::op_equal)
        ("<>",   ast::op_inequal)
        ("like", ast::op_like)
        ("is",   ast::op_is);
    op          = no_case [ op_token ];
    nulllit     = no_case [ "NULL" >> attr(ast::null()) ];
    and_        = no_case [ "AND" ];
    stringlit   = "'" >> *~char_("'") >> "'";

    //// other productions
    field       = ident;
    value       = stringlit | nulllit;
    condition   = field >> op >> value;

    whereclause = condition % and_;
    start       = whereclause;

您可以看到與原始草圖略有偏差,這很有趣:

  • 在標識符char中添加了_
  • op_token移至符號匹配器中(因為這樣更容易映射枚舉值)

實時查看全部內容, 在Coliru上工作 ,輸出:

Pair Test 
parse success
No Of Key-Value Pairs=  4
( [book.author_id] = 1234 )
( [book.isbn] LIKE xy99 )
( [book.type] = abc )
( [book.lang] IS NULL )

book.author_id = '1234' and book.isbn liKE 'xy99' and book.type = 'abc' and book.lang IS null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM