Skip to content

Added NotDeleted helper method on SelectBuilder #2

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 6 additions & 1 deletion select.go
Original file line number Diff line number Diff line change
@@ -282,7 +282,8 @@ func (b SelectBuilder) RemoveColumns() 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:
// Column("IF(col IN ("+squirrel.Placeholders(3)+"), 1, 0) as col", 1, 2, 3)
//
// Column("IF(col IN ("+squirrel.Placeholders(3)+"), 1, 0) as col", 1, 2, 3)
func (b SelectBuilder) Column(column interface{}, args ...interface{}) SelectBuilder {
return builder.Append(b, "Columns", newPart(column, args...)).(SelectBuilder)
}
@@ -362,6 +363,10 @@ func (b SelectBuilder) Where(pred interface{}, args ...interface{}) SelectBuilde
return builder.Append(b, "WhereParts", newWherePart(pred, args...)).(SelectBuilder)
}

func (b SelectBuilder) NotDeleted() SelectBuilder {
return b.Where("deleted_at IS NULL")
}

// GroupBy adds GROUP BY expressions to the query.
func (b SelectBuilder) GroupBy(groupBys ...string) SelectBuilder {
return builder.Extend(b, "GroupBys", groupBys).(SelectBuilder)
9 changes: 9 additions & 0 deletions select_test.go
Original file line number Diff line number Diff line change
@@ -68,6 +68,15 @@ func TestAsOfSystemTime(t *testing.T) {
assert.Equal(t, expectedArgs, args)
}

func TestNotDeleted(t *testing.T) {
b := Select("test").Where(Eq{"f4": true}).NotDeleted()
query, args, _ := b.ToSql()
expectedSql := `SELECT test WHERE f4 = ? AND deleted_at IS NULL`
expectedArgs := []interface{}{true}
assert.Equal(t, expectedSql, query)
assert.Equal(t, expectedArgs, args)
}

func TestSelectBuilderFromSelect(t *testing.T) {
subQ := Select("c").From("d").Where(Eq{"i": 0})
b := Select("a", "b").FromSelect(subQ, "subq")