cockroach: github.com/cockroachdb/cockroach/pkg/sql/parser Index | Files | Directories

package parser

import "github.com/cockroachdb/cockroach/pkg/sql/parser"

Index

Package Files

help.go lexer.go parse.go scan.go show_syntax.go

Variables

var AllHelp = func(h map[string]HelpMessageBody) string {

    cmds := make(map[string][]string)
    for c, details := range h {
        if details.Category == "" {
            continue
        }
        cmds[details.Category] = append(cmds[details.Category], c)
    }

    // Ensure the result is deterministic.
    var categories []string
    for c, l := range cmds {
        categories = append(categories, c)
        sort.Strings(l)
    }
    sort.Strings(categories)

    // Compile the final help index.
    var buf bytes.Buffer
    w := tabwriter.NewWriter(&buf, 0, 0, 1, ' ', 0)
    for _, cat := range categories {
        fmt.Fprintf(w, "%s:\n", strings.Title(cat))
        for _, item := range cmds[cat] {
            fmt.Fprintf(w, "\t\t%s\t%s\n", item, h[item].ShortDescription)
        }
        fmt.Fprintln(w)
    }
    _ = w.Flush()
    return buf.String()
}(helpMessages)

AllHelp contains an overview of all statements with help messages. For example, displayed in the CLI shell with \h without additional parameters.

var HelpMessages = func(h map[string]HelpMessageBody) map[string]HelpMessageBody {
    appendSeeAlso := func(newItem, prevItems string) string {

        if prevItems != "" {
            return newItem + "\n  " + prevItems
        }
        return newItem
    }
    reformatSeeAlso := func(seeAlso string) string {
        return strings.Replace(
            strings.Replace(seeAlso, ", ", "\n  ", -1),
            "WEBDOCS", docs.URLBase, -1)
    }
    srcMsg := h["<SOURCE>"]
    srcMsg.SeeAlso = reformatSeeAlso(strings.TrimSpace(srcMsg.SeeAlso))
    selectMsg := h["<SELECTCLAUSE>"]
    selectMsg.SeeAlso = reformatSeeAlso(strings.TrimSpace(selectMsg.SeeAlso))
    for k, m := range h {
        m = h[k]
        m.ShortDescription = strings.TrimSpace(m.ShortDescription)
        m.Text = strings.TrimSpace(m.Text)
        m.SeeAlso = strings.TrimSpace(m.SeeAlso)

        if strings.Contains(m.Text, "<source>") && k != "<SOURCE>" {
            m.Text = strings.TrimSpace(m.Text) + "\n\n" + strings.TrimSpace(srcMsg.Text)
            m.SeeAlso = appendSeeAlso(srcMsg.SeeAlso, m.SeeAlso)
        }

        if strings.Contains(m.Text, "<selectclause>") && k != "<SELECTCLAUSE>" {
            m.Text = strings.TrimSpace(m.Text) + "\n\n" + strings.TrimSpace(selectMsg.Text)
            m.SeeAlso = appendSeeAlso(selectMsg.SeeAlso, m.SeeAlso)
        }

        if strings.Contains(m.Text, "<tablename>") {
            m.SeeAlso = appendSeeAlso("SHOW TABLES", m.SeeAlso)
        }
        m.SeeAlso = reformatSeeAlso(m.SeeAlso)
        h[k] = m
    }
    return h
}(helpMessages)

HelpMessages is the registry of all help messages, keyed by the top-level statement that they document. The key is intended for use via the \h client-side command.

func GetTypeFromValidSQLSyntax Uses

func GetTypeFromValidSQLSyntax(sql string) (tree.ResolvableTypeReference, error)

GetTypeFromValidSQLSyntax retrieves a type from its SQL syntax. The caller is responsible for guaranteeing that the type expression is valid SQL. This includes verifying that complex identifiers are enclosed in double quotes, etc.

func GetTypeReferenceFromName Uses

func GetTypeReferenceFromName(typeName tree.Name) (tree.ResolvableTypeReference, error)

GetTypeReferenceFromName turns a type name into a type reference. This supports only “simple” (single-identifier) references to built-in types, when the identifer has already been parsed away from the input SQL syntax.

func HasMultipleStatements Uses

func HasMultipleStatements(sql string) bool

HasMultipleStatements returns true if the sql string contains more than one statements.

func LastLexicalToken Uses

func LastLexicalToken(sql string) (lastTok int, ok bool)

LastLexicalToken returns the last lexical token. If the string has no lexical tokens, returns 0 and ok=false.

func NakedIntTypeFromDefaultIntSize Uses

func NakedIntTypeFromDefaultIntSize(defaultIntSize int32) *types.T

NakedIntTypeFromDefaultIntSize given the size in bits or bytes (preferred) of how a "naked" INT type should be parsed returns the corresponding integer type.

func ParseExpr Uses

func ParseExpr(sql string) (tree.Expr, error)

ParseExpr parses a SQL scalar expression. The caller is responsible for ensuring that the input is, in fact, a valid SQL scalar expression — the results are undefined if the string contains invalid SQL syntax.

func ParseExprWithInt Uses

func ParseExprWithInt(sql string, nakedIntType *types.T) (tree.Expr, error)

ParseExprWithInt parses a SQL scalar expression, using the given type when INT is used as type name in the SQL syntax. The caller is responsible for ensuring that the input is, in fact, a valid SQL scalar expression — the results are undefined if the string contains invalid SQL syntax.

func ParseExprs Uses

func ParseExprs(sql []string) (tree.Exprs, error)

