Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export WhereID, Alias, WhereNamedID #637

Merged
merged 3 commits into from Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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