Add TransationIso

fixes #5
This commit is contained in:
Jack Christensen 2013-07-13 19:58:44 -05:00
parent 98d4ce4c09
commit 00e3ec32ca
2 changed files with 37 additions and 1 deletions

View File

@ -416,7 +416,22 @@ func (c *Connection) Execute(sql string, arguments ...interface{}) (commandTag s
}
func (c *Connection) Transaction(f func() bool) (committed bool, err error) {
if _, err = c.Execute("begin"); err != nil {
return c.transaction("", f)
}
func (c *Connection) TransactionIso(isoLevel string, f func() bool) (committed bool, err error) {
return c.transaction(isoLevel, f)
}
func (c *Connection) transaction(isoLevel string, f func() bool) (committed bool, err error) {
var beginSql string
if isoLevel == "" {
beginSql = "begin"
} else {
beginSql = fmt.Sprintf("begin isolation level %s", isoLevel)
}
if _, err = c.Execute(beginSql); err != nil {
return
}
defer func() {

View File

@ -536,3 +536,24 @@ func TestTransaction(t *testing.T) {
}
}()
}
func TestTransactionIso(t *testing.T) {
conn, err := Connect(*defaultConnectionParameters)
if err != nil {
t.Fatalf("Unable to establish connection: %v", err)
}
defer conn.Close()
isoLevels := []string{"serializable", "repeatable read", "read committed", "read uncommitted"}
for _, iso := range isoLevels {
_, err := conn.TransactionIso(iso, func() bool {
if level := mustSelectValue(t, conn, "select current_setting('transaction_isolation')"); level != iso {
t.Errorf("Expected to be in isolation level %v but was %v", iso, level)
}
return true
})
if err != nil {
t.Fatalf("Unexpected transaction failure: %v", err)
}
}
}