Skip to content

Commit

Permalink
Add ability to select using indirect values.
Browse files Browse the repository at this point in the history
ADD: add the option to pass indirect values to select statement columns
  • Loading branch information
guyyakir authored and ido50 committed Jan 28, 2025
1 parent 2f64be1 commit f3ddf55
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type SelectStmt struct {
queryer Queryer
DistinctColumns []string
Columns []string
IndirectColumns []IndirectValue
Joins []JoinClause
Conditions []WhereCondition
Ordering []SQLStmt
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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, ", "))

Check failure on line 412 in select.go

View workflow job for this annotation

GitHub Actions / Build

append only allowed to cuddle with appended value (wsl)
}

if len(stmt.Table) > 0 {
Expand Down

0 comments on commit f3ddf55

Please sign in to comment.