Skip to content

Commit 49f2683

Browse files
authored
feat: add remove select builder columns method (#331)
1 parent 9b18b54 commit 49f2683

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

select.go

+7
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@ func (b SelectBuilder) Columns(columns ...string) SelectBuilder {
262262
return builder.Extend(b, "Columns", parts).(SelectBuilder)
263263
}
264264

265+
// RemoveColumns remove all columns from query.
266+
// Must add a new column with Column or Columns methods, otherwise
267+
// return a error.
268+
func (b SelectBuilder) RemoveColumns() SelectBuilder {
269+
return builder.Delete(b, "Columns").(SelectBuilder)
270+
}
271+
265272
// Column adds a result column to the query.
266273
// Unlike Columns, Column accepts args which will be bound to placeholders in
267274
// the columns string, for example:

select_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,13 @@ func ExampleSelectBuilder_ToSql() {
451451
// scan...
452452
}
453453
}
454+
455+
func TestRemoveColumns(t *testing.T) {
456+
query := Select("id").
457+
From("users").
458+
RemoveColumns()
459+
query = query.Columns("name")
460+
sql, _, err := query.ToSql()
461+
assert.NoError(t, err)
462+
assert.Equal(t, "SELECT name FROM users", sql)
463+
}

0 commit comments

Comments
 (0)