Files
db/exp/col.go
T
whatandClaude 304d553b3c docs: Add CLAUDE.md with codebase guidance
Create comprehensive documentation for future Claude Code instances working in this repository, including:
- Development commands for testing, building, and code quality
- Core architecture overview of the SQL query builder system
- Directory structure and component explanations
- Testing patterns and conventions
- Key dependencies and their purposes

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-27 15:47:28 +08:00

93 lines
2.1 KiB
Go

package exp
import (
"fmt"
"reflect"
"git.fsdpf.net/go/db/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:
refVal := reflect.Indirect(reflect.ValueOf(val))
_, valKind := util.GetTypeInfo(val, refVal)
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 if refVal.Kind() == reflect.Slice {
items := make([]any, refVal.Len())
for i := 0; i < refVal.Len(); i++ {
items[i] = refVal.Index(i).Interface()
}
return NewColumnListExpression(items...)
} 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
}