【问题标题】:Unable to parse SQL type where condition using boost::spirit::qi无法使用 boost::spirit::qi 解析 SQL 类型 where 条件
【发布时间】:2014-05-07 13:56:40
【问题描述】:

我可能会问一个非常微不足道的问题,但我并没有从我的大脑中抽出块来破解它。 尝试使用 boost::spirit::qi 解析如下所示的 SQL like where 子句以生成对的向量

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

我已经完成了以下线程但仍然无法做到:-( Thread5 Thread4Thread3 Thread2 Thread1

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

我真诚地请求,请帮助我了解如何实现这一目标......可能是我没有完全付出我的 100%,但请善待......

这是完整的代码(我希望做的一些注释部分),作为第一步,我只是检查是否可以获取 Vector 中的所有标记,然后解析每个 Vector 元素以生成另一个 std:: 向量对

#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;
}

输出:

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 个条件!!

提前致谢 问候, 维韦克

一些可以解决的例子-live on coliru

【问题讨论】:

标签: c++ boost-spirit qi


【解决方案1】:

很抱歉打断了你,但你的语法比你想象的要糟糕得多。

    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 是解决测试驱动开发的一个非常好的工具(除了编译时间,但是嘿,你有更多的时间思考!)。

这是我刚刚编造的东西,从第一个构建块 indentifier 开始思考,然后逐步提升到更高级别的元素:

// 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;

你可以看到我原来的草图有细微的偏差,这很有趣:

  • 在标识符字符中添加了_
  • op_token 移动到符号匹配器中(因为这样更容易映射枚举值)

全部查看Live And Working On 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

【讨论】:

  • 非常感谢 Sehe 探索细节......我想我已经付出了非常天真的努力,你的帖子令人大开眼界.. 会更仔细地思考并努力打造一个清洁器,更简单的语法...问候 Vivek
  • 请原谅再次打扰..但是如果提供而不是'=',则实现无法解析LIKE或IS!而如果我将标记分解为“and_”,则使用“|”在“字段>>(op_like | op_is)>>值”然后它工作..是否有任何隐藏的故障..问候Vivek
  • 发布了对 cme​​ts at the mailing list 的请求(包括最小复制器)并更新了答案。
  • 糟糕。它被记录在案!我们需要put the symbols entries in lowercase for use in no_case。很高兴解决了:)
  • 再次感谢,一切正常,非常感谢您的辛勤努力
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-26
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
相关资源
最近更新 更多