From bb07a373cc0e689f778a9701c84d61a1c58f314c Mon Sep 17 00:00:00 2001 From: hackerman <3372410+aeneasr@users.noreply.github.com> Date: Tue, 8 Jun 2021 12:57:45 +0200 Subject: [PATCH] Export WhereID, Alias, WhereNamedID (#637) 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> --- dialect_cockroach.go | 2 +- dialect_common.go | 4 ++-- dialect_postgresql.go | 2 +- finders.go | 2 +- model.go | 10 +++++----- model_test.go | 4 ++-- sql_builder.go | 4 ++-- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/dialect_cockroach.go b/dialect_cockroach.go index cab020a5..d8dd0a08 100644 --- a/dialect_cockroach.go +++ b/dialect_cockroach.go @@ -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 } diff --git a/dialect_common.go b/dialect_common.go index 7da27adb..0905b049 100644 --- a/dialect_common.go +++ b/dialect_common.go @@ -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 { @@ -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 diff --git a/dialect_postgresql.go b/dialect_postgresql.go index ccb8e43d..3590141e 100644 --- a/dialect_postgresql.go +++ b/dialect_postgresql.go @@ -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 diff --git a/finders.go b/finders.go index 7de66924..440fb703 100644 --- a/finders.go +++ b/finders.go @@ -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) diff --git a/model.go b/model.go index a8625551..d83ee380 100644 --- a/model.go +++ b/model.go @@ -219,11 +219,11 @@ 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(), ".", "_") @@ -231,8 +231,8 @@ func (m *Model) alias() string { 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 { diff --git a/model_test.go b/model_test.go index 6f9a6f79..1bcc7e2d 100644 --- a/model_test.go +++ b/model_test.go @@ -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 diff --git a/sql_builder.go b/sql_builder.go index 44820f4e..b6e07223 100644 --- a/sql_builder.go +++ b/sql_builder.go @@ -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, @@ -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()