mirror of
https://github.com/jackc/pgx.git
synced 2025-04-27 13:14:32 +00:00
Extract TxOptions beginSQL
This commit is contained in:
parent
07c5b76a24
commit
e896e8c311
41
tx.go
41
tx.go
@ -48,6 +48,26 @@ type TxOptions struct {
|
|||||||
DeferrableMode TxDeferrableMode
|
DeferrableMode TxDeferrableMode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (txOptions *TxOptions) beginSQL() string {
|
||||||
|
if txOptions == nil {
|
||||||
|
return "begin"
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
buf.WriteString("begin")
|
||||||
|
if txOptions.IsoLevel != "" {
|
||||||
|
fmt.Fprintf(buf, " isolation level %s", txOptions.IsoLevel)
|
||||||
|
}
|
||||||
|
if txOptions.AccessMode != "" {
|
||||||
|
fmt.Fprintf(buf, " %s", txOptions.AccessMode)
|
||||||
|
}
|
||||||
|
if txOptions.DeferrableMode != "" {
|
||||||
|
fmt.Fprintf(buf, " %s", txOptions.DeferrableMode)
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
var ErrTxClosed = errors.New("tx is closed")
|
var ErrTxClosed = errors.New("tx is closed")
|
||||||
|
|
||||||
// ErrTxCommitRollback occurs when an error has occurred in a transaction and
|
// ErrTxCommitRollback occurs when an error has occurred in a transaction and
|
||||||
@ -65,26 +85,7 @@ func (c *Conn) Begin() (*Tx, error) {
|
|||||||
// mode. Unlike database/sql, the context only affects the begin command. i.e.
|
// mode. Unlike database/sql, the context only affects the begin command. i.e.
|
||||||
// there is no auto-rollback on context cancelation.
|
// there is no auto-rollback on context cancelation.
|
||||||
func (c *Conn) BeginEx(ctx context.Context, txOptions *TxOptions) (*Tx, error) {
|
func (c *Conn) BeginEx(ctx context.Context, txOptions *TxOptions) (*Tx, error) {
|
||||||
var beginSQL string
|
_, err := c.ExecEx(ctx, txOptions.beginSQL(), nil)
|
||||||
if txOptions == nil {
|
|
||||||
beginSQL = "begin"
|
|
||||||
} else {
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
buf.WriteString("begin")
|
|
||||||
if txOptions.IsoLevel != "" {
|
|
||||||
fmt.Fprintf(buf, " isolation level %s", txOptions.IsoLevel)
|
|
||||||
}
|
|
||||||
if txOptions.AccessMode != "" {
|
|
||||||
fmt.Fprintf(buf, " %s", txOptions.AccessMode)
|
|
||||||
}
|
|
||||||
if txOptions.DeferrableMode != "" {
|
|
||||||
fmt.Fprintf(buf, " %s", txOptions.DeferrableMode)
|
|
||||||
}
|
|
||||||
|
|
||||||
beginSQL = buf.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := c.ExecEx(ctx, beginSQL, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// begin should never fail unless there is an underlying connection issue or
|
// begin should never fail unless there is an underlying connection issue or
|
||||||
// a context timeout. In either case, the connection is possibly broken.
|
// a context timeout. In either case, the connection is possibly broken.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user