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, MultilineFunctions: true, FunctionArgThreshold: 4, MultilineFunctionNames: []string{"multiIf", "case", "transform", "multiSearchAllPositions"}, FunctionIndentSize: 4, SmartFunctionPairing: true, PairedFunctionNames: []string{"multiIf", "if", "case", "transform"}, PairSize: 2, }
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
- Multi-line formatting for complex functions
- 4+ arguments triggers multi-line formatting
- Specific functions always formatted multi-line
- Smart argument pairing for conditional functions
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 DDLFormatter ¶ added in v0.1.9
type DDLFormatter struct {
// contains filtered or unexported fields
}
DDLFormatter provides common functionality for formatting DDL statements.
func NewDDLFormatter ¶ added in v0.1.9
func NewDDLFormatter(f *Formatter) *DDLFormatter
NewDDLFormatter creates a new DDL formatter with the given formatter.
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. Sequential comments have no blank lines between them, while comments and SQL statements are separated by blank lines. 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
// MultilineFunctions enables multi-line formatting for complex function expressions
MultilineFunctions bool
// FunctionArgThreshold is the number of function arguments that triggers multi-line formatting
FunctionArgThreshold int
// MultilineFunctionNames contains function names that should always be formatted multi-line
MultilineFunctionNames []string
// FunctionIndentSize specifies extra indentation for function arguments (defaults to IndentSize)
FunctionIndentSize int
// SmartFunctionPairing enables intelligent argument pairing for specific functions
SmartFunctionPairing bool
// PairedFunctionNames contains function names that should use argument pairing
PairedFunctionNames []string
// PairSize specifies how many arguments to group per line (default: 2)
PairSize int
}
FormatterOptions controls formatting behavior
type RenameItem ¶ added in v0.1.9
RenameItem represents a single rename operation (from -> to).