Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ycalansy committed Oct 12, 2022
1 parent 2cfc5b8 commit 0c7762f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions select.go
Expand Up @@ -326,6 +326,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
26 changes: 26 additions & 0 deletions select_test.go
Expand Up @@ -312,6 +312,22 @@ func TestCTEErrorBubblesUp(t *testing.T) {
assert.Error(t, err)
}

func TestSelectJoinClausePlaceholderNumbering(t *testing.T) {
subquery := Select("a").Where(Eq{"b": 2}).PlaceholderFormat(Dollar)

sql, args, err := Select("t1.a").
From("t1").
Where(Eq{"a": 1}).
JoinClause(subquery.Prefix("JOIN (").Suffix(") t2 ON (t1.a = t2.a)")).
PlaceholderFormat(Dollar).
FinalizeSql()
assert.NoError(t, err)

expectedSql := "SELECT t1.a FROM t1 JOIN ( SELECT a WHERE b = $1 ) t2 ON (t1.a = t2.a) WHERE a = $2"
assert.Equal(t, expectedSql, sql)
assert.Equal(t, []interface{}{2, 1}, args)
}

func ExampleSelect() {
Select("id", "created", "first_name").From("users") // ... continue building up your query

Expand Down Expand Up @@ -468,3 +484,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 0c7762f

Please sign in to comment.