Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Rename "DB" to "Querier", which seems to be the standard name.
Browse files Browse the repository at this point in the history
See https://godoc.org/github.com/golang-sql/sqlexp#Querier

DB still used for now, but is deprecated.

Intention is to change Queries to use context in future, but this will break backwards compatibility.
  • Loading branch information
jjeffery committed Jun 30, 2018
1 parent a366276 commit c852ad5
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 26 deletions.
9 changes: 7 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"database/sql"
)

// The DB interface defines the SQL database access methods used by this package.
// The Querier interface defines the SQL database access methods used by this package.
//
// The *DB and *Tx types in the standard library package "database/sql"
// both implement this interface.
type DB interface {
type Querier interface {
// Exec executes a query without returning any rows.
// The args are for any placeholder parameters in the query.
Exec(query string, args ...interface{}) (sql.Result, error)
Expand All @@ -17,3 +17,8 @@ type DB interface {
// The args are for any placeholder parameters in the query.
Query(query string, args ...interface{}) (*sql.Rows, error)
}

// DB is the deprecated name for Querier.
//
// Deprecated: use Querier instead.
type DB = Querier
14 changes: 8 additions & 6 deletions private/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,14 @@ func Parse(filename string) (*Model, error) {
}

var dbTypeNames = map[string]bool{
"sqlr.DB": true,
"sqlrow.DB": true,
"sql.DB": true,
"sql.Tx": true,
"sqlx.DB": true,
"sqlx.Tx": true,
"sql.Querier": true,
"sqlexp.Querier": true,
"sqlr.Querier": true,
"sqlr.DB": true, // deprecated
"sql.DB": true,
"sql.Tx": true,
"sqlx.DB": true,
"sqlx.Tx": true,
}

func newQueryType(file *ast.File, ir *importResolver, typeSpec *ast.TypeSpec, structType *ast.StructType) (*QueryType, error) {
Expand Down
2 changes: 1 addition & 1 deletion private/codegen/testdata/test0.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Row0 struct {
}

type Row0Query struct {
db sqlr.DB `methods:"Get,Select,SelectRow,Insert,Update,Delete,Upsert"`
querier sqlr.Querier `methods:"Get,Select,SelectRow,Insert,Update,Delete,Upsert"`
schema *sqlr.Schema
rowType *Row0 `table:"xyz.rows" singular:"document" plural:"documents"`
}
16 changes: 8 additions & 8 deletions private/codegen/testdata/test0_sqlr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion private/codegen/testdata/test1.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Document struct {
}

type DocumentQuery struct {
db sqlr.DB
db sqlr.Querier
schema *sqlr.Schema
rowType *Document
}
Expand Down
2 changes: 1 addition & 1 deletion private/codegen/testdata/test2.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Row2 struct {
}

type Row2Query struct {
db sqlr.DB
db sqlr.Querier
schema *sqlr.Schema
rowType *Row2
}
2 changes: 1 addition & 1 deletion private/codegen/testdata/test3.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type Row3Query struct {
db sqlr.DB
db sqlr.Querier
schema *sqlr.Schema
rowType *rowtype.Row3
}
2 changes: 1 addition & 1 deletion private/codegen/testdata/test4.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type Row4Query struct {
db sqlr.DB
db sqlr.Querier
schema *sqlr.Schema
rowType *rowtype.Row4
}
4 changes: 2 additions & 2 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (s *Schema) Prepare(row interface{}, query string) (*Stmt, error) {
//
// Select returns the number of rows returned by the SELECT
// query.
func (s *Schema) Select(db DB, rows interface{}, sql string, args ...interface{}) (int, error) {
func (s *Schema) Select(db Querier, rows interface{}, sql string, args ...interface{}) (int, error) {
stmt, err := s.Prepare(rows, sql)
if err != nil {
return 0, err
Expand All @@ -155,7 +155,7 @@ func (s *Schema) Select(db DB, rows interface{}, sql string, args ...interface{}
// If the statement is an INSERT statement and the row has an auto-increment field,
// then the row is updated with the value of the auto-increment column, as long as
// the SQL driver supports this functionality.
func (s *Schema) Exec(db DB, row interface{}, sql string, args ...interface{}) (int, error) {
func (s *Schema) Exec(db Querier, row interface{}, sql string, args ...interface{}) (int, error) {
stmt, err := s.Prepare(row, sql)
if err != nil {
return 0, err
Expand Down
6 changes: 3 additions & 3 deletions stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (stmt *Stmt) String() string {
// If the statement is an INSERT statement and the row has an auto-increment field,
// then the row is updated with the value of the auto-increment column as long as
// the SQL driver supports this functionality.
func (stmt *Stmt) Exec(db DB, row interface{}, args ...interface{}) (int, error) {
func (stmt *Stmt) Exec(db Querier, row interface{}, args ...interface{}) (int, error) {
if stmt.queryType == querySelect {
return 0, errors.New("attempt to call Exec on select statement")
}
Expand Down Expand Up @@ -185,7 +185,7 @@ func (stmt *Stmt) Exec(db DB, row interface{}, args ...interface{}) (int, error)
// is a pointer to a struct then that struct is filled with the result of the first
// row returned by the query. In both cases Select returns the number of rows returned
// by the query.
func (stmt *Stmt) Select(db DB, rows interface{}, args ...interface{}) (int, error) {
func (stmt *Stmt) Select(db Querier, rows interface{}, args ...interface{}) (int, error) {
if rows == nil {
return 0, errors.New("nil pointer")
}
Expand Down Expand Up @@ -297,7 +297,7 @@ func (stmt *Stmt) Select(db DB, rows interface{}, args ...interface{}) (int, err

// TODO(jpj): need to merge the common code in Select and selectOne

func (stmt *Stmt) selectOne(db DB, dest interface{}, rowValue reflect.Value, args []interface{}) (int, error) {
func (stmt *Stmt) selectOne(db Querier, dest interface{}, rowValue reflect.Value, args []interface{}) (int, error) {
expandedQuery, expandedArgs, err := wherein.Expand(stmt.query, args)
if err != nil {
return 0, err
Expand Down

0 comments on commit c852ad5

Please sign in to comment.