refactor: extracted common logic for column aliasing

This commit is contained in:
Yosyp Buchma 2024-07-26 14:51:45 +02:00
parent c599fca5ea
commit 583ecba4a1
4 changed files with 27 additions and 19 deletions

View file

@ -1,7 +1,5 @@
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)
@ -35,12 +33,6 @@ func (pl ProjectionList) serializeForProjection(statement StatementType, out *SQ
// For instance: If projection list has a column 'Artist.Name', and tableAlias is 'Musician.*', returned projection list will
// have a column wrapped in alias 'Musician.Name'. If tableAlias is empty string, it removes existing table alias ('Artist.Name' becomes 'Name').
func (pl ProjectionList) As(tableAlias string) ProjectionList {
tableAliasWithDot := ""
if tableAlias != "" {
tableAlias = strings.TrimRight(tableAlias, ".*")
tableAliasWithDot = tableAlias + "."
}
newProjectionList := ProjectionList{}
for _, projection := range pl {
@ -50,11 +42,11 @@ func (pl ProjectionList) As(tableAlias string) ProjectionList {
case ColumnList:
newProjectionList = append(newProjectionList, p.As(tableAlias))
case ColumnExpression:
newProjectionList = append(newProjectionList, newAlias(p, tableAliasWithDot+p.Name()))
newProjectionList = append(newProjectionList, newAlias(p, joinAlias(tableAlias, p.Name())))
case *alias:
newAlias := *p
_, columnName := extractTableAndColumnName(newAlias.alias)
newAlias.alias = tableAliasWithDot + columnName
newAlias.alias = joinAlias(tableAlias, columnName)
newProjectionList = append(newProjectionList, &newAlias)
}
}