SQL 是否有标准的 XML 查询编码?

发布于 2024-07-19 03:50:34 字数 1003 浏览 4 评论 0原文

是否有将 SQL 查询编码为 XML 的标准方法? 我的意思是类似的东西

select name from users where name like 'P%' group by name order by name desc

可能会编码为(我的 5 分钟模型,可能是线轴)...

<?xml version="1.0" encoding="UTF-8"?>
<query>
    <select>
        <table name="users">
            <column name="name"/>
        </table>
    </select>
    <from>
        <table name="users"/>
    </from>
    <where>
        <operator name="like">
            <column name="name"/>
            <value>P%</value>
        </operator>
    </where>
    <aggregation>
        <groupby>
            <column name="name"/>
        </groupby>
    </aggregation>
    <order>
        <order-by>
            <column name="name" order="desc"/>
        </order-by>
    </order>
</query>

...这将使构建、存储、验证结构和内容(通过生成基于数据库模式的模式)等变得容易。

Is there a standard way of encoding SQL queries as XML? I mean something like

select name from users where name like 'P%' group by name order by name desc

might encode as (my 5-minute mockup, probably bobbins)...

<?xml version="1.0" encoding="UTF-8"?>
<query>
    <select>
        <table name="users">
            <column name="name"/>
        </table>
    </select>
    <from>
        <table name="users"/>
    </from>
    <where>
        <operator name="like">
            <column name="name"/>
            <value>P%</value>
        </operator>
    </where>
    <aggregation>
        <groupby>
            <column name="name"/>
        </groupby>
    </aggregation>
    <order>
        <order-by>
            <column name="name" order="desc"/>
        </order-by>
    </order>
</query>

...which would make it easy to build, store, validate structure and content (by producing a schema based on the database schema) etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

笑忘罢 2024-07-26 03:50:34

我不知道有这样的标准。 到目前为止你所拥有的看起来相当可行。 但我质疑你为什么要这样做。 我认为这是一个内部平台(反模式)。 此外,它还专门重新发明了 SQL,这是该反模式的一个众所周知的实例。 最重要的是,它是用 XML 编程的,这被广泛认为是一个坏主意。

SQL 语法非常简单,您可以使用普通的解析器生成器工具(网络上可能已经存在一些开源工具)快速为其构建一个解析器。 这将是验证 SQL 语法的更简洁的方法。

I'm unaware of any such standard. What you have so far looks pretty workable. I question why you want to do this, though. I think this is a inner platform (an anti-pattern). Also, it's specifically re-inventing SQL, which is a well-known instance of that anti-pattern. To top it all off, it's programming in XML, which is widely regarded as a bad idea.

The SQL grammar is simple enough that you can probably build a parser for it in short order using normal parser-generator tools (there are likely some already existing on the web that are open source). That would be a much cleaner way of verifying your SQL syntax.

给我一枪 2024-07-26 03:50:34

我一直在尝试做同样的事情。

回答评论中关于我为什么想要的问题。 好吧,我想用一组可用列和过滤条件定义一个基本查询,并且我想允许用户选择他们想要显示的列,并启用和禁用 where 子句中的某些表达式。

我尝试过 XML 模式的一些变体并获得了一些不错的结果。 我什至能够应用 XSLT 根据用户的偏好提取 SQL 文本。

我也去寻找 SQL 解析器。 http://www.sqlparser.com 有一个,但它是商业性的,它的 API 很像 Delphi 风格,但不是很我认为可以访问。

正如其他人所说,使用 ANTLR 之类的工具来生成解析 SQL 的 C# 代码是可行的。 您可以在此处找到一些 SQL 语法。 列出的最新 MS SQL 语法是 MS SQL 2000。我还没有时间尝试这些东西。

我希望 Oslo SDK 中有一个像样的 M Grammer 能够解析查询,但我还没有找到。

I have been trying to do the same thing.

To answer the question in the comments as to why I would want to. Well, I want to define a base query with the set of available columns and filter conditions and I want to allow a user to select the columns they want to display and enable and disable certain expressions in the where clause.

