86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
package exp
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
|
|
"git.fsdpf.net/go/db/v2/internal/util"
|
|
)
|
|
|
|
type columnList struct {
|
|
columns []Expression
|
|
}
|
|
|
|
func NewColumnListExpression(vals ...interface{}) ColumnListExpression {
|
|
cols := []Expression{}
|
|
for _, val := range vals {
|
|
switch t := val.(type) {
|
|
case nil: // do nothing
|
|
case string:
|
|
cols = append(cols, ParseIdentifier(t))
|
|
case ColumnListExpression:
|
|
cols = append(cols, t.Columns()...)
|
|
case Expression:
|
|
cols = append(cols, t)
|
|
default:
|
|
_, valKind := util.GetTypeInfo(val, reflect.Indirect(reflect.ValueOf(val)))
|
|
|
|
if valKind == reflect.Struct {
|
|
cm, err := util.GetColumnMap(val)
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
structCols := cm.Cols()
|
|
for _, col := range structCols {
|
|
i := ParseIdentifier(col)
|
|
var sc Expression = i
|
|
if i.IsQualified() {
|
|
sc = i.As(NewIdentifierExpression("", "", col))
|
|
}
|
|
cols = append(cols, sc)
|
|
}
|
|
} else {
|
|
panic(fmt.Sprintf("Cannot create expression from %+v", val))
|
|
}
|
|
}
|
|
}
|
|
return columnList{columns: cols}
|
|
}
|
|
|
|
func NewOrderedColumnList(vals ...OrderedExpression) ColumnListExpression {
|
|
exps := make([]interface{}, 0, len(vals))
|
|
for _, col := range vals {
|
|
exps = append(exps, col.Expression())
|
|
}
|
|
return NewColumnListExpression(exps...)
|
|
}
|
|
|
|
func (cl columnList) Clone() Expression {
|
|
newExps := make([]Expression, 0, len(cl.columns))
|
|
for _, exp := range cl.columns {
|
|
newExps = append(newExps, exp.Clone())
|
|
}
|
|
return columnList{columns: newExps}
|
|
}
|
|
|
|
func (cl columnList) Expression() Expression {
|
|
return cl
|
|
}
|
|
|
|
func (cl columnList) IsEmpty() bool {
|
|
return len(cl.columns) == 0
|
|
}
|
|
|
|
func (cl columnList) Columns() []Expression {
|
|
return cl.columns
|
|
}
|
|
|
|
func (cl columnList) Append(cols ...Expression) ColumnListExpression {
|
|
ret := columnList{}
|
|
exps := ret.columns
|
|
exps = append(exps, cl.columns...)
|
|
exps = append(exps, cols...)
|
|
ret.columns = exps
|
|
return ret
|
|
}
|