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

feat: add remove select builder columns method #331

Merged
merged 1 commit into from
Sep 14, 2022
Merged
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
7 changes: 7 additions & 0 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ func (b SelectBuilder) Columns(columns ...string) SelectBuilder {
return builder.Extend(b, "Columns", parts).(SelectBuilder)
}

// RemoveColumns remove all columns from query.
// Must add a new column with Column or Columns methods, otherwise
// return a error.
func (b SelectBuilder) RemoveColumns() SelectBuilder {
return builder.Delete(b, "Columns").(SelectBuilder)
}

// 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
10 changes: 10 additions & 0 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,13 @@ func ExampleSelectBuilder_ToSql() {
// scan...
}
}

func TestRemoveColumns(t *testing.T) {
query := Select("id").
From("users").
RemoveColumns()
query = query.Columns("name")
sql, _, err := query.ToSql()
assert.NoError(t, err)
assert.Equal(t, "SELECT name FROM users", sql)
}