Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
115c288a0b
|
|||
|
702a05f405
|
39
session.go
39
session.go
@@ -14,6 +14,10 @@ func (s *Session) Close() {
|
||||
s.session.Close()
|
||||
}
|
||||
|
||||
func (s *Session) Copy() *Session {
|
||||
return &Session{session: s.session.Copy()}
|
||||
}
|
||||
|
||||
func NewSession(dial string) (*Session, error) {
|
||||
session, err := mgo.Dial(dial)
|
||||
if err != nil {
|
||||
@@ -105,7 +109,11 @@ func (q *Query) Select(selector interface{}) *Query {
|
||||
}
|
||||
|
||||
func (q *Query) One(result interface{}) error {
|
||||
return q.query.One(result)
|
||||
err := q.query.One(result)
|
||||
if err == mgo.ErrNotFound {
|
||||
err = &ErrNotFound{msg: err.Error()}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (q *Query) Sort(fields ...string) *Query {
|
||||
@@ -124,11 +132,19 @@ func (q *Query) Limit(n int) *Query {
|
||||
}
|
||||
|
||||
func (q *Query) All(result interface{}) error {
|
||||
return q.query.All(result)
|
||||
err := q.query.All(result)
|
||||
if err == mgo.ErrNotFound {
|
||||
err = &ErrNotFound{msg: err.Error()}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (q *Query) Count() (int, error) {
|
||||
return q.query.Count()
|
||||
c, err := q.query.Count()
|
||||
if err == mgo.ErrNotFound {
|
||||
err = &ErrNotFound{msg: err.Error()}
|
||||
}
|
||||
return c, err
|
||||
}
|
||||
|
||||
type Index struct {
|
||||
@@ -151,9 +167,24 @@ type Pipe struct {
|
||||
}
|
||||
|
||||
func (p *Pipe) All(result interface{}) error {
|
||||
return p.pipe.All(result)
|
||||
err := p.pipe.All(result)
|
||||
if err == mgo.ErrNotFound {
|
||||
err = &ErrNotFound{msg: err.Error()}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Collection) Pipe(pipeline interface{}) *Pipe {
|
||||
return &Pipe{pipe: c.collection.Pipe(pipeline).AllowDiskUse()}
|
||||
}
|
||||
|
||||
type ErrNotFound struct {
|
||||
msg string
|
||||
}
|
||||
|
||||
func (e *ErrNotFound) Error() string {
|
||||
if e.msg == "" {
|
||||
return "not found"
|
||||
}
|
||||
return e.msg
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user