Debian Codegraph @

parsed_query.go

raw symbols SCIP
1 /*
2 Copyright 2017 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package sqlparser
18
19 import (
20 "bytes"
21 "fmt"
22
23 "github.com/xwb1989/sqlparser/dependency/querypb"
24 "github.com/xwb1989/sqlparser/dependency/sqltypes"
25 )
26
27 // ParsedQuery represents a parsed query where
28 // bind locations are precompued for fast substitutions.
29 type ParsedQuery struct {
30 Query string
31 bindLocations []bindLocation
32 }
33
34 type bindLocation struct {
35 offset, length int
36 }
37
38 // NewParsedQuery returns a ParsedQuery of the ast.
39 func NewParsedQuery(node SQLNode) *ParsedQuery {
40 buf := NewTrackedBuffer(nil)
41 buf.Myprintf("%v", node)
42 return buf.ParsedQuery()
43 }
44
45 // GenerateQuery generates a query by substituting the specified
46 // bindVariables. The extras parameter specifies special parameters
47 // that can perform custom encoding.
48 func (pq *ParsedQuery) GenerateQuery(bindVariables map[string]*querypb.BindVariable, extras map[string]Encodable) ([]byte, error) {
49 if len(pq.bindLocations) == 0 {
50 return []byte(pq.Query), nil
51 }
52 buf := bytes.NewBuffer(make([]byte, 0, len(pq.Query)))
53 current := 0
54 for _, loc := range pq.bindLocations {
55 buf.WriteString(pq.Query[current:loc.offset])
56 name := pq.Query[loc.offset : loc.offset+loc.length]
57 if encodable, ok := extras[name[1:]]; ok {
58 encodable.EncodeSQL(buf)
59 } else {
60 supplied, _, err := FetchBindVar(name, bindVariables)
61 if err != nil {
62 return nil, err
63 }
64 EncodeValue(buf, supplied)
65 }
66 current = loc.offset + loc.length
67 }
68 buf.WriteString(pq.Query[current:])
69 return buf.Bytes(), nil
70 }
71
72 // EncodeValue encodes one bind variable value into the query.
73 func EncodeValue(buf *bytes.Buffer, value *querypb.BindVariable) {
74 if value.Type != querypb.Type_TUPLE {
75 // Since we already check for TUPLE, we don't expect an error.
76 v, _ := sqltypes.BindVariableToValue(value)
77 v.EncodeSQL(buf)
78 return
79 }
80
81 // It's a TUPLE.
82 buf.WriteByte('(')
83 for i, bv := range value.Values {
84 if i != 0 {
85 buf.WriteString(", ")
86 }
87 sqltypes.ProtoToValue(bv).EncodeSQL(buf)
88 }
89 buf.WriteByte(')')
90 }
91
92 // FetchBindVar resolves the bind variable by fetching it from bindVariables.
93 func FetchBindVar(name string, bindVariables map[string]*querypb.BindVariable) (val *querypb.BindVariable, isList bool, err error) {
94 name = name[1:]
95 if name[0] == ':' {
96 name = name[1:]
97 isList = true
98 }
99 supplied, ok := bindVariables[name]
100 if !ok {
101 return nil, false, fmt.Errorf("missing bind var %s", name)
102 }
103
104 if isList {
105 if supplied.Type != querypb.Type_TUPLE {
106 return nil, false, fmt.Errorf("unexpected list arg type (%v) for key %s", supplied.Type, name)
107 }
108 if len(supplied.Values) == 0 {
109 return nil, false, fmt.Errorf("empty list supplied for %s", name)
110 }
111 return supplied, true, nil
112 }
113
114 if supplied.Type == querypb.Type_TUPLE {
115 return nil, false, fmt.Errorf("unexpected arg type (TUPLE) for non-list key %s", name)
116 }
117
118 return supplied, false, nil
119 }