Debian Codegraph @

tracked_buffer.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
24 // NodeFormatter defines the signature of a custom node formatter
25 // function that can be given to TrackedBuffer for code generation.
26 type NodeFormatter func(buf *TrackedBuffer, node SQLNode)
27
28 // TrackedBuffer is used to rebuild a query from the ast.
29 // bindLocations keeps track of locations in the buffer that
30 // use bind variables for efficient future substitutions.
31 // nodeFormatter is the formatting function the buffer will
32 // use to format a node. By default(nil), it's FormatNode.
33 // But you can supply a different formatting function if you
34 // want to generate a query that's different from the default.
35 type TrackedBuffer struct {
36 *bytes.Buffer
37 bindLocations []bindLocation
38 nodeFormatter NodeFormatter
39 }
40
41 // NewTrackedBuffer creates a new TrackedBuffer.
42 func NewTrackedBuffer(nodeFormatter NodeFormatter) *TrackedBuffer {
43 return &TrackedBuffer{
44 Buffer: new(bytes.Buffer),
45 nodeFormatter: nodeFormatter,
46 }
47 }
48
49 // WriteNode function, initiates the writing of a single SQLNode tree by passing
50 // through to Myprintf with a default format string
51 func (buf *TrackedBuffer) WriteNode(node SQLNode) *TrackedBuffer {
52 buf.Myprintf("%v", node)
53 return buf
54 }
55
56 // Myprintf mimics fmt.Fprintf(buf, ...), but limited to Node(%v),
57 // Node.Value(%s) and string(%s). It also allows a %a for a value argument, in
58 // which case it adds tracking info for future substitutions.
59 //
60 // The name must be something other than the usual Printf() to avoid "go vet"
61 // warnings due to our custom format specifiers.
62 func (buf *TrackedBuffer) Myprintf(format string, values ...interface{}) {
63 end := len(format)
64 fieldnum := 0
65 for i := 0; i < end; {
66 lasti := i
67 for i < end && format[i] != '%' {
68 i++
69 }
70 if i > lasti {
71 buf.WriteString(format[lasti:i])
72 }
73 if i >= end {
74 break
75 }
76 i++ // '%'
77 switch format[i] {
78 case 'c':
79 switch v := values[fieldnum].(type) {
80 case byte:
81 buf.WriteByte(v)
82 case rune:
83 buf.WriteRune(v)
84 default:
85 panic(fmt.Sprintf("unexpected TrackedBuffer type %T", v))
86 }
87 case 's':
88 switch v := values[fieldnum].(type) {
89 case []byte:
90 buf.Write(v)
91 case string:
92 buf.WriteString(v)
93 default:
94 panic(fmt.Sprintf("unexpected TrackedBuffer type %T", v))
95 }
96 case 'v':
97 node := values[fieldnum].(SQLNode)
98 if buf.nodeFormatter == nil {
99 node.Format(buf)
100 } else {
101 buf.nodeFormatter(buf, node)
102 }
103 case 'a':
104 buf.WriteArg(values[fieldnum].(string))
105 default:
106 panic("unexpected")
107 }
108 fieldnum++
109 i++
110 }
111 }
112
113 // WriteArg writes a value argument into the buffer along with
114 // tracking information for future substitutions. arg must contain
115 // the ":" or "::" prefix.
116 func (buf *TrackedBuffer) WriteArg(arg string) {
117 buf.bindLocations = append(buf.bindLocations, bindLocation{
118 offset: buf.Len(),
119 length: len(arg),
120 })
121 buf.WriteString(arg)
122 }
123
124 // ParsedQuery returns a ParsedQuery that contains bind
125 // locations for easy substitution.
126 func (buf *TrackedBuffer) ParsedQuery() *ParsedQuery {
127 return &ParsedQuery{Query: buf.String(), bindLocations: buf.bindLocations}
128 }
129
130 // HasBindVars returns true if the parsed query uses bind vars.
131 func (buf *TrackedBuffer) HasBindVars() bool {
132 return len(buf.bindLocations) != 0
133 }
134
135 // BuildParsedQuery builds a ParsedQuery from the input.
136 func BuildParsedQuery(in string, vars ...interface{}) *ParsedQuery {
137 buf := NewTrackedBuffer(nil)
138 buf.Myprintf(in, vars...)
139 return buf.ParsedQuery()
140 }