Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions spanner/spansql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,18 @@ func (cp CreateProtoBundle) SQL() string {
}

func (cv CreateView) SQL() string {
str := "CREATE"
var sb strings.Builder
sb.WriteString("CREATE")
if cv.OrReplace {
str += " OR REPLACE"
sb.WriteString(" OR REPLACE")
}
str += " VIEW " + cv.Name.SQL() + " SQL SECURITY " + cv.SecurityType.SQL() + " AS " + cv.Query.SQL()
return str
sb.WriteString(" VIEW ")
sb.WriteString(cv.Name.SQL())
sb.WriteString(" SQL SECURITY ")
sb.WriteString(cv.SecurityType.SQL())
sb.WriteString(" AS ")
cv.Query.addSQL(&sb)
return sb.String()
}

func (st SecurityType) SQL() string {
Expand Down Expand Up @@ -838,17 +844,21 @@ func (kp KeyPart) SQL() string {
func (q Query) SQL() string { return buildSQL(q) }
func (q Query) addSQL(sb *strings.Builder) {
q.Select.addSQL(sb)

// ORDER BY clause
if len(q.Order) > 0 {
sb.WriteString(" ORDER BY ")
sb.WriteString("\nORDER BY ")
for i, o := range q.Order {
if i > 0 {
sb.WriteString(", ")
}
o.addSQL(sb)
}
}

// LIMIT/OFFSET clauses
if q.Limit != nil {
sb.WriteString(" LIMIT ")
sb.WriteString("\nLIMIT ")
sb.WriteString(q.Limit.SQL())
if q.Offset != nil {
sb.WriteString(" OFFSET ")
Expand All @@ -859,14 +869,17 @@ func (q Query) addSQL(sb *strings.Builder) {

func (sel Select) SQL() string { return buildSQL(sel) }
func (sel Select) addSQL(sb *strings.Builder) {
sb.WriteString("SELECT ")
sb.WriteString("SELECT")
if sel.Distinct {
sb.WriteString("DISTINCT ")
sb.WriteString(" DISTINCT")
}

// SELECT list with aliases
for i, e := range sel.List {
if i > 0 {
sb.WriteString(", ")
if i == 0 {
sb.WriteString("\n")
}
sb.WriteString("\t")
e.addSQL(sb)
if len(sel.ListAliases) > 0 {
alias := sel.ListAliases[i]
Expand All @@ -875,24 +888,40 @@ func (sel Select) addSQL(sb *strings.Builder) {
sb.WriteString(alias.SQL())
}
}
// Add comma and newline, except after last item
if i < len(sel.List)-1 {
sb.WriteString(",\n")
}
}

// FROM clause
if len(sel.From) > 0 {
sb.WriteString(" FROM ")
sb.WriteString("\nFROM ")
for i, f := range sel.From {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(f.SQL())
}
}

// WHERE clause
if sel.Where != nil {
sb.WriteString(" WHERE ")
sb.WriteString("\nWHERE ")
sel.Where.addSQL(sb)
}

// GROUP BY clause
if len(sel.GroupBy) > 0 {
sb.WriteString(" GROUP BY ")
sb.WriteString("\nGROUP BY ")
addExprList(sb, sel.GroupBy, ", ")
}

// TODO: HAVING clause when supported
// if sel.Having != nil {
// sb.WriteString("\nHAVING ")
// sel.Having.addSQL(sb)
// }
}

func (sft SelectFromTable) SQL() string {
Expand All @@ -918,7 +947,7 @@ func (sft SelectFromTable) SQL() string {

func (sfj SelectFromJoin) SQL() string {
// TODO: The grammar permits arbitrary nesting. Does this need to add parens?
str := sfj.LHS.SQL() + " " + joinTypes[sfj.Type] + " JOIN "
str := sfj.LHS.SQL() + "\n" + joinTypes[sfj.Type] + " JOIN "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While adding the newline here improves readability, this function and the lines that follow still use string concatenation (+ and +=). For consistency with the other changes in this PR (like in CreateView.SQL) that adopt strings.Builder, it would be beneficial to refactor this entire function to use strings.Builder as well. This would improve performance and maintainability.

// TODO: hints go here
str += sfj.RHS.SQL()
if sfj.On != nil {
Expand Down
Loading
Loading