diff --git a/go/vt/sqlparser/ast.go b/go/vt/sqlparser/ast.go index f68e69329..1fa3415ad 100644 --- a/go/vt/sqlparser/ast.go +++ b/go/vt/sqlparser/ast.go @@ -1795,7 +1795,16 @@ func (node *ColName) Format(buf *TrackedBuffer) { if !node.Qualifier.IsEmpty() { buf.astPrintf(node, "%v.", node.Qualifier) } - buf.astPrintf(node, "%v", node.Name) + if !buf.IsDelimitCols() { + buf.astPrintf(node, "%v", node.Name) + return + } + if node.Name.IsEmpty() { + return + } + buf.WriteString(`"`) + buf.WriteString(node.Name.GetRawVal()) + buf.WriteString(`"`) } // Format formats the node. diff --git a/go/vt/sqlparser/parser.go b/go/vt/sqlparser/parser.go index 2d5efa8d6..04ac2c3d6 100644 --- a/go/vt/sqlparser/parser.go +++ b/go/vt/sqlparser/parser.go @@ -221,3 +221,14 @@ func String(node SQLNode) string { buf.Myprintf("%v", node) return buf.String() } + +// String returns a string representation of an SQLNode. +func ColDelimitedString(node SQLNode) string { + if node == nil { + return "" + } + + buf := NewTrackedBuffer(nil).WithDelimitCols(true) + buf.Myprintf("%v", node) + return buf.String() +} diff --git a/go/vt/sqlparser/tracked_buffer.go b/go/vt/sqlparser/tracked_buffer.go index 9f11f426a..a6ee9a769 100644 --- a/go/vt/sqlparser/tracked_buffer.go +++ b/go/vt/sqlparser/tracked_buffer.go @@ -36,6 +36,7 @@ type TrackedBuffer struct { *strings.Builder bindLocations []bindLocation nodeFormatter NodeFormatter + isDelimitCols bool } // NewTrackedBuffer creates a new TrackedBuffer. @@ -46,6 +47,15 @@ func NewTrackedBuffer(nodeFormatter NodeFormatter) *TrackedBuffer { } } +func (buf *TrackedBuffer) IsDelimitCols() bool { + return buf.isDelimitCols +} + +func (buf *TrackedBuffer) WithDelimitCols(isDelimitCols bool) *TrackedBuffer { + buf.isDelimitCols = isDelimitCols + return buf +} + // WriteNode function, initiates the writing of a single SQLNode tree by passing // through to Myprintf with a default format string func (buf *TrackedBuffer) WriteNode(node SQLNode) *TrackedBuffer {