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.
- 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.xmlfor advanced formatting configurations, which can be customized as needed. - Flexible SQLcl Path: Allows specifying the SQLcl path using the
SQL_PROGRAMenvironment variable.
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:
- Download SQLcl: The latest version can be downloaded from Oracle SQLcl Downloads.
- Install SQLcl: Refer to the installation guide if needed.
- Version 23.4 are supported - https://www.oracle.com/sqlcl/download/sqlcl-downloads-23.4.html
- Oracle SQL Developer, Version 23.1.1.345 (requires dbtools-common.jar from SQLcl 23.4.0 using ParseNode.class from SQL Developer 23.1.1.345)
- Run the sqlcl -> sql Program just with the jdk version 11
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"Ensure Git is installed as this pre-commit hook uses Git’s pre-commit hook system.
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-hookThen, install the hook by running:
pre-commit installThis command sets up the pre-commit hook to run automatically before each commit.
- Identify Relevant Files: The hook detects files based on specified extensions.
- Format Files: Each identified file is formatted using
formatter/format.jsvia SQLcl, applying the settings intrivadis_advanced_format.xml.
- Stage SQL Files: Stage files for commit using
git add. - Commit Changes: Commit staged files with
git commit. The pre-commit hook will automatically format applicable files.
To format all files, not just staged files, you can run the pre-commit hook manually:
pre-commit run --all-filesThis triggers the formatting of all files matching the specified extensions in the repository.
To manually format files without running the pre-commit hook, use the orafmt command directly if your module is installed:
orafmt file1.sql file2.sqlAlternatively, you can use the Python script directly:
python3 -m orafmt file1.sql file2.sqlThis command will format the specified files according to the configurations.
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.
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.