Commit 9a06c1b

mo khan <mo@mokhan.ca>
2025-09-24 18:50:23
docs: add README and LICENSE
1 parent eab25b5
Changed files (2)
LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2025 mo khan
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
README.md
@@ -1,3 +1,264 @@
-# sqlfmt
+# xlg-sqlfmt
 
-TBD
+A fast, reliable SQL formatter written in Rust that reads SQL from stdin and writes beautifully formatted SQL to stdout.
+
+[![Crates.io](https://img.shields.io/crates/v/xlg-sqlfmt)](https://crates.io/crates/xlg-sqlfmt)
+[![Documentation](https://docs.rs/xlg-sqlfmt/badge.svg)](https://docs.rs/xlg-sqlfmt)
+[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
+
+## Features
+
+- **Fast and reliable** - Built with Rust for maximum performance
+- **Stdin/stdout interface** - Perfect for shell pipelines and editor integration
+- **Idempotent formatting** - Running the formatter multiple times produces identical output
+- **Comprehensive SQL support** - Handles complex queries with subqueries, CTEs, JOINs, and more
+- **Smart formatting** - Single-line SELECT for simple projections, multi-line for complex ones
+- **Proper AND/OR formatting** - Each logical operator on its own line with correct indentation
+- **Comment preservation** - Maintains your SQL comments in their proper positions
+- **Error handling** - On parse errors, outputs original SQL and exits with non-zero code
+
+## Installation
+
+### Using Cargo
+
+```bash
+cargo install xlg-sqlfmt
+```
+
+### From Source
+
+```bash
+git clone https://github.com/xlgmokha/sqlfmt.git
+cd sqlfmt
+cargo install --path .
+```
+
+## Usage
+
+### Basic Usage
+
+```bash
+# Format SQL from stdin
+echo "select * from users where active=1" | xlg-sqlfmt
+
+# Format a SQL file
+xlg-sqlfmt < query.sql
+
+# Format and save to file
+xlg-sqlfmt < input.sql > formatted.sql
+
+# Use in a pipeline
+cat messy.sql | xlg-sqlfmt | less
+```
+
+### Integration with Editors
+
+#### Vim/Neovim
+
+Add to your `.vimrc` or `init.vim`:
+
+```vim
+" Format SQL with xlg-sqlfmt
+command! SQLFormat %!xlg-sqlfmt
+```
+
+## Formatting Rules
+
+### SELECT Statements
+
+**Simple projections** (single column or literal) are formatted on one line:
+
+```sql
+-- Input
+SELECT 1 AS one FROM users WHERE active = true
+
+-- Output
+SELECT 1 AS one
+FROM
+  users
+WHERE active = TRUE;
+```
+
+**Complex projections** are formatted with each column on its own line:
+
+```sql
+-- Input
+SELECT id, name, email, created_at FROM users
+
+-- Output
+SELECT
+  id,
+  name,
+  email,
+  created_at
+FROM
+  users;
+```
+
+### WHERE Clauses with AND/OR
+
+Each logical operator starts a new line with proper indentation:
+
+```sql
+-- Input
+SELECT * FROM users WHERE active = true AND age > 18 AND name LIKE 'A%'
+
+-- Output
+SELECT *
+FROM
+  users
+WHERE active = TRUE
+  AND age > 18
+  AND name LIKE 'A%';
+```
+
+### JOINs
+
+Each JOIN clause is on its own line, aligned properly:
+
+```sql
+-- Input
+SELECT u.name, p.title FROM users u INNER JOIN posts p ON u.id = p.user_id LEFT JOIN comments c ON p.id = c.post_id
+
+-- Output
+SELECT
+  u.name,
+  p.title
+FROM
+  users AS u
+  INNER JOIN posts AS p ON u.id = p.user_id
+  LEFT JOIN comments AS c ON p.id = c.post_id;
+```
+
+### Subqueries
+
+Subqueries are indented and recursively formatted:
+
+```sql
+-- Input
+SELECT * FROM (SELECT id, name FROM users WHERE active = true) AS active_users WHERE name LIKE 'A%'
+
+-- Output
+SELECT *
+FROM
+  (
+    SELECT
+      id,
+      name
+    FROM
+      users
+    WHERE active = TRUE
+  ) AS active_users
+WHERE name LIKE 'A%';
+```
+
+### Common Table Expressions (CTEs)
+
+```sql
+-- Input
+WITH active_users AS (SELECT * FROM users WHERE active = true) SELECT * FROM active_users
+
+-- Output
+WITH
+  active_users AS (
+    SELECT *
+    FROM
+      users
+    WHERE active = TRUE
+  )
+SELECT *
+FROM
+  active_users;
+```
+
+### CASE Expressions
+
+```sql
+-- Input
+SELECT CASE WHEN age < 18 THEN 'minor' WHEN age >= 65 THEN 'senior' ELSE 'adult' END AS age_group FROM users
+
+-- Output
+SELECT
+  CASE
+  WHEN age < 18 THEN 'minor'
+  WHEN age >= 65 THEN 'senior'
+  ELSE 'adult'
+END AS age_group
+FROM
+  users;
+```
+
+## Error Handling
+
+If the SQL cannot be parsed, `xlg-sqlfmt` will:
+
+1. Output the original, unmodified SQL to stdout
+2. Exit with a non-zero exit code (1)
+
+This makes it safe to use in pipelines and scripts:
+
+```bash
+# This will pass through invalid SQL unchanged
+echo "INVALID SQL SYNTAX" | xlg-sqlfmt
+echo $? # Returns 1
+
+# Valid SQL will be formatted
+echo "SELECT * FROM users" | xlg-sqlfmt
+echo $? # Returns 0
+```
+
+## Idempotent Formatting
+
+Running `xlg-sqlfmt` multiple times on the same SQL produces identical output:
+
+```bash
+echo "SELECT * FROM users" | xlg-sqlfmt | xlg-sqlfmt | xlg-sqlfmt
+# All three runs produce identical output
+```
+
+## Contributing
+
+Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
+
+### Development
+
+```bash
+# Clone the repository
+git clone https://github.com/xlgmokha/sqlfmt.git
+cd sqlfmt
+
+# Run tests
+cargo test
+
+# Run clippy for linting
+cargo clippy
+
+# Format code
+cargo fmt
+
+# Test the binary
+echo "SELECT * FROM users" | cargo run
+```
+
+## License
+
+This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
+
+## Changelog
+
+### v0.1.1
+
+- Added smart SELECT formatting (single-line for simple projections)
+- Improved AND/OR formatting in WHERE clauses with proper indentation
+- Better handling of complex expressions
+
+### v0.1.0
+
+- Initial release
+- Basic SQL formatting with stdin/stdout interface
+- Support for SELECT, INSERT, UPDATE, DELETE statements
+- Subquery and CTE formatting
+- JOIN clause formatting
+- CASE expression formatting
+- Comment preservation
+- Error handling with original SQL pass-through