I have played around with a few variations of an XML schema and got some decent results. I was even able to apply an XSLT to extract the SQL text based on the users' preferences.

I have also gone looking for SQL parsers. http://www.sqlparser.com has one but its commercial and it's API is heavily Delphi styled and not very accessible in my opinion.

As others have said it is feasible to use something like ANTLR to generate C# code that will parse SQL. You can find a few SQL grammars here. The most recent MS SQL Gramar listed there is MS SQL 2000. I haven't had time to play around with this stuff.

I was hoping that there would be a decent M Grammer in the Oslo SDK that would be able to parse queries, but I have not found one yet.

冷清清 2024-07-26 03:50:34

没有一个确切的标准。 也许你不应该做你想做的事。 但如果我要的话,我会看以下之一:
(1)对 xml 查询计划中的一些构造进行混用。 默认 sql 2005 安装上的 XSD 位于 C:\Program Files\Microsoft SQL Server\90\Tools\Binn\schemas\sqlserver\2004\07\showplan\showplanxml.xsd

(2)Sql 2008 在 IDE 中附带智能感知,它似乎是在这个 .net 程序集的帮助下实现的 -
C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Microsoft.SqlServer.SqlParser.dll
如果添加对此程序集的引用,则可以解析源脚本并提取解析器程序集提供的 xml 定义。 例如,

SqlScript script = Parser.Parse(strSqlScript);
foreach (SqlBatch batch in script.Batches)
{
  foreach (SqlStatement s in batch.Statements)
  {
    Console.Write(s.Xml);
  }
}

您可能会发现它提供的 xml 定义可以根据您的需要重用。

There is not exactly a standard. Probably you should not do what you intend to do. But if I were going to, I would look at one of:
(1)Bastardize some constructs from the xml query plan. The XSD on a default sql 2005 installation is at C:\Program Files\Microsoft SQL Server\90\Tools\Binn\schemas\sqlserver\2004\07\showplan\showplanxml.xsd

(2)Sql 2008 comes with intellisense in the IDE, and it appears to implement this with the help of this .net assembly -
C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Microsoft.SqlServer.SqlParser.dll
If you add a reference to this assembly you can parse the source script and pull out the xml definition that the parser assembly provides. E.g.

SqlScript script = Parser.Parse(strSqlScript);
foreach (SqlBatch batch in script.Batches)
{
  foreach (SqlStatement s in batch.Statements)
  {
    Console.Write(s.Xml);
  }
}

You may find that the xml definition it gives could be reused for your needs.

我的痛♀有谁懂 2024-07-26 03:50:34

XQuery 是一种 XML 查询语言,它支持一些与 SQL 相同的想法。 这些不是相互可以理解的语言,但如果您熟悉 SQL,那么使用 XQuery 也不会有太多麻烦。

据我所知,XQuery 只有一种现实的实现,exist

我只是出于一般性的探索性兴趣而使用这两种技术。

XQuery is an XML query language that supports some of the same ideas as SQL. These are not mutually comprehensible languages, but if you're familiar with SQL, you shouldn't have too terribly much trouble with XQuery either.

As far as i Know, there is only one realistic implementation of XQuery, exist.

I have not used either technology in any more than general, exploratory interest.

他不在意 2024-07-26 03:50:34

好吧,我自己只是在思考这个问题,试图记住 CRM 是如何在定义实体时动态构建表的。

然后几分钟前我终于偶然发现了正确的名称“fetchXML”

http:// msdn.microsoft.com/en-us/library/gg328117.aspx

当然,这是 MS 专有的,但至少您可以非常确定它们已经涵盖了任何可能的情况。

Well, I was just pondering on this myself, trying to remember how it is done for CRM which builds tables dynamically as the entities are defined.

Then I finally stumbled on the right name "fetchXML" a few minutes ago

http://msdn.microsoft.com/en-us/library/gg328117.aspx

It's MS proprietry of course, but at least you can be pretty sure they've covered any likely scenario.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文