Documentation
¶
Overview ¶
Package format provides well-formatted SQL output for ClickHouse DDL statements.
This package takes parsed parser statements and generates clean, readable SQL with consistent formatting, proper indentation, and standardized styling. It separates formatting concerns from parsing and migration logic.
Key features:
- Consistent indentation and spacing
- Proper line breaks for readability
- Standardized keyword casing
- Column alignment in table definitions
- Comprehensive support for all DDL statement types
- ClickHouse-optimized backtick formatting for identifiers
Usage:
// Object-oriented API with default options
formatter := format.New(format.Defaults)
// Object-oriented API with custom options
formatter := format.New(format.FormatterOptions{
IndentSize: 2,
UppercaseKeywords: false,
AlignColumns: true,
})
var buf bytes.Buffer
err := formatter.Format(&buf, statements...)
// Functional API
var buf bytes.Buffer
err := format.Format(&buf, format.Defaults, statements...)
// Convenient SQL formatting
sqlResult, _ := parser.ParseString("CREATE DATABASE test; CREATE TABLE test.users (id UInt64) ENGINE = MergeTree();")
var buf bytes.Buffer
err := format.FormatSQL(&buf, format.Defaults, sqlResult)
The formatter supports all ClickHouse DDL operations including databases, tables, dictionaries, views, and SELECT statements with proper formatting for complex features like CTEs, window functions, and nested structures.
Package format provides well-formatted SQL output for ClickHouse DDL statements.
This package takes parsed parser statements and generates clean, readable SQL with consistent formatting, proper indentation, and standardized styling. It separates formatting concerns from parsing and migration logic.
Key features: - Consistent indentation and spacing - Proper line breaks for readability - Standardized keyword casing - Column alignment in table definitions - Comprehensive support for all DDL statement types
Example usage:
stmt := &parser.Statement{CreateTable: &parser.CreateTableStmt{
Name: "users",
Database: stringPtr("analytics"),
Columns: []*parser.Column{
{Name: "id", Type: &parser.DataType{Simple: &parser.SimpleDataType{Name: "UInt64"}}},
{Name: "name", Type: &parser.DataType{Simple: &parser.SimpleDataType{Name: "String"}}},
},
Engine: &parser.TableEngine{Name: "MergeTree"},
}}
formatted := format.Format(nil, stmt)
fmt.Println(formatted)
Output:
CREATE TABLE `analytics`.`users` (
`id` UInt64,
`name` String
) ENGINE = MergeTree();
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Defaults = FormatterOptions{ IndentSize: 4, MaxLineLength: 120, UppercaseKeywords: true, AlignColumns: true, }
Defaults provides the standard formatting options for ClickHouse DDL statements.
These defaults use:
- 4-space indentation
- Uppercase SQL keywords
- Column alignment in table definitions
- 120 character line length suggestion
Functions ¶
func Format ¶
Format provides a convenient way to format statements without creating a Formatter instance.
This is equivalent to calling New(&opts).Format(w, statements...) but more concise when you only need to format statements once.
Example:
import (
"bytes"
"github.com/pseudomuto/housekeeper/pkg/format"
"github.com/pseudomuto/housekeeper/pkg/parser"
)
sql, _ := parser.ParseString("CREATE DATABASE test;")
var buf bytes.Buffer
err := format.Format(&buf, format.Defaults, sqlResult.Statements...)
if err != nil {
panic(err)
}
func FormatSQL ¶
FormatSQL provides a convenient way to format all statements from a parsed SQL structure.
This is equivalent to calling Format(w, opts, sqlResult.Statements...) but more concise when you have a complete parsed SQL object.
Example:
import (
"bytes"
"github.com/pseudomuto/housekeeper/pkg/format"
"github.com/pseudomuto/housekeeper/pkg/parser"
)
sqlResult, _ := parser.ParseString("CREATE DATABASE test; CREATE TABLE test.users (id UInt64) ENGINE = MergeTree();")
var buf bytes.Buffer
err := format.FormatSQL(&buf, format.Defaults, sqlResult)
if err != nil {
panic(err)
}
Types ¶
type Formatter ¶
type Formatter struct {
// contains filtered or unexported fields
}
Formatter handles SQL statement formatting with configurable options
func New ¶
func New(options FormatterOptions) *Formatter
New creates a new Formatter instance with the specified formatting options. Use this when you need to format multiple statements with the same options.
Example:
// Create formatter with custom options
formatter := format.New(format.FormatterOptions{
IndentSize: 4,
UppercaseKeywords: true,
AlignColumns: true,
})
// Format multiple statements
var buf1, buf2 bytes.Buffer
err := formatter.Format(&buf1, stmt1, stmt2)
err = formatter.Format(&buf2, stmt3, stmt4)
func (*Formatter) Format ¶
Format writes formatted SQL statements to the provided writer.
Each statement is formatted according to the formatter's configuration and written to the writer. Multiple statements are separated by double newlines. Any write errors are returned immediately.
Parameters:
- w: The io.Writer to write formatted SQL to
- statements: Variable number of parsed statements to format
Returns an error if any write operation fails, nil otherwise.
type FormatterOptions ¶
type FormatterOptions struct {
// IndentSize specifies the number of spaces for each indent level
IndentSize int
// MaxLineLength suggests when to break long lines (0 = no limit)
MaxLineLength int
// UppercaseKeywords whether to uppercase SQL keywords
UppercaseKeywords bool
// AlignColumns whether to align column definitions in tables
AlignColumns bool
}
FormatterOptions controls formatting behavior