diff --git a/select.go b/select.go index 0175af6..96ead46 100644 --- a/select.go +++ b/select.go @@ -52,6 +52,7 @@ type SelectStmt struct { queryer Queryer DistinctColumns []string Columns []string + IndirectColumns []IndirectValue Joins []JoinClause Conditions []WhereCondition Ordering []SQLStmt @@ -183,6 +184,13 @@ func (tx *Tx) Select(cols ...string) *SelectStmt { } } +// InDirectColumns adds sqlz.IndirectValue columns to the statement. +// This can be useful when using database function inside the select statement +func (stmt *SelectStmt) InDirectColumns(cols ...IndirectValue) *SelectStmt { + stmt.IndirectColumns = append([]IndirectValue{}, cols...) + return stmt +} + // Distinct marks the statements as a SELECT DISTINCT // statement func (stmt *SelectStmt) Distinct(cols ...string) *SelectStmt { @@ -387,10 +395,21 @@ func (stmt *SelectStmt) ToSQL(rebind bool) (asSQL string, bindings []interface{} } } - if len(stmt.Columns) == 0 { + if len(stmt.Columns) == 0 && len(stmt.IndirectColumns) == 0 { clauses = append(clauses, "*") } else { - clauses = append(clauses, strings.Join(stmt.Columns, ", ")) + columnsClauses := make([]string, 0) + if len(stmt.Columns) > 0 { + columnsClauses = append(columnsClauses, stmt.Columns...) + } + + if len(stmt.IndirectColumns) > 0 { + for _, indirectColumn := range stmt.IndirectColumns { + bindings = append(bindings, indirectColumn.Bindings...) + columnsClauses = append(columnsClauses, indirectColumn.Reference) + } + } + clauses = append(clauses, strings.Join(columnsClauses, ", ")) } if len(stmt.Table) > 0 {