Skip to content

Commit a14692e

Browse files
committed
docs: update the Readme to be more useful
1 parent fd28063 commit a14692e

File tree

1 file changed

+201
-9
lines changed

1 file changed

+201
-9
lines changed

README.md

Lines changed: 201 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,207 @@
11
# sqlformat
22

3-
[![Build Status](https://github.com/shssoichiro/sqlformat-rs/workflows/sqlformat/badge.svg)](https://github.com/shssoichiro/sqlformat-rs/actions?query=branch%3Amaster)
4-
[![unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/)
53
[![Version](https://img.shields.io/crates/v/sqlformat.svg)](https://crates.io/crates/sqlformat)
64
[![Docs](https://docs.rs/sqlformat/badge.svg)](https://docs.rs/sqlformat)
5+
[![Build Status](https://github.com/shssoichiro/sqlformat-rs/workflows/sqlformat/badge.svg)](https://github.com/shssoichiro/sqlformat-rs/actions?query=branch%3Amaster)
6+
[![unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/)
7+
[![Codecov](https://img.shields.io/codecov/c/github/shssoichiro/sqlformat-rs)](https://app.codecov.io/gh/shssoichiro/sqlformat-rs)
8+
9+
Format SQL strings into readable, consistently styled output. `sqlformat` is a pure-Rust library designed to pretty-print SQL from a variety of mainstream dialects, ideal for logging, debugging, tests, or developer tools.
10+
11+
This crate is a Rust port of [sql-formatter-plus](https://github.com/kufii/sql-formatter-plus). There is currently no binary; the crate is intended to be used as a library.
12+
13+
## Key features
14+
15+
- **Broad SQL support**: Common constructs from PostgreSQL, MySQL/MariaDB, SQLite, SQL Server, and Oracle (DDL, DML, CTEs, CASE, JOINs, window functions, operators, type casts, etc.).
16+
- **Configurable style**: Indentation (spaces or tabs), upper/lower/preserve keyword case, control lines between statements.
17+
- **Inline controls**: Keep short blocks or argument lists inline when they fit; split when they don’t.
18+
- **Parameter interpolation**: Supports `?`, `?1`, `$1`, `$name`, `:name`, `@name`, and bracketed variants via `QueryParams`.
19+
- **Comment-aware**: Respects line/block comments; supports in-query toggles to temporarily disable formatting.
20+
- **Safe Rust**: `#![forbid(unsafe_code)]`.
21+
22+
## Quick start
23+
24+
```rust
25+
use sqlformat::{format, FormatOptions, Indent, QueryParams};
26+
27+
fn main() {
28+
let sql = "SELECT id, name FROM users WHERE created_at > NOW();";
29+
let options = FormatOptions::default();
30+
let formatted = format(sql, &QueryParams::None, &options);
31+
println!("{}", formatted);
32+
}
33+
```
34+
35+
Output:
36+
37+
```text
38+
SELECT
39+
id,
40+
name
41+
FROM
42+
users
43+
WHERE
44+
created_at > NOW();
45+
```
46+
47+
## Installation
48+
49+
Add via Cargo:
50+
51+
```bash
52+
cargo add sqlformat
53+
```
54+
55+
Or manually in `Cargo.toml`:
56+
57+
```toml
58+
[dependencies]
59+
sqlformat = "*"
60+
```
61+
62+
Minimum Supported Rust Version (MSRV): `1.84`.
63+
64+
## Usage examples
65+
66+
### Basic formatting
67+
68+
```rust
69+
use sqlformat::{format, FormatOptions, QueryParams};
70+
71+
let sql = "SELECT count(*), col FROM t WHERE a = 1 AND b = 2;";
72+
let out = format(sql, &QueryParams::None, &FormatOptions::default());
73+
```
74+
75+
### Indentation
76+
77+
```rust
78+
use sqlformat::{format, FormatOptions, Indent, QueryParams};
79+
80+
let options = FormatOptions { indent: Indent::Spaces(4), ..Default::default() };
81+
let out = format("SELECT a, b FROM t;", &QueryParams::None, &options);
82+
83+
let options = FormatOptions { indent: Indent::Tabs, ..Default::default() };
84+
let out = format("SELECT a, b FROM t;", &QueryParams::None, &options);
85+
```
86+
87+
### Keyword case conversion
88+
89+
```rust
90+
use sqlformat::{format, FormatOptions, QueryParams};
91+
92+
// Uppercase reserved keywords
93+
let options = FormatOptions { uppercase: Some(true), ..Default::default() };
94+
let out = format("select distinct * from foo where bar = 1", &QueryParams::None, &options);
95+
96+
// Lowercase reserved keywords
97+
let options = FormatOptions { uppercase: Some(false), ..Default::default() };
98+
let out = format("SELECT DISTINCT * FROM FOO WHERE BAR = 1", &QueryParams::None, &options);
99+
100+
// Preserve case with exceptions
101+
let options = FormatOptions {
102+
uppercase: Some(true),
103+
ignore_case_convert: Some(vec!["from", "where"]),
104+
..Default::default()
105+
};
106+
let out = format("select * from foo where bar = 1", &QueryParams::None, &options);
107+
```
108+
109+
### Inline/compact formatting
110+
111+
Control how aggressively short blocks and argument lists are kept on one line.
112+
113+
```rust
114+
use sqlformat::{format, FormatOptions, QueryParams};
115+
116+
let options = FormatOptions {
117+
inline: false, // when true, forces single-line output
118+
max_inline_block: 50, // characters allowed to keep a parenthesized block inline
119+
max_inline_arguments: Some(40),
120+
max_inline_top_level: Some(40),
121+
..Default::default()
122+
};
123+
let out = format("SELECT a, b, c, d, e, f, g, h FROM t;", &QueryParams::None, &options);
124+
```
125+
126+
### JOIN layout
127+
128+
Treat any JOIN as a top-level keyword (affects line breaks):
129+
130+
```rust
131+
use sqlformat::{format, FormatOptions, QueryParams};
132+
133+
let options = FormatOptions { joins_as_top_level: true, ..Default::default() };
134+
let out = format("SELECT * FROM a INNER JOIN b ON a.id = b.a_id", &QueryParams::None, &options);
135+
```
136+
137+
### Parameter interpolation
138+
139+
`sqlformat` can substitute placeholders using `QueryParams`:
140+
141+
```rust
142+
use sqlformat::{format, FormatOptions, QueryParams};
143+
144+
// Numbered / positional (e.g., ?, ?1, $1)
145+
let sql = "SELECT ?1, ?, $2;";
146+
let params = QueryParams::Indexed(vec!["first".to_string(), "second".to_string(), "third".to_string()]);
147+
let out = format(sql, &params, &FormatOptions::default());
148+
149+
// Named (e.g., $name, :name, @name, :\"weird name\")
150+
let sql = "SELECT $hash, :name, @`var name`;";
151+
let params = QueryParams::Named(vec![
152+
("hash".to_string(), "hash value".to_string()),
153+
("name".to_string(), "Alice".to_string()),
154+
("var name".to_string(), "Bob".to_string()),
155+
]);
156+
let out = format(sql, &params, &FormatOptions::default());
157+
```
158+
159+
### Controlling blank lines between statements
160+
161+
```rust
162+
use sqlformat::{format, FormatOptions, QueryParams};
163+
164+
let options = FormatOptions { lines_between_queries: 2, ..Default::default() };
165+
let out = format("SELECT 1; SELECT 2;", &QueryParams::None, &options);
166+
```
167+
168+
### Temporarily disabling the formatter
169+
170+
You can turn formatting off/on using SQL comments. This is helpful when you want to preserve a very specific layout.
171+
172+
```sql
173+
-- fmt: off
174+
SELECT * FROM t WHERE a=1 AND b=2; -- preserved as-is
175+
-- fmt: on
176+
177+
/* fmt: off */ SELECT 1 + 2; /* fmt: on */
178+
```
179+
180+
## Configuration reference
181+
182+
The formatter is configured through `FormatOptions`. See the full API on the docs site for list of options.
183+
184+
## API reference
185+
186+
- Crate docs: [`docs.rs/sqlformat`](https://docs.rs/sqlformat)
187+
- Primary entry point: `format(query: &str, params: &QueryParams, options: &FormatOptions) -> String`
188+
189+
## Contributing
190+
191+
Contributions are welcome!
192+
193+
- Run tests: `cargo test`
194+
- Run benchmarks (optional): `cargo bench`
195+
196+
Please open issues and pull requests with clear descriptions and examples. Bug reports that include an input SQL snippet, your `FormatOptions`, and the actual vs. expected output are especially helpful.
197+
198+
## License
199+
200+
Dual-licensed under either of:
201+
202+
- MIT License (`LICENSE-MIT`)
203+
- Apache License, Version 2.0 (`LICENSE-APACHE`)
7204

8-
This crate is a port of [sql-formatter-plus](https://github.com/kufii/sql-formatter-plus)
9-
written in Rust. It is intended to be usable as a pure-Rust library
10-
for formatting SQL queries.
205+
## Acknowledgements
11206

12-
There is currently no binary interface.
13-
This crate was written for formatting queries to logs
14-
within `sqlx`, but it may be useful to other crates
15-
in the Rust ecosystem.
207+
Based on the excellent work in [`sql-formatter-plus`](https://github.com/kufii/sql-formatter-plus).

0 commit comments

Comments
 (0)