Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SetColumns to SelectBuilder #85

Open
wants to merge 1 commit into
base: v1
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
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)
}