Skip to content

Commit

Permalink
refs #164: new API SelectMore to add more cols to SELECT after callin…
Browse files Browse the repository at this point in the history
…g Select()
  • Loading branch information
huandu committed Sep 8, 2024
1 parent 01acaab commit d6f429c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ func (sb *SelectBuilder) Select(col ...string) *SelectBuilder {
return sb
}

// SelectMore adds more columns in SELECT.
func (sb *SelectBuilder) SelectMore(col ...string) *SelectBuilder {
sb.selectCols = append(sb.selectCols, col...)
sb.marker = selectMarkerAfterSelect
return sb
}

// Distinct marks this SELECT as DISTINCT.
func (sb *SelectBuilder) Distinct() *SelectBuilder {
sb.distinct = true
Expand Down
11 changes: 11 additions & 0 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package sqlbuilder
import (
"database/sql"
"fmt"
"testing"

"github.com/huandu/go-assert"
)

func ExampleSelect() {
Expand Down Expand Up @@ -364,3 +367,11 @@ func ExampleSelectBuilder_With() {
// Output:
// WITH users AS (SELECT id, name FROM users WHERE prime IS NOT NULL), orders AS (SELECT id, user_id FROM orders) SELECT orders.id FROM orders JOIN users ON orders.user_id = users.id LIMIT 10
}

func TestSelectBuilderSelectMore(t *testing.T) {
a := assert.New(t)
sb := Select("id").SQL("/* first */").Where(
"name IS NOT NULL",
).SQL("/* second */").SelectMore("name").SQL("/* third */")
a.Equal(sb.String(), "SELECT id, name /* first */ /* third */ WHERE name IS NOT NULL /* second */")
}

0 comments on commit d6f429c

Please sign in to comment.