extended interface
This commit is contained in:
parent
6834a79601
commit
ede07bb49a
24
mgo.go
24
mgo.go
@ -36,8 +36,13 @@ func (c *MgoConnection) Close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type MgoSession struct {
|
type MgoSession struct {
|
||||||
session *mgo.Session
|
connection *MgoConnection
|
||||||
closed bool
|
session *mgo.Session
|
||||||
|
closed bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *MgoSession) Connection() Connection {
|
||||||
|
return s.connection
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MgoSession) Close() {
|
func (s *MgoSession) Close() {
|
||||||
@ -49,7 +54,10 @@ func (s *MgoSession) Close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *MgoConnection) NewSession() Session {
|
func (c *MgoConnection) NewSession() Session {
|
||||||
s := &MgoSession{session: c.connection.Copy()}
|
s := &MgoSession{
|
||||||
|
connection: c,
|
||||||
|
session: c.connection.Copy(),
|
||||||
|
}
|
||||||
runtime.SetFinalizer(s, func(s *MgoSession) {
|
runtime.SetFinalizer(s, func(s *MgoSession) {
|
||||||
if !s.closed {
|
if !s.closed {
|
||||||
s.Close()
|
s.Close()
|
||||||
@ -68,15 +76,23 @@ func (s *MgoSession) DB(name string) Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type MgoCollection struct {
|
type MgoCollection struct {
|
||||||
|
database *MgoDatabase
|
||||||
collection *mgo.Collection
|
collection *mgo.Collection
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *MgoCollection) DB() Database {
|
||||||
|
return c.database
|
||||||
|
}
|
||||||
|
|
||||||
func (db *MgoDatabase) Session() Session {
|
func (db *MgoDatabase) Session() Session {
|
||||||
return db.session
|
return db.session
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *MgoDatabase) C(name string) Collection {
|
func (db *MgoDatabase) C(name string) Collection {
|
||||||
return &MgoCollection{collection: db.database.C(name)}
|
return &MgoCollection{
|
||||||
|
database: db,
|
||||||
|
collection: db.database.C(name),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *MgoDatabase) Name() string {
|
func (db *MgoDatabase) Name() string {
|
||||||
|
2
types.go
2
types.go
@ -16,6 +16,7 @@ type Connection interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Session interface {
|
type Session interface {
|
||||||
|
Connection() Connection
|
||||||
Close()
|
Close()
|
||||||
DB(name string) Database
|
DB(name string) Database
|
||||||
}
|
}
|
||||||
@ -36,6 +37,7 @@ type Database interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Collection interface {
|
type Collection interface {
|
||||||
|
DB() Database
|
||||||
Insert(docs ...interface{}) error
|
Insert(docs ...interface{}) error
|
||||||
UpdateId(id interface{}, update interface{}) error
|
UpdateId(id interface{}, update interface{}) error
|
||||||
RemoveId(id interface{}) error
|
RemoveId(id interface{}) error
|
||||||
|
Loading…
Reference in New Issue
Block a user