Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ A Rust library for formatting PostgreSQL SQL and PL/pgSQL, powered by

Supports 7 formatting styles based on popular SQL style guides:

| Style | Description |
|-------|-------------|
| **river** (default) | Keywords right-aligned to form a visual "river" ([sqlstyle.guide](https://www.sqlstyle.guide/)) |
| **mozilla** | Keywords left-aligned, content indented 4 spaces |
| **aweber** | River style with JOINs participating in keyword alignment |
| **dbt** | Lowercase keywords, blank lines between clauses |
| **gitlab** | 2-space indent, uppercase keywords |
| **kickstarter** | 2-space indent, compact JOIN...ON on same line |
| **mattmc3** | Lowercase river with leading commas |
| Style | Description |
| ------------------- | -------------------------------------------------- |
| [**aweber**](https://gist.github.com/gmr/2cceb85bb37be96bc96f05c5b8de9e1b) (default) | River style with JOINs participating in keyword alignment |
| [**dbt**](https://docs.getdbt.com/best-practices/how-we-style/2-how-we-style-our-sql) | Lowercase keywords, blank lines between clauses |
| [**gitlab**](https://handbook.gitlab.com/handbook/enterprise-data/platform/sql-style-guide/) | 2-space indent, uppercase keywords |
| [**kickstarter**](https://gist.github.com/fredbenenson/7bb92718e19138c20591) | 2-space indent, compact JOIN...ON on same line |
| [**mattmc3**](https://gist.github.com/mattmc3/38a85e6a4ca1093816c08d4815fbebfb) | Lowercase river with leading commas |
| [**mozilla**](https://docs.telemetry.mozilla.org/concepts/sql_style.html) | Keywords left-aligned, content indented 4 spaces |
| [**river**](https://www.sqlstyle.guide/) | Keywords right-aligned to form a visual "river" |

## Usage

Expand Down Expand Up @@ -99,13 +99,15 @@ match format("SELECT * FORM broken", Style::River) {
Given: `SELECT file_hash FROM file_system WHERE file_name = '.vimrc'`

**River** (default):

```sql
SELECT file_hash
FROM file_system
WHERE file_name = '.vimrc';
```
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Inconsistency: "River (default)" should be updated.

Line 101 still labels River as the default style, but the actual Style::default() now returns Aweber (per changes in src/style.rs). The table at lines 8-16 correctly marks AWeber as the default.

📝 Proposed fix
-**River** (default):
+**River**:

And add "(default)" to the AWeber heading if you want to highlight it in the examples section as well.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
**River** (default):
```sql
SELECT file_hash
FROM file_system
WHERE file_name = '.vimrc';
```
**River**:
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@README.md` around lines 101 - 107, The README examples incorrectly label
"River" as the default; update the example headings so they match the actual
default returned by Style::default() in src/style.rs: remove "(default)" from
the "River" heading and add "(default)" to the "AWeber" (or "AWeber"/"Aweber"
exact heading used) heading in the examples section so the README and
Style::default() are consistent.


**Mozilla**:

```sql
SELECT file_hash
FROM file_system
Expand All @@ -114,6 +116,7 @@ WHERE
```

**dbt**:

```sql
select file_hash

Expand All @@ -124,6 +127,7 @@ where
```

**mattmc3** (leading commas):

```sql
select file_hash
from file_system
Expand Down
35 changes: 35 additions & 0 deletions examples/dump_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use tree_sitter::Parser;
use tree_sitter_postgres::LANGUAGE;

fn print_tree(node: tree_sitter::Node, source: &str, indent: usize) {
let kind = node.kind();
let text = &source[node.byte_range()];
let short = if text.len() > 80 { &text[..80] } else { text };
let short = short.replace('\n', "\\n");
let pad = " ".repeat(indent);
if node.is_named() {
println!("{pad}{kind}: {short:?}");
} else {
println!("{pad}[{kind}]: {short:?}");
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
print_tree(child, source, indent + 1);
}
}

fn main() {
let path = std::env::args()
.nth(1)
.expect("usage: dump_tree <file.sql>");
let sql = std::fs::read_to_string(&path).unwrap();
let input = if sql.trim().ends_with(';') {
sql.trim().to_string()
} else {
format!("{};", sql.trim())
};
let mut parser = Parser::new();
parser.set_language(&LANGUAGE.into()).unwrap();
let tree = parser.parse(&input, None).unwrap();
print_tree(tree.root_node(), &input, 0);
}
13 changes: 13 additions & 0 deletions examples/format_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use libpgfmt::{format, style::Style};
fn main() {
let sql = std::fs::read_to_string(std::env::args().nth(1).unwrap()).unwrap();
let style: Style = std::env::args()
.nth(2)
.unwrap_or("aweber".to_string())
.parse()
.unwrap();
match format(sql.trim(), style) {
Ok(f) => print!("{f}\n"),
Err(e) => eprintln!("Error: {e}"),
}
}
Loading
Loading