From 702e0643a3dc61ae5e6c5497dda30a91a1ea0c1f Mon Sep 17 00:00:00 2001 From: Chris Dueck Date: Thu, 28 Aug 2025 13:33:49 -0400 Subject: [PATCH] Add NamedQueryer interfaces to avoid unnecessary Execer dependency The NamedQuery funcs take Ext interface arguments which creates an unnecessary dependency on unused Execer methods. The newly added NamedQueryer interfaces narrow down the method set to only what is needed. This change should be backwards compatible since any type that would satisfy Ext also satisfies NamedQueryer. --- named.go | 4 ++-- named_context.go | 4 ++-- sqlx.go | 9 ++++++++- sqlx_context.go | 9 ++++++++- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/named.go b/named.go index 6ac44777..878fa238 100644 --- a/named.go +++ b/named.go @@ -436,9 +436,9 @@ func bindNamedMapper(bindType int, query string, arg interface{}, m *reflectx.Ma } // NamedQuery binds a named query and then runs Query on the result using the -// provided Ext (sqlx.Tx, sqlx.Db). It works with both structs and with +// provided NamedQueryer (sqlx.Tx, sqlx.Db). It works with both structs and with // map[string]interface{} types. -func NamedQuery(e Ext, query string, arg interface{}) (*Rows, error) { +func NamedQuery(e NamedQueryer, query string, arg interface{}) (*Rows, error) { q, args, err := bindNamedMapper(BindType(e.DriverName()), query, arg, mapperFor(e)) if err != nil { return nil, err diff --git a/named_context.go b/named_context.go index 9ad23f4e..f22d3752 100644 --- a/named_context.go +++ b/named_context.go @@ -111,9 +111,9 @@ func (n *NamedStmt) GetContext(ctx context.Context, dest interface{}, arg interf } // NamedQueryContext binds a named query and then runs Query on the result using the -// provided Ext (sqlx.Tx, sqlx.Db). It works with both structs and with +// provided NamedQueryerContext (sqlx.Tx, sqlx.Db). It works with both structs and with // map[string]interface{} types. -func NamedQueryContext(ctx context.Context, e ExtContext, query string, arg interface{}) (*Rows, error) { +func NamedQueryContext(ctx context.Context, e NamedQueryerContext, query string, arg interface{}) (*Rows, error) { q, args, err := bindNamedMapper(BindType(e.DriverName()), query, arg, mapperFor(e)) if err != nil { return nil, err diff --git a/sqlx.go b/sqlx.go index 8259a4fe..c6a60c2e 100644 --- a/sqlx.go +++ b/sqlx.go @@ -93,13 +93,20 @@ type binder interface { } // Ext is a union interface which can bind, query, and exec, used by -// NamedQuery and NamedExec. +// NamedExec. type Ext interface { binder Queryer Execer } +// NamedQueryer is a union interface which can bind and query, used by +// NamedQuery. +type NamedQueryer interface { + binder + Queryer +} + // Preparer is an interface used by Preparex. type Preparer interface { Prepare(query string) (*sql.Stmt, error) diff --git a/sqlx_context.go b/sqlx_context.go index 32621d56..83afb779 100644 --- a/sqlx_context.go +++ b/sqlx_context.go @@ -40,13 +40,20 @@ type ExecerContext interface { } // ExtContext is a union interface which can bind, query, and exec, with Context -// used by NamedQueryContext and NamedExecContext. +// used by NamedExecContext. type ExtContext interface { binder QueryerContext ExecerContext } +// NamedQueryerContext is a union interface which can bind and query, with Context +// used by NamedQueryContext. +type NamedQueryerContext interface { + binder + QueryerContext +} + // SelectContext executes a query using the provided Queryer, and StructScans // each row into dest, which must be a slice. If the slice elements are // scannable, then the result set must have only one column. Otherwise,