forked from cockroachdb/cockroach
/
parse.go
150 lines (130 loc) · 4.26 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
// This code was derived from https://github.com/youtube/vitess.
//
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file
package parser
import (
"bytes"
"errors"
"github.com/cockroachdb/cockroach/util"
)
//go:generate make
// StatementList is a list of statements.
type StatementList []Statement
func (l StatementList) String() string {
var buf bytes.Buffer
for i, s := range l {
if i > 0 {
buf.WriteString("; ")
}
buf.WriteString(s.String())
}
return buf.String()
}
// Syntax is an enum of the various syntax types.
type Syntax int
//go:generate stringer -type=Syntax
const (
// Implicit default, must stay in the zero-value position.
Traditional Syntax = iota
Modern
)
// Parser wraps a scanner, parser and other utilities present in the parser
// package.
type Parser struct {
scanner scanner
parserImpl sqlParserImpl
argVisitor argVisitor
normalizeVisitor normalizeVisitor
}
// Parse parses the sql and returns a list of statements.
func (p *Parser) Parse(sql string, syntax Syntax) (StatementList, error) {
p.scanner.init(sql, syntax)
if p.parserImpl.Parse(&p.scanner) != 0 {
return nil, errors.New(p.scanner.lastError)
}
return p.scanner.stmts, nil
}
// FillArgs replaces any placeholder nodes in the expression with arguments
// supplied with the query.
func (p *Parser) FillArgs(stmt Statement, args Args) error {
p.argVisitor = argVisitor{args: args}
WalkStmt(&p.argVisitor, stmt)
return p.argVisitor.err
}
// NormalizeExpr is wrapper around ctx.NormalizeExpr which avoids allocation of
// a normalizeVisitor.
func (p *Parser) NormalizeExpr(ctx EvalContext, expr Expr) (Expr, error) {
p.normalizeVisitor = normalizeVisitor{ctx: ctx}
expr = WalkExpr(&p.normalizeVisitor, expr)
return expr, p.normalizeVisitor.err
}
// TypeCheckAndNormalizeExpr is a combination of TypeCheck() and
// NormalizeExpr(). It returns an error if either of TypeCheck() or
// NormalizeExpr() return one, and otherwise returns the Expr returned by
// NormalizeExpr().
func (p *Parser) TypeCheckAndNormalizeExpr(ctx EvalContext, expr Expr) (Expr, error) {
if _, err := expr.TypeCheck(ctx.Args); err != nil {
return nil, err
}
return p.NormalizeExpr(ctx, expr)
}
// parse parses the sql and returns a list of statements.
func parse(sql string, syntax Syntax) (StatementList, error) {
var p Parser
return p.Parse(sql, syntax)
}
// parseTraditional is short-hand for parse(sql, Traditional)
func parseTraditional(sql string) (StatementList, error) {
return parse(sql, Traditional)
}
// parseOne parses a sql statement.
func parseOne(sql string, syntax Syntax) (Statement, error) {
stmts, err := parse(sql, syntax)
if err != nil {
return nil, err
}
if len(stmts) != 1 {
return nil, util.Errorf("expected 1 statement, but found %d", len(stmts))
}
return stmts[0], nil
}
// ParseOneTraditional is short-hand for parseOne(sql, Traditional)
func ParseOneTraditional(sql string) (Statement, error) {
return parseOne(sql, Traditional)
}
// parseExpr parses a sql expression.
func parseExpr(expr string, syntax Syntax) (Expr, error) {
stmt, err := parseOne(`SELECT `+expr, syntax)
if err != nil {
return nil, err
}
sel, ok := stmt.(*Select)
if !ok {
return nil, util.Errorf("expected a SELECT statement, but found %T", stmt)
}
if n := len(sel.Exprs); n != 1 {
return nil, util.Errorf("expected 1 expression, but found %d", n)
}
return sel.Exprs[0].Expr, nil
}
// ParseExprTraditional is a short-hand for parseExpr(sql, Traditional)
func ParseExprTraditional(sql string) (Expr, error) {
return parseExpr(sql, Traditional)
}