-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create Tx and Conn interfaces to allow prepared statements to b…
…e used in transactions (#40) * tx * fix test * comments * wrap connection * comments * statement doesn't need sqlDB instance * don't use any * return any statement * check RETURNING in prepared queries --------- Co-authored-by: Iman Tumorang <iman.tumorang@gmail.com>
- Loading branch information
1 parent
404cf52
commit 1f99231
Showing
5 changed files
with
296 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package dbresolver | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"strings" | ||
) | ||
|
||
// Conn is a *sql.Conn wrapper. | ||
// Its main purpose is to be able to return the internal Tx and Stmt interfaces. | ||
type Conn interface { | ||
Close() error | ||
BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error) | ||
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) | ||
PingContext(ctx context.Context) error | ||
PrepareContext(ctx context.Context, query string) (Stmt, error) | ||
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) | ||
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row | ||
Raw(f func(driverConn interface{}) error) (err error) | ||
} | ||
|
||
type conn struct { | ||
sourceDB *sql.DB | ||
conn *sql.Conn | ||
} | ||
|
||
func (c *conn) Close() error { | ||
return c.conn.Close() | ||
} | ||
|
||
func (c *conn) BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error) { | ||
stx, err := c.conn.BeginTx(ctx, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &tx{ | ||
sourceDB: c.sourceDB, | ||
tx: stx, | ||
}, nil | ||
} | ||
|
||
func (c *conn) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) { | ||
return c.conn.ExecContext(ctx, query, args...) | ||
} | ||
|
||
func (c *conn) PingContext(ctx context.Context) error { | ||
return c.conn.PingContext(ctx) | ||
} | ||
|
||
func (c *conn) PrepareContext(ctx context.Context, query string) (Stmt, error) { | ||
pstmt, err := c.conn.PrepareContext(ctx, query) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
_query := strings.ToUpper(query) | ||
writeFlag := strings.Contains(_query, "RETURNING") | ||
|
||
return newSingleDBStmt(c.sourceDB, pstmt, writeFlag), nil | ||
} | ||
|
||
func (c *conn) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) { | ||
return c.conn.QueryContext(ctx, query, args...) | ||
} | ||
|
||
func (c *conn) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row { | ||
return c.conn.QueryRowContext(ctx, query, args...) | ||
} | ||
|
||
func (c *conn) Raw(f func(driverConn interface{}) error) (err error) { | ||
return c.conn.Raw(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.