Skip to content

Commit af0806f

Browse files
authored
feat(spansql): improve the SQL formatting when printing out SQL (#13267)
## Summary This is a very simple PR to improve the readability of SQL written out by this package. ## Highlights * improve the SQL output for all types from a single long line to a more human readable format * Add test coverage for all fields updated Please note that this is related to another of my PR's as I really need both to progress please. Related: #13266
1 parent cdaf6a6 commit af0806f

File tree

2 files changed

+477
-33
lines changed

2 files changed

+477
-33
lines changed

spanner/spansql/sql.go

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,18 @@ func (cp CreateProtoBundle) SQL() string {
171171
}
172172

173173
func (cv CreateView) SQL() string {
174-
str := "CREATE"
174+
var sb strings.Builder
175+
sb.WriteString("CREATE")
175176
if cv.OrReplace {
176-
str += " OR REPLACE"
177+
sb.WriteString(" OR REPLACE")
177178
}
178-
str += " VIEW " + cv.Name.SQL() + " SQL SECURITY " + cv.SecurityType.SQL() + " AS " + cv.Query.SQL()
179-
return str
179+
sb.WriteString(" VIEW ")
180+
sb.WriteString(cv.Name.SQL())
181+
sb.WriteString(" SQL SECURITY ")
182+
sb.WriteString(cv.SecurityType.SQL())
183+
sb.WriteString(" AS ")
184+
cv.Query.addSQL(&sb)
185+
return sb.String()
180186
}
181187

182188
func (st SecurityType) SQL() string {
@@ -838,17 +844,21 @@ func (kp KeyPart) SQL() string {
838844
func (q Query) SQL() string { return buildSQL(q) }
839845
func (q Query) addSQL(sb *strings.Builder) {
840846
q.Select.addSQL(sb)
847+
848+
// ORDER BY clause
841849
if len(q.Order) > 0 {
842-
sb.WriteString(" ORDER BY ")
850+
sb.WriteString("\nORDER BY ")
843851
for i, o := range q.Order {
844852
if i > 0 {
845853
sb.WriteString(", ")
846854
}
847855
o.addSQL(sb)
848856
}
849857
}
858+
859+
// LIMIT/OFFSET clauses
850860
if q.Limit != nil {
851-
sb.WriteString(" LIMIT ")
861+
sb.WriteString("\nLIMIT ")
852862
sb.WriteString(q.Limit.SQL())
853863
if q.Offset != nil {
854864
sb.WriteString(" OFFSET ")
@@ -859,14 +869,17 @@ func (q Query) addSQL(sb *strings.Builder) {
859869

860870
func (sel Select) SQL() string { return buildSQL(sel) }
861871
func (sel Select) addSQL(sb *strings.Builder) {
862-
sb.WriteString("SELECT ")
872+
sb.WriteString("SELECT")
863873
if sel.Distinct {
864-
sb.WriteString("DISTINCT ")
874+
sb.WriteString(" DISTINCT")
865875
}
876+
877+
// SELECT list with aliases
866878
for i, e := range sel.List {
867-
if i > 0 {
868-
sb.WriteString(", ")
879+
if i == 0 {
880+
sb.WriteString("\n")
869881
}
882+
sb.WriteString("\t")
870883
e.addSQL(sb)
871884
if len(sel.ListAliases) > 0 {
872885
alias := sel.ListAliases[i]
@@ -875,24 +888,40 @@ func (sel Select) addSQL(sb *strings.Builder) {
875888
sb.WriteString(alias.SQL())
876889
}
877890
}
891+
// Add comma and newline, except after last item
892+
if i < len(sel.List)-1 {
893+
sb.WriteString(",\n")
894+
}
878895
}
896+
897+
// FROM clause
879898
if len(sel.From) > 0 {
880-
sb.WriteString(" FROM ")
899+
sb.WriteString("\nFROM ")
881900
for i, f := range sel.From {
882901
if i > 0 {
883902
sb.WriteString(", ")
884903
}
885904
sb.WriteString(f.SQL())
886905
}
887906
}
907+
908+
// WHERE clause
888909
if sel.Where != nil {
889-
sb.WriteString(" WHERE ")
910+
sb.WriteString("\nWHERE ")
890911
sel.Where.addSQL(sb)
891912
}
913+
914+
// GROUP BY clause
892915
if len(sel.GroupBy) > 0 {
893-
sb.WriteString(" GROUP BY ")
916+
sb.WriteString("\nGROUP BY ")
894917
addExprList(sb, sel.GroupBy, ", ")
895918
}
919+
920+
// TODO: HAVING clause when supported
921+
// if sel.Having != nil {
922+
// sb.WriteString("\nHAVING ")
923+
// sel.Having.addSQL(sb)
924+
// }
896925
}
897926

898927
func (sft SelectFromTable) SQL() string {
@@ -918,7 +947,7 @@ func (sft SelectFromTable) SQL() string {
918947

919948
func (sfj SelectFromJoin) SQL() string {
920949
// TODO: The grammar permits arbitrary nesting. Does this need to add parens?
921-
str := sfj.LHS.SQL() + " " + joinTypes[sfj.Type] + " JOIN "
950+
str := sfj.LHS.SQL() + "\n" + joinTypes[sfj.Type] + " JOIN "
922951
// TODO: hints go here
923952
str += sfj.RHS.SQL()
924953
if sfj.On != nil {

0 commit comments

Comments
 (0)