Skip to content

Commit

Permalink
Export WhereID, Alias, WhereNamedID (#637)
Browse files Browse the repository at this point in the history
This patch export some model convenience functions which are useful when constructing queries outside of pop: custom updates, deletes, inserts, ...

Signed-off-by: aeneasr <3372410+aeneasr@users.noreply.github.com>

Co-authored-by: Antonio Pagano <645522+paganotoni@users.noreply.github.com>
  • Loading branch information
aeneasr and paganotoni committed Jun 8, 2021
1 parent bef765a commit bb07a37
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dialect_cockroach.go
Expand Up @@ -105,7 +105,7 @@ func (p *cockroach) Update(s store, model *Model, cols columns.Columns) error {
}

func (p *cockroach) Destroy(s store, model *Model) error {
stmt := p.TranslateSQL(fmt.Sprintf("DELETE FROM %s AS %s WHERE %s", p.Quote(model.TableName()), model.alias(), model.whereID()))
stmt := p.TranslateSQL(fmt.Sprintf("DELETE FROM %s AS %s WHERE %s", p.Quote(model.TableName()), model.Alias(), model.WhereID()))
_, err := genericExec(s, stmt, model.ID())
return err
}
Expand Down
4 changes: 2 additions & 2 deletions dialect_common.go
Expand Up @@ -99,7 +99,7 @@ func genericCreate(s store, model *Model, cols columns.Columns, quoter quotable)
}

func genericUpdate(s store, model *Model, cols columns.Columns, quoter quotable) error {
stmt := fmt.Sprintf("UPDATE %s AS %s SET %s WHERE %s", quoter.Quote(model.TableName()), model.alias(), cols.Writeable().QuotedUpdateString(quoter), model.whereNamedID())
stmt := fmt.Sprintf("UPDATE %s AS %s SET %s WHERE %s", quoter.Quote(model.TableName()), model.Alias(), cols.Writeable().QuotedUpdateString(quoter), model.WhereNamedID())
log(logging.SQL, stmt, model.ID())
_, err := s.NamedExec(stmt, model.Value)
if err != nil {
Expand All @@ -109,7 +109,7 @@ func genericUpdate(s store, model *Model, cols columns.Columns, quoter quotable)
}

func genericDestroy(s store, model *Model, quoter quotable) error {
stmt := fmt.Sprintf("DELETE FROM %s AS %s WHERE %s", quoter.Quote(model.TableName()), model.alias(), model.whereID())
stmt := fmt.Sprintf("DELETE FROM %s AS %s WHERE %s", quoter.Quote(model.TableName()), model.Alias(), model.WhereID())
_, err := genericExec(s, stmt, model.ID())
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion dialect_postgresql.go
Expand Up @@ -92,7 +92,7 @@ func (p *postgresql) Update(s store, model *Model, cols columns.Columns) error {
}

func (p *postgresql) Destroy(s store, model *Model) error {
stmt := p.TranslateSQL(fmt.Sprintf("DELETE FROM %s AS %s WHERE %s", p.Quote(model.TableName()), model.alias(), model.whereID()))
stmt := p.TranslateSQL(fmt.Sprintf("DELETE FROM %s AS %s WHERE %s", p.Quote(model.TableName()), model.Alias(), model.WhereID()))
_, err := genericExec(s, stmt, model.ID())
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion finders.go
Expand Up @@ -30,7 +30,7 @@ func (c *Connection) Find(model interface{}, id interface{}) error {
// q.Find(&User{}, 1)
func (q *Query) Find(model interface{}, id interface{}) error {
m := NewModel(model, q.Connection.Context())
idq := m.whereID()
idq := m.WhereID()
switch t := id.(type) {
case uuid.UUID:
return q.Where(idq, t.String()).First(model)
Expand Down
10 changes: 5 additions & 5 deletions model.go
Expand Up @@ -219,20 +219,20 @@ func (m *Model) setUpdatedAt(now time.Time) {
}
}

func (m *Model) whereID() string {
return fmt.Sprintf("%s.%s = ?", m.alias(), m.IDField())
func (m *Model) WhereID() string {
return fmt.Sprintf("%s.%s = ?", m.Alias(), m.IDField())
}

func (m *Model) alias() string {
func (m *Model) Alias() string {
as := m.As
if as == "" {
as = strings.ReplaceAll(m.TableName(), ".", "_")
}
return as
}

func (m *Model) whereNamedID() string {
return fmt.Sprintf("%s.%s = :%s", m.alias(), m.IDField(), m.IDField())
func (m *Model) WhereNamedID() string {
return fmt.Sprintf("%s.%s = :%s", m.Alias(), m.IDField(), m.IDField())
}

func (m *Model) isSlice() bool {
Expand Down
4 changes: 2 additions & 2 deletions model_test.go
Expand Up @@ -207,8 +207,8 @@ func Test_WhereID(t *testing.T) {
r := require.New(t)
m := Model{Value: &testPrefixID{ID: 1}}

r.Equal("foo_bar.custom_id = ?", m.whereID())
r.Equal("foo_bar.custom_id = :custom_id", m.whereNamedID())
r.Equal("foo_bar.custom_id = ?", m.WhereID())
r.Equal("foo_bar.custom_id = :custom_id", m.WhereNamedID())

type testNormalID struct {
ID int
Expand Down
4 changes: 2 additions & 2 deletions sql_builder.go
Expand Up @@ -125,7 +125,7 @@ func (sq *sqlBuilder) buildfromClauses() fromClauses {
fc := sq.Query.fromClauses
for _, m := range models {
tableName := m.TableName()
asName := m.alias()
asName := m.Alias()
fc = append(fc, fromClause{
From: tableName,
As: asName,
Expand Down Expand Up @@ -213,7 +213,7 @@ var columnCacheMutex = sync.RWMutex{}

func (sq *sqlBuilder) buildColumns() columns.Columns {
tableName := sq.Model.TableName()
asName := sq.Model.alias()
asName := sq.Model.Alias()
acl := len(sq.AddColumns)
if acl == 0 {
columnCacheMutex.RLock()
Expand Down

0 comments on commit bb07a37

Please sign in to comment.