Skip to main content

SQL Formatter Pre-Commit Hook

This repository includes a pre-configured pre-commit hook to automatically format SQL and PL/SQL files before each commit, ensuring consistent code style across the project. It leverages Trivadis' advanced format settings for alignment, spacing, and other formatting standards.

Features

  • Automatic Formatting: Formats SQL files with specified extensions to maintain code consistency.
  • Supported File Types: Includes common SQL and PL/SQL file extensions such as .sql, .prc, .fnc, .pks, .pkb, .trg, .pls, and more.
  • Configurable Settings: Uses trivadis_advanced_format.xml for advanced formatting configurations, which can be customized as needed.
  • Flexible SQLcl Path: Allows specifying the SQLcl path using the SQL_PROGRAM environment variable.

Requirements

SQLcl

This pre-commit hook requires Oracle SQLcl (SQL Command Line) to perform SQL formatting. SQLcl provides powerful SQL scripting and formatting capabilities. Follow these steps to download and install it:

Optional: Set SQLcl Path

If SQLcl is not in your system PATH or if you want to specify a custom path, set the SQL_PROGRAM environment variable to the path of the sql binary. For example:

export SQL_PROGRAM="/path/to/sqlcl/bin/sql"

Git

Ensure Git is installed as this pre-commit hook uses Git’s pre-commit hook system.

Installation

To use this hook with pre-commit, add it to your .pre-commit-config.yaml file in the target repository. Here’s an example configuration:

# .pre-commit-config.yaml
repos:
- repo: https://github.com/GentleGhostCoder/oracle-formatter-pre-commit-hook  # Replace with your GitHub repo URL
  rev: v1.0.0  # Use a tag, branch name, or commit SHA
  hooks:
    - id: oracle-formatter-hook

Then, install the hook by running:

pre-commit install

This command sets up the pre-commit hook to run automatically before each commit.

How It Works

  1. Identify Relevant Files: The hook detects files based on specified extensions.
  2. Format Files: Each identified file is formatted using formatter/format.js via SQLcl, applying the settings in trivadis_advanced_format.xml.

Usage

  1. Stage SQL Files: Stage files for commit using git add.
  2. Commit Changes: Commit staged files with git commit. The pre-commit hook will automatically format applicable files.

Running Manually with --all-files

To format all files, not just staged files, you can run the pre-commit hook manually:

pre-commit run --all-files

This triggers the formatting of all files matching the specified extensions in the repository.

Manual Formatting with the Python Entry Point

To manually format files without running the pre-commit hook, use the orafmt command directly if your module is installed:

orafmt file1.sql file2.sql

Alternatively, you can use the Python script directly:

python3 -m orafmt file1.sql file2.sql

This command will format the specified files according to the configurations.

Advanced Configuration

To modify formatting settings, edit the trivadis_advanced_format.xml file. These options allow customization of alignment, line breaks, and spacing based on project needs.

License

This formatter uses Trivadis PL/SQL Formatter, licensed under the Apache License, Version 2.0.
The pre-commit hook itself is also available under the Apache License, Version 2.0.

Metadata

Release files for orafmt 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for orafmt 1.0.2
File Size Uploaded
orafmt-1.0.2.tar.gz 46.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for orafmt 1.0.2
File Interpreter ABI Platform
orafmt-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 95.4 kB

Release files / orafmt-1.0.2.tar.gz

Download URL orafmt-1.0.2.tar.gz
Size 46.6 kB
Tags Source
SHA-256 checksum
How to use checksums
24d43a0c9aebc7d3f02bb05fdb977b938d9a36097b9d6fa57d4ea0d579b33400
BLAKE2b-256 checksum
How to use checksums
87a318ae57aabeaeb6e288a0ee6bf105418100d4181a5be98cb1f42f2fe977d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.11.4 Windows/10

Release files / orafmt-1.0.2-py3-none-any.whl

Download URL orafmt-1.0.2-py3-none-any.whl
Size 48.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6ad34d38a4a40ab34695d3853c571cabb392f88c15d9ad9eb7ba0ef4eddc547b
BLAKE2b-256 checksum
How to use checksums
6258909dd5fb808b22b6064703bbddf3558682ba4a1cc1c86fcafb5124dfa5ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.11.4 Windows/10

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page