Skip to content

Commit df374c1

Browse files
Carlos Villelalann
Carlos Villela
authored andcommitted
Move Context-related methods to 1.8+-specific files (with build tags)
1 parent e388911 commit df374c1

9 files changed

+201
-147
lines changed

select.go

-45
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"fmt"
77
"strings"
88

9-
"context"
10-
119
"github.com/lann/builder"
1210
)
1311

@@ -53,31 +51,6 @@ func (d *selectData) QueryRow() RowScanner {
5351
return QueryRowWith(queryRower, d)
5452
}
5553

56-
func (d *selectData) ExecContext(ctx context.Context) (sql.Result, error) {
57-
if d.RunWith == nil {
58-
return nil, RunnerNotSet
59-
}
60-
return ExecContextWith(ctx, d.RunWith, d)
61-
}
62-
63-
func (d *selectData) QueryContext(ctx context.Context) (*sql.Rows, error) {
64-
if d.RunWith == nil {
65-
return nil, RunnerNotSet
66-
}
67-
return QueryContextWith(ctx, d.RunWith, d)
68-
}
69-
70-
func (d *selectData) QueryRowContext(ctx context.Context) RowScanner {
71-
if d.RunWith == nil {
72-
return &Row{err: RunnerNotSet}
73-
}
74-
queryRower, ok := d.RunWith.(QueryRower)
75-
if !ok {
76-
return &Row{err: RunnerNotQueryRunner}
77-
}
78-
return QueryRowContextWith(ctx, queryRower, d)
79-
}
80-
8154
func (d *selectData) ToSql() (sqlStr string, args []interface{}, err error) {
8255
if len(d.Columns) == 0 {
8356
err = fmt.Errorf("select statements must have at least one result column")
@@ -208,24 +181,6 @@ func (b SelectBuilder) QueryRow() RowScanner {
208181
return data.QueryRow()
209182
}
210183

211-
// ExecContext builds and ExecContexts the query with the Runner set by RunWith.
212-
func (b SelectBuilder) ExecContext(ctx context.Context) (sql.Result, error) {
213-
data := builder.GetStruct(b).(selectData)
214-
return data.ExecContext(ctx)
215-
}
216-
217-
// QueryContext builds and QueryContexts the query with the Runner set by RunWith.
218-
func (b SelectBuilder) QueryContext(ctx context.Context) (*sql.Rows, error) {
219-
data := builder.GetStruct(b).(selectData)
220-
return data.QueryContext(ctx)
221-
}
222-
223-
// QueryRowContext builds and QueryRowContexts the query with the Runner set by RunWith.
224-
func (b SelectBuilder) QueryRowContext(ctx context.Context) RowScanner {
225-
data := builder.GetStruct(b).(selectData)
226-
return data.QueryRowContext(ctx)
227-
}
228-
229184
// Scan is a shortcut for QueryRow().Scan.
230185
func (b SelectBuilder) Scan(dest ...interface{}) error {
231186
return b.QueryRow().Scan(dest...)

select_go18.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// +build go1.8
2+
3+
package squirrel
4+
5+
import (
6+
"context"
7+
"database/sql"
8+
9+
"github.com/lann/builder"
10+
)
11+
12+
func (d *selectData) ExecContext(ctx context.Context) (sql.Result, error) {
13+
if d.RunWith == nil {
14+
return nil, RunnerNotSet
15+
}
16+
return ExecContextWith(ctx, d.RunWith, d)
17+
}
18+
19+
func (d *selectData) QueryContext(ctx context.Context) (*sql.Rows, error) {
20+
if d.RunWith == nil {
21+
return nil, RunnerNotSet
22+
}
23+
return QueryContextWith(ctx, d.RunWith, d)
24+
}
25+
26+
func (d *selectData) QueryRowContext(ctx context.Context) RowScanner {
27+
if d.RunWith == nil {
28+
return &Row{err: RunnerNotSet}
29+
}
30+
queryRower, ok := d.RunWith.(QueryRower)
31+
if !ok {
32+
return &Row{err: RunnerNotQueryRunner}
33+
}
34+
return QueryRowContextWith(ctx, queryRower, d)
35+
}
36+
37+
// ExecContext builds and ExecContexts the query with the Runner set by RunWith.
38+
func (b SelectBuilder) ExecContext(ctx context.Context) (sql.Result, error) {
39+
data := builder.GetStruct(b).(selectData)
40+
return data.ExecContext(ctx)
41+
}
42+
43+
// QueryContext builds and QueryContexts the query with the Runner set by RunWith.
44+
func (b SelectBuilder) QueryContext(ctx context.Context) (*sql.Rows, error) {
45+
data := builder.GetStruct(b).(selectData)
46+
return data.QueryContext(ctx)
47+
}
48+
49+
// QueryRowContext builds and QueryRowContexts the query with the Runner set by RunWith.
50+
func (b SelectBuilder) QueryRowContext(ctx context.Context) RowScanner {
51+
data := builder.GetStruct(b).(selectData)
52+
return data.QueryRowContext(ctx)
53+
}

squirrel.go

-57
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package squirrel
55

66
import (
77
"bytes"
8-
"context"
98
"database/sql"
109
"fmt"
1110
"strings"
@@ -21,30 +20,6 @@ type Sqlizer interface {
2120
ToSql() (string, []interface{}, error)
2221
}
2322

24-
// Execer is the interface that wraps the Exec method.
25-
//
26-
// Exec executes the given query as implemented by database/sql.Exec.
27-
type Execer interface {
28-
Exec(query string, args ...interface{}) (sql.Result, error)
29-
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
30-
}
31-
32-
// Queryer is the interface that wraps the Query method.
33-
//
34-
// Query executes the given query as implemented by database/sql.Query.
35-
type Queryer interface {
36-
Query(query string, args ...interface{}) (*sql.Rows, error)
37-
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
38-
}
39-
40-
// QueryRower is the interface that wraps the QueryRow method.
41-
//
42-
// QueryRow executes the given query as implemented by database/sql.QueryRow.
43-
type QueryRower interface {
44-
QueryRow(query string, args ...interface{}) RowScanner
45-
QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner
46-
}
47-
4823
// BaseRunner groups the Execer and Queryer interfaces.
4924
type BaseRunner interface {
5025
Execer
@@ -67,10 +42,6 @@ func (r *dbRunner) QueryRow(query string, args ...interface{}) RowScanner {
6742
return r.DB.QueryRow(query, args...)
6843
}
6944

70-
func (r *dbRunner) QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner {
71-
return r.DB.QueryRowContext(ctx, query, args...)
72-
}
73-
7445
type txRunner struct {
7546
*sql.Tx
7647
}
@@ -79,10 +50,6 @@ func (r *txRunner) QueryRow(query string, args ...interface{}) RowScanner {
7950
return r.Tx.QueryRow(query, args...)
8051
}
8152

82-
func (r *txRunner) QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner {
83-
return r.Tx.QueryRowContext(ctx, query, args...)
84-
}
85-
8653
func setRunWith(b interface{}, baseRunner BaseRunner) interface{} {
8754
var runner Runner
8855
switch r := baseRunner.(type) {
@@ -126,30 +93,6 @@ func QueryRowWith(db QueryRower, s Sqlizer) RowScanner {
12693
return &Row{RowScanner: db.QueryRow(query, args...), err: err}
12794
}
12895

129-
// ExecContextWith ExecContexts the SQL returned by s with db.
130-
func ExecContextWith(ctx context.Context, db Execer, s Sqlizer) (res sql.Result, err error) {
131-
query, args, err := s.ToSql()
132-
if err != nil {
133-
return
134-
}
135-
return db.ExecContext(ctx, query, args...)
136-
}
137-
138-
// QueryContextWith QueryContexts the SQL returned by s with db.
139-
func QueryContextWith(ctx context.Context, db Queryer, s Sqlizer) (rows *sql.Rows, err error) {
140-
query, args, err := s.ToSql()
141-
if err != nil {
142-
return
143-
}
144-
return db.QueryContext(ctx, query, args...)
145-
}
146-
147-
// QueryRowContextWith QueryRowContexts the SQL returned by s with db.
148-
func QueryRowContextWith(ctx context.Context, db QueryRower, s Sqlizer) RowScanner {
149-
query, args, err := s.ToSql()
150-
return &Row{RowScanner: db.QueryRowContext(ctx, query, args...), err: err}
151-
}
152-
15396
// DebugSqlizer calls ToSql on s and shows the approximate SQL to be executed
15497
//
15598
// If ToSql returns an error, the result of this method will look like:

squirrel_go18.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// +build go1.8
2+
3+
package squirrel
4+
5+
import (
6+
"context"
7+
"database/sql"
8+
)
9+
10+
// Execer is the interface that wraps the Exec method.
11+
//
12+
// Exec executes the given query as implemented by database/sql.Exec.
13+
type Execer interface {
14+
Exec(query string, args ...interface{}) (sql.Result, error)
15+
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
16+
}
17+
18+
// Queryer is the interface that wraps the Query method.
19+
//
20+
// Query executes the given query as implemented by database/sql.Query.
21+
type Queryer interface {
22+
Query(query string, args ...interface{}) (*sql.Rows, error)
23+
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
24+
}
25+
26+
// QueryRower is the interface that wraps the QueryRow method.
27+
//
28+
// QueryRow executes the given query as implemented by database/sql.QueryRow.
29+
type QueryRower interface {
30+
QueryRow(query string, args ...interface{}) RowScanner
31+
QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner
32+
}
33+
34+
func (r *dbRunner) QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner {
35+
return r.DB.QueryRowContext(ctx, query, args...)
36+
}
37+
38+
func (r *txRunner) QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner {
39+
return r.Tx.QueryRowContext(ctx, query, args...)
40+
}
41+
42+
// ExecContextWith ExecContexts the SQL returned by s with db.
43+
func ExecContextWith(ctx context.Context, db Execer, s Sqlizer) (res sql.Result, err error) {
44+
query, args, err := s.ToSql()
45+
if err != nil {
46+
return
47+
}
48+
return db.ExecContext(ctx, query, args...)
49+
}
50+
51+
// QueryContextWith QueryContexts the SQL returned by s with db.
52+
func QueryContextWith(ctx context.Context, db Queryer, s Sqlizer) (rows *sql.Rows, err error) {
53+
query, args, err := s.ToSql()
54+
if err != nil {
55+
return
56+
}
57+
return db.QueryContext(ctx, query, args...)
58+
}
59+
60+
// QueryRowContextWith QueryRowContexts the SQL returned by s with db.
61+
func QueryRowContextWith(ctx context.Context, db QueryRower, s Sqlizer) RowScanner {
62+
query, args, err := s.ToSql()
63+
return &Row{RowScanner: db.QueryRowContext(ctx, query, args...), err: err}
64+
}

squirrel_go18_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// +build go1.8
2+
3+
package squirrel
4+
5+
import (
6+
"context"
7+
"database/sql"
8+
)
9+
10+
func (s *DBStub) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
11+
s.LastExecSql = query
12+
s.LastExecArgs = args
13+
return nil, nil
14+
}
15+
16+
func (s *DBStub) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
17+
s.LastQuerySql = query
18+
s.LastQueryArgs = args
19+
return nil, nil
20+
}
21+
22+
func (s *DBStub) QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner {
23+
s.LastQueryRowSql = query
24+
s.LastQueryRowArgs = args
25+
return &Row{RowScanner: &RowStub{}}
26+
}

squirrel_oldgo.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// +build !go1.8
2+
3+
package squirrel
4+
5+
import "database/sql"
6+
7+
// Execer is the interface that wraps the Exec method.
8+
//
9+
// Exec executes the given query as implemented by database/sql.Exec.
10+
type Execer interface {
11+
Exec(query string, args ...interface{}) (sql.Result, error)
12+
}
13+
14+
// Queryer is the interface that wraps the Query method.
15+
//
16+
// Query executes the given query as implemented by database/sql.Query.
17+
type Queryer interface {
18+
Query(query string, args ...interface{}) (*sql.Rows, error)
19+
}
20+
21+
// QueryRower is the interface that wraps the QueryRow method.
22+
//
23+
// QueryRow executes the given query as implemented by database/sql.QueryRow.
24+
type QueryRower interface {
25+
QueryRow(query string, args ...interface{}) RowScanner
26+
}

squirrel_test.go

-20
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"strings"
77
"testing"
88

9-
"context"
10-
119
"github.com/stretchr/testify/assert"
1210
)
1311

@@ -53,24 +51,6 @@ func (s *DBStub) QueryRow(query string, args ...interface{}) RowScanner {
5351
return &Row{RowScanner: &RowStub{}}
5452
}
5553

56-
func (s *DBStub) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
57-
s.LastExecSql = query
58-
s.LastExecArgs = args
59-
return nil, nil
60-
}
61-
62-
func (s *DBStub) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
63-
s.LastQuerySql = query
64-
s.LastQueryArgs = args
65-
return nil, nil
66-
}
67-
68-
func (s *DBStub) QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner {
69-
s.LastQueryRowSql = query
70-
s.LastQueryRowArgs = args
71-
return &Row{RowScanner: &RowStub{}}
72-
}
73-
7454
var sqlizer = Select("test")
7555
var sqlStr = "SELECT test"
7656

0 commit comments

Comments
 (0)