Add helper method to set ProjectionList alias.

This commit is contained in:
go-jet 2021-12-30 16:23:59 +01:00
parent 038a32b032
commit 392ba63bc5
2 changed files with 82 additions and 0 deletions

View file

@ -1,5 +1,7 @@
package jet
import "strings"
// Projection is interface for all projection types. Types that can be part of, for instance SELECT clause.
type Projection interface {
serializeForProjection(statement StatementType, out *SQLBuilder)
@ -27,3 +29,23 @@ func (cl ProjectionList) fromImpl(subQuery SelectTable) Projection {
func (cl ProjectionList) serializeForProjection(statement StatementType, out *SQLBuilder) {
SerializeProjectionList(statement, cl, out)
}
// As is used to set aliases of the projection list. alias should be in the form 'name' or 'name.*'.
// For instance: If projection list has a column 'Artist.Name', and alias is 'Musician.*', returned projection list will
// have column wrapped in alias 'Musician.Name'.
func (cl ProjectionList) As(alias string) ProjectionList {
alias = strings.TrimRight(alias, ".*")
newProjectionList := ProjectionList{}
for _, projection := range cl {
switch p := projection.(type) {
case ProjectionList:
newProjectionList = append(newProjectionList, p.As(alias))
case ColumnExpression:
newProjectionList = append(newProjectionList, newAlias(p, alias+"."+p.Name()))
}
}
return newProjectionList
}