2
0

Add TxOptions support to stdlib

This commit is contained in:
Jack Christensen
2017-05-06 16:29:37 -05:00
parent ffae1b1345
commit 4cbefbb27e
2 changed files with 104 additions and 18 deletions
+22 -18
View File
@@ -170,16 +170,34 @@ func (c *Conn) Close() error {
}
func (c *Conn) Begin() (driver.Tx, error) {
return c.BeginTx(context.Background(), driver.TxOptions{})
}
func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
if !c.conn.IsAlive() {
return nil, driver.ErrBadConn
}
_, err := c.conn.Exec("begin")
if err != nil {
return nil, err
var pgxOpts pgx.TxOptions
switch sql.IsolationLevel(opts.Isolation) {
case sql.LevelDefault:
case sql.LevelReadUncommitted:
pgxOpts.IsoLevel = pgx.ReadUncommitted
case sql.LevelReadCommitted:
pgxOpts.IsoLevel = pgx.ReadCommitted
case sql.LevelSnapshot:
pgxOpts.IsoLevel = pgx.RepeatableRead
case sql.LevelSerializable:
pgxOpts.IsoLevel = pgx.Serializable
default:
return nil, fmt.Errorf("unsupported isolation: %v", opts.Isolation)
}
return &Tx{conn: c.conn}, nil
if opts.ReadOnly {
pgxOpts.AccessMode = pgx.ReadOnly
}
return c.conn.BeginEx(&pgxOpts)
}
func (c *Conn) Exec(query string, argsV []driver.Value) (driver.Result, error) {
@@ -389,17 +407,3 @@ func namedValueToInterface(argsV []driver.NamedValue) []interface{} {
}
return args
}
type Tx struct {
conn *pgx.Conn
}
func (t *Tx) Commit() error {
_, err := t.conn.Exec("commit")
return err
}
func (t *Tx) Rollback() error {
_, err := t.conn.Exec("rollback")
return err
}