Skip to content

Commit

Permalink
feat: add remove select builder columns method (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatricPippi committed Sep 14, 2022
1 parent 9b18b54 commit 49f2683
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions select.go
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
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)
}

0 comments on commit 49f2683

Please sign in to comment.