-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Generic *sqlx.DB or *sqlx.Tx? #344
Comments
@mewben This is consistent with *sql.Tx and *sql.DB in the standard library. Over in golang-nuts, @davecheney suggests defining your own interface. See: https://groups.google.com/forum/#!topic/golang-nuts/yLg6IWbwPPw |
@mewben I've achieved this by creating an interface that covers the sqlx methods I need and then creating two implementations that wrap sqlx.DB and sqlx.Tx respectively. Then your GetUser method can accept that interface instead of the concrete types. |
Hey, you need to create a common interface that is used by both type DB interface {
Querier
Begin() (Tx, error)
Close() error
}
type Tx interface {
Querier
Commit() error
Rollback() error
}
type Querier interface {
QueryRow(query string, args ...interface{}) Row
Select(dest interface{}, query string, args ...interface{}) error
NamedQuery(query string, arg interface{}) (Rows, error)
NamedExec(query string, arg interface{}) (sql.Result, error)
}
func GetUser(q *Querier, username string) {} But of course, it requires you to wrap sqlx :) |
Ahhh.. I get it now.. Thank you all... |
I'd still love to see this, bit of a pain to need yet-another layer. It'd be easy to argue it's not sqlx's concern, but sqlx exists to provide convenience so it seems fine to me. |
Thank you for your advice. I written a wrapper of *sqlx.DB and *sqlx.Tx in my open source microservice framework go-doudou. Here is the link: https://github.com/unionj-cloud/go-doudou/blob/main/ddl/wrapper.go. Hope to help others. |
Hi, is there a way I could accept a parameter which is either
sqlx.DB
orsqlx.Tx
?I have this function wherein I have to pass
*sqlx.DB
:There's no problem with this, until I create a new Transaction and inside that transaction, I need to call
GetUser
. So I ended up implementing 2 functions to provide the same result...Is there a way I can make this into 1 function? I tried this:
but is this okay? I wish there is something like this:
:)
The text was updated successfully, but these errors were encountered: