@@ -52,6 +52,7 @@ type SelectStmt struct {
5252 queryer Queryer
5353 DistinctColumns []string
5454 Columns []string
55+ IndirectColumns []IndirectValue
5556 Joins []JoinClause
5657 Conditions []WhereCondition
5758 Ordering []SQLStmt
@@ -183,6 +184,13 @@ func (tx *Tx) Select(cols ...string) *SelectStmt {
183184 }
184185}
185186
187+ // InDirectColumns adds IndirectValue columns to the statement. This can be
188+ // be useful when using database functions for columns.
189+ func (stmt * SelectStmt ) InDirectColumns (cols ... IndirectValue ) * SelectStmt {
190+ stmt .IndirectColumns = append ([]IndirectValue {}, cols ... )
191+ return stmt
192+ }
193+
186194// Distinct marks the statements as a SELECT DISTINCT
187195// statement
188196func (stmt * SelectStmt ) Distinct (cols ... string ) * SelectStmt {
@@ -387,10 +395,21 @@ func (stmt *SelectStmt) ToSQL(rebind bool) (asSQL string, bindings []interface{}
387395 }
388396 }
389397
390- if len (stmt .Columns ) == 0 {
398+ if len (stmt .Columns ) == 0 && len ( stmt . IndirectColumns ) == 0 {
391399 clauses = append (clauses , "*" )
392400 } else {
393- clauses = append (clauses , strings .Join (stmt .Columns , ", " ))
401+ columnsClauses := make ([]string , 0 )
402+ if len (stmt .Columns ) > 0 {
403+ columnsClauses = append (columnsClauses , stmt .Columns ... )
404+ }
405+
406+ if len (stmt .IndirectColumns ) > 0 {
407+ for _ , indirectColumn := range stmt .IndirectColumns {
408+ bindings = append (bindings , indirectColumn .Bindings ... )
409+ columnsClauses = append (columnsClauses , indirectColumn .Reference )
410+ }
411+ }
412+ clauses = append (clauses , strings .Join (columnsClauses , ", " ))
394413 }
395414
396415 if len (stmt .Table ) > 0 {
0 commit comments