Skip to content

Add ParseStmt, ParseExpr and FormatBeautify convenience API#278

Open
git-hulk wants to merge 1 commit into
masterfrom
public-api-additions
Open

Add ParseStmt, ParseExpr and FormatBeautify convenience API#278
git-hulk wants to merge 1 commit into
masterfrom
public-api-additions

Conversation

@git-hulk

Copy link
Copy Markdown
Member

Summary

Purely additive public API for the three most common call patterns that previously required boilerplate:

// Parse exactly one statement (errors on zero or multiple statements)
stmt, err := clickhouse.ParseStmt("SELECT a FROM t")

// Parse an expression fragment, e.g. a column expression or function call
expr, err := clickhouse.ParseExpr("toDate(created_at) + 1")

// One-call beautified formatting (counterpart of Format)
sql := clickhouse.FormatBeautify(stmt)

Previously a single statement needed NewParser(sql).ParseStmts() plus slice-length handling, expression fragments could not be parsed at all without wrapping them in a dummy SELECT, and beautified output required NewFormatter().WithBeautify().WriteExpr() wiring.

Changes

  • New parser/api.go with ParseStmt, ParseExpr, FormatBeautify; errors flow through the existing wrapError path so they carry positions and caret rendering.
  • CLI -beautify path now uses FormatBeautify (behavior unchanged, verified by CLI smoke test).
  • README section documenting the helpers.

The DDL.Type() → typed Kind enum item from the same backlog entry is intentionally left out: it is a breaking change and belongs with the v0.5/v1 interface-split batch.

Testing

New api_test.go covering single/zero/multiple statements, fragment parsing, trailing-token rejection, and FormatBeautify equivalence with the long-form formatter plus reparse round-trip. Full suite passes including -race -compatible.

🤖 Generated with Claude Code

- ParseStmt(sql) parses exactly one statement, erroring on zero or
  multiple statements, so callers no longer need to go through
  NewParser(...).ParseStmts() and validate the slice length.
- ParseExpr(sql) parses an expression fragment (column expression,
  function call, ...) and requires the whole input to be consumed.
- FormatBeautify(expr) renders multi-line indented SQL, the one-call
  counterpart of Format; previously beautified output required wiring
  NewFormatter().WithBeautify().WriteExpr() by hand. The CLI -beautify
  path now uses it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant