Skip to content

Commit

Permalink
Update gorp.go
Browse files Browse the repository at this point in the history
share tables between many dbmaps
  • Loading branch information
hpgood committed Mar 24, 2013
1 parent 4a31ec5 commit 9b1aa8f
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions gorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ type DbMap struct {

TypeConverter TypeConverter

tables []*TableMap
tables *[]*TableMap
logger *log.Logger
logPrefix string
}
Expand Down Expand Up @@ -576,10 +576,12 @@ func (m *DbMap) AddTableWithName(i interface{}, name string) *TableMap {
name = t.Name()
}

m.autoNewTables()

// check if we have a table for this type already
// if so, update the name and return the existing pointer
for i := range m.tables {
table := m.tables[i]
for i := range *m.tables {
table := (*m.tables)[i]
if table.gotype == t {
table.TableName = name
return table
Expand Down Expand Up @@ -608,7 +610,8 @@ func (m *DbMap) AddTableWithName(i interface{}, name string) *TableMap {
tmap.version = tmap.columns[len(tmap.columns)-1]
}
}
m.tables = append(m.tables, tmap)
m.autoNewTables()
*m.tables = append(*m.tables, tmap)

return tmap
}
Expand All @@ -620,8 +623,8 @@ func (m *DbMap) AddTableWithName(i interface{}, name string) *TableMap {
// and destroy the schema automatically.
func (m *DbMap) CreateTables() error {
var err error
for i := range m.tables {
table := m.tables[i]
for i := range *m.tables {
table := (*m.tables)[i]

s := bytes.Buffer{}
s.WriteString(fmt.Sprintf("create table %s (", m.Dialect.QuoteField(table.TableName)))
Expand Down Expand Up @@ -675,8 +678,8 @@ func (m *DbMap) CreateTables() error {
// executes "drop table" statements against the database for each.
func (m *DbMap) DropTables() error {
var err error
for i := range m.tables {
table := m.tables[i]
for i := range *m.tables {
table := (*m.tables)[i]
_, e := m.Exec(fmt.Sprintf("drop table %s;", m.Dialect.QuoteField(table.TableName)))
if e != nil {
err = e
Expand Down Expand Up @@ -822,8 +825,8 @@ func (m *DbMap) tableFor(t reflect.Type, checkPK bool) (*TableMap, error) {
}

func tableOrNil(m *DbMap, t reflect.Type) *TableMap {
for i := range m.tables {
table := m.tables[i]
for i := range *m.tables {
table := (*m.tables)[i]
if table.gotype == t {
return table
}
Expand Down Expand Up @@ -864,6 +867,16 @@ func (m *DbMap) trace(query string, args ...interface{}) {
}
}

func (m *DbMap) autoNewTables() {
if m.tables == nil {
m.tables = new([]*TableMap) //自动new
}
}

func (m *DbMap) SetTables(tab *[]*TableMap) {
m.tables = tab
}

///////////////

// Same behavior as DbMap.Insert(), but runs in a transaction
Expand Down

0 comments on commit 9b1aa8f

Please sign in to comment.