mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-22 15:04:04 +00:00
Add Postgres-specific PREPARE, EXECUTE and DEALLOCATE (#243)
Adds top-statements PREPARE, EXECUTE and DEALLOCATE for Postgres-specific feature prepared statement.
This commit is contained in:
parent
d2e4340a32
commit
8020b2e5f0
3 changed files with 212 additions and 2 deletions
|
@ -423,6 +423,136 @@ fn parse_show() {
|
|||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_deallocate() {
|
||||
let stmt = pg_and_generic().verified_stmt("DEALLOCATE a");
|
||||
assert_eq!(
|
||||
stmt,
|
||||
Statement::Deallocate {
|
||||
name: "a".into(),
|
||||
prepare: false,
|
||||
}
|
||||
);
|
||||
|
||||
let stmt = pg_and_generic().verified_stmt("DEALLOCATE ALL");
|
||||
assert_eq!(
|
||||
stmt,
|
||||
Statement::Deallocate {
|
||||
name: "ALL".into(),
|
||||
prepare: false,
|
||||
}
|
||||
);
|
||||
|
||||
let stmt = pg_and_generic().verified_stmt("DEALLOCATE PREPARE a");
|
||||
assert_eq!(
|
||||
stmt,
|
||||
Statement::Deallocate {
|
||||
name: "a".into(),
|
||||
prepare: true,
|
||||
}
|
||||
);
|
||||
|
||||
let stmt = pg_and_generic().verified_stmt("DEALLOCATE PREPARE ALL");
|
||||
assert_eq!(
|
||||
stmt,
|
||||
Statement::Deallocate {
|
||||
name: "ALL".into(),
|
||||
prepare: true,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_execute() {
|
||||
let stmt = pg_and_generic().verified_stmt("EXECUTE a");
|
||||
assert_eq!(
|
||||
stmt,
|
||||
Statement::Execute {
|
||||
name: "a".into(),
|
||||
parameters: vec![],
|
||||
}
|
||||
);
|
||||
|
||||
let stmt = pg_and_generic().verified_stmt("EXECUTE a(1, 't')");
|
||||
|
||||
#[cfg(feature = "bigdecimal")]
|
||||
assert_eq!(
|
||||
stmt,
|
||||
Statement::Execute {
|
||||
name: "a".into(),
|
||||
parameters: vec![
|
||||
Expr::Value(Value::Number(bigdecimal::BigDecimal::from(1))),
|
||||
Expr::Value(Value::SingleQuotedString("t".to_string()))
|
||||
],
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_prepare() {
|
||||
let stmt =
|
||||
pg_and_generic().verified_stmt("PREPARE a AS INSERT INTO customers VALUES (a1, a2, a3)");
|
||||
let sub_stmt = match stmt {
|
||||
Statement::Prepare {
|
||||
name,
|
||||
data_types,
|
||||
statement,
|
||||
..
|
||||
} => {
|
||||
assert_eq!(name, "a".into());
|
||||
assert!(data_types.is_empty());
|
||||
|
||||
statement
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
match sub_stmt.as_ref() {
|
||||
Statement::Insert {
|
||||
table_name,
|
||||
columns,
|
||||
source,
|
||||
..
|
||||
} => {
|
||||
assert_eq!(table_name.to_string(), "customers");
|
||||
assert!(columns.is_empty());
|
||||
|
||||
let expected_values = [vec![
|
||||
Expr::Identifier("a1".into()),
|
||||
Expr::Identifier("a2".into()),
|
||||
Expr::Identifier("a3".into()),
|
||||
]];
|
||||
match &source.body {
|
||||
SetExpr::Values(Values(values)) => assert_eq!(values.as_slice(), &expected_values),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let stmt = pg_and_generic()
|
||||
.verified_stmt("PREPARE a (INT, TEXT) AS SELECT * FROM customers WHERE customers.id = a1");
|
||||
let sub_stmt = match stmt {
|
||||
Statement::Prepare {
|
||||
name,
|
||||
data_types,
|
||||
statement,
|
||||
..
|
||||
} => {
|
||||
assert_eq!(name, "a".into());
|
||||
assert_eq!(data_types, vec![DataType::Int, DataType::Text]);
|
||||
|
||||
statement
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
assert_eq!(
|
||||
sub_stmt,
|
||||
Box::new(Statement::Query(Box::new(pg_and_generic().verified_query(
|
||||
"SELECT * FROM customers WHERE customers.id = a1"
|
||||
))))
|
||||
);
|
||||
}
|
||||
|
||||
fn pg() -> TestedDialects {
|
||||
TestedDialects {
|
||||
dialects: vec![Box::new(PostgreSqlDialect {})],
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue