refs #164: new API SelectMore to add more cols to SELECT after calling Select()

This commit is contained in:
Huan Du
2024-09-09 02:05:14 +08:00
parent cd1b266513
commit 03e80e6f8c
2 changed files with 18 additions and 0 deletions
+7
View File
@@ -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
+11
View File
@@ -6,6 +6,9 @@ package sqlbuilder
import (
"database/sql"
"fmt"
"testing"
"github.com/huandu/go-assert"
)
func ExampleSelect() {
@@ -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 */")
}