Skip to content

Commit

Permalink
Refactor whereID to use a tokenized value (#249)
Browse files Browse the repository at this point in the history
* Refactor whereID to use a tokenized value

* This will help to use non-string values as IDs, like in #243.
* Allow genericExec to use tokenized queries.
* Refactor dialect file names.

* Fix conflict with named and non-named tokens

* Fix typo in genericExec

* Translate SQL token for postgres and cockroach
  • Loading branch information
stanislas-m committed Sep 27, 2018
1 parent e2be28e commit e121e74
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
12 changes: 6 additions & 6 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func genericCreate(s store, model *Model, cols columns.Columns) error {
}

func genericUpdate(s store, model *Model, cols columns.Columns) error {
stmt := fmt.Sprintf("UPDATE %s SET %s where %s", model.TableName(), cols.Writeable().UpdateString(), model.whereID())
log(logging.SQL, stmt)
stmt := fmt.Sprintf("UPDATE %s SET %s WHERE %s", model.TableName(), cols.Writeable().UpdateString(), model.whereNamedID())
log(logging.SQL, stmt, model.ID())
_, err := s.NamedExec(stmt, model.Value)
if err != nil {
return errors.WithStack(err)
Expand All @@ -102,16 +102,16 @@ func genericUpdate(s store, model *Model, cols columns.Columns) error {

func genericDestroy(s store, model *Model) error {
stmt := fmt.Sprintf("DELETE FROM %s WHERE %s", model.TableName(), model.whereID())
err := genericExec(s, stmt)
err := genericExec(s, stmt, model.ID())
if err != nil {
return errors.WithStack(err)
}
return nil
}

func genericExec(s store, stmt string) error {
log(logging.SQL, stmt)
_, err := s.Exec(stmt)
func genericExec(s store, stmt string, args ...interface{}) error {
log(logging.SQL, stmt, args...)
_, err := s.Exec(stmt, args...)
if err != nil {
return errors.WithStack(err)
}
Expand Down
7 changes: 6 additions & 1 deletion dialect_cockroach.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ func (p *cockroach) Update(s store, model *Model, cols columns.Columns) error {
}

func (p *cockroach) Destroy(s store, model *Model) error {
return genericDestroy(s, model)
stmt := p.TranslateSQL(fmt.Sprintf("DELETE FROM %s WHERE %s", model.TableName(), model.whereID()))
err := genericExec(s, stmt, model.ID())
if err != nil {
return errors.WithStack(err)
}
return nil
}

func (p *cockroach) SelectOne(s store, model *Model, query Query) error {
Expand Down
File renamed without changes.
7 changes: 6 additions & 1 deletion dialect_postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ func (p *postgresql) Update(s store, model *Model, cols columns.Columns) error {
}

func (p *postgresql) Destroy(s store, model *Model) error {
return genericDestroy(s, model)
stmt := p.TranslateSQL(fmt.Sprintf("DELETE FROM %s WHERE %s", model.TableName(), model.whereID()))
err := genericExec(s, stmt, model.ID())
if err != nil {
return errors.WithStack(err)
}
return nil
}

func (p *postgresql) SelectOne(s store, model *Model, query Query) error {
Expand Down
File renamed without changes.
14 changes: 5 additions & 9 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,11 @@ func (m *Model) touchUpdatedAt() {
}

func (m *Model) whereID() string {
id := m.ID()
var value string
switch id.(type) {
case int, int64:
value = fmt.Sprintf("%s.id = %d", m.TableName(), id)
default:
value = fmt.Sprintf("%s.id ='%s'", m.TableName(), id)
}
return value
return fmt.Sprintf("%s.id = ?", m.TableName())
}

func (m *Model) whereNamedID() string {
return fmt.Sprintf("%s.id = :id", m.TableName())
}

func (m *Model) isSlice() bool {
Expand Down

0 comments on commit e121e74

Please sign in to comment.