Skip to main content

A Python tool for formatting Oracle SQL files with SQLcl and custom formatting options.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

orafmt-1.0.2.tar.gz (46.6 kB view details)

Uploaded Source

Built Distribution

orafmt-1.0.2-py3-none-any.whl (48.8 kB view details)

Uploaded Python 3

File details

Details for the file orafmt-1.0.2.tar.gz.

File metadata

  • Download URL: orafmt-1.0.2.tar.gz
  • Upload date:
  • Size: 46.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.11.4 Windows/10

File hashes

Hashes for orafmt-1.0.2.tar.gz
Algorithm Hash digest
SHA256 24d43a0c9aebc7d3f02bb05fdb977b938d9a36097b9d6fa57d4ea0d579b33400
MD5 9a1c2322d611a1ca1cacb9f7a2fc0e27
BLAKE2b-256 87a318ae57aabeaeb6e288a0ee6bf105418100d4181a5be98cb1f42f2fe977d7

See more details on using hashes here.

File details

Details for the file orafmt-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: orafmt-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 48.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.11.4 Windows/10

File hashes

Hashes for orafmt-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6ad34d38a4a40ab34695d3853c571cabb392f88c15d9ad9eb7ba0ef4eddc547b
MD5 d895f202d0130f1e9087a697f8cdf830
BLAKE2b-256 6258909dd5fb808b22b6064703bbddf3558682ba4a1cc1c86fcafb5124dfa5ca

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page