This commit is contained in:
parent
a377448de3
commit
187ed5987d
@ -66,3 +66,18 @@ func TestQueryWithRange(t *testing.T) {
|
|||||||
}
|
}
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUpdateQuery(t *testing.T) {
|
||||||
|
db := openTestDb(t)
|
||||||
|
func() {
|
||||||
|
tx := db.MustBegin()
|
||||||
|
defer tx.MustRollback()
|
||||||
|
tx.Query("insert into mytable(key, value) values ('lorem', 'bar')").MustExec()
|
||||||
|
tx.Query("update mytable set value = 'ipsum' where key = 'lorem'").MustExec()
|
||||||
|
tx.MustCommit()
|
||||||
|
}()
|
||||||
|
|
||||||
|
var value string
|
||||||
|
db.Query("select value from mytable where value = 'ipsum'").MustScanSingle(&value)
|
||||||
|
require.Equal(t, "ipsum", value)
|
||||||
|
}
|
||||||
|
@ -15,11 +15,26 @@ func (d *Db) Begin() (*Tx, error) {
|
|||||||
return &Tx{db: d}, nil
|
return &Tx{db: d}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Db) MustBegin() *Tx {
|
||||||
|
tx, err := d.Begin()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return tx
|
||||||
|
}
|
||||||
|
|
||||||
func (tx *Tx) Commit() error {
|
func (tx *Tx) Commit() error {
|
||||||
defer tx.unlock()
|
defer tx.unlock()
|
||||||
return tx.Query("COMMIT").Exec()
|
return tx.Query("COMMIT").Exec()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) MustCommit() {
|
||||||
|
err := tx.Commit()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (tx *Tx) Rollback() error {
|
func (tx *Tx) Rollback() error {
|
||||||
if tx.db == nil {
|
if tx.db == nil {
|
||||||
// The transaction was already commited
|
// The transaction was already commited
|
||||||
|
Loading…
x
Reference in New Issue
Block a user