Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,18 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports a leading `WITH XMLNAMESPACES (...)`
/// clause in queries.
///
/// Example:
/// ```sql
/// WITH XMLNAMESPACES ('urn:example' AS ns)
/// SELECT 1
/// ```
fn supports_with_xmlnamespaces_clause(&self) -> bool {
false
}

/// Returns true if the dialect supports `USING <format>` in `CREATE TABLE`.
///
/// Example:
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ impl Dialect for MsSqlDialect {
_ => None,
}
}

fn supports_with_xmlnamespaces_clause(&self) -> bool {
true
}
}

impl MsSqlDialect {
Expand Down
31 changes: 25 additions & 6 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14108,12 +14108,31 @@ impl<'a> Parser<'a> {
pub fn parse_query(&mut self) -> Result<Box<Query>, ParserError> {
let _guard = self.recursion_counter.try_decrease()?;
let with = if self.parse_keyword(Keyword::WITH) {
let with_token = self.get_current_token();
Some(With {
with_token: with_token.clone().into(),
recursive: self.parse_keyword(Keyword::RECURSIVE),
cte_tables: self.parse_comma_separated(Parser::parse_cte)?,
})
let with_token = self.get_current_token().clone();
if self.dialect.supports_with_xmlnamespaces_clause()
&& self.parse_keyword(Keyword::XMLNAMESPACES)
{
self.expect_token(&Token::LParen)?;
let _namespaces =
self.parse_comma_separated(Parser::parse_xml_namespace_definition)?;
self.expect_token(&Token::RParen)?;

if self.consume_token(&Token::Comma) {
Some(With {
with_token: with_token.clone().into(),
recursive: self.parse_keyword(Keyword::RECURSIVE),
cte_tables: self.parse_comma_separated(Parser::parse_cte)?,
})
} else {
None
}
} else {
Some(With {
with_token: with_token.clone().into(),
recursive: self.parse_keyword(Keyword::RECURSIVE),
cte_tables: self.parse_comma_separated(Parser::parse_cte)?,
})
}
} else {
None
};
Expand Down
22 changes: 22 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2923,3 +2923,25 @@ fn parse_mssql_money_constants() {
expr_from_projection(only(&select.projection)),
);
}

#[test]
fn parse_xmlnamespaces() {
let sql = r#"WITH XMLNAMESPACES ('urn:test' AS ns)
SELECT 1 AS [ns:Value]
FOR XML PATH('ns:Root');"#;

ms().parse_sql_statements(sql).unwrap();

}

#[test]
fn parse_xmlnamespaces_with_cte() {
let sql = r#"
WITH XMLNAMESPACES ('urn:example' AS ns), t AS (
SELECT 1 AS id
)
SELECT id FROM t
"#;

ms().parse_sql_statements(sql).unwrap();
}