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 ¶
- Variables
- func HasContent(s string) bool
- func IsComplete(input string) bool
- func IsIdentPart(r rune) bool
- func IsIdentStart(r rune) bool
- func IsKeyword(word string) bool
- func NeedsQuoting(name string) bool
- func ParseSet(sql string) (settings map[string]string, ok bool)
- func ParseUse(sql string) (db string, ok bool)
- func QuoteIdent(name string) string
- func ReturnsRows(verb string) bool
- func SplitStatements(script string) (stmts []string, rest string)
- func Unquote(ident string) string
- type InsertKind
- type Statement
- type Token
- type TokenKind
Constants ¶
This section is empty.
Variables ¶
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 ¶
HasContent reports whether s contains anything other than whitespace and comments.
func IsComplete ¶
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 ¶
IsIdentPart reports whether r can appear inside a bare identifier.
func IsIdentStart ¶
IsIdentStart reports whether r can start a bare identifier.
func NeedsQuoting ¶
NeedsQuoting reports whether name must be quoted to be used as an identifier.
func ParseSet ¶
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 QuoteIdent ¶
QuoteIdent quotes name with backticks when it cannot be written bare.
func ReturnsRows ¶
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 ¶
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.
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 ¶
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 ¶
SignificantTokens returns the tokens of s without whitespace and comments.
func Tokenize ¶
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) Significant ¶
Significant reports whether the token carries meaning (not whitespace or a comment).