Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ func (b SelectBuilder) Columns(columns ...string) SelectBuilder {
return builder.Extend(b, "Columns", parts).(SelectBuilder)
}

// SetColumns sets the columns in the query.
func (b SelectBuilder) SetColumns(columns ...string) SelectBuilder {
return builder.Delete(b, "Columns").(SelectBuilder).Columns(columns...)
}

// Column adds a result column to the query.
// Unlike Columns, Column accepts args which will be bound to placeholders in
// the columns string, for example:
Expand Down
11 changes: 11 additions & 0 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,14 @@ func TestSelectWithOptions(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "SELECT DISTINCT SQL_NO_CACHE * FROM foo", sql)
}

func TestSetColumns(t *testing.T) {
builder := Select("blarg").From("foo")
sql, _, err := builder.ToSql()
assert.NoError(t, err)
assert.Equal(t, "SELECT blarg FROM foo", sql)

sql, _, err = builder.SetColumns("biz", "baz").ToSql()
assert.NoError(t, err)
assert.Equal(t, "SELECT biz, baz FROM foo", sql)
}