Getting Started#
Installation#
Install SQLTidy using pip:
pip install sqltidy
Tidy#
API Usage#
The simplest way to use SQLTidy is to import the format_sql function:
from sqltidy import format_sql
sql = "SELECT id, name FROM users WHERE age > 18"
formatted = format_sql(sql)
print(formatted)
You can customize formatting behavior using a SQLTidyConfig object:
from sqltidy import format_sql
from sqltidy.rulebook import SQLTidyConfig
config = SQLTidyConfig()
sql = "SELECT * FROM users"
formatted = format_sql(sql, config=config)
Command-Line Interface#
SQLTidy provides a command-line tool for formatting SQL files:
sqltidy tidy input.sql -o output.sql
To see all available options:
sqltidy --help
Rewrite#
Command-Line Interface#
You can also use SQLTidy to rewrite SQL files in place:
sqltidy rewrite input.sql -o output.sql
Extending with Plugins#
You can register custom formatting rules at runtime:
from sqltidy import format_sql, register_plugin
from sqltidy.rules.base import BaseRule
class MyCustomRule(BaseRule):
def apply(self, tokens):
# Your custom formatting logic here
return tokens
register_plugin(MyCustomRule())
sql = "SELECT * FROM users"
formatted = format_sql(sql)
For more information about creating custom rules, see the plugins documentation.
Configuration#
Configuration options can be set using the SQLTidyConfig class:
from sqltidy.config import SQLTidyConfig
config = SQLTidyConfig(
# Add your configuration options here
)
See the api documentation for all available configuration options.