SQL 解析器库 - 从查询中获取表名 [英] SQL parser library - get table names from query

查看:68
本文介绍了SQL 解析器库 - 从查询中获取表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个 C/C++ SQL 解析库,它能够为我提供查询所依赖的表的名称.

I'm looking for a C/C++ SQL parsing library which is able to provide me with the names of the tables the query depends on.

我的期望:

    SELECT * FROM TABLEA NATURAL JOIN TABLEB

结果:TABLEA、TABLEB

Result: TABLEA, TABLEB

当然提供的例子非常简单.我已经编写了自己的解析器(基于 Boost.Spirit)来处理 SQL 语法的一个子集,但我需要的是一个能够处理复杂(递归等)查询的解析器.

Certainly provided example is extremly simple. I've already written my own parser (based on Boost.Spirit) which handles a subset of SQL grammar, but what I need is a parser which is able to handle complicated (recursive etc.) queries.

你知道什么有用的东西吗?

Do you know anything useful for this purpose?

我发现的是 http://www.sqlparser.com - 它是商业的,但正是我需要的.我也研究过PostgreSQL源码,没有效果.

What I found is http://www.sqlparser.com - it's commercial but does exactly what I need. I also digged into the PostgreSQL sources, no effect.

推荐答案

Antlr 可以为你生成一个不错的 SQL 解析器(解析器的源代码可以是 C++),并且可用的 SQL 语法很少:http://www.antlr3.org/grammar/list.html

Antlr can produce a nice SQL parser (the source of the parser can be C++) for you, and there is few SQL grammars for it available: http://www.antlr3.org/grammar/list.html

如果您只对表名感兴趣,那么采用其中一种语法并添加收集这些名称的语义操作应该相当容易.

If all you are interested in are the table names, then taking one of those grammars and adding a semantic action collecting those names should be fairly easy.

对 Antlr 和 Bison/Yacc 有一些经验 &Lex/Flex 我绝对推荐 Antlr.它是用 Java 编写的,但目标语言可以是 C++ - 生成的代码实际上是可读的,看起来像人写的.Antlr生成的解析器调试还算可以,Bison生成的就不能说了.

Having some experience with Antlr and Bison/Yacc & Lex/Flex I definitely recommend Antlr. It is written in Java, but the target language can be C++ - the generated code is actually readable, looks like written by a human. The debugging of Antlr generated parsers is quite OK, which cannot be said about those generated by Bison..

还有其他选项,例如 Lemon 和 sqlite 语法,如果您愿意,可以看看这个问题:C 中的 SQL 解析器

There are other options, like for example Lemon and sqlite grammar, have a look at this question if you like: SQL parser in C

这篇关于SQL 解析器库 - 从查询中获取表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