Skip to content

Commit 765486c

Browse files
committed
Support context
1 parent 09d8d45 commit 765486c

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

dgw.go

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33

44
import (
55
"bytes"
6+
"context"
67
"database/sql"
78
"fmt"
89
"go/format"
@@ -22,6 +23,9 @@ type Queryer interface {
2223
Exec(string, ...interface{}) (sql.Result, error)
2324
Query(string, ...interface{}) (*sql.Rows, error)
2425
QueryRow(string, ...interface{}) *sql.Row
26+
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
27+
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
28+
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
2529
}
2630

2731
// OpenDB opens database connection
@@ -39,6 +43,9 @@ type Queryer interface {
3943
Exec(string, ...interface{}) (sql.Result, error)
4044
Query(string, ...interface{}) (*sql.Rows, error)
4145
QueryRow(string, ...interface{}) *sql.Row
46+
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
47+
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
48+
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
4249
}
4350
`
4451

template/method.tmpl

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
// Create inserts the {{ .Struct.Name }} to the database.
22
func (r *{{ .Struct.Name }}) Create(db Queryer) error {
3+
return r.CreateContext(context.Background(), db)
4+
}
5+
6+
// Get{{ .Struct.Name }}ByPk select the {{ .Struct.Name }} from the database.
7+
func Get{{ .Struct.Name }}ByPk(db Queryer, {{ createSelectByPkFuncParams .Struct }}) (*{{ .Struct.Name }}, error) {
8+
return Get{{ .Struct.Name }}ByPkContext(context.Background(), db, {{ createSelectByPkSQLParams .Struct }})
9+
}
10+
11+
// CreateContext inserts the {{ .Struct.Name }} to the database.
12+
func (r *{{ .Struct.Name }}) CreateContext(ctx context.Context, db Queryer) error {
313
{{- if .Struct.Table.AutoGenPk }}
4-
err := db.QueryRow(
14+
err := db.QueryRowContext(ctx,
515
`{{ createInsertSQL .Struct }}`,
616
{{ createInsertParams .Struct }}).Scan({{ createInsertScan .Struct }})
717
{{- else }}
8-
_, err := db.Exec(
18+
_, err := db.ExecContext(ctx,
919
`{{ createInsertSQL .Struct }}`,
1020
{{ createInsertParams .Struct }})
1121
{{- end }}
@@ -15,10 +25,10 @@ func (r *{{ .Struct.Name }}) Create(db Queryer) error {
1525
return nil
1626
}
1727

18-
// Get{{ .Struct.Name }}ByPk select the {{ .Struct.Name }} from the database.
19-
func Get{{ .Struct.Name }}ByPk(db Queryer, {{ createSelectByPkFuncParams .Struct }}) (*{{ .Struct.Name }}, error) {
28+
// Get{{ .Struct.Name }}ByPkContext select the {{ .Struct.Name }} from the database.
29+
func Get{{ .Struct.Name }}ByPkContext(ctx context.Context, db Queryer, {{ createSelectByPkFuncParams .Struct }}) (*{{ .Struct.Name }}, error) {
2030
var r {{ .Struct.Name }}
21-
err := db.QueryRow(
31+
err := db.QueryRowContext(ctx,
2232
`{{ createSelectByPkSQL .Struct }}`,
2333
{{ createSelectByPkSQLParams .Struct }}).Scan({{ createSelectByPkScan .Struct }})
2434
if err != nil {

0 commit comments

Comments
 (0)