New method: func (ColumnList) As(tableAlias string) ProjectionList

This commit is contained in:
Yosyp Buchma 2024-07-25 17:09:52 +02:00
parent fb66fbd31c
commit c599fca5ea
3 changed files with 40 additions and 9 deletions

View file

@ -1,5 +1,7 @@
package jet package jet
import "strings"
// ColumnList is a helper type to support list of columns as single projection // ColumnList is a helper type to support list of columns as single projection
type ColumnList []ColumnExpression type ColumnList []ColumnExpression
@ -39,6 +41,21 @@ func (cl ColumnList) Except(excludedColumns ...Column) ColumnList {
return ret return ret
} }
// As will create new projection list where each column is wrapped with a new table alias.
// tableAlias should be in the form 'name' or 'name.*', or it can also be an empty string.
// 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 (cl ColumnList) As(tableAlias string) ProjectionList {
if tableAlias > "" {
tableAlias = strings.TrimRight(tableAlias, ".*") + "."
}
ret := make(ProjectionList, 0, len(cl))
for _, c := range cl {
ret = append(ret, c.AS(tableAlias+c.Name()))
}
return ret
}
func (cl ColumnList) fromImpl(subQuery SelectTable) Projection { func (cl ColumnList) fromImpl(subQuery SelectTable) Projection {
newProjectionList := ProjectionList{} newProjectionList := ProjectionList{}

View file

@ -31,11 +31,15 @@ func (pl ProjectionList) serializeForProjection(statement StatementType, out *SQ
} }
// As will create new projection list where each column is wrapped with a new table alias. // As will create new projection list where each column is wrapped with a new table alias.
// tableAlias should be in the form 'name' or 'name.*'. // tableAlias should be in the form 'name' or 'name.*', or it can be an empty string, which will remove existing table alias.
// For instance: If projection list has a column 'Artist.Name', and tableAlias is 'Musician.*', returned projection list will // 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'. // 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 { func (pl ProjectionList) As(tableAlias string) ProjectionList {
tableAliasWithDot := ""
if tableAlias != "" {
tableAlias = strings.TrimRight(tableAlias, ".*") tableAlias = strings.TrimRight(tableAlias, ".*")
tableAliasWithDot = tableAlias + "."
}
newProjectionList := ProjectionList{} newProjectionList := ProjectionList{}
@ -43,16 +47,14 @@ func (pl ProjectionList) As(tableAlias string) ProjectionList {
switch p := projection.(type) { switch p := projection.(type) {
case ProjectionList: case ProjectionList:
newProjectionList = append(newProjectionList, p.As(tableAlias)) newProjectionList = append(newProjectionList, p.As(tableAlias))
case ColumnExpression:
newProjectionList = append(newProjectionList, newAlias(p, tableAlias+"."+p.Name()))
case ColumnList: case ColumnList:
for _, c := range p { newProjectionList = append(newProjectionList, p.As(tableAlias))
newProjectionList = append(newProjectionList, newAlias(c, tableAlias+"."+c.Name())) case ColumnExpression:
} newProjectionList = append(newProjectionList, newAlias(p, tableAliasWithDot+p.Name()))
case *alias: case *alias:
newAlias := *p newAlias := *p
_, columnName := extractTableAndColumnName(newAlias.alias) _, columnName := extractTableAndColumnName(newAlias.alias)
newAlias.alias = tableAlias + "." + columnName newAlias.alias = tableAliasWithDot + columnName
newProjectionList = append(newProjectionList, &newAlias) newProjectionList = append(newProjectionList, &newAlias)
} }
} }

View file

@ -27,6 +27,18 @@ AVG(table1.col_int) AS "new_alias.avg",
table2.col3 AS "new_alias.col3", table2.col3 AS "new_alias.col3",
table2.col4 AS "new_alias.col4"`) table2.col4 AS "new_alias.col4"`)
aliasedProjectionList = projectionList.As("")
assertProjectionSerialize(t, aliasedProjectionList,
`table1.col3 AS "col3",
SUM(table1.col_int) AS "sum",
SUM(table1.col_int) AS "sum",
table1.col_bool AS "col_bool",
AVG(table1.col_int) AS "avg",
AVG(table1.col_int) AS "avg",
table2.col3 AS "col3",
table2.col4 AS "col4"`)
subQueryProjections := projectionList.fromImpl(NewSelectTable(nil, "subQuery")) subQueryProjections := projectionList.fromImpl(NewSelectTable(nil, "subQuery"))
assertProjectionSerialize(t, subQueryProjections, assertProjectionSerialize(t, subQueryProjections,