Skip to content

Commit

Permalink
Use an object for columns.IDField to dictate if the id field is write…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
WilliamNHarvey committed Jul 19, 2023
1 parent 2f54061 commit f7b2de4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
8 changes: 4 additions & 4 deletions columns/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Columns struct {
lock *sync.RWMutex
TableName string
TableAlias string
IDField string
IDField IDField
}

// Add a column to the list.
Expand Down Expand Up @@ -75,7 +75,7 @@ func (c *Columns) Add(names ...string) []*Column {
} else if xs[1] == "w" {
col.Readable = false
}
} else if col.Name == c.IDField {
} else if col.Name == c.IDField.Name && c.IDField.Writeable == false {
col.Writeable = false
}

Expand Down Expand Up @@ -158,13 +158,13 @@ func (c Columns) SymbolizedString() string {
}

// NewColumns constructs a list of columns for a given table name.
func NewColumns(tableName, idField string) Columns {
func NewColumns(tableName string, idField IDField) Columns {
return NewColumnsWithAlias(tableName, "", idField)
}

// NewColumnsWithAlias constructs a list of columns for a given table
// name, using a given alias for the table.
func NewColumnsWithAlias(tableName, tableAlias, idField string) Columns {
func NewColumnsWithAlias(tableName, tableAlias string, idField IDField) Columns {
return Columns{
lock: &sync.RWMutex{},
Cols: map[string]*Column{},
Expand Down
9 changes: 7 additions & 2 deletions columns/columns_for_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ import (
"reflect"
)

type IDField struct {
Name string
Writeable bool
}

// ForStruct returns a Columns instance for
// the struct passed in.
func ForStruct(s interface{}, tableName, idField string) (columns Columns) {
return ForStructWithAlias(s, tableName, "", idField)
return ForStructWithAlias(s, tableName, "", IDField{Name: idField})
}

// ForStructWithAlias returns a Columns instance for the struct passed in.
// If the tableAlias is not empty, it will be used.
func ForStructWithAlias(s interface{}, tableName, tableAlias, idField string) (columns Columns) {
func ForStructWithAlias(s interface{}, tableName, tableAlias string, idField IDField) (columns Columns) {
columns = NewColumnsWithAlias(tableName, tableAlias, idField)
defer func() {
if r := recover(); r != nil {
Expand Down
8 changes: 4 additions & 4 deletions executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (c *Connection) Update(model interface{}, excludeColumns ...string) error {
}

tn := m.TableName()
cols := columns.ForStructWithAlias(model, tn, m.As, m.IDField())
cols := columns.ForStructWithAlias(model, tn, m.As, columns.IDField{Name: m.IDField(), Writeable: !m.UsingAutoIncrement()})
cols.Remove(m.IDField(), "created_at")

if tn == sm.TableName() {
Expand Down Expand Up @@ -401,7 +401,7 @@ func (q *Query) UpdateQuery(model interface{}, columnNames ...string) (int64, er
return 0, fmt.Errorf("model must be a struct; got %s", modelKind)
}

cols := columns.NewColumnsWithAlias(sm.TableName(), sm.As, sm.IDField())
cols := columns.NewColumnsWithAlias(sm.TableName(), sm.As, columns.IDField{Name: sm.IDField(), Writeable: !sm.UsingAutoIncrement()})
cols.Add(columnNames...)
if _, err := sm.fieldByName("UpdatedAt"); err == nil {
cols.Add("updated_at")
Expand Down Expand Up @@ -435,11 +435,11 @@ func (c *Connection) UpdateColumns(model interface{}, columnNames ...string) err

cols := columns.Columns{}
if len(columnNames) > 0 && tn == sm.TableName() {
cols = columns.NewColumnsWithAlias(tn, m.As, sm.IDField())
cols = columns.NewColumnsWithAlias(tn, m.As, columns.IDField{Name: sm.IDField(), Writeable: !sm.UsingAutoIncrement()})
cols.Add(columnNames...)

} else {
cols = columns.ForStructWithAlias(model, tn, m.As, m.IDField())
cols = columns.ForStructWithAlias(model, tn, m.As, columns.IDField{Name: m.IDField(), Writeable: !m.UsingAutoIncrement()})
}
cols.Remove("id", "created_at")

Expand Down
2 changes: 1 addition & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (m *Model) TableName() string {
}

func (m *Model) Columns() columns.Columns {
return columns.ForStructWithAlias(m.Value, m.TableName(), m.As, m.IDField())
return columns.ForStructWithAlias(m.Value, m.TableName(), m.As, columns.IDField{Name: m.IDField(), Writeable: !m.UsingAutoIncrement()})
}

func (m *Model) cacheKey(t reflect.Type) string {
Expand Down
4 changes: 2 additions & 2 deletions sql_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,15 @@ func (sq *sqlBuilder) buildColumns() columns.Columns {
if ok && cols.TableAlias == asName {
return cols
}
cols = columns.ForStructWithAlias(sq.Model.Value, tableName, asName, sq.Model.IDField())
cols = columns.ForStructWithAlias(sq.Model.Value, tableName, asName, columns.IDField{Name: sq.Model.IDField(), Writeable: !sq.Model.UsingAutoIncrement()})
columnCacheMutex.Lock()
columnCache[tableName] = cols
columnCacheMutex.Unlock()
return cols
}

// acl > 0
cols := columns.NewColumns("", sq.Model.IDField())
cols := columns.NewColumns("", columns.IDField{Name: sq.Model.IDField(), Writeable: !sq.Model.UsingAutoIncrement()})
cols.Add(sq.AddColumns...)
return cols
}

0 comments on commit f7b2de4

Please sign in to comment.