...

Source file src/github.com/doug-martin/goqu/v9/exp/col.go

Documentation: github.com/doug-martin/goqu/v9/exp

     1  package exp
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  
     7  	"github.com/doug-martin/goqu/v9/internal/util"
     8  )
     9  
    10  type columnList struct {
    11  	columns []Expression
    12  }
    13  
    14  func NewColumnListExpression(vals ...interface{}) ColumnListExpression {
    15  	cols := []Expression{}
    16  	for _, val := range vals {
    17  		switch t := val.(type) {
    18  		case nil: // do nothing
    19  		case string:
    20  			cols = append(cols, ParseIdentifier(t))
    21  		case ColumnListExpression:
    22  			cols = append(cols, t.Columns()...)
    23  		case Expression:
    24  			cols = append(cols, t)
    25  		default:
    26  			_, valKind := util.GetTypeInfo(val, reflect.Indirect(reflect.ValueOf(val)))
    27  
    28  			if valKind == reflect.Struct {
    29  				cm, err := util.GetColumnMap(val)
    30  				if err != nil {
    31  					panic(err.Error())
    32  				}
    33  				structCols := cm.Cols()
    34  				for _, col := range structCols {
    35  					i := ParseIdentifier(col)
    36  					var sc Expression = i
    37  					if i.IsQualified() {
    38  						sc = i.As(NewIdentifierExpression("", "", col))
    39  					}
    40  					cols = append(cols, sc)
    41  				}
    42  			} else {
    43  				panic(fmt.Sprintf("Cannot created expression from  %+v", val))
    44  			}
    45  		}
    46  	}
    47  	return columnList{columns: cols}
    48  }
    49  
    50  func NewOrderedColumnList(vals ...OrderedExpression) ColumnListExpression {
    51  	exps := make([]interface{}, 0, len(vals))
    52  	for _, col := range vals {
    53  		exps = append(exps, col.Expression())
    54  	}
    55  	return NewColumnListExpression(exps...)
    56  }
    57  
    58  func (cl columnList) Clone() Expression {
    59  	newExps := make([]Expression, 0, len(cl.columns))
    60  	for _, exp := range cl.columns {
    61  		newExps = append(newExps, exp.Clone())
    62  	}
    63  	return columnList{columns: newExps}
    64  }
    65  
    66  func (cl columnList) Expression() Expression {
    67  	return cl
    68  }
    69  
    70  func (cl columnList) IsEmpty() bool {
    71  	return len(cl.columns) == 0
    72  }
    73  
    74  func (cl columnList) Columns() []Expression {
    75  	return cl.columns
    76  }
    77  
    78  func (cl columnList) Append(cols ...Expression) ColumnListExpression {
    79  	ret := columnList{}
    80  	exps := append(ret.columns, cl.columns...)
    81  	exps = append(exps, cols...)
    82  	ret.columns = exps
    83  	return ret
    84  }
    85  

View as plain text