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.1.tar.gz (46.3 kB view details)

Uploaded Source

Built Distribution

orafmt-1.0.1-py3-none-any.whl (48.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: orafmt-1.0.1.tar.gz
  • Upload date:
  • Size: 46.3 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.1.tar.gz
Algorithm Hash digest
SHA256 043ea8f5a54d1fce8623461266dba00872adca1174fc8e9a599a3539d624f8fa
MD5 928172ed7090726ba952d88234e9e3b5
BLAKE2b-256 84a05d0154448f6209f3dfa8624ac6ebc006a612d908ced863f7c71924bd26db

See more details on using hashes here.

File details

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

File metadata

  • Download URL: orafmt-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 48.5 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3cc0d23677033ecc2a1a203770905233b4f1d461c275863e1304c77fcd99ced8
MD5 d9c7aba1df1f158810967e5f7aa755b1
BLAKE2b-256 1fc9ce818c513adfc449b7fe988e5999fc20e13560399c3b7c3752b86377c18d

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