A SQL formatter tool written in Go that can be used both as a library and as a CLI command-line tool.
- 🔧 Format compressed SQL statements into readable multi-line format
- 📚 Can be integrated into your projects as a Go library
- 💻 Provides command-line tool with multiple input methods
- ⚙️ Configurable formatting options (indent size, keyword case, etc.)
- 🚀 Supports complex SQL statements including JOIN, subqueries, etc.
- SELECT (including JOIN, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, etc.)
- INSERT
- UPDATE
- DELETE
go get github.com/BruceDu521/sql-formattergo build -o sqlformatter cmd/main.goOr download pre-built binaries from Releases.
package main
import (
"fmt"
"log"
sqlformatter "github.com/BruceDu521/sql-formatter"
)
func main() {
// Create formatter
formatter := sqlformatter.NewFormatter()
// Configuration options
formatter.IndentSize = 2 // Number of spaces for indentation
formatter.KeywordUpper = true // Use uppercase for keywords
// Format SQL
sql := "select u.id, u.name from users u where u.age > 25"
formatted, err := formatter.Format(sql)
if err != nil {
log.Fatal(err)
}
fmt.Println(formatted)
}# Direct SQL input
./sqlformatter -sql "select * from users where id = 1"
# Read from file
./sqlformatter -input input.sql
# Output to file
./sqlformatter -input input.sql -output formatted.sql
# Use pipe
echo "select * from users" | ./sqlformatterOptions:
-sql string SQL statement to format
-input string Input SQL file
-output string Output file
-indent int Number of spaces for indentation (default: 2)
-uppercase Use uppercase for keywords (default: true)
-help Show help information
Input:
select u.id, p.product_name, u.name from users u join products p on u.id = p.user_id where u.age > 25 and p.category = 'electronics' group by u.id order by p.price desc limit 10Output:
SELECT
u.id,
p.product_name,
u.name
FROM
users u
JOIN products p on u.id = p.user_id
WHERE
u.age > 25 and p.category = 'electronics'
GROUP BY
u.id
ORDER BY
p.price desc
LIMIT
10Input:
INSERT INTO users (name, email) VALUES ('Alice', '[email protected]')Output:
INSERT INTO users
(name, email)
VALUES
('Alice', '[email protected]')Input:
UPDATE users SET name = 'John', email = '[email protected]' WHERE id = 1Output:
UPDATE users
SET
name = 'John',
email = '[email protected]'
WHERE
id = 1Input:
DELETE FROM users WHERE age < 18Output:
DELETE FROM users
WHERE
age < 18sql-formatter/
├── formatter.go # Core formatting logic
├── cmd/
│ └── main.go # CLI tool
├── example/
│ └── main.go # Usage examples
├── formatter_test.go # Unit tests
├── go.mod # Go module file
├── README.md # Project documentation (English)
├── README_zh.md # Project documentation (Chinese)
├── test.sql # Test SQL file
└── formatted.sql # Formatted output example
# Run library usage example
go run example/main.go
# Run CLI tool
go run cmd/main.go -help
go run cmd/main.go -sql "select * from users"
go run cmd/main.go -input test.sql
# Run tests
go test -v
# Run benchmarks
go test -bench=.- Support more SQL statement types (CREATE TABLE, ALTER TABLE, etc.)
- Add more formatting options
- Support different database dialects
Issues and Pull Requests are welcome!
MIT License