Skip to content

Commit

Permalink
Support single db select order by. (arana-db#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
dongzl authored May 21, 2022
1 parent f8c6c9d commit 2cf11cd
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pkg/runtime/plan/simple_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,38 @@ func (s *SimpleQueryPlan) generate(sb *strings.Builder, args *[]int) error {
return errors.WithStack(err)
}
}

if len(stmt.OrderBy) > 0 {
s.resetOrderBy(s.Stmt, sb, args)
}
}

return nil
}

func (s *SimpleQueryPlan) resetOrderBy(tgt *ast.SelectStatement, sb *strings.Builder, args *[]int) error {
var (
builder strings.Builder
)
builder.WriteString("SELECT * FROM (")
builder.WriteString(sb.String())
builder.WriteString(") ")
if len(tgt.From[0].Alias()) > 0 {
builder.WriteString(tgt.From[0].Alias())
} else {
builder.WriteString(" T ")
}
builder.WriteString(" ORDER BY ")
if err := tgt.OrderBy[0].Restore(ast.RestoreDefault, &builder, args); err != nil {
return errors.WithStack(err)
}
for i := 1; i < len(tgt.OrderBy); i++ {
builder.WriteString(", ")
if err := tgt.OrderBy[i].Restore(ast.RestoreDefault, &builder, args); err != nil {
return errors.WithStack(err)
}
}
sb.Reset()
sb.WriteString(builder.String())
return nil
}

0 comments on commit 2cf11cd

Please sign in to comment.