Files
leanote/app/db/Mgo.go

380 lines
10 KiB
Go
Raw Normal View History

2014-05-07 13:06:24 +08:00
package db
import (
"fmt"
. "github.com/leanote/leanote/app/lea"
2015-11-13 17:58:41 +08:00
"github.com/revel/revel"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
2015-09-09 10:10:24 +08:00
"strings"
2014-05-07 13:06:24 +08:00
)
// Init mgo and the common DAO
// 数据连接
var Session *mgo.Session
// 各个表的Collection对象
var Notebooks *mgo.Collection
var Notes *mgo.Collection
var NoteContents *mgo.Collection
var NoteContentHistories *mgo.Collection
var ShareNotes *mgo.Collection
var ShareNotebooks *mgo.Collection
var HasShareNotes *mgo.Collection
var Blogs *mgo.Collection
var Users *mgo.Collection
2014-11-09 16:24:19 +08:00
var Groups *mgo.Collection
var GroupUsers *mgo.Collection
2014-05-07 13:06:24 +08:00
var Tags *mgo.Collection
2015-03-31 14:27:26 +08:00
var NoteTags *mgo.Collection
2014-11-10 23:56:15 +08:00
var TagCounts *mgo.Collection
2014-05-07 13:06:24 +08:00
var UserBlogs *mgo.Collection
var Tokens *mgo.Collection
var Suggestions *mgo.Collection
// Album & file(image)
var Albums *mgo.Collection
var Files *mgo.Collection
2014-09-21 22:05:04 +08:00
var Attachs *mgo.Collection
2014-09-19 17:18:53 +08:00
var NoteImages *mgo.Collection
var Configs *mgo.Collection
2014-10-22 16:20:45 +08:00
var EmailLogs *mgo.Collection
// blog
var BlogLikes *mgo.Collection
var BlogComments *mgo.Collection
var Reports *mgo.Collection
2014-11-09 16:24:19 +08:00
var BlogSingles *mgo.Collection
var Themes *mgo.Collection
2014-10-22 16:20:45 +08:00
// session
var Sessions *mgo.Collection
2014-09-19 17:18:53 +08:00
2014-05-07 13:06:24 +08:00
// 初始化时连接数据库
func Init(url, dbname string) {
ok := true
2014-11-09 16:24:19 +08:00
config := revel.Config
2015-11-13 17:58:41 +08:00
if url == "" {
url, ok = config.String("db.url")
2015-09-09 10:10:24 +08:00
if !ok {
url, ok = config.String("db.urlEnv")
2015-10-07 00:28:35 +08:00
if ok {
Log("get db conf from urlEnv: " + url)
}
} else {
Log("get db conf from db.url: " + url)
2015-09-09 10:10:24 +08:00
}
2015-11-13 17:58:41 +08:00
2015-09-09 10:10:24 +08:00
if ok {
2015-10-07 00:28:35 +08:00
// get dbname from urlEnv
2015-09-09 10:10:24 +08:00
urls := strings.Split(url, "/")
dbname = urls[len(urls)-1]
if strings.Contains(dbname, "?") {
urls = strings.Split(dbname, "?")
dbname = urls[0]
}
2015-09-09 10:10:24 +08:00
}
}
if dbname == "" {
dbname, _ = config.String("db.dbname")
}
2015-11-13 17:58:41 +08:00
2015-10-07 00:28:35 +08:00
// get db config from host, port, username, password
2014-05-07 13:06:24 +08:00
if !ok {
host, _ := revel.Config.String("db.host")
port, _ := revel.Config.String("db.port")
username, _ := revel.Config.String("db.username")
password, _ := revel.Config.String("db.password")
2014-05-09 14:59:26 +08:00
usernameAndPassword := username + ":" + password + "@"
if username == "" || password == "" {
usernameAndPassword = ""
}
2014-11-09 16:24:19 +08:00
url = "mongodb://" + usernameAndPassword + host + ":" + port + "/" + dbname
2014-05-07 13:06:24 +08:00
}
Log(url)
2014-11-09 16:24:19 +08:00
2014-05-07 13:06:24 +08:00
// [mongodb://][user:pass@]host1[:port1][,host2[:port2],...][/database][?options]
// mongodb://myuser:mypass@localhost:40001,otherhost:40001/mydb
var err error
Session, err = mgo.Dial(url)
if err != nil {
panic(err)
}
// Optional. Switch the session to a monotonic behavior.
Session.SetMode(mgo.Monotonic, true)
// notebook
Notebooks = Session.DB(dbname).C("notebooks")
2014-11-09 16:24:19 +08:00
2014-05-07 13:06:24 +08:00
// notes
Notes = Session.DB(dbname).C("notes")
2014-11-09 16:24:19 +08:00
2014-05-07 13:06:24 +08:00
// noteContents
NoteContents = Session.DB(dbname).C("note_contents")
NoteContentHistories = Session.DB(dbname).C("note_content_histories")
2014-11-09 16:24:19 +08:00
2014-05-07 13:06:24 +08:00
// share
ShareNotes = Session.DB(dbname).C("share_notes")
ShareNotebooks = Session.DB(dbname).C("share_notebooks")
HasShareNotes = Session.DB(dbname).C("has_share_notes")
2014-11-09 16:24:19 +08:00
2014-05-07 13:06:24 +08:00
// user
Users = Session.DB(dbname).C("users")
2015-11-13 17:58:41 +08:00
// group
2014-11-09 16:24:19 +08:00
Groups = Session.DB(dbname).C("groups")
GroupUsers = Session.DB(dbname).C("group_users")
2014-05-07 13:06:24 +08:00
// blog
Blogs = Session.DB(dbname).C("blogs")
2014-11-09 16:24:19 +08:00
2014-05-07 13:06:24 +08:00
// tag
Tags = Session.DB(dbname).C("tags")
2015-03-31 14:27:26 +08:00
NoteTags = Session.DB(dbname).C("note_tags")
2014-11-10 23:56:15 +08:00
TagCounts = Session.DB(dbname).C("tag_count")
2014-11-09 16:24:19 +08:00
2014-05-07 13:06:24 +08:00
// blog
UserBlogs = Session.DB(dbname).C("user_blogs")
2014-11-09 16:24:19 +08:00
BlogSingles = Session.DB(dbname).C("blog_singles")
Themes = Session.DB(dbname).C("themes")
2014-05-07 13:06:24 +08:00
// find password
Tokens = Session.DB(dbname).C("tokens")
2014-11-09 16:24:19 +08:00
// Suggestion
2014-05-07 13:06:24 +08:00
Suggestions = Session.DB(dbname).C("suggestions")
2014-11-09 16:24:19 +08:00
// Album & file
Albums = Session.DB(dbname).C("albums")
Files = Session.DB(dbname).C("files")
2014-09-21 22:05:04 +08:00
Attachs = Session.DB(dbname).C("attachs")
2014-11-09 16:24:19 +08:00
2014-09-19 17:18:53 +08:00
NoteImages = Session.DB(dbname).C("note_images")
2014-11-09 16:24:19 +08:00
Configs = Session.DB(dbname).C("configs")
2014-10-22 16:20:45 +08:00
EmailLogs = Session.DB(dbname).C("email_logs")
2014-11-09 16:24:19 +08:00
2014-10-22 16:20:45 +08:00
// 社交
BlogLikes = Session.DB(dbname).C("blog_likes")
BlogComments = Session.DB(dbname).C("blog_comments")
2014-11-09 16:24:19 +08:00
2014-10-22 16:20:45 +08:00
// 举报
Reports = Session.DB(dbname).C("reports")
2014-11-09 16:24:19 +08:00
2014-10-22 16:20:45 +08:00
// session
Sessions = Session.DB(dbname).C("sessions")
2014-05-07 13:06:24 +08:00
}
func close() {
Session.Close()
}
// common DAO
// 公用方法
//----------------------
func Insert(collection *mgo.Collection, i interface{}) bool {
err := collection.Insert(i)
return Err(err)
}
//----------------------
// 适合一条记录全部更新
func Update(collection *mgo.Collection, query interface{}, i interface{}) bool {
err := collection.Update(query, i)
return Err(err)
}
func Upsert(collection *mgo.Collection, query interface{}, i interface{}) bool {
_, err := collection.Upsert(query, i)
return Err(err)
}
func UpdateAll(collection *mgo.Collection, query interface{}, i interface{}) bool {
_, err := collection.UpdateAll(query, i)
return Err(err)
}
func UpdateByIdAndUserId(collection *mgo.Collection, id, userId string, i interface{}) bool {
err := collection.Update(GetIdAndUserIdQ(id, userId), i)
return Err(err)
}
func UpdateByIdAndUserId2(collection *mgo.Collection, id, userId bson.ObjectId, i interface{}) bool {
err := collection.Update(GetIdAndUserIdBsonQ(id, userId), i)
return Err(err)
}
func UpdateByIdAndUserIdField(collection *mgo.Collection, id, userId, field string, value interface{}) bool {
2014-11-09 16:24:19 +08:00
return UpdateByIdAndUserId(collection, id, userId, bson.M{"$set": bson.M{field: value}})
2014-05-07 13:06:24 +08:00
}
func UpdateByIdAndUserIdMap(collection *mgo.Collection, id, userId string, v bson.M) bool {
return UpdateByIdAndUserId(collection, id, userId, bson.M{"$set": v})
}
2014-11-09 16:24:19 +08:00
2014-05-07 13:06:24 +08:00
func UpdateByIdAndUserIdField2(collection *mgo.Collection, id, userId bson.ObjectId, field string, value interface{}) bool {
2014-11-09 16:24:19 +08:00
return UpdateByIdAndUserId2(collection, id, userId, bson.M{"$set": bson.M{field: value}})
2014-05-07 13:06:24 +08:00
}
func UpdateByIdAndUserIdMap2(collection *mgo.Collection, id, userId bson.ObjectId, v bson.M) bool {
return UpdateByIdAndUserId2(collection, id, userId, bson.M{"$set": v})
}
2014-11-09 16:24:19 +08:00
//
2014-05-07 13:06:24 +08:00
func UpdateByQField(collection *mgo.Collection, q interface{}, field string, value interface{}) bool {
_, err := collection.UpdateAll(q, bson.M{"$set": bson.M{field: value}})
return Err(err)
}
2014-11-09 16:24:19 +08:00
func UpdateByQI(collection *mgo.Collection, q interface{}, v interface{}) bool {
_, err := collection.UpdateAll(q, bson.M{"$set": v})
return Err(err)
}
2014-05-07 13:06:24 +08:00
// 查询条件和值
func UpdateByQMap(collection *mgo.Collection, q interface{}, v interface{}) bool {
_, err := collection.UpdateAll(q, bson.M{"$set": v})
return Err(err)
}
//------------------------
// 删除一条
func Delete(collection *mgo.Collection, q interface{}) bool {
err := collection.Remove(q)
return Err(err)
}
func DeleteByIdAndUserId(collection *mgo.Collection, id, userId string) bool {
err := collection.Remove(GetIdAndUserIdQ(id, userId))
return Err(err)
}
func DeleteByIdAndUserId2(collection *mgo.Collection, id, userId bson.ObjectId) bool {
err := collection.Remove(GetIdAndUserIdBsonQ(id, userId))
return Err(err)
}
// 删除所有
func DeleteAllByIdAndUserId(collection *mgo.Collection, id, userId string) bool {
_, err := collection.RemoveAll(GetIdAndUserIdQ(id, userId))
return Err(err)
}
func DeleteAllByIdAndUserId2(collection *mgo.Collection, id, userId bson.ObjectId) bool {
_, err := collection.RemoveAll(GetIdAndUserIdBsonQ(id, userId))
return Err(err)
}
func DeleteAll(collection *mgo.Collection, q interface{}) bool {
_, err := collection.RemoveAll(q)
return Err(err)
}
//-------------------------
func Get(collection *mgo.Collection, id string, i interface{}) {
collection.FindId(bson.ObjectIdHex(id)).One(i)
}
func Get2(collection *mgo.Collection, id bson.ObjectId, i interface{}) {
collection.FindId(id).One(i)
}
func GetByQ(collection *mgo.Collection, q interface{}, i interface{}) {
collection.Find(q).One(i)
}
func ListByQ(collection *mgo.Collection, q interface{}, i interface{}) {
collection.Find(q).All(i)
}
func ListByQLimit(collection *mgo.Collection, q interface{}, i interface{}, limit int) {
collection.Find(q).Limit(limit).All(i)
}
// 查询某些字段, q是查询条件, fields是字段名列表
func GetByQWithFields(collection *mgo.Collection, q bson.M, fields []string, i interface{}) {
selector := make(bson.M, len(fields))
for _, field := range fields {
selector[field] = true
}
collection.Find(q).Select(selector).One(i)
}
2014-11-09 16:24:19 +08:00
2014-05-07 13:06:24 +08:00
// 查询某些字段, q是查询条件, fields是字段名列表
func ListByQWithFields(collection *mgo.Collection, q bson.M, fields []string, i interface{}) {
selector := make(bson.M, len(fields))
for _, field := range fields {
selector[field] = true
}
collection.Find(q).Select(selector).All(i)
}
func GetByIdAndUserId(collection *mgo.Collection, id, userId string, i interface{}) {
collection.Find(GetIdAndUserIdQ(id, userId)).One(i)
}
func GetByIdAndUserId2(collection *mgo.Collection, id, userId bson.ObjectId, i interface{}) {
collection.Find(GetIdAndUserIdBsonQ(id, userId)).One(i)
}
2014-11-09 16:24:19 +08:00
// 按field去重
func Distinct(collection *mgo.Collection, q bson.M, field string, i interface{}) {
collection.Find(q).Distinct(field, i)
}
2014-05-07 13:06:24 +08:00
//----------------------
func Count(collection *mgo.Collection, q interface{}) int {
cnt, err := collection.Find(q).Count()
if err != nil {
Err(err)
}
return cnt
}
func Has(collection *mgo.Collection, q interface{}) bool {
if Count(collection, q) > 0 {
return true
}
return false
}
//-----------------
// 得到主键和userId的复合查询条件
func GetIdAndUserIdQ(id, userId string) bson.M {
return bson.M{"_id": bson.ObjectIdHex(id), "UserId": bson.ObjectIdHex(userId)}
}
func GetIdAndUserIdBsonQ(id, userId bson.ObjectId) bson.M {
return bson.M{"_id": id, "UserId": userId}
}
// DB处理错误
func Err(err error) bool {
if err != nil {
fmt.Println(err)
// 删除时, 查找
if err.Error() == "not found" {
2014-11-09 16:24:19 +08:00
return true
2014-05-07 13:06:24 +08:00
}
return false
}
return true
2014-11-09 16:24:19 +08:00
}
2015-10-10 14:49:02 +08:00
// 检查mognodb是否lost connection
// 每个请求之前都要检查!!
func CheckMongoSessionLost() {
// fmt.Println("检查CheckMongoSessionLostErr")
2015-11-13 17:58:41 +08:00
err := Session.Ping()
if err != nil {
Log("Lost connection to db!")
Session.Refresh()
err = Session.Ping()
if err == nil {
Log("Reconnect to db successful.")
} else {
Log("重连失败!!!! 警告")
}
}
2015-10-10 14:49:02 +08:00
}