-
-
Notifications
You must be signed in to change notification settings - Fork 246
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
Add AfterEagerFind()
#802
Add AfterEagerFind()
#802
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,24 +66,29 @@ func (c *Connection) First(model interface{}) error { | |
// | ||
// q.Where("name = ?", "mark").First(&User{}) | ||
func (q *Query) First(model interface{}) error { | ||
var m *Model | ||
err := q.Connection.timeFunc("First", func() error { | ||
q.Limit(1) | ||
m := NewModel(model, q.Connection.Context()) | ||
m = NewModel(model, q.Connection.Context()) | ||
if err := q.Connection.Dialect.SelectOne(q.Connection, m, *q); err != nil { | ||
return err | ||
} | ||
return m.afterFind(q.Connection) | ||
return m.afterFind(q.Connection, false) | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if q.eager { | ||
err = q.eagerAssociations(model) | ||
err := q.eagerAssociations(model) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious if there is a specific reason for adding ':' here. If there is a specific reason, an inline comment could be useful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's mostly a coding habit on my end, also fine either way |
||
q.disableEager() | ||
return err | ||
if err != nil { | ||
return err | ||
} | ||
return m.afterFind(q.Connection, true) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
@@ -98,14 +103,15 @@ func (c *Connection) Last(model interface{}) error { | |
// | ||
// q.Where("name = ?", "mark").Last(&User{}) | ||
func (q *Query) Last(model interface{}) error { | ||
var m *Model | ||
err := q.Connection.timeFunc("Last", func() error { | ||
q.Limit(1) | ||
q.Order("created_at DESC, id DESC") | ||
m := NewModel(model, q.Connection.Context()) | ||
m = NewModel(model, q.Connection.Context()) | ||
if err := q.Connection.Dialect.SelectOne(q.Connection, m, *q); err != nil { | ||
return err | ||
} | ||
return m.afterFind(q.Connection) | ||
return m.afterFind(q.Connection, false) | ||
}) | ||
|
||
if err != nil { | ||
|
@@ -115,7 +121,10 @@ func (q *Query) Last(model interface{}) error { | |
if q.eager { | ||
err = q.eagerAssociations(model) | ||
q.disableEager() | ||
return err | ||
if err != nil { | ||
return err | ||
} | ||
return m.afterFind(q.Connection, true) | ||
} | ||
|
||
return nil | ||
|
@@ -132,17 +141,20 @@ func (c *Connection) All(models interface{}) error { | |
// | ||
// q.Where("name = ?", "mark").All(&[]User{}) | ||
func (q *Query) All(models interface{}) error { | ||
var m *Model | ||
err := q.Connection.timeFunc("All", func() error { | ||
m := NewModel(models, q.Connection.Context()) | ||
m = NewModel(models, q.Connection.Context()) | ||
err := q.Connection.Dialect.SelectMany(q.Connection, m, *q) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = q.paginateModel(models) | ||
if err != nil { | ||
return err | ||
} | ||
return m.afterFind(q.Connection) | ||
|
||
return m.afterFind(q.Connection, false) | ||
}) | ||
|
||
if err != nil { | ||
|
@@ -152,7 +164,10 @@ func (q *Query) All(models interface{}) error { | |
if q.eager { | ||
err = q.eagerAssociations(models) | ||
q.disableEager() | ||
return err | ||
if err != nil { | ||
return err | ||
} | ||
return m.afterFind(q.Connection, true) | ||
} | ||
|
||
return nil | ||
|
@@ -301,7 +316,7 @@ func (q *Query) eagerDefaultAssociations(model interface{}) error { | |
// Exists returns true/false if a record exists in the database that matches | ||
// the query. | ||
// | ||
// q.Where("name = ?", "mark").Exists(&User{}) | ||
// q.Where("name = ?", "mark").Exists(&User{}) | ||
func (q *Query) Exists(model interface{}) (bool, error) { | ||
tmpQuery := Q(q.Connection) | ||
q.Clone(tmpQuery) // avoid meddling with original query | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be tiny. Since those two blocks are exclusive to each other, I would prefer to rewrite the block for readability as below. What do you think? (same for blocks in line 49)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically just a habit of keeping nesting flat - would be fine either way :)