Skip to content

Commit 41e64b5

Browse files
Merge pull request #8 from stackql/feature/column-delimit-intelligence
column-delimit
2 parents 3cf97fc + f6e3f5b commit 41e64b5

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

go/vt/sqlparser/ast.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1795,7 +1795,16 @@ func (node *ColName) Format(buf *TrackedBuffer) {
17951795
if !node.Qualifier.IsEmpty() {
17961796
buf.astPrintf(node, "%v.", node.Qualifier)
17971797
}
1798-
buf.astPrintf(node, "%v", node.Name)
1798+
if !buf.IsDelimitCols() {
1799+
buf.astPrintf(node, "%v", node.Name)
1800+
return
1801+
}
1802+
if node.Name.IsEmpty() {
1803+
return
1804+
}
1805+
buf.WriteString(`"`)
1806+
buf.WriteString(node.Name.GetRawVal())
1807+
buf.WriteString(`"`)
17991808
}
18001809

18011810
// Format formats the node.

go/vt/sqlparser/parser.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,14 @@ func String(node SQLNode) string {
221221
buf.Myprintf("%v", node)
222222
return buf.String()
223223
}
224+
225+
// String returns a string representation of an SQLNode.
226+
func ColDelimitedString(node SQLNode) string {
227+
if node == nil {
228+
return "<nil>"
229+
}
230+
231+
buf := NewTrackedBuffer(nil).WithDelimitCols(true)
232+
buf.Myprintf("%v", node)
233+
return buf.String()
234+
}

go/vt/sqlparser/tracked_buffer.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type TrackedBuffer struct {
3636
*strings.Builder
3737
bindLocations []bindLocation
3838
nodeFormatter NodeFormatter
39+
isDelimitCols bool
3940
}
4041

4142
// NewTrackedBuffer creates a new TrackedBuffer.
@@ -46,6 +47,15 @@ func NewTrackedBuffer(nodeFormatter NodeFormatter) *TrackedBuffer {
4647
}
4748
}
4849

50+
func (buf *TrackedBuffer) IsDelimitCols() bool {
51+
return buf.isDelimitCols
52+
}
53+
54+
func (buf *TrackedBuffer) WithDelimitCols(isDelimitCols bool) *TrackedBuffer {
55+
buf.isDelimitCols = isDelimitCols
56+
return buf
57+
}
58+
4959
// WriteNode function, initiates the writing of a single SQLNode tree by passing
5060
// through to Myprintf with a default format string
5161
func (buf *TrackedBuffer) WriteNode(node SQLNode) *TrackedBuffer {

0 commit comments

Comments
 (0)