## Getting Started
Install via Composer:
```bash
composer require vendor/sql-formatter-package
Register the package in config/app.php under providers (if not auto-discovered). The core class SqlFormatter is now more feature-rich—start with basic usage:
use Vendor\SqlFormatter\SqlFormatter;
$query = "SELECT * FROM users WHERE id = (SELECT MAX(id) FROM posts)";
$formatter = new SqlFormatter();
echo $formatter->format($query); // Pretty-prints SQL
First use case: Debugging raw queries in logs or IDEs. The new compress() method is ideal for CLI debugging:
echo $formatter->compress($query); // Single-line output for copy/pasting
format() for readable logs or IDE tooltips.
$logger->info('Query:', ['formatted' => $formatter->format($rawQuery)]);
compress() for one-liners in terminal commands or scripts:
$compressed = $formatter->compress($query);
shell_exec("mysql -e \"$compressed\"");
@var) and binary/hex literals (e.g., 0b1010, 0xFF). Configure your IDE (PHPStorm/VSCode) to use the formatter for SQL snippets.count (now only flagged as reserved when followed by (). Safe to use in dynamic queries:
$column = 'count'; // Won't trigger reserved-word warnings unless used as `count()`
$formatter = new SqlFormatter(); // Reuse instance
foreach ($queries as $query) {
$formatter->format($query);
}
$safeQuery = $formatter->format($userInput);
DB::select($safeQuery); // Reduced risk of syntax errors
Reserved Word Overrides:
count keyword is now context-sensitive. If you dynamically build queries like:
$query = "SELECT $column FROM users"; // ❌ Fails if `$column = 'count'`
Use backticks or aliases:
$query = "SELECT `$column` FROM users"; // ✅ Works
Binary/Hex Literals:
0b or 0x prefixes. Test edge cases like:
WHERE data = 0xFF OR flag = 0b1010
LIMIT Clause Formatting:
LIMIT clause now groups better with OFFSET:
-- Before: Might split across lines
-- After: Cleanly formatted
SELECT * FROM users LIMIT 10 OFFSET 20
compress() to debug syntax errors in the terminal:
php artisan tinker
>>> $formatter->compress($query);
RETURNING, MySQL ENGINE).Custom Reserved Words:
Add to the formatter’s config (if supported) or extend the ReservedWords class:
$formatter->addReservedWords(['my_custom_keyword']);
Output Customization:
Override the format() method or use a decorator pattern for custom formatting rules.
Performance Profiling: For large datasets, profile the formatter’s impact on query generation time:
$start = microtime(true);
$formatter->format($hugeQuery);
echo microtime(true) - $start; // Benchmark
NO_UPDATE_NEEDED would **not** apply here—this release introduces meaningful new features and usage patterns.
How can I help you explore Laravel packages today?