Skip to content

Commit 846a0d3

Browse files
authored
Remove UpdateColumns (#19)
This removes the unused `UpdateColumns` API and the related `ColumnFilter` plumbing. Boulder does not use this API, and removing it avoids preserving the cached filtered-update behavior reported in #15. The change deletes `DbMap.UpdateColumns`, `Transaction.UpdateColumns`, `ColumnFilter`, the internal filtered update path, and the column-filter test/helper. Fixed #15
1 parent 64290a7 commit 846a0d3

5 files changed

Lines changed: 6 additions & 69 deletions

File tree

db.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -529,23 +529,7 @@ func (m *DbMap) Insert(ctx context.Context, list ...interface{}) error {
529529
// Returns an error if SetKeys has not been called on the TableMap
530530
// Panics if any interface in the list has not been registered with AddTable
531531
func (m *DbMap) Update(ctx context.Context, list ...interface{}) (int64, error) {
532-
return update(ctx, m, m, nil, list...)
533-
}
534-
535-
// UpdateColumns runs a SQL UPDATE statement for each element in list. List
536-
// items must be pointers.
537-
//
538-
// Only the columns accepted by filter are included in the UPDATE.
539-
//
540-
// The hook functions PreUpdate() and/or PostUpdate() will be executed
541-
// before/after the UPDATE statement if the interface defines them.
542-
//
543-
// Returns the number of rows updated.
544-
//
545-
// Returns an error if SetKeys has not been called on the TableMap
546-
// Panics if any interface in the list has not been registered with AddTable
547-
func (m *DbMap) UpdateColumns(ctx context.Context, filter ColumnFilter, list ...interface{}) (int64, error) {
548-
return update(ctx, m, m, filter, list...)
532+
return update(ctx, m, m, list...)
549533
}
550534

551535
// Delete runs a SQL DELETE statement for each element in list. List

gorp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ func delete(ctx context.Context, m *DbMap, exec SqlExecutor, list ...interface{}
458458
return count, nil
459459
}
460460

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

477-
bi, err := table.bindUpdate(elem, colFilter)
477+
bi, err := table.bindUpdate(elem)
478478
if err != nil {
479479
return -1, err
480480
}

gorp_test.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,29 +1700,6 @@ func TestWithIgnoredColumn(t *testing.T) {
17001700
}
17011701
}
17021702

1703-
func TestColumnFilter(t *testing.T) {
1704-
dbmap := initDBMap(t)
1705-
defer dropAndClose(dbmap)
1706-
1707-
inv1 := &Invoice{0, 100, 200, "a", 0, false}
1708-
_insert(dbmap, inv1)
1709-
1710-
inv1.Memo = "c"
1711-
inv1.IsPaid = true
1712-
_updateColumns(dbmap, func(col *borp.ColumnMap) bool {
1713-
return col.ColumnName == "Memo"
1714-
}, inv1)
1715-
1716-
inv2 := &Invoice{}
1717-
inv2 = _get(dbmap, inv2, inv1.Id).(*Invoice)
1718-
if inv2.Memo != "c" {
1719-
t.Errorf("Expected column to be updated (%#v)", inv2)
1720-
}
1721-
if inv2.IsPaid {
1722-
t.Error("IsPaid shouldn't have been updated")
1723-
}
1724-
}
1725-
17261703
func TestTypeConversionDBMapExample(t *testing.T) {
17271704
dbmap := initDBMap(t)
17281705
defer dropAndClose(dbmap)
@@ -2971,14 +2948,6 @@ func _update(dbmap *borp.DbMap, list ...interface{}) int64 {
29712948
return count
29722949
}
29732950

2974-
func _updateColumns(dbmap *borp.DbMap, filter borp.ColumnFilter, list ...interface{}) int64 {
2975-
count, err := dbmap.UpdateColumns(context.Background(), filter, list...)
2976-
if err != nil {
2977-
panic(err)
2978-
}
2979-
return count
2980-
}
2981-
29822951
func _del(dbmap *borp.DbMap, list ...interface{}) int64 {
29832952
count, err := dbmap.Delete(context.Background(), list...)
29842953
if err != nil {

table_bindings.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ type CustomScanner struct {
2626
Binder func(holder interface{}, target interface{}) error
2727
}
2828

29-
// Used to filter columns when selectively updating
30-
type ColumnFilter func(*ColumnMap) bool
31-
32-
func acceptAllFilter(col *ColumnMap) bool {
33-
return true
34-
}
35-
3629
// Bind is called automatically by gorp after Scan()
3730
func (me CustomScanner) Bind() error {
3831
return me.Binder(me.Holder, me.Target)
@@ -161,11 +154,7 @@ func (t *TableMap) bindInsert(elem reflect.Value) (bindInstance, error) {
161154
return plan.createBindInstance(elem, t.dbmap.TypeConverter)
162155
}
163156

164-
func (t *TableMap) bindUpdate(elem reflect.Value, colFilter ColumnFilter) (bindInstance, error) {
165-
if colFilter == nil {
166-
colFilter = acceptAllFilter
167-
}
168-
157+
func (t *TableMap) bindUpdate(elem reflect.Value) (bindInstance, error) {
169158
plan := &t.updatePlan
170159
plan.once.Do(func() {
171160
s := bytes.Buffer{}
@@ -174,7 +163,7 @@ func (t *TableMap) bindUpdate(elem reflect.Value, colFilter ColumnFilter) (bindI
174163

175164
for y := range t.Columns {
176165
col := t.Columns[y]
177-
if !col.isAutoIncr && !col.Transient && colFilter(col) {
166+
if !col.isAutoIncr && !col.Transient {
178167
if x > 0 {
179168
s.WriteString(", ")
180169
}

transaction.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@ func (t *Transaction) Insert(ctx context.Context, list ...interface{}) error {
2727

2828
// Update had the same behavior as DbMap.Update(), but runs in a transaction.
2929
func (t *Transaction) Update(ctx context.Context, list ...interface{}) (int64, error) {
30-
return update(ctx, t.dbmap, t, nil, list...)
31-
}
32-
33-
// UpdateColumns had the same behavior as DbMap.UpdateColumns(), but runs in a transaction.
34-
func (t *Transaction) UpdateColumns(ctx context.Context, filter ColumnFilter, list ...interface{}) (int64, error) {
35-
return update(ctx, t.dbmap, t, filter, list...)
30+
return update(ctx, t.dbmap, t, list...)
3631
}
3732

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

0 commit comments

Comments
 (0)