ParseExprs parses a comma-delimited sequence of SQL scalar expressions. The caller is responsible for ensuring that the input is, in fact, a comma-delimited sequence of SQL scalar expressions — the results are undefined if the string contains invalid SQL syntax.

func ParseQualifiedTableName Uses

func ParseQualifiedTableName(sql string) (*tree.TableName, error)

ParseQualifiedTableName parses a possibly qualified table name. The table name must contain one or more name parts, using the full input SQL syntax: each name part containing special characters, or non-lowercase characters, must be enclosed in double quote. The name may not be an invalid table name (the caller is responsible for guaranteeing that only valid table names are provided as input).

func ParseTableName Uses

func ParseTableName(sql string) (*tree.UnresolvedObjectName, error)

ParseTableName parses a table name. The table name must contain one or more name parts, using the full input SQL syntax: each name part containing special characters, or non-lowercase characters, must be enclosed in double quote. The name may not be an invalid table name (the caller is responsible for guaranteeing that only valid table names are provided as input).

func RunShowSyntax Uses

func RunShowSyntax(
    ctx context.Context,
    stmt string,
    report func(ctx context.Context, field, msg string),
    reportErr func(ctx context.Context, err error),
)

RunShowSyntax analyzes the syntax and reports its structure as data for the client. Even an error is reported as data.

Since errors won't propagate to the client as an error, but as a result, the usual code path to capture and record errors will not be triggered. Instead, the caller can pass a reportErr closure to capture errors instead. May be nil.

func SplitFirstStatement Uses

func SplitFirstStatement(sql string) (pos int, ok bool)

SplitFirstStatement returns the length of the prefix of the string up to and including the first semicolon that separates statements. If there is no semicolon, returns ok=false.

type HelpMessage Uses

type HelpMessage struct {
    // Command is set if the message is about a statement.
    Command string
    // Function is set if the message is about a built-in function.
    Function string

    // HelpMessageBody contains the details of the message.
    HelpMessageBody
}

HelpMessage describes a contextual help message.

func (*HelpMessage) Format Uses

func (h *HelpMessage) Format(w io.Writer)

Format prints out details about the message onto the specified output stream.

func (*HelpMessage) String Uses

func (h *HelpMessage) String() string

String implements the fmt.String interface.

type HelpMessageBody Uses

type HelpMessageBody struct {
    Category         string
    ShortDescription string
    Text             string
    SeeAlso          string
}

HelpMessageBody defines the body of a help text. The messages are structured to facilitate future help navigation functionality.

type Parser Uses

type Parser struct {
    // contains filtered or unexported fields
}

Parser wraps a scanner, parser and other utilities present in the parser package.

func (*Parser) Parse Uses

func (p *Parser) Parse(sql string) (Statements, error)

Parse parses the sql and returns a list of statements.

func (*Parser) ParseWithInt Uses

func (p *Parser) ParseWithInt(sql string, nakedIntType *types.T) (Statements, error)

ParseWithInt parses a sql statement string and returns a list of Statements. The INT token will result in the specified TInt type.

type Statement Uses

type Statement struct {
    // AST is the root of the AST tree for the parsed statement.
    AST tree.Statement

    // SQL is the original SQL from which the statement was parsed. Note that this
    // is not appropriate for use in logging, as it may contain passwords and
    // other sensitive data.
    SQL string

    // NumPlaceholders indicates the number of arguments to the statement (which
    // are referenced through placeholders). This corresponds to the highest
    // argument position (i.e. the x in "$x") that appears in the query.
    //
    // Note: where there are "gaps" in the placeholder positions, this number is
    // based on the highest position encountered. For example, for `SELECT $3`,
    // NumPlaceholders is 3. These cases are malformed and will result in a
    // type-check error.
    NumPlaceholders int

    // NumAnnotations indicates the number of annotations in the tree. It is equal
    // to the maximum annotation index.
    NumAnnotations tree.AnnotationIdx
}

Statement is the result of parsing a single statement. It contains the AST node along with other information.

func ParseOne Uses

func ParseOne(sql string) (Statement, error)

ParseOne parses a sql statement string, ensuring that it contains only a single statement, and returns that Statement. ParseOne will always interpret the INT and SERIAL types as 64-bit types, since this is used in various internal-execution paths where we might receive bits of SQL from other nodes. In general,earwe expect that all user-generated SQL has been run through the ParseWithInt() function.

func ParseOneWithInt Uses

func ParseOneWithInt(sql string, nakedIntType *types.T) (Statement, error)

ParseOneWithInt is similar to ParseOn but interprets the INT and SERIAL types as the provided integer type.

type Statements Uses

type Statements []Statement

Statements is a list of parsed statements.

func Parse Uses

func Parse(sql string) (Statements, error)

Parse parses a sql statement string and returns a list of Statements.

func (Statements) String Uses

func (stmts Statements) String() string

String returns the AST formatted as a string.

func (Statements) StringWithFlags Uses

func (stmts Statements) StringWithFlags(flags tree.FmtFlags) string

StringWithFlags returns the AST formatted as a string (with the given flags).

type TokenString Uses

type TokenString struct {
    TokenID int32
    Str     string
}

TokenString is the unit value returned by Tokens.

func Tokens Uses

func Tokens(sql string) (tokens []TokenString, ok bool)

Tokens decomposes the input into lexical tokens.

Directories

PathSynopsis
fuzz

Package parser imports 22 packages (graph) and is imported by 366 packages. Updated 2021-01-15. Refresh now. Tools for package owners.