Skip to content

Commit

Permalink
Support EXECUTE ... USING for Postgres (#1153)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanlehto authored Mar 1, 2024
1 parent 9db9d22 commit 68b52a4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2404,11 +2404,15 @@ pub enum Statement {
/// Note: this is a PostgreSQL-specific statement.
Deallocate { name: Ident, prepare: bool },
/// ```sql
/// EXECUTE name [ ( parameter [, ...] ) ]
/// EXECUTE name [ ( parameter [, ...] ) ] [USING <expr>]
/// ```
///
/// Note: this is a PostgreSQL-specific statement.
Execute { name: Ident, parameters: Vec<Expr> },
Execute {
name: Ident,
parameters: Vec<Expr>,
using: Vec<Expr>,
},
/// ```sql
/// PREPARE name [ ( data_type [, ...] ) ] AS statement
/// ```
Expand Down Expand Up @@ -3824,11 +3828,18 @@ impl fmt::Display for Statement {
prepare = if *prepare { "PREPARE " } else { "" },
name = name,
),
Statement::Execute { name, parameters } => {
Statement::Execute {
name,
parameters,
using,
} => {
write!(f, "EXECUTE {name}")?;
if !parameters.is_empty() {
write!(f, "({})", display_comma_separated(parameters))?;
}
if !using.is_empty() {
write!(f, " USING {}", display_comma_separated(using))?;
};
Ok(())
}
Statement::Prepare {
Expand Down
15 changes: 14 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8948,7 +8948,20 @@ impl<'a> Parser<'a> {
self.expect_token(&Token::RParen)?;
}

Ok(Statement::Execute { name, parameters })
let mut using = vec![];
if self.parse_keyword(Keyword::USING) {
using.push(self.parse_expr()?);

while self.consume_token(&Token::Comma) {
using.push(self.parse_expr()?);
}
};

Ok(Statement::Execute {
name,
parameters,
using,
})
}

pub fn parse_prepare(&mut self) -> Result<Statement, ParserError> {
Expand Down
24 changes: 24 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,7 @@ fn parse_execute() {
Statement::Execute {
name: "a".into(),
parameters: vec![],
using: vec![]
}
);

Expand All @@ -1433,6 +1434,29 @@ fn parse_execute() {
Expr::Value(number("1")),
Expr::Value(Value::SingleQuotedString("t".to_string()))
],
using: vec![]
}
);

let stmt = pg_and_generic()
.verified_stmt("EXECUTE a USING CAST(1337 AS SMALLINT), CAST(7331 AS SMALLINT)");
assert_eq!(
stmt,
Statement::Execute {
name: "a".into(),
parameters: vec![],
using: vec![
Expr::Cast {
expr: Box::new(Expr::Value(Value::Number("1337".parse().unwrap(), false))),
data_type: DataType::SmallInt(None),
format: None
},
Expr::Cast {
expr: Box::new(Expr::Value(Value::Number("7331".parse().unwrap(), false))),
data_type: DataType::SmallInt(None),
format: None
},
]
}
);
}
Expand Down

0 comments on commit 68b52a4

Please sign in to comment.