Add DB.CommonDB() instead of DB.Tx(), as discussed in the PR thread.

This commit is contained in:
Timothy Stranex 2014-03-17 12:08:44 +02:00
parent a336f51444
commit 42448cb5d6
2 changed files with 8 additions and 14 deletions

15
main.go
View File

@ -28,20 +28,15 @@ func Open(driver, source string) (DB, error) {
return db, err return db, err
} }
// Return the underlying sql.DB instance.
//
// If called inside a transaction, it will panic.
// Use Tx() instead in this case.
func (s *DB) DB() *sql.DB { func (s *DB) DB() *sql.DB {
return s.db.(*sql.DB) return s.db.(*sql.DB)
} }
// Return the underlying sql.Tx instance. // Return the underlying sql.DB or sql.Tx instance.
// // Use of this method is discouraged. It's mainly intended to allow
// If called outside of a transaction, it will panic. // coexistence with legacy non-GORM code.
// Use DB() instead in this case. func (s *DB) CommonDB() sqlCommon {
func (s *DB) Tx() *sql.Tx { return s.db
return s.db.(*sql.Tx)
} }
func (s *DB) Callback() *callback { func (s *DB) Callback() *callback {

View File

@ -1542,9 +1542,8 @@ func TestTransaction(t *testing.T) {
t.Errorf("Should find saved record, but got", err) t.Errorf("Should find saved record, but got", err)
} }
sql_tx := tx.Tx() // This shouldn't panic. if sql_tx, ok := tx.CommonDB().(*sql.Tx); !ok || sql_tx == nil {
if sql_tx == nil { t.Errorf("Should return the underlying sql.Tx")
t.Errorf("Should return the underlying sql.Tx, but got nil")
} }
tx.Rollback() tx.Rollback()