用于将SQL(SELECT查询)转换为XML FORM的API或工具

时间:2022-10-17 19:18:40

Need help to convert SQL queries to some sort of standard XML format.

需要帮助将SQL查询转换为某种标准XML格式。

For example, lets say I have a query "SELECT A, B, C FROM XYZ WHERE B = C AND A > 2"

例如,假设我有一个查询“SELECT A,B,C FROM XYZ WHERE B = C AND A> 2”

I should get the result as

我应该得到结果

<QUERY>
<SELECT>
    <COLUMNS>
        <COLUMN>A</COLUMN>
        <COLUMN>B</COLUMN>
        <COLUMN>B</COLUMN>
    </COLUMNS>
</SELECT>
<FROM>
    <TABLES>
    </TABLES>
</FROM>
<WHERE>
    <CONDITIONS>
        <AND>
            <CONDITION>
                <EQUALS>
                <VARIABLE>B</VARIABLE>
                <VARIABLE>C</VARIABLE>
                </EQUALS>
            </CONDITION>
            <CONDITION>
                <GREATER>
                <VARIABLE>A</VARIABLE>
                <VARIABLE>2</VARIABLE>
                </GREATER>
            </CONDITION>
        </AND>
    </CONDITIONS>
</WHERE>
</QUERY>

Please suggest me some API or tools which can provide me this XML result.

请向我推荐一些可以为我提供此XML结果的API或工具。

3 个解决方案

#1


1  

have look to this link in codeproject SQL Parser it give you a great idea,

看看codeproject中的这个链接SQL Parser它给你一个好主意,

#2


1  

This looks interesting. Although late in my response, I tried the following to see if I could come close to coming up with a way to do this. It needs more work, but I believe that by using this format you can come up with a workable solution.

这看起来很有趣。虽然在我的回复中,我尝试了以下内容,看看我是否能够提出一种方法来做到这一点。它需要更多的工作,但我相信通过使用这种格式,您可以提出一个可行的解决方案。

    SET NOCOUNT ON

    ------------------------------------------------------------------------------
    ----------- DECLARE VARIABLES AND TABLES -------------------------------------
    ------------------------------------------------------------------------------
    DECLARE @QueryID                  INT
    DECLARE @SelectID                 INT
    DECLARE @ColumnsBeginID           INT
    DECLARE @ColumnsEndID             INT
    DECLARE @FromID                   INT
    DECLARE @TablesBeginID            INT
    DECLARE @TablesEndID              INT
    DECLARE @WhereID                  INT
    DECLARE @ConditionsBeginID        INT
    DECLARE @ConditionsEndID          INT

    DECLARE @XMLQuery                 XML = '<QUERY></QUERY>'


    DECLARE @Query TABLE
    (
          QueryID                     INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [SELECT]                    INT             NOT NULL    
        , [FROM]                      INT             NOT NULL    
        , [WHERE]                     INT                 NULL      
    )

    DECLARE @Select TABLE
    (
          SelectID                    INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [COLUMNS_BEGIN]             INT             NOT NULL    
        , [COLUMNS_END]               INT             NOT NULL    
    )

    DECLARE @Column TABLE
    (
          ColumnID                    INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [Column]                    SYSNAME         NOT NULL
        , [TableAlias]                SYSNAME         NOT NULL
        , [ColumnAlias]               SYSNAME         NOT NULL
    )

    DECLARE @From TABLE
    (
          FromID                      INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [TABLES_BEGIN]              INT             NOT NULL    
        , [TABLES_END]                INT             NOT NULL    
    )

    DECLARE @Table TABLE
    (
          TableID                      INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [Join]                       SYSNAME         NOT NULL    
        , [Tablename]                  SYSNAME         NOT NULL    
        , [TableAlias]                 SYSNAME         NOT NULL    
        , [On]                         SYSNAME         NOT NULL    
    )

    DECLARE @Where TABLE
    (
          WhereID                     INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [CONDITIONS_BEGIN]          INT             NOT NULL    
        , [CONDITIONS_END]            INT             NOT NULL    
    )

    DECLARE @Condition TABLE
    (
          ConditionID                 INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [AndOr]                     SYSNAME         NOT NULL    
        , [Equality]                  SYSNAME         NOT NULL
        , [Variable1]                 SYSNAME         NOT NULL    
        , [Variable2]                 SYSNAME         NOT NULL    
    )

    ------------------------------------------------------------------------------
    ----------- USERS QUERY HERE -------------------------------------------------
    ------------------------------------------------------------------------------

    --SELECT A, B, C FROM XYZ WHERE B = C AND A > 2

    INSERT INTO @Column ([Column], [TableAlias], [ColumnAlias]) VALUES ('A', 'x', 'a')
    SET @ColumnsBeginID = @@IDENTITY 
    INSERT INTO @Column ([Column], [TableAlias], [ColumnAlias]) VALUES ('B', 'x', 'b')
    INSERT INTO @Column ([Column], [TableAlias], [ColumnAlias]) VALUES ('C', 'x', 'c')
    SET @ColumnsEndID = @@IDENTITY 

    INSERT INTO @Select ([COLUMNS_BEGIN], [COLUMNS_END]) VALUES (@ColumnsBeginID, @ColumnsEndID)
    SET @SelectID = @@IDENTITY


    INSERT INTO @Table ([Join], [Tablename], [TableAlias], [On]) VALUES ('FROM', 'XYZ', 'x', '')
    SET @TablesBeginID = @@IDENTITY
    SET @TablesEndID = @TablesBeginID

    INSERT INTO @From ([TABLES_BEGIN], [TABLES_END]) VALUES (@TablesBeginID, @TablesEndID)
    SET @FromID = @@IDENTITY


    INSERT INTO @Condition ([AndOr], [Equality], [Variable1], [Variable2]) VALUES ('AND','EQUALS','B','C')
    SET @ConditionsBeginID = @@IDENTITY
    INSERT INTO @Condition ([AndOr], [Equality], [Variable1], [Variable2]) VALUES ('AND','GREATER','A','2')
    SET @ConditionsEndID = @@IDENTITY

    INSERT INTO @Where ([CONDITIONS_BEGIN], [CONDITIONS_END]) VALUES (@ConditionsBeginID, @ConditionsEndID)
    SET @WhereID = @@IDENTITY


    INSERT INTO @Query ([SELECT], [FROM], [WHERE]) VALUES (@SelectID, @FromID, @WhereID)


    ------------------------------------------------------------------------------ 
    ----------- BUILD THE @XMLQuery VARIABLE ------------------------------------- 
    ------------------------------------------------------------------------------ 

    SET @XMLQuery = (SELECT SelectColumn            = c.[Column]
                          , SelectColumnTableAlias  = c.[TableAlias]
                          , SelectColumnColumnAlias = c.[ColumnAlias]

                          , FromTableJoin           = t.[Join]
                          , FromTable               = t.[Tablename]
                          , FromTableAlias          = t.[TableAlias]
                          , FromOn                  = t.[On]

                          , WhereConditionAndOr     = n.[AndOr]     
                          , WhereConditionEquaility = n.[Equality]  
                          , WhereConditionVariable1 = n.[Variable1] 
                          , WhereConditionVariable2 = n.[Variable2] 

                       FROM  @Query     q
                       JOIN  @Select    s  ON s.SelectID    = q.[SELECT]
                       JOIN  @From      f  ON f.FromID      = q.[FROM]
                       JOIN  @Where     w  ON w.WhereID     = q.[WHERE]
                       JOIN  @Column    c  ON c.ColumnID    BETWEEN s.[COLUMNS_BEGIN]    AND s.[COLUMNS_END]
                       JOIN  @Table     t  ON t.TableID     BETWEEN f.[TABLES_BEGIN]     AND f.[TABLES_END]
                       JOIN  @Condition n  ON n.ConditionID BETWEEN w.[CONDITIONS_BEGIN] AND w.[CONDITIONS_END]

                   ORDER BY c.ColumnID
                          , t.TableID
                          , n.ConditionID
                        FOR XML AUTO, ROOT ('QUERY') 
                        --FOR XML RAW, ROOT ('QUERY') 
                        --FOR XML PATH, ROOT ('QUERY') 
                      )

    SELECT @XMLQuery

