Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
7dd238342c
|
|||
|
115c288a0b
|
|||
|
702a05f405
|
32
session.go
32
session.go
@@ -6,6 +6,10 @@ import (
|
|||||||
mgo "gopkg.in/mgo.v2"
|
mgo "gopkg.in/mgo.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNotFound = errors.New("not found")
|
||||||
|
)
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
session *mgo.Session
|
session *mgo.Session
|
||||||
}
|
}
|
||||||
@@ -14,6 +18,10 @@ func (s *Session) Close() {
|
|||||||
s.session.Close()
|
s.session.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Session) Copy() *Session {
|
||||||
|
return &Session{session: s.session.Copy()}
|
||||||
|
}
|
||||||
|
|
||||||
func NewSession(dial string) (*Session, error) {
|
func NewSession(dial string) (*Session, error) {
|
||||||
session, err := mgo.Dial(dial)
|
session, err := mgo.Dial(dial)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -105,7 +113,11 @@ func (q *Query) Select(selector interface{}) *Query {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (q *Query) One(result interface{}) error {
|
func (q *Query) One(result interface{}) error {
|
||||||
return q.query.One(result)
|
err := q.query.One(result)
|
||||||
|
if err == mgo.ErrNotFound {
|
||||||
|
err = ErrNotFound
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Query) Sort(fields ...string) *Query {
|
func (q *Query) Sort(fields ...string) *Query {
|
||||||
@@ -124,11 +136,19 @@ func (q *Query) Limit(n int) *Query {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (q *Query) All(result interface{}) error {
|
func (q *Query) All(result interface{}) error {
|
||||||
return q.query.All(result)
|
err := q.query.All(result)
|
||||||
|
if err == mgo.ErrNotFound {
|
||||||
|
err = ErrNotFound
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Query) Count() (int, error) {
|
func (q *Query) Count() (int, error) {
|
||||||
return q.query.Count()
|
c, err := q.query.Count()
|
||||||
|
if err == mgo.ErrNotFound {
|
||||||
|
err = ErrNotFound
|
||||||
|
}
|
||||||
|
return c, err
|
||||||
}
|
}
|
||||||
|
|
||||||
type Index struct {
|
type Index struct {
|
||||||
@@ -151,7 +171,11 @@ type Pipe struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pipe) All(result interface{}) error {
|
func (p *Pipe) All(result interface{}) error {
|
||||||
return p.pipe.All(result)
|
err := p.pipe.All(result)
|
||||||
|
if err == mgo.ErrNotFound {
|
||||||
|
err = ErrNotFound
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Collection) Pipe(pipeline interface{}) *Pipe {
|
func (c *Collection) Pipe(pipeline interface{}) *Pipe {
|
||||||
|
|||||||
Reference in New Issue
Block a user