Expand description
A Rust library for formatting PostgreSQL SQL and PL/pgSQL.
Uses tree-sitter-postgres for parsing, supporting 7 formatting styles based on popular SQL style guides.
§Quick start
use libpgfmt::{format, style::Style};
let sql = "SELECT id, name FROM users WHERE active = TRUE";
let formatted = format(sql, Style::River).unwrap();
assert_eq!(formatted, "SELECT id,\n name\n FROM users\n WHERE active = TRUE;");§Styles
The Style enum provides 7 formatting variants:
- River — keywords right-aligned to form a visual river
- Mozilla — keywords left-aligned, content indented 4 spaces
- AWeber (default) — river with JOINs in keyword alignment
- Dbt — lowercase keywords, blank lines between clauses
- Gitlab — 2-space indent, uppercase keywords
- Kickstarter — 2-space indent, compact JOINs
- Mattmc3 — lowercase river with leading commas
Styles can be parsed from strings:
use libpgfmt::style::Style;
let style: Style = "dbt".parse().unwrap();
assert_eq!(style, Style::Dbt);§PL/pgSQL
Format PL/pgSQL function bodies (the content between $$ delimiters)
with format_plpgsql:
use libpgfmt::{format_plpgsql, style::Style};
let body = "BEGIN RETURN 1; END";
let formatted = format_plpgsql(body, Style::River).unwrap();
assert_eq!(formatted, "BEGIN\n RETURN 1;\nEND;");Modules§
Functions§
- format
- Format one or more PostgreSQL SQL statements according to the specified style.
- format_
plpgsql - Format PL/pgSQL code according to the specified style.