The project is in a healthy, maintained state
Adds Database#format_sql and Dataset#formatted_sql to Sequel. Formatting runs inside the Ruby process through a native extension (magnus) that wraps the Rust sqlformat crate, so no external process starts and many statements format in milliseconds.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

~> 0.9
~> 5.0
 Project Readme

sequel-sqlformat

A Sequel extension that formats SQL for reading. It adds Dataset#formatted_sql and Database#format_sql. Formatting runs inside the Ruby process, through a native extension built with magnus that wraps the Rust sqlformat crate. No external process starts, so formatting many statements costs milliseconds, not seconds.

DB.extension :sqlformat

DB[:items].where(id: 1).formatted_sql(uppercase: true)
# SELECT
#   *
# FROM
#   "items"
# WHERE
#   ("id" = 1)

DB.format_sql("select a, b from t where a = 1")   # any SQL string

To format one dataset only, load the extension into it: DB[:items].extension(:sqlformat).

Without a database:

require "sequel/sqlformat"

Sequel::SQLFormat.format("select a from t", dialect: :postgresql)

Options

Option Default Meaning
indent 2 Spaces for each indentation level (0 to 255).
uppercase nil true uppercases keywords, false lowercases them, nil keeps them.
lines_between_queries 1 Line breaks after each statement (0 to 255).
dialect :generic :postgresql adds array syntax. :sqlserver adds [brackets] and @variables.

The defaults are the crate's defaults. Through the extension, the dialect comes from the database type: :postgresql for PostgreSQL, :sqlserver for Microsoft SQL Server, and :generic for every other database.

Set options for a database when you connect, or before you freeze it, or for one call:

DB = Sequel.connect(url, sqlformat: { uppercase: true, indent: 4 })
DB.sqlformat_options = { uppercase: true }
DB[:items].formatted_sql(uppercase: false)

An unknown option name raises ArgumentError.

What it does and does not do

  • The crate splits SQL into tokens and does not parse it. It accepts any dialect, including DuckDB and Spark SQL, and it never fails on syntax that it does not know.
  • It changes only whitespace and, with uppercase: true or false, the case of keywords.
  • With uppercase: true or false, an unquoted identifier that is also a keyword (for example status) changes case too. Sequel quotes identifiers by default, so this matters only for SQL that you write yourself.

Installation

Precompiled gems exist for x86_64 and ARM64 Linux, and for Intel and Apple Silicon macOS, for Ruby 3.3, 3.4 and 4.0. They need no Rust. Every other platform builds the source gem, which needs a Rust toolchain (rustc 1.84 or later) and libclang.

Development

just setup   # tools, bundle and the git hooks
just test    # compile the extension and run the tests
just ci      # everything CI runs

License

MIT. The sqlformat crate is MIT OR Apache-2.0.