Add ability to change alias of all projections in the ProjectionList.

Add ability to exclude list of columns from ProjectionList.
This commit is contained in:
go-jet 2022-01-05 18:00:20 +01:00
parent 392ba63bc5
commit 5cbf4aac86
8 changed files with 377 additions and 31 deletions

View file

@ -16,36 +16,68 @@ func SerializeForProjection(projection Projection, statementType StatementType,
// ProjectionList is a redefined type, so that ProjectionList can be used as a Projection.
type ProjectionList []Projection
func (cl ProjectionList) fromImpl(subQuery SelectTable) Projection {
func (pl ProjectionList) fromImpl(subQuery SelectTable) Projection {
newProjectionList := ProjectionList{}
for _, projection := range cl {
for _, projection := range pl {
newProjectionList = append(newProjectionList, projection.fromImpl(subQuery))
}
return newProjectionList
}
func (cl ProjectionList) serializeForProjection(statement StatementType, out *SQLBuilder) {
SerializeProjectionList(statement, cl, out)
func (pl ProjectionList) serializeForProjection(statement StatementType, out *SQLBuilder) {
SerializeProjectionList(statement, pl, 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, ".*")
// 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.*'.
// 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'.
func (pl ProjectionList) As(tableAlias string) ProjectionList {
tableAlias = strings.TrimRight(tableAlias, ".*")
newProjectionList := ProjectionList{}
for _, projection := range cl {
for _, projection := range pl {
switch p := projection.(type) {
case ProjectionList:
newProjectionList = append(newProjectionList, p.As(alias))
newProjectionList = append(newProjectionList, p.As(tableAlias))
case ColumnExpression:
newProjectionList = append(newProjectionList, newAlias(p, alias+"."+p.Name()))
newProjectionList = append(newProjectionList, newAlias(p, tableAlias+"."+p.Name()))
case *alias:
newAlias := *p
_, columnName := extractTableAndColumnName(newAlias.alias)
newAlias.alias = tableAlias + "." + columnName
newProjectionList = append(newProjectionList, &newAlias)
}
}
return newProjectionList
}
// Except will create new projection list in which columns contained in excluded column names are removed
func (pl ProjectionList) Except(toExclude ...Column) ProjectionList {
excludedColumnList := UnwidColumnList(toExclude)
excludedColumnNames := map[string]bool{}
for _, excludedColumn := range excludedColumnList {
excludedColumnNames[excludedColumn.Name()] = true
}
var ret ProjectionList
for _, projection := range pl {
switch p := projection.(type) {
case ProjectionList:
ret = append(ret, p.Except(toExclude...))
case ColumnExpression:
if excludedColumnNames[p.Name()] {
continue
}
ret = append(ret, p)
}
}
return ret
}