Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[update] bindplan sticked with a columnfilter #326

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (m *DbMap) AddTableWithNameAndSchema(i interface{}, schema string, name str
}
}

tmap := &TableMap{gotype: t, TableName: name, SchemaName: schema, dbmap: m}
tmap := NewTableMap(t, name, schema, m)
var primaryKey []*ColumnMap
tmap.Columns, primaryKey = m.readStructColumns(t)
m.tables = append(m.tables, tmap)
Expand Down
15 changes: 15 additions & 0 deletions gorp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,21 @@ func TestColumnFilter(t *testing.T) {
if inv2.IsPaid {
t.Error("IsPaid shouldn't have been updated")
}

// update isPaid field only
_updateColumns(dbmap, func(col *gorp.ColumnMap) bool {
return col.ColumnName == "IsPaid"
}, inv1)

inv2 = &Invoice{}
inv2 = _get(dbmap, inv2, inv1.Id).(*Invoice)
if inv2.Memo != "c" {
t.Errorf("Expected column to be updated (%#v)", inv2)
}
if !inv2.IsPaid {
t.Error("IsPaid should have been updated")
}

}

func TestTypeConversionExample(t *testing.T) {
Expand Down
17 changes: 15 additions & 2 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"fmt"
"reflect"
"strings"
"sync"
)

// TableMap represents a mapping between a Go struct and a database table
Expand All @@ -31,18 +32,30 @@ type TableMap struct {
uniqueTogether [][]string
version *ColumnMap
insertPlan bindPlan
updatePlan bindPlan
deletePlan bindPlan
getPlan bindPlan
dbmap *DbMap

updatePlan map[string]bindPlan
muForUpdate sync.Mutex
}

func NewTableMap(t reflect.Type, name string, schema string, dbmap *DbMap) *TableMap {
return &TableMap{
gotype: t,
TableName: name,
SchemaName: schema,
dbmap: dbmap,
updatePlan: map[string]bindPlan{},
}
}

// ResetSql removes cached insert/update/select/delete SQL strings
// associated with this TableMap. Call this if you've modified
// any column names or the table name itself.
func (t *TableMap) ResetSql() {
t.insertPlan = bindPlan{}
t.updatePlan = bindPlan{}
t.updatePlan = map[string]bindPlan{}
t.deletePlan = bindPlan{}
t.getPlan = bindPlan{}
}
Expand Down
25 changes: 24 additions & 1 deletion table_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"bytes"
"fmt"
"reflect"
"strings"
"sync"
)

Expand Down Expand Up @@ -167,13 +168,35 @@ func (t *TableMap) bindInsert(elem reflect.Value) (bindInstance, error) {

return plan.createBindInstance(elem, t.dbmap.TypeConverter)
}
func (t *TableMap) signatureForColumns(colFilter ColumnFilter) string {
var tokens []string
for y := range t.Columns {
col := t.Columns[y]
if colFilter(col) {
tokens = append(tokens, col.ColumnName)
}
}
return strings.Join(tokens, ",")
}

func (t *TableMap) bindUpdate(elem reflect.Value, colFilter ColumnFilter) (bindInstance, error) {
var key string
if colFilter == nil {
colFilter = acceptAllFilter
key = "default" // if we wants to update all columns, make it simple
} else {
key = t.signatureForColumns(colFilter)
}

t.muForUpdate.Lock()
_, ok := t.updatePlan[key]
if !ok {
t.updatePlan[key] = bindPlan{}
}
updatePlan := t.updatePlan[key]
t.muForUpdate.Unlock()

plan := &t.updatePlan
plan := &updatePlan
plan.once.Do(func() {
s := bytes.Buffer{}
s.WriteString(fmt.Sprintf("update %s set ", t.dbmap.Dialect.QuotedTableForQuery(t.SchemaName, t.TableName)))
Expand Down