Skip to content

Commit

Permalink
Reduce required methods of StdInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenafamo committed Dec 12, 2022
1 parent e5d6b7c commit 4ca1130
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ func (q commonQueryer[T]) QueryContext(ctx context.Context, query string, args .
// An interface that *sql.DB, *sql.Tx and *sql.Conn satisfy
type StdInterface interface {
StdQueryer
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

// New wraps an StdInterface to make it comply with Queryer
Expand All @@ -42,6 +40,13 @@ func New[T StdInterface](wrapped T) common[T] {
return common[T]{commonQueryer[T]{wrapped: wrapped}}
}

// To make sure it works
var (
_ Executor = common[*sql.DB]{}
_ Executor = common[*sql.Tx]{}
_ Executor = common[*sql.Conn]{}
)

type common[T StdInterface] struct {
commonQueryer[T]
}
Expand All @@ -51,18 +56,6 @@ func (q common[T]) ExecContext(ctx context.Context, query string, args ...any) (
return q.wrapped.ExecContext(ctx, query, args...)
}

// QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.
func (q common[T]) QueryRowContext(ctx context.Context, query string, args ...any) scan.Row {
return q.wrapped.QueryRowContext(ctx, query, args...)
}

// PrepareContext creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's Close method when the statement is no longer needed.
//
// The provided context is used for the preparation of the statement, not for the execution of the statement.
func (q common[T]) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
return q.wrapped.PrepareContext(ctx, query)
}

// Open works just like [sql.Open], but converts the returned [*sql.DB] to [DB]
func Open(driverName string, dataSource string) (DB, error) {
db, err := sql.Open(driverName, dataSource)
Expand Down

0 comments on commit 4ca1130

Please sign in to comment.