Skip to content

Commit

Permalink
Make subsequent Limit calls override the initial limit
Browse files Browse the repository at this point in the history
  • Loading branch information
leporo committed Sep 26, 2019
1 parent 4955695 commit 8966d9a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ Note that if a subquery uses no arguments, it's more effective to add it as SQL
q.Close()
```

To select from sub-query pass an empty string to From and immediately call a SubQuery method:

```go
q := sqlf.Select("").
From("").
SubQuery(
"(", ") counted_news",
sqlf.From("news").
Select("id, section, header, score").
Select("row_number() OVER (PARTITION BY section ORDER BY score DESC) AS rating_in_section").
OrderBy("section, rating_in_section")).
Where("rating_in_section <= 5")
// ...
q.Close()
```

The query constructed by this example returns top 5 news in each section.

#### Unions

Use `Union` method to combine results of two queries:
Expand Down
2 changes: 1 addition & 1 deletion dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (b Dialect) DeleteFrom(tableName string) *Stmt {
}

// writePg function copies s into buf and replaces ? placeholders with $1, $2...
func writePg(argNo int64, s []byte, buf *bytebufferpool.ByteBuffer) (int64, error) {
func writePg(argNo int, s []byte, buf *bytebufferpool.ByteBuffer) (int, error) {
var err error
start := 0
// Iterate by runes
Expand Down
16 changes: 16 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,22 @@ func ExampleStmt_With() {
// WITH regional_sales AS (SELECT region, SUM(amount) AS total_sales FROM orders GROUP BY region), top_regions AS (SELECT region FROM regional_sales WHERE total_sales > (SELECT SUM(total_sales)/10 FROM regional_sales)) SELECT region, product, SUM(quantity) AS product_units, SUM(amount) AS product_sales FROM orders WHERE region IN (SELECT region FROM top_regions) GROUP BY region, product
}

func ExampleStmt_From() {
q := sqlf.Select("*").
From("").
SubQuery(
"(", ") counted_news",
sqlf.From("news").
Select("id, section, header, score").
Select("row_number() OVER (PARTITION BY section ORDER BY score DESC) AS rating_in_section").
OrderBy("section, rating_in_section")).
Where("rating_in_section <= 5")
fmt.Println(q.String())
q.Close()
// Output:
//SELECT * FROM (SELECT id, section, header, score, row_number() OVER (PARTITION BY section ORDER BY score DESC) AS rating_in_section FROM news ORDER BY section, rating_in_section) counted_news WHERE rating_in_section <= 5
}

func ExampleStmt_SubQuery() {
q := sqlf.From("orders o").
Select("date, region").
Expand Down
6 changes: 5 additions & 1 deletion stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func (q *Stmt) Clause(expr string, args ...interface{}) *Stmt {
// String method builds and returns an SQL statement.
func (q *Stmt) String() string {
if q.sql == nil {
var argNo int64 = 1
var argNo int = 1
// Build a query
buf := getBuffer()
q.sql = buf
Expand Down Expand Up @@ -666,6 +666,10 @@ loop:
case chunk.pos == pos:
// Do nothing if a clause is already there and no expressions are to be added
if expr == "" {
// See if arguments are to be updated
if argLen > 0 {
copy(q.args[len(q.args)-argTail-chunk.argLen:], args)
}
return i
}
// Write a separator
Expand Down
12 changes: 12 additions & 0 deletions stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,15 @@ func TestUnion(t *testing.T) {
defer q.Close()
assert.Equal(t, "SELECT id, status FROM tasks WHERE status = ? UNION SELECT id, status FROM tasks WHERE status = ?", q.String())
}

func TestLimit(t *testing.T) {
q := sqlf.From("items").
Select("id").
Where("id > ?", 42).
Limit(10).
Limit(11).
Limit(20)
defer q.Close()
assert.Equal(t, "SELECT id FROM items WHERE id > ? LIMIT ?", q.String())
assert.Equal(t, []interface{}{42, 20}, q.Args())
}

0 comments on commit 8966d9a

Please sign in to comment.