Skip to content

Commit

Permalink
doc: refer to database/sql via the 'sql' shortcut
Browse files Browse the repository at this point in the history
In stmt.go, database/sql wasn't imported. So I had to do a fake variable
declaration jsut to make a reference to one database/sql symbol to have
an import not cleared by goimports.

Also, fix '*' being external to the brackets in godoc:

   // *[database/sql.Rows] -> [*sql.Rows]
  • Loading branch information
dolmen committed Nov 4, 2023
1 parent 124cec8 commit 6973dcc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
6 changes: 3 additions & 3 deletions scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"reflect"
)

// Scan allows to define a function that will scan one row from an *[database/sql.Rows].
// Scan allows to define a function that will scan one row from an [*sql.Rows].
//
// The signature of the function defines how the column values are retrieved into variables.
// Two styles are available:
// - as pointer variables (like [database/sql.Rows.Scan]): func (rows *sql.Rows, pval1 *int, pval2 *string) error
// - as pointer variables (like [sql.Rows.Scan]): func (rows *sql.Rows, pval1 *int, pval2 *string) error
// - as returned values (implies copies): func (rows *sql.Rows) (val1 int, val2 string, err error)
func Scan(fnPtr interface{}) {
vPtr := reflect.ValueOf(fnPtr)
Expand Down Expand Up @@ -94,7 +94,7 @@ func Scan(fnPtr interface{}) {
vPtr.Elem().Set(reflect.MakeFunc(fnType, fn))
}

// ForEach iterates an *[database/sql.Rows], scans the values of the row and calls the given callback function with the values.
// ForEach iterates an [*sql.Rows], scans the values of the row and calls the given callback function with the values.
//
// The callback receives the scanned columns values as arguments and may return an error or a bool (false) to stop iterating.
//
Expand Down
27 changes: 15 additions & 12 deletions stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ package sqlfunc

import (
"context"
"database/sql"
"reflect"
)

// Exec prepares an SQL statement and creates a function wrapping [database/sql.Stmt.ExecContext].
var _ *sql.DB // Fake var just to have database/sql imported for go doc

// Exec prepares an SQL statement and creates a function wrapping [sql.Stmt.ExecContext].
//
// fnPtr is a pointer to a func variable. The function signature tells how it will be called.
//
// The first argument is a [context.Context].
// If a *[database/sql.Tx] is given as the second argument, the statement will be localized to the transaction (using [database/sql.Tx.StmtContext]).
// The following arguments will be given as arguments to [database/sql.Stmt.ExecContext].
// If a [*sql.Tx] is given as the second argument, the statement will be localized to the transaction (using [sql.Tx.StmtContext]).
// The following arguments will be given as arguments to [sql.Stmt.ExecContext].
//
// The function will return an [database/sql.Result] and an error.
// The function will return an [sql.Result] and an error.
//
// The returned func 'close' must be called once the statement is not needed anymore.
//
Expand Down Expand Up @@ -109,15 +112,15 @@ func Exec(ctx context.Context, db PrepareConn, query string, fnPtr interface{})
return stmt.Close, nil
}

// QueryRow prepares an SQL statement and creates a function wrapping [database/sql.Stmt.QueryRowContext] and [database/sql.Row.Scan].
// QueryRow prepares an SQL statement and creates a function wrapping [sql.Stmt.QueryRowContext] and [sql.Row.Scan].
//
// fnPtr is a pointer to a func variable. The function signature tells how it will be called.
//
// The first argument is a [context.Context].
// If a *[database/sql.Tx] is given as the second argument, the statement will be localized to the transaction (using [database/sql.Tx.StmtContext]).
// The following arguments will be given as arguments to [database/sql.Stmt.QueryRowContext].
// If a [*sql.Tx] is given as the second argument, the statement will be localized to the transaction (using [sql.Tx.StmtContext]).
// The following arguments will be given as arguments to [sql.Stmt.QueryRowContext].
//
// The function will return values scanned from the [database/sql.Row] and an error.
// The function will return values scanned from the [sql.Row] and an error.
//
// The returned func 'close' must be called once the statement is not needed anymore.
func QueryRow(ctx context.Context, db PrepareConn, query string, fnPtr interface{}) (close func() error, err error) {
Expand Down Expand Up @@ -188,15 +191,15 @@ func QueryRow(ctx context.Context, db PrepareConn, query string, fnPtr interface
return stmt.Close, nil
}

// Query prepares an SQL statement and creates a function wrapping [database/sql.Stmt.QueryContext].
// Query prepares an SQL statement and creates a function wrapping [sql.Stmt.QueryContext].
//
// fnPtr is a pointer to a func variable. The function signature tells how it will be called.
//
// The first argument is a [context.Context].
// If an *[database/sql.Tx] is given as the second argument, the statement will be localized to the transaction (using [database/sql.Tx.StmtContext]).
// The following arguments will be given as arguments to [database/sql.Stmt.QueryRowContext].
// If an [*sql.Tx] is given as the second argument, the statement will be localized to the transaction (using [sql.Tx.StmtContext]).
// The following arguments will be given as arguments to [sql.Stmt.QueryRowContext].
//
// The function will return an *[database/sql.Rows] and an error.
// The function will return an [*sql.Rows] and an error.
//
// The returned func 'close' must be called once the statement is not needed anymore.
func Query(ctx context.Context, db PrepareConn, query string, fnPtr interface{}) (close func() error, err error) {
Expand Down
4 changes: 2 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (
"reflect"
)

// PrepareConn is a subset of *[database/sql.DB], *[database/sql.Conn] or *[database/sql.Tx].
// PrepareConn is a subset of [*database/sql.DB], [*database/sql.Conn] or [*database/sql.Tx].
type PrepareConn interface {
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

// txStmt is a subset of *[database/sql.Tx].
// txStmt is a subset of [*database/sql.Tx].
type txStmt interface {
StmtContext(ctx context.Context, stmt *sql.Stmt) *sql.Stmt
}
Expand Down

0 comments on commit 6973dcc

Please sign in to comment.