Skip to content
Merged
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
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ pub fn format_plpgsql(code: &str, style: Style) -> Result<String, FormatError> {
.ok_or_else(|| FormatError::Parser("Failed to parse PL/pgSQL".into()))?;
let root = tree.root_node();
if root.has_error() {
// The body may not be PL/pgSQL at all — e.g. a LANGUAGE sql function
// body, which is a bare SQL statement (WITH … SELECT, etc.). Fall back
// to SQL formatting; if that parses cleanly, use it. Otherwise report
// the original PL/pgSQL syntax error.
if let Ok(sql) = format(trimmed, style) {
return Ok(sql);
}
return Err(FormatError::Syntax(find_error_message(&root, trimmed)));
}
let fmt = Formatter::new(trimmed, style);
Expand Down
10 changes: 10 additions & 0 deletions tests/plpgsql_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,13 @@ fn for_over_query_keeps_query() {
);
assert!(result.contains("RETURN NEXT r;"), "\nGot:\n{result}");
}

// Regression: format_plpgsql falls back to SQL formatting when the body is not
// PL/pgSQL (e.g. a LANGUAGE sql function body), rather than erroring.
#[test]
fn sql_body_fallback() {
let body = "WITH t AS (SELECT 1 AS n) SELECT n FROM t";
let result = format_plpgsql(body, Style::Aweber).unwrap();
assert!(result.contains("SELECT"), "\nGot:\n{result}");
assert!(result.trim_end().ends_with(';'), "\nGot:\n{result}");
}
Loading