sqlutil

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package sqlutil contains a small, forgiving tokenizer for ClickHouse SQL and helpers built on top of it. It is deliberately not a parser: everything here has to work on incomplete input, because it is used while the user is typing (syntax highlighting, completion) as well as for splitting scripts.

Index

Constants

This section is empty.

Variables

View Source
var Keywords = []string{}/* 160 elements not displayed */

Keywords is the static list of ClickHouse SQL keywords used for syntax highlighting and as the completion fallback when the server's system.keywords table is unavailable. Only single-word keywords are listed.

Functions

func HasContent

func HasContent(s string) bool

HasContent reports whether s contains anything other than whitespace and comments.

func IsComplete

func IsComplete(input string) bool

IsComplete reports whether the input ends a statement: its last significant token is a semicolon or the \G terminator. Input with an open string or comment is never complete.

func IsIdentPart

func IsIdentPart(r rune) bool

IsIdentPart reports whether r can appear inside a bare identifier.

func IsIdentStart

func IsIdentStart(r rune) bool

IsIdentStart reports whether r can start a bare identifier.

func IsKeyword

func IsKeyword(word string) bool

IsKeyword reports whether word is a known SQL keyword (case-insensitive).

func NeedsQuoting

func NeedsQuoting(name string) bool

NeedsQuoting reports whether name must be quoted to be used as an identifier.

func ParseSet

func ParseSet(sql string) (settings map[string]string, ok bool)

ParseSet extracts the assignments of a plain "SET name = value [, ...]" statement. It returns ok=false for anything else (SET ROLE, SET DEFAULT ROLE, query parameters, expressions), which callers should send to the server unchanged.

func ParseUse

func ParseUse(sql string) (db string, ok bool)

ParseUse extracts the database name from a "USE db" statement.

func QuoteIdent

func QuoteIdent(name string) string

QuoteIdent quotes name with backticks when it cannot be written bare.

func ReturnsRows

func ReturnsRows(verb string) bool

ReturnsRows reports whether a statement starting with verb produces a result set. The driver needs to know up front: statements without one must be executed, not queried.

func SplitStatements

func SplitStatements(script string) (stmts []string, rest string)

SplitStatements splits a script into statements on top-level semicolons (semicolons inside strings, quoted identifiers and comments are ignored). Statements are returned trimmed and without the terminating semicolon; empty statements are dropped. rest is the trailing text that has no terminating semicolon (possibly empty or whitespace/comment only).

An INSERT that expects its data from the client (INSERT ... FORMAT CSV) swallows the remainder of the script: what follows is data, in which a semicolon means nothing, and must never be mistaken for statements.

func Unquote

func Unquote(ident string) string

Unquote removes identifier quoting (backticks or double quotes) and resolves escapes. Bare identifiers are returned unchanged.

Types

type InsertKind

type InsertKind int

InsertKind says where an INSERT statement gets its data from.

const (
	// InsertNoData: no data clause, or VALUES with nothing after it. The
	// server would wait for data that never comes.
	InsertNoData InsertKind = iota
	// InsertValues: INSERT ... VALUES (...), data inline.
	InsertValues
	// InsertSelect: INSERT ... SELECT (or WITH ... SELECT).
	InsertSelect
	// InsertFormat: INSERT ... FORMAT <name>, data supplied by the client.
	InsertFormat
)

func ClassifyInsert

func ClassifyInsert(sig []Token) InsertKind

ClassifyInsert inspects the significant tokens of an INSERT statement. It goes by the position of the data clause, not by the mere presence of a keyword, because inline data may itself contain words like "select".

type Statement

type Statement struct {
	// SQL is the statement text with terminators and any trailing FORMAT
	// clause removed.
	SQL string
	// Verb is the upper-cased first keyword (SELECT, INSERT, USE, ...).
	Verb string
	// Format is the name given in a trailing FORMAT clause, if any.
	Format string
	// Vertical is set when the statement was terminated with \G.
	Vertical bool
}

Statement is a single statement prepared for execution.

func ParseStatement

func ParseStatement(input string) Statement

ParseStatement prepares one statement for execution. It strips a trailing semicolon or \G and, for statements that return rows, the "FORMAT <name>" clause: result formatting is done by the client, so the clause must not reach the server.

type Token

type Token struct {
	Kind  TokenKind
	Text  string
	Start int
	End   int
	// Unterminated is set for strings, quoted identifiers and block comments
	// that reach the end of input without being closed.
	Unterminated bool
}

Token is a lexical unit of SQL text. Start and End are byte offsets into the original input, with End exclusive.

func SignificantTokens

func SignificantTokens(s string) []Token

SignificantTokens returns the tokens of s without whitespace and comments.

func Tokenize

func Tokenize(s string) []Token

Tokenize splits s into tokens. It never fails: malformed input produces tokens flagged as Unterminated, and unknown bytes become single-character operators. Concatenating the Text of all tokens yields s.

func (Token) Is

func (t Token) Is(keyword string) bool

Is reports whether the token is the given keyword (case-insensitive).

func (Token) IsOp

func (t Token) IsOp(op string) bool

IsOp reports whether the token is the given operator.

func (Token) Significant

func (t Token) Significant() bool

Significant reports whether the token carries meaning (not whitespace or a comment).

func (Token) Upper

func (t Token) Upper() string

Upper returns the upper-cased token text.

type TokenKind

type TokenKind int

TokenKind classifies a token.

const (
	Whitespace TokenKind = iota
	Comment
	Keyword
	Ident
	QuotedIdent
	String
	Number
	Operator
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL