Skip to content

Commit c77d589

Browse files
committed
Add ability to select using indirect values.
ADD: add the option to pass indirect values to select statement columns
1 parent 2f64be1 commit c77d589

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

select.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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,11 @@ func (tx *Tx) Select(cols ...string) *SelectStmt {
183184
}
184185
}
185186

187+
func (stmt *SelectStmt) InDirectColumns(cols ...IndirectValue) *SelectStmt {
188+
stmt.IndirectColumns = append([]IndirectValue{}, cols...)
189+
return stmt
190+
}
191+
186192
// Distinct marks the statements as a SELECT DISTINCT
187193
// statement
188194
func (stmt *SelectStmt) Distinct(cols ...string) *SelectStmt {
@@ -387,10 +393,21 @@ func (stmt *SelectStmt) ToSQL(rebind bool) (asSQL string, bindings []interface{}
387393
}
388394
}
389395

390-
if len(stmt.Columns) == 0 {
396+
if len(stmt.Columns) == 0 && len(stmt.IndirectColumns) == 0 {
391397
clauses = append(clauses, "*")
392398
} else {
393-
clauses = append(clauses, strings.Join(stmt.Columns, ", "))
399+
columnsClauses := make([]string, 0)
400+
if len(stmt.Columns) > 0 {
401+
columnsClauses = append(columnsClauses, stmt.Columns...)
402+
}
403+
404+
if len(stmt.IndirectColumns) > 0 {
405+
for _, indirectColumn := range stmt.IndirectColumns {
406+
bindings = append(bindings, indirectColumn.Bindings...)
407+
columnsClauses = append(columnsClauses, indirectColumn.Reference)
408+
}
409+
}
410+
clauses = append(clauses, strings.Join(columnsClauses, ", "))
394411
}
395412

396413
if len(stmt.Table) > 0 {

0 commit comments

Comments
 (0)