I have that exact interface in all of my DB code, and I never use sql.Tx or
sql.DB directly. You don't need Go's libraries to adopt this at all, just
use your own.

On Wed, Jan 15, 2020 at 11:20 PM Mhd Shulhan <m.shul...@gmail.com> wrote:

> ## Problem
>
> At some point we have a function that receive an instance of database
> connection to query rows in specific table.
>
> Let's say that function F() accept DB that query table T.
>
> If function F() called with DB instance, it will query only rows that has
> been committed into database.
>
> IF function F() called with Tx instance, it will query all rows including
> the one that has not been committed yet into database.
>
> Since DB and Tx are different types, we will have two functions that
> almost have identical code,
>
> func F(db *sql.DB) (output int) {
>   q := `SELECT … FROM T WHERE …`
>   err := db.QueryRow(q).Scan(&output)
>   …
>   return output
> }
>
> func FWithTx(tx *sql.Tx)(output int) {
>   q := `SELECT … FROM T WHERE …`
>   err := tx.QueryRow(q).Scan(&output)
>   …
>   return output
> }
>
>
> ## Proposed solution
>
> Add an interface Session (the name is not fixed yet) to package sql with
> the following signature,
>
> type Session interface {
>     func Exec(query string, args ...interface{}) (Result, error)
>     func ExecContext(ctx context.Context, query string, args
> ...interface{}) (Result, error)
>     func Prepare(query string) (*Stmt, error)
>     func PrepareContext(ctx context.Context, query string) (*Stmt, error)
>     func Query(query string, args ...interface{}) (*Rows, error)
>     func QueryContext(ctx context.Context, query string, args
> ...interface{}) (*Rows, error)
>     func QueryRow(query string, args ...interface{}) *Row
>     func QueryRowContext(ctx context.Context, query string, args
> ...interface{}) *Row
> }
>
> Session interface is combination of DB and Tx that contains all identical
> methods.
>
>
> ## Rationale
>
> Without Session, user will have two functions that have the same code,
>
> By using Session, we can minimise duplicate code in the user level. for
> example using the previous problems definition the function F() become one,
>
> func F(session sql.Session)(output int) {
>   q := `SELECT … FROM T WHERE …`
>   err := session.QueryRow().Scan(&output)
>   …
>   return output
> }
>
>
> ## Discussion
>
> Any thought about this proposal?
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/C8AB29FF-7A85-445A-B09E-A0E7CB322A4C%40gmail.com
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CA%2Bv29LsuLd_-LHqi4ZGadOihnY%3D8r4bYMe3T6ssVaCWYpcHN3A%40mail.gmail.com.

Reply via email to