Skip to content

Commit ebd32cf

Browse files
kyleconroyclaude
andauthored
refactor(ast): rename Formatter interface to Dialect (#4208)
Renames the Formatter interface to Dialect and refactors Format methods to accept the dialect as a parameter instead of storing it in TrackedBuffer. **Interface Changes:** - Rename `format.Formatter` to `format.Dialect` - Change Format signature from `Format(buf *TrackedBuffer)` to `Format(buf *TrackedBuffer, d format.Dialect)` **TrackedBuffer Simplification:** - Remove `formatter` field from TrackedBuffer struct - TrackedBuffer is now a simple strings.Builder wrapper - NewTrackedBuffer() no longer takes a dialect parameter **Method Call Updates:** - `buf.astFormat(x)` → `buf.astFormat(x, d)` - `buf.join(x, sep)` → `buf.join(x, d, sep)` - Helper methods now called directly on dialect: - `buf.QuoteIdent(x)` → `d.QuoteIdent(x)` - `buf.TypeName(ns, name)` → `d.TypeName(ns, name)` - `buf.Param(n)` → `d.Param(n)` - `buf.Cast(arg, t)` → `d.Cast(arg, t)` - `buf.NamedParam(name)` → `d.NamedParam(name)` This change makes the dialect dependency explicit in Format methods and simplifies TrackedBuffer to be purely a buffer utility. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent 166398d commit ebd32cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+435
-338
lines changed

internal/endtoend/fmt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type sqlParser interface {
2525

2626
// sqlFormatter is an interface for formatters
2727
type sqlFormatter interface {
28-
format.Formatter
28+
format.Dialect
2929
}
3030

3131
func TestFormat(t *testing.T) {

internal/engine/postgresql/reserved.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func hasMixedCase(s string) bool {
1717
}
1818

1919
// QuoteIdent returns a quoted identifier if it needs quoting.
20-
// This implements the format.Formatter interface.
20+
// This implements the format.Dialect interface.
2121
func (p *Parser) QuoteIdent(s string) string {
2222
if p.IsReservedKeyword(s) || hasMixedCase(s) {
2323
return `"` + s + `"`
@@ -26,7 +26,7 @@ func (p *Parser) QuoteIdent(s string) string {
2626
}
2727

2828
// TypeName returns the SQL type name for the given namespace and name.
29-
// This implements the format.Formatter interface.
29+
// This implements the format.Dialect interface.
3030
func (p *Parser) TypeName(ns, name string) string {
3131
if ns == "pg_catalog" {
3232
switch name {

internal/sql/ast/CLAUDE.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ The `TrackedBuffer` type (`pg_query.go`) handles SQL formatting with dialect-spe
1717
- `QuoteIdent(name string)` - quotes identifiers (dialect-specific)
1818
- `TypeName(ns, name string)` - formats type names (dialect-specific)
1919

20-
### Formatter Interface
21-
Dialect-specific formatting is handled via the `Formatter` interface:
20+
### Dialect Interface
21+
Dialect-specific formatting is handled via the `Dialect` interface:
2222
```go
23-
type Formatter interface {
23+
type Dialect interface {
2424
QuoteIdent(string) string
2525
TypeName(ns, name string) string
2626
Param(int) string // $1 for PostgreSQL, ? for MySQL
27+
NamedParam(string) string // @name for PostgreSQL, :name for SQLite
2728
Cast(string) string
2829
}
2930
```

internal/sql/ast/a_array_expr.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ast
22

3+
import "github.com/sqlc-dev/sqlc/internal/sql/format"
4+
35
type A_ArrayExpr struct {
46
Elements *List
57
Location int
@@ -9,11 +11,11 @@ func (n *A_ArrayExpr) Pos() int {
911
return n.Location
1012
}
1113

12-
func (n *A_ArrayExpr) Format(buf *TrackedBuffer) {
14+
func (n *A_ArrayExpr) Format(buf *TrackedBuffer, d format.Dialect) {
1315
if n == nil {
1416
return
1517
}
1618
buf.WriteString("ARRAY[")
17-
buf.join(n.Elements, ", ")
19+
buf.join(n.Elements, d, ", ")
1820
buf.WriteString("]")
1921
}

internal/sql/ast/a_const.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ast
22

3+
import "github.com/sqlc-dev/sqlc/internal/sql/format"
4+
35
type A_Const struct {
46
Val Node
57
Location int
@@ -9,15 +11,15 @@ func (n *A_Const) Pos() int {
911
return n.Location
1012
}
1113

12-
func (n *A_Const) Format(buf *TrackedBuffer) {
14+
func (n *A_Const) Format(buf *TrackedBuffer, d format.Dialect) {
1315
if n == nil {
1416
return
1517
}
1618
if _, ok := n.Val.(*String); ok {
1719
buf.WriteString("'")
18-
buf.astFormat(n.Val)
20+
buf.astFormat(n.Val, d)
1921
buf.WriteString("'")
2022
} else {
21-
buf.astFormat(n.Val)
23+
buf.astFormat(n.Val, d)
2224
}
2325
}

internal/sql/ast/a_expr.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ast
22

3+
import "github.com/sqlc-dev/sqlc/internal/sql/format"
4+
35
type A_Expr struct {
46
Kind A_Expr_Kind
57
Name *List
@@ -31,75 +33,75 @@ func (n *A_Expr) isNamedParam() (string, bool) {
3133
return "", false
3234
}
3335

34-
func (n *A_Expr) Format(buf *TrackedBuffer) {
36+
func (n *A_Expr) Format(buf *TrackedBuffer, d format.Dialect) {
3537
if n == nil {
3638
return
3739
}
3840

3941
// Check for named parameter first (works regardless of Kind)
4042
if name, ok := n.isNamedParam(); ok {
41-
buf.WriteString(buf.NamedParam(name))
43+
buf.WriteString(d.NamedParam(name))
4244
return
4345
}
4446

4547
switch n.Kind {
4648
case A_Expr_Kind_IN:
47-
buf.astFormat(n.Lexpr)
49+
buf.astFormat(n.Lexpr, d)
4850
buf.WriteString(" IN (")
49-
buf.astFormat(n.Rexpr)
51+
buf.astFormat(n.Rexpr, d)
5052
buf.WriteString(")")
5153
case A_Expr_Kind_LIKE:
52-
buf.astFormat(n.Lexpr)
54+
buf.astFormat(n.Lexpr, d)
5355
buf.WriteString(" LIKE ")
54-
buf.astFormat(n.Rexpr)
56+
buf.astFormat(n.Rexpr, d)
5557
case A_Expr_Kind_ILIKE:
56-
buf.astFormat(n.Lexpr)
58+
buf.astFormat(n.Lexpr, d)
5759
buf.WriteString(" ILIKE ")
58-
buf.astFormat(n.Rexpr)
60+
buf.astFormat(n.Rexpr, d)
5961
case A_Expr_Kind_SIMILAR:
60-
buf.astFormat(n.Lexpr)
62+
buf.astFormat(n.Lexpr, d)
6163
buf.WriteString(" SIMILAR TO ")
62-
buf.astFormat(n.Rexpr)
64+
buf.astFormat(n.Rexpr, d)
6365
case A_Expr_Kind_BETWEEN:
64-
buf.astFormat(n.Lexpr)
66+
buf.astFormat(n.Lexpr, d)
6567
buf.WriteString(" BETWEEN ")
6668
if l, ok := n.Rexpr.(*List); ok && len(l.Items) == 2 {
67-
buf.astFormat(l.Items[0])
69+
buf.astFormat(l.Items[0], d)
6870
buf.WriteString(" AND ")
69-
buf.astFormat(l.Items[1])
71+
buf.astFormat(l.Items[1], d)
7072
}
7173
case A_Expr_Kind_NOT_BETWEEN:
72-
buf.astFormat(n.Lexpr)
74+
buf.astFormat(n.Lexpr, d)
7375
buf.WriteString(" NOT BETWEEN ")
7476
if l, ok := n.Rexpr.(*List); ok && len(l.Items) == 2 {
75-
buf.astFormat(l.Items[0])
77+
buf.astFormat(l.Items[0], d)
7678
buf.WriteString(" AND ")
77-
buf.astFormat(l.Items[1])
79+
buf.astFormat(l.Items[1], d)
7880
}
7981
case A_Expr_Kind_DISTINCT:
80-
buf.astFormat(n.Lexpr)
82+
buf.astFormat(n.Lexpr, d)
8183
buf.WriteString(" IS DISTINCT FROM ")
82-
buf.astFormat(n.Rexpr)
84+
buf.astFormat(n.Rexpr, d)
8385
case A_Expr_Kind_NOT_DISTINCT:
84-
buf.astFormat(n.Lexpr)
86+
buf.astFormat(n.Lexpr, d)
8587
buf.WriteString(" IS NOT DISTINCT FROM ")
86-
buf.astFormat(n.Rexpr)
88+
buf.astFormat(n.Rexpr, d)
8789
case A_Expr_Kind_NULLIF:
8890
buf.WriteString("NULLIF(")
89-
buf.astFormat(n.Lexpr)
91+
buf.astFormat(n.Lexpr, d)
9092
buf.WriteString(", ")
91-
buf.astFormat(n.Rexpr)
93+
buf.astFormat(n.Rexpr, d)
9294
buf.WriteString(")")
9395
default:
9496
// Standard operator (including A_Expr_Kind_OP)
9597
if set(n.Lexpr) {
96-
buf.astFormat(n.Lexpr)
98+
buf.astFormat(n.Lexpr, d)
9799
buf.WriteString(" ")
98100
}
99-
buf.astFormat(n.Name)
101+
buf.astFormat(n.Name, d)
100102
if set(n.Rexpr) {
101103
buf.WriteString(" ")
102-
buf.astFormat(n.Rexpr)
104+
buf.astFormat(n.Rexpr, d)
103105
}
104106
}
105107
}

internal/sql/ast/a_indices.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ast
22

3+
import "github.com/sqlc-dev/sqlc/internal/sql/format"
4+
35
type A_Indices struct {
46
IsSlice bool
57
Lidx Node
@@ -10,21 +12,21 @@ func (n *A_Indices) Pos() int {
1012
return 0
1113
}
1214

13-
func (n *A_Indices) Format(buf *TrackedBuffer) {
15+
func (n *A_Indices) Format(buf *TrackedBuffer, d format.Dialect) {
1416
if n == nil {
1517
return
1618
}
1719
buf.WriteString("[")
1820
if n.IsSlice {
1921
if set(n.Lidx) {
20-
buf.astFormat(n.Lidx)
22+
buf.astFormat(n.Lidx, d)
2123
}
2224
buf.WriteString(":")
2325
if set(n.Uidx) {
24-
buf.astFormat(n.Uidx)
26+
buf.astFormat(n.Uidx, d)
2527
}
2628
} else {
27-
buf.astFormat(n.Uidx)
29+
buf.astFormat(n.Uidx, d)
2830
}
2931
buf.WriteString("]")
3032
}

internal/sql/ast/a_star.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package ast
22

3+
import "github.com/sqlc-dev/sqlc/internal/sql/format"
4+
35
type A_Star struct {
46
}
57

68
func (n *A_Star) Pos() int {
79
return 0
810
}
911

10-
func (n *A_Star) Format(buf *TrackedBuffer) {
12+
func (n *A_Star) Format(buf *TrackedBuffer, d format.Dialect) {
1113
if n == nil {
1214
return
1315
}

internal/sql/ast/alias.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ast
22

3+
import "github.com/sqlc-dev/sqlc/internal/sql/format"
4+
35
type Alias struct {
46
Aliasname *string
57
Colnames *List
@@ -9,7 +11,7 @@ func (n *Alias) Pos() int {
911
return 0
1012
}
1113

12-
func (n *Alias) Format(buf *TrackedBuffer) {
14+
func (n *Alias) Format(buf *TrackedBuffer, d format.Dialect) {
1315
if n == nil {
1416
return
1517
}
@@ -18,7 +20,7 @@ func (n *Alias) Format(buf *TrackedBuffer) {
1820
}
1921
if items(n.Colnames) {
2022
buf.WriteString("(")
21-
buf.astFormat((n.Colnames))
23+
buf.astFormat(n.Colnames, d)
2224
buf.WriteString(")")
2325
}
2426
}

internal/sql/ast/alter_table_cmd.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ast
22

3+
import "github.com/sqlc-dev/sqlc/internal/sql/format"
4+
35
const (
46
AT_AddColumn AlterTableType = iota
57
AT_AlterColumnType
@@ -40,7 +42,7 @@ func (n *AlterTableCmd) Pos() int {
4042
return 0
4143
}
4244

43-
func (n *AlterTableCmd) Format(buf *TrackedBuffer) {
45+
func (n *AlterTableCmd) Format(buf *TrackedBuffer, d format.Dialect) {
4446
if n == nil {
4547
return
4648
}
@@ -51,5 +53,5 @@ func (n *AlterTableCmd) Format(buf *TrackedBuffer) {
5153
buf.WriteString(" DROP COLUMN ")
5254
}
5355

54-
buf.astFormat(n.Def)
56+
buf.astFormat(n.Def, d)
5557
}

0 commit comments

Comments
 (0)