Merge pull request #77 from tstranex/master

Add DB.Tx() method to provide access to the underlying sql.Tx instance.
This commit is contained in:
Jinzhu 2014-03-17 19:45:14 +08:00
commit 6d48c9357d
2 changed files with 11 additions and 0 deletions

View File

@ -32,6 +32,13 @@ func (s *DB) DB() *sql.DB {
return s.db.(*sql.DB) return s.db.(*sql.DB)
} }
// Return the underlying sql.DB or sql.Tx instance.
// Use of this method is discouraged. It's mainly intended to allow
// coexistence with legacy non-GORM code.
func (s *DB) CommonDB() sqlCommon {
return s.db
}
func (s *DB) Callback() *callback { func (s *DB) Callback() *callback {
s.parent.callback = s.parent.callback.clone() s.parent.callback = s.parent.callback.clone()
return s.parent.callback return s.parent.callback

View File

@ -1542,6 +1542,10 @@ func TestTransaction(t *testing.T) {
t.Errorf("Should find saved record, but got", err) t.Errorf("Should find saved record, but got", err)
} }
if sql_tx, ok := tx.CommonDB().(*sql.Tx); !ok || sql_tx == nil {
t.Errorf("Should return the underlying sql.Tx")
}
tx.Rollback() tx.Rollback()
if err := tx.First(&User{}, "name = ?", "transcation").Error; err == nil { if err := tx.First(&User{}, "name = ?", "transcation").Error; err == nil {