Repository: gobuffalo/pop
Pull request #637 (2021-04-12 13:38:58)

Export WhereID, Alias, WhereNamedID
This patch export some model convenience functions which are useful when constructing queries outside of pop: custom updates, deletes, inserts, ...

aeneasr: MySQL fails unrelated to this PR
aeneasr: @paganotoni could you maybe take a look / review? :) I can't merge without it. Thank you! :)

Patch:
diff --git a/dialect_cockroach.go b/dialect_cockroach.go
index cab020a..d8dd0a0 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 7da27ad..0905b04 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 ccb8e43..3590141 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 7de6692..440fb70 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 a862555..d83ee38 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/sql_builder.go b/sql_builder.go
index 44820f4..b6e0722 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()


Test patch:
diff --git a/model_test.go b/model_test.go
index 6f9a6f7..1bcc7e2 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