Skip to content

Commit

Permalink
Implement sqlJoin, sqlOpt using sqlJoinCtx, sqlOptCtx
Browse files Browse the repository at this point in the history
  • Loading branch information
apstndb committed Dec 18, 2024
1 parent 4c720b2 commit 7c3b999
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions ast/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,7 @@ func sqlOpt[T interface {
Node
comparable
}](left string, node T, right string) string {
var zero T
if node == zero {
return ""
}
return left + node.SQL() + right
return sqlOptCtx(nil, left, node, right)
}

// strOpt outputs:
Expand Down Expand Up @@ -166,14 +162,7 @@ func strIfElse(pred bool, ifStr string, elseStr string) string {
// sqlJoin outputs joined string of SQL() of all elems by sep.
// This function corresponds to sqlJoin in ast.go
func sqlJoin[T Node](elems []T, sep string) string {
var b strings.Builder
for i, r := range elems {
if i > 0 {
b.WriteString(sep)
}
b.WriteString(r.SQL())
}
return b.String()
return sqlJoinCtx(nil, elems, sep)
}

// formatBoolUpper formats bool value as uppercase.
Expand Down Expand Up @@ -309,7 +298,7 @@ func (c *CTE) SQL() string {

func (s *Select) sqlContext(fc *FormatContext) string {
return "SELECT" +
strOpt(s.AllOrDistinct != "", string(s.AllOrDistinct)+" ") +
strOpt(s.AllOrDistinct != "", " "+string(s.AllOrDistinct)) +
sqlOptCtx(fc, " ", s.As, "") +
fc.indentScope(func(fc *FormatContext) string {
if len(s.Results) == 1 {
Expand Down

0 comments on commit 7c3b999

Please sign in to comment.