#1


1  

have look to this link in codeproject SQL Parser it give you a great idea,

看看codeproject中的这个链接SQL Parser它给你一个好主意,

#2


1  

This looks interesting. Although late in my response, I tried the following to see if I could come close to coming up with a way to do this. It needs more work, but I believe that by using this format you can come up with a workable solution.

这看起来很有趣。虽然在我的回复中,我尝试了以下内容,看看我是否能够提出一种方法来做到这一点。它需要更多的工作,但我相信通过使用这种格式,您可以提出一个可行的解决方案。

    SET NOCOUNT ON

    ------------------------------------------------------------------------------
    ----------- DECLARE VARIABLES AND TABLES -------------------------------------
    ------------------------------------------------------------------------------
    DECLARE @QueryID                  INT
    DECLARE @SelectID                 INT
    DECLARE @ColumnsBeginID           INT
    DECLARE @ColumnsEndID             INT
    DECLARE @FromID                   INT
    DECLARE @TablesBeginID            INT
    DECLARE @TablesEndID              INT
    DECLARE @WhereID                  INT
    DECLARE @ConditionsBeginID        INT
    DECLARE @ConditionsEndID          INT

    DECLARE @XMLQuery                 XML = '<QUERY></QUERY>'


    DECLARE @Query TABLE
    (
          QueryID                     INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [SELECT]                    INT             NOT NULL    
        , [FROM]                      INT             NOT NULL    
        , [WHERE]                     INT                 NULL      
    )

    DECLARE @Select TABLE
    (
          SelectID                    INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [COLUMNS_BEGIN]             INT             NOT NULL    
        , [COLUMNS_END]               INT             NOT NULL    
    )

    DECLARE @Column TABLE
    (
          ColumnID                    INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [Column]                    SYSNAME         NOT NULL
        , [TableAlias]                SYSNAME         NOT NULL
        , [ColumnAlias]               SYSNAME         NOT NULL
    )

    DECLARE @From TABLE
    (
          FromID                      INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [TABLES_BEGIN]              INT             NOT NULL    
        , [TABLES_END]                INT             NOT NULL    
    )

    DECLARE @Table TABLE
    (
          TableID                      INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [Join]                       SYSNAME         NOT NULL    
        , [Tablename]                  SYSNAME         NOT NULL    
        , [TableAlias]                 SYSNAME         NOT NULL    
        , [On]                         SYSNAME         NOT NULL    
    )

    DECLARE @Where TABLE
    (
          WhereID                     INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [CONDITIONS_BEGIN]          INT             NOT NULL    
        , [CONDITIONS_END]            INT             NOT NULL    
    )

    DECLARE @Condition TABLE
    (
          ConditionID                 INT             NOT NULL    IDENTITY (1,1)   PRIMARY KEY
        , [AndOr]                     SYSNAME         NOT NULL    
        , [Equality]                  SYSNAME         NOT NULL
        , [Variable1]                 SYSNAME         NOT NULL    
        , [Variable2]                 SYSNAME         NOT NULL    
    )

    ------------------------------------------------------------------------------
    ----------- USERS QUERY HERE -------------------------------------------------
    ------------------------------------------------------------------------------

    --SELECT A, B, C FROM XYZ WHERE B = C AND A > 2

    INSERT INTO @Column ([Column], [TableAlias], [ColumnAlias]) VALUES ('A', 'x', 'a')
    SET @ColumnsBeginID = @@IDENTITY 
    INSERT INTO @Column ([Column], [TableAlias], [ColumnAlias]) VALUES ('B', 'x', 'b')
    INSERT INTO @Column ([Column], [TableAlias], [ColumnAlias]) VALUES ('C', 'x', 'c')
    SET @ColumnsEndID = @@IDENTITY 

    INSERT INTO @Select ([COLUMNS_BEGIN], [COLUMNS_END]) VALUES (@ColumnsBeginID, @ColumnsEndID)
    SET @SelectID = @@IDENTITY


    INSERT INTO @Table ([Join], [Tablename], [TableAlias], [On]) VALUES ('FROM', 'XYZ', 'x', '')
    SET @TablesBeginID = @@IDENTITY
    SET @TablesEndID = @TablesBeginID

    INSERT INTO @From ([TABLES_BEGIN], [TABLES_END]) VALUES (@TablesBeginID, @TablesEndID)
    SET @FromID = @@IDENTITY


    INSERT INTO @Condition ([AndOr], [Equality], [Variable1], [Variable2]) VALUES ('AND','EQUALS','B','C')
    SET @ConditionsBeginID = @@IDENTITY
    INSERT INTO @Condition ([AndOr], [Equality], [Variable1], [Variable2]) VALUES ('AND','GREATER','A','2')
    SET @ConditionsEndID = @@IDENTITY

    INSERT INTO @Where ([CONDITIONS_BEGIN], [CONDITIONS_END]) VALUES (@ConditionsBeginID, @ConditionsEndID)
    SET @WhereID = @@IDENTITY


    INSERT INTO @Query ([SELECT], [FROM], [WHERE]) VALUES (@SelectID, @FromID, @WhereID)


    ------------------------------------------------------------------------------ 
    ----------- BUILD THE @XMLQuery VARIABLE ------------------------------------- 
    ------------------------------------------------------------------------------ 

    SET @XMLQuery = (SELECT SelectColumn            = c.[Column]
                          , SelectColumnTableAlias  = c.[TableAlias]
                          , SelectColumnColumnAlias = c.[ColumnAlias]

                          , FromTableJoin           = t.[Join]
                          , FromTable               = t.[Tablename]
                          , FromTableAlias          = t.[TableAlias]
                          , FromOn                  = t.[On]

                          , WhereConditionAndOr     = n.[AndOr]     
                          , WhereConditionEquaility = n.[Equality]  
                          , WhereConditionVariable1 = n.[Variable1] 
                          , WhereConditionVariable2 = n.[Variable2] 

                       FROM  @Query     q
                       JOIN  @Select    s  ON s.SelectID    = q.[SELECT]
                       JOIN  @From      f  ON f.FromID      = q.[FROM]
                       JOIN  @Where     w  ON w.WhereID     = q.[WHERE]
                       JOIN  @Column    c  ON c.ColumnID    BETWEEN s.[COLUMNS_BEGIN]    AND s.[COLUMNS_END]
                       JOIN  @Table     t  ON t.TableID     BETWEEN f.[TABLES_BEGIN]     AND f.[TABLES_END]
                       JOIN  @Condition n  ON n.ConditionID BETWEEN w.[CONDITIONS_BEGIN] AND w.[CONDITIONS_END]

                   ORDER BY c.ColumnID
                          , t.TableID
                          , n.ConditionID
                        FOR XML AUTO, ROOT ('QUERY') 
                        --FOR XML RAW, ROOT ('QUERY') 
                        --FOR XML PATH, ROOT ('QUERY') 
                      )

    SELECT @XMLQuery

#3