@@ -13,6 +13,7 @@ package gorp
1313
1414import (
1515 "bytes"
16+ "context"
1617 "database/sql"
1718 "database/sql/driver"
1819 "errors"
@@ -33,6 +34,8 @@ import (
3334// dbmap := &gorp.DbMap{Db: db, Dialect: dialect}
3435//
3536type DbMap struct {
37+ ctx context.Context
38+
3639 // Db handle to use with this map
3740 Db * sql.DB
3841
@@ -69,8 +72,14 @@ func (m *DbMap) dynamicTableMap() map[string]*TableMap {
6972 return m .tablesDynamic
7073}
7174
72- func (m * DbMap ) CreateIndex () error {
75+ func (m * DbMap ) WithContext (ctx context.Context ) SqlExecutor {
76+ copy := & DbMap {}
77+ * copy = * m
78+ copy .ctx = ctx
79+ return copy
80+ }
7381
82+ func (m * DbMap ) CreateIndex () error {
7483 var err error
7584 dialect := reflect .TypeOf (m .Dialect )
7685 for _ , table := range m .tables {
@@ -602,7 +611,7 @@ func (m *DbMap) Exec(query string, args ...interface{}) (sql.Result, error) {
602611 now := time .Now ()
603612 defer m .trace (now , query , args ... )
604613 }
605- return exec (m , query , args ... )
614+ return maybeExpandNamedQueryAndExec (m , query , args ... )
606615}
607616
608617// SelectInt is a convenience wrapper around the gorp.SelectInt function
@@ -646,11 +655,15 @@ func (m *DbMap) Begin() (*Transaction, error) {
646655 now := time .Now ()
647656 defer m .trace (now , "begin;" )
648657 }
649- tx , err := m . Db . Begin ( )
658+ tx , err := begin ( m )
650659 if err != nil {
651660 return nil , err
652661 }
653- return & Transaction {m , tx , false }, nil
662+ return & Transaction {
663+ dbmap : m ,
664+ tx : tx ,
665+ closed : false ,
666+ }, nil
654667}
655668
656669// TableFor returns the *TableMap corresponding to the given Go Type
@@ -698,7 +711,7 @@ func (m *DbMap) Prepare(query string) (*sql.Stmt, error) {
698711 now := time .Now ()
699712 defer m .trace (now , query , nil )
700713 }
701- return m . Db . Prepare ( query )
714+ return prepare ( m , query )
702715}
703716
704717func tableOrNil (m * DbMap , t reflect.Type , name string ) * TableMap {
@@ -751,15 +764,15 @@ func (m *DbMap) QueryRow(query string, args ...interface{}) *sql.Row {
751764 now := time .Now ()
752765 defer m .trace (now , query , args ... )
753766 }
754- return m . Db . QueryRow ( query , args ... )
767+ return queryRow ( m , query , args ... )
755768}
756769
757- func (m * DbMap ) Query (query string , args ... interface {}) (* sql.Rows , error ) {
770+ func (m * DbMap ) Query (q string , args ... interface {}) (* sql.Rows , error ) {
758771 if m .logger != nil {
759772 now := time .Now ()
760- defer m .trace (now , query , args ... )
773+ defer m .trace (now , q , args ... )
761774 }
762- return m . Db . Query ( query , args ... )
775+ return query ( m , q , args ... )
763776}
764777
765778func (m * DbMap ) trace (started time.Time , query string , args ... interface {}) {
0 commit comments