Skip to content

Commit

Permalink
* added Tx to DbMap
Browse files Browse the repository at this point in the history
* added logic in createTables() to switch between Tx and DbMap for the Exec
  • Loading branch information
mattc58 committed Apr 11, 2014
1 parent f296a21 commit 25cbcab
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion gorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ type DbMap struct {
// Db handle to use with this map
Db *sql.DB

// Transaction handle to use with this map
Tx *sql.Tx

// Dialect implementation to use with this map
Dialect Dialect

Expand Down Expand Up @@ -811,7 +814,17 @@ func (m *DbMap) createTables(ifNotExists bool) error {
s.WriteString(") ")
s.WriteString(m.Dialect.CreateTableSuffix())
s.WriteString(";")
_, err = m.Exec(s.String())

// use the transaction if it's there. otherwise, use the db connection.
if m.Tx != nil {
fmt.Println("Using the Transaction")
_, err = m.Tx.Exec(s.String())
}
else {
fmt.Println("Using the database")
_, err = m.Exec(s.String())
}

if err != nil {
break
}
Expand Down

0 comments on commit 25cbcab

Please sign in to comment.