Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add missing methods #941

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

TheSench
Copy link

@TheSench TheSench commented Aug 31, 2024

This PR adds some missing methods to flesh out interfaces and make DB and Tx have more of a shared interface.

This opens up the possibility of creating a common interface that represents either one so that functions can be written that execute queries without needing to know if they're being run within a transaction or not.

  • TODO: Add unit tests.
    I wasn't sure how the tests were organized for this project. If there's a specific test function you'd like them added to (or a new one), let me know.

📌 Long-term (future PR), I'd like to create an interface (or a few) that represent the remaining shared query-related methods, and then one interface that represents either a DB or Tx. These are the additional methods not represented in another interface right now:

  • Get(dest interface{}, query string, args ...interface{}) error
  • GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
  • MustExec(query string, args ...interface{}) sql.Result
  • MustExecContext(ctx context.Context, query string, args ...interface{}) sql.Result
  • NamedExec(query string, arg interface{}) (sql.Result, error)
  • NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)
  • NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)
  • NamedQueryContext(ctx context.Context, query string, arg interface{}) (*sqlx.Rows, error)
  • PrepareNamed(query string) (*sqlx.NamedStmt, error)
  • PrepareNamedContext(ctx context.Context, query string) (*sqlx.NamedStmt, error)
  • Preparex(query string) (*sqlx.Stmt, error)
  • PreparexContext(ctx context.Context, query string) (*sqlx.Stmt, error)
  • Select(dest interface{}, query string, args ...interface{}) error
  • SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error

Copy link
Author

@TheSench TheSench left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation notes:

@@ -77,6 +77,7 @@ type ColScanner interface {
type Queryer interface {
Query(query string, args ...interface{}) (*sql.Rows, error)
Queryx(query string, args ...interface{}) (*Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Adding a missing method to this interface.

We have Query/Queryx, and QueryRowx, but this method was missing.

@@ -26,6 +26,7 @@ func ConnectContext(ctx context.Context, driverName, dataSourceName string) (*DB
type QueryerContext interface {
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryxContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Adding a missing method to this interface.

We have QueryContext/QueryxContext, and QueryRowxContext, but this method was missing.

Comment on lines +359 to +361
func (tx *Tx) NamedQueryContext(ctx context.Context, query string, arg interface{}) (*Rows, error) {
return NamedQueryContext(ctx, tx, query, arg)
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

This method exists on DB, but was missing from Tx.

This is the only query-related method I found that is needed to allow these two types to be used interchangeably.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants