Skip to content
Merged
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
18 changes: 1 addition & 17 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,23 +521,7 @@ func (m *DbMap) Insert(ctx context.Context, list ...interface{}) error {
// Returns an error if SetKeys has not been called on the TableMap
// Panics if any interface in the list has not been registered with AddTable
func (m *DbMap) Update(ctx context.Context, list ...interface{}) (int64, error) {
return update(ctx, m, m, nil, list...)
}

// UpdateColumns runs a SQL UPDATE statement for each element in list. List
// items must be pointers.
//
// Only the columns accepted by filter are included in the UPDATE.
//
// The hook functions PreUpdate() and/or PostUpdate() will be executed
// before/after the UPDATE statement if the interface defines them.
//
// Returns the number of rows updated.
//
// Returns an error if SetKeys has not been called on the TableMap
// Panics if any interface in the list has not been registered with AddTable
func (m *DbMap) UpdateColumns(ctx context.Context, filter ColumnFilter, list ...interface{}) (int64, error) {
return update(ctx, m, m, filter, list...)
return update(ctx, m, m, list...)
}

// Delete runs a SQL DELETE statement for each element in list. List
Expand Down
4 changes: 2 additions & 2 deletions gorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ func delete(ctx context.Context, m *DbMap, exec SqlExecutor, list ...interface{}
return count, nil
}

func update(ctx context.Context, m *DbMap, exec SqlExecutor, colFilter ColumnFilter, list ...interface{}) (int64, error) {
func update(ctx context.Context, m *DbMap, exec SqlExecutor, list ...interface{}) (int64, error) {
count := int64(0)
for _, ptr := range list {
table, elem, err := m.tableForPointer(ptr, true)
Expand All @@ -474,7 +474,7 @@ func update(ctx context.Context, m *DbMap, exec SqlExecutor, colFilter ColumnFil
}
}

bi, err := table.bindUpdate(elem, colFilter)
bi, err := table.bindUpdate(elem)
if err != nil {
return -1, err
}
Expand Down
31 changes: 0 additions & 31 deletions gorp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1700,29 +1700,6 @@ func TestWithIgnoredColumn(t *testing.T) {
}
}

func TestColumnFilter(t *testing.T) {
dbmap := initDBMap(t)
defer dropAndClose(dbmap)

inv1 := &Invoice{0, 100, 200, "a", 0, false}
_insert(dbmap, inv1)

inv1.Memo = "c"
inv1.IsPaid = true
_updateColumns(dbmap, func(col *borp.ColumnMap) bool {
return col.ColumnName == "Memo"
}, 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 shouldn't have been updated")
}
}

func TestTypeConversionDBMapExample(t *testing.T) {
dbmap := initDBMap(t)
defer dropAndClose(dbmap)
Expand Down Expand Up @@ -2971,14 +2948,6 @@ func _update(dbmap *borp.DbMap, list ...interface{}) int64 {
return count
}

func _updateColumns(dbmap *borp.DbMap, filter borp.ColumnFilter, list ...interface{}) int64 {
count, err := dbmap.UpdateColumns(context.Background(), filter, list...)
if err != nil {
panic(err)
}
return count
}

func _del(dbmap *borp.DbMap, list ...interface{}) int64 {
count, err := dbmap.Delete(context.Background(), list...)
if err != nil {
Expand Down
15 changes: 2 additions & 13 deletions table_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ type CustomScanner struct {
Binder func(holder interface{}, target interface{}) error
}

// Used to filter columns when selectively updating
type ColumnFilter func(*ColumnMap) bool

func acceptAllFilter(col *ColumnMap) bool {
return true
}

// Bind is called automatically by gorp after Scan()
func (me CustomScanner) Bind() error {
return me.Binder(me.Holder, me.Target)
Expand Down Expand Up @@ -161,11 +154,7 @@ func (t *TableMap) bindInsert(elem reflect.Value) (bindInstance, error) {
return plan.createBindInstance(elem, t.dbmap.TypeConverter)
}

func (t *TableMap) bindUpdate(elem reflect.Value, colFilter ColumnFilter) (bindInstance, error) {
if colFilter == nil {
colFilter = acceptAllFilter
}

func (t *TableMap) bindUpdate(elem reflect.Value) (bindInstance, error) {
plan := &t.updatePlan
plan.once.Do(func() {
s := bytes.Buffer{}
Expand All @@ -174,7 +163,7 @@ func (t *TableMap) bindUpdate(elem reflect.Value, colFilter ColumnFilter) (bindI

for y := range t.Columns {
col := t.Columns[y]
if !col.isAutoIncr && !col.Transient && colFilter(col) {
if !col.isAutoIncr && !col.Transient {
if x > 0 {
s.WriteString(", ")
}
Expand Down
7 changes: 1 addition & 6 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ func (t *Transaction) Insert(ctx context.Context, list ...interface{}) error {

// Update had the same behavior as DbMap.Update(), but runs in a transaction.
func (t *Transaction) Update(ctx context.Context, list ...interface{}) (int64, error) {
return update(ctx, t.dbmap, t, nil, list...)
}

// UpdateColumns had the same behavior as DbMap.UpdateColumns(), but runs in a transaction.
func (t *Transaction) UpdateColumns(ctx context.Context, filter ColumnFilter, list ...interface{}) (int64, error) {
return update(ctx, t.dbmap, t, filter, list...)
return update(ctx, t.dbmap, t, list...)
}

// Delete has the same behavior as DbMap.Delete(), but runs in a transaction.
Expand Down
Loading