Add postgresql support (#792)

* use false for bool type

Signed-off-by: ChangFeng <chang_196700@hotmail.com>

* use `limit ? offset ?` instead limit ?,? for database compatibility

Signed-off-by: ChangFeng <chang_196700@hotmail.com>

* add postgresql support

Signed-off-by: ChangFeng <chang_196700@hotmail.com>

* add postgres options and descriptions

Signed-off-by: ChangFeng <chang_196700@hotmail.com>

---------

close #791

Signed-off-by: ChangFeng <chang_196700@hotmail.com>
Co-authored-by: 玖亖伍 <gsw945@foxmail.com>
pull/866/head
Feng Chang 2023-04-14 11:09:53 +08:00 committed by GitHub
parent bc4cfdbbe6
commit 8b57c6aaf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 61 additions and 30 deletions

View File

@ -26,6 +26,7 @@ import (
"github.com/beego/beego/v2/server/web" "github.com/beego/beego/v2/server/web"
"github.com/beego/i18n" "github.com/beego/i18n"
"github.com/howeyc/fsnotify" "github.com/howeyc/fsnotify"
_ "github.com/lib/pq"
"github.com/lifei6671/gocaptcha" "github.com/lifei6671/gocaptcha"
"github.com/mindoc-org/mindoc/cache" "github.com/mindoc-org/mindoc/cache"
"github.com/mindoc-org/mindoc/conf" "github.com/mindoc-org/mindoc/conf"
@ -84,6 +85,30 @@ func RegisterDataBase() {
if err != nil { if err != nil {
logs.Error("注册默认数据库失败->", err) logs.Error("注册默认数据库失败->", err)
} }
} else if strings.EqualFold(dbadapter, "postgres") {
host, _ := web.AppConfig.String("db_host")
database, _ := web.AppConfig.String("db_database")
username, _ := web.AppConfig.String("db_username")
password, _ := web.AppConfig.String("db_password")
sslmode, _ := web.AppConfig.String("db_sslmode")
timezone, _ := web.AppConfig.String("timezone")
location, err := time.LoadLocation(timezone)
if err == nil {
orm.DefaultTimeLoc = location
} else {
logs.Error("加载时区配置信息失败,请检查是否存在 ZONEINFO 环境变量->", err)
}
port, _ := web.AppConfig.String("db_port")
dataSource := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s", username, password, host, port, database, sslmode)
if err := orm.RegisterDataBase("default", "postgres", dataSource); err != nil {
logs.Error("注册默认数据库失败->", err)
os.Exit(1)
}
} else { } else {
logs.Error("不支持的数据库类型.") logs.Error("不支持的数据库类型.")
os.Exit(1) os.Exit(1)

View File

@ -53,13 +53,19 @@ timezone = Asia/Shanghai
####################MySQL 数据库配置########################### ####################MySQL 数据库配置###########################
#支持MySQL和sqlite3两种数据库如果是sqlite3 则 db_database 标识数据库的物理目录 #支持MySQLsqlite3postgres三种数据库如果是sqlite3 则 db_database 标识数据库的物理目录
db_adapter="${MINDOC_DB_ADAPTER||sqlite3}" db_adapter="${MINDOC_DB_ADAPTER||sqlite3}"
db_host="${MINDOC_DB_HOST||127.0.0.1}" db_host="${MINDOC_DB_HOST||127.0.0.1}"
db_port="${MINDOC_DB_PORT||3306}" db_port="${MINDOC_DB_PORT||3306}"
db_database="${MINDOC_DB_DATABASE||./database/mindoc.db}" db_database="${MINDOC_DB_DATABASE||./database/mindoc.db}"
db_username="${MINDOC_DB_USERNAME||root}" db_username="${MINDOC_DB_USERNAME||root}"
db_password="${MINDOC_DB_PASSWORD||123456}" db_password="${MINDOC_DB_PASSWORD||123456}"
#是否使用SSL支持posgres可选的值有
#disable - No SSL
#require - Always SSL (skip verification)
#verify-ca - Always SSL (verify that the certificate presented by the server was signed by a trusted CA)
#verify-full - Always SSL (verify that the certification presented by the server was signed by a trusted CA and the server host name matches the one in the certificate)
db_sslmode="${MINDOC_DB_SSLMODE||disable}"
####################sqlite3 数据库配置########################### ####################sqlite3 数据库配置###########################
#db_adapter=sqlite3 #db_adapter=sqlite3

View File

@ -393,7 +393,7 @@ FROM md_books AS book
// " LEFT JOIN " + relationship.TableNameWithPrefix() + " AS rel ON book.book_id=rel.book_id AND rel.member_id = ?" + // " LEFT JOIN " + relationship.TableNameWithPrefix() + " AS rel ON book.book_id=rel.book_id AND rel.member_id = ?" +
// " LEFT JOIN " + relationship.TableNameWithPrefix() + " AS rel1 ON book.book_id=rel1.book_id AND rel1.role_id=0" + // " LEFT JOIN " + relationship.TableNameWithPrefix() + " AS rel1 ON book.book_id=rel1.book_id AND rel1.role_id=0" +
// " LEFT JOIN " + NewMember().TableNameWithPrefix() + " AS m ON rel1.member_id=m.member_id " + // " LEFT JOIN " + NewMember().TableNameWithPrefix() + " AS m ON rel1.member_id=m.member_id " +
// " WHERE rel.relationship_id > 0 ORDER BY book.order_index DESC,book.book_id DESC LIMIT " + fmt.Sprintf("%d,%d", offset, pageSize) // " WHERE rel.relationship_id > 0 ORDER BY book.order_index DESC,book.book_id DESC LIMIT " + fmt.Sprintf("%d,%d", pageSize, offset)
sql2 := `SELECT sql2 := `SELECT
book.*, book.*,
@ -410,9 +410,9 @@ FROM md_books AS book
LEFT JOIN md_relationship AS rel1 ON book.book_id = rel1.book_id AND rel1.role_id = 0 LEFT JOIN md_relationship AS rel1 ON book.book_id = rel1.book_id AND rel1.role_id = 0
LEFT JOIN md_members AS m ON rel1.member_id = m.member_id LEFT JOIN md_members AS m ON rel1.member_id = m.member_id
WHERE rel.role_id >= 0 or team.role_id >= 0 WHERE rel.role_id >= 0 or team.role_id >= 0
ORDER BY book.order_index, book.book_id DESC limit ?,?` ORDER BY book.order_index, book.book_id DESC limit ? offset ?`
_, err = o.Raw(sql2, memberId, memberId, offset, pageSize).QueryRows(&books) _, err = o.Raw(sql2, memberId, memberId, pageSize, offset).QueryRows(&books)
if err != nil { if err != nil {
logs.Error("分页查询项目列表 => ", err) logs.Error("分页查询项目列表 => ", err)
return return
@ -550,9 +550,9 @@ WHERE book.privately_owned = 0 or rel.role_id >=0 or team.role_id >=0`
as t group by book_id) as team on team.book_id=book.book_id as t group by book_id) as team on team.book_id=book.book_id
LEFT JOIN md_relationship AS rel1 ON rel1.book_id = book.book_id AND rel1.role_id = 0 LEFT JOIN md_relationship AS rel1 ON rel1.book_id = book.book_id AND rel1.role_id = 0
LEFT JOIN md_members AS mdmb ON rel1.member_id = mdmb.member_id LEFT JOIN md_members AS mdmb ON rel1.member_id = mdmb.member_id
WHERE book.privately_owned = 0 or rel.role_id >=0 or team.role_id >=0 ORDER BY order_index desc,book.book_id DESC LIMIT ?,?` WHERE book.privately_owned = 0 or rel.role_id >=0 or team.role_id >=0 ORDER BY order_index desc,book.book_id DESC limit ? offset ?`
_, err = o.Raw(sql2, memberId, memberId, offset, pageSize).QueryRows(&books) _, err = o.Raw(sql2, memberId, memberId, pageSize, offset).QueryRows(&books)
} else { } else {
count, err1 := o.QueryTable(book.TableNameWithPrefix()).Filter("privately_owned", 0).Count() count, err1 := o.QueryTable(book.TableNameWithPrefix()).Filter("privately_owned", 0).Count()
@ -566,9 +566,9 @@ WHERE book.privately_owned = 0 or rel.role_id >=0 or team.role_id >=0 ORDER BY o
sql := `SELECT book.*,rel.*,mdmb.account AS create_name,mdmb.real_name FROM md_books AS book sql := `SELECT book.*,rel.*,mdmb.account AS create_name,mdmb.real_name FROM md_books AS book
LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.role_id = 0 LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.role_id = 0
LEFT JOIN md_members AS mdmb ON rel.member_id = mdmb.member_id LEFT JOIN md_members AS mdmb ON rel.member_id = mdmb.member_id
WHERE book.privately_owned = 0 ORDER BY order_index DESC ,book.book_id DESC LIMIT ?,?` WHERE book.privately_owned = 0 ORDER BY order_index DESC ,book.book_id DESC limit ? offset ?`
_, err = o.Raw(sql, offset, pageSize).QueryRows(&books) _, err = o.Raw(sql, pageSize, offset).QueryRows(&books)
} }
return return
@ -604,9 +604,9 @@ WHERE (relationship_id > 0 OR book.privately_owned = 0 or team.team_member_id >
LEFT JOIN md_relationship AS rel1 ON rel1.book_id = book.book_id AND rel1.role_id = 0 LEFT JOIN md_relationship AS rel1 ON rel1.book_id = book.book_id AND rel1.role_id = 0
LEFT JOIN md_members AS mdmb ON rel1.member_id = mdmb.member_id LEFT JOIN md_members AS mdmb ON rel1.member_id = mdmb.member_id
WHERE (rel.relationship_id > 0 OR book.privately_owned = 0 or team.team_member_id > 0) WHERE (rel.relationship_id > 0 OR book.privately_owned = 0 or team.team_member_id > 0)
AND book.label LIKE ? ORDER BY order_index DESC ,book.book_id DESC LIMIT ?,?` AND book.label LIKE ? ORDER BY order_index DESC ,book.book_id DESC limit ? offset ?`
_, err = o.Raw(sql2, memberId, memberId, keyword, offset, pageSize).QueryRows(&books) _, err = o.Raw(sql2, memberId, memberId, keyword, pageSize, offset).QueryRows(&books)
return return
@ -622,9 +622,9 @@ WHERE (relationship_id > 0 OR book.privately_owned = 0 or team.team_member_id >
sql := `SELECT book.*,rel.*,mdmb.account AS create_name FROM md_books AS book sql := `SELECT book.*,rel.*,mdmb.account AS create_name FROM md_books AS book
LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.role_id = 0 LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.role_id = 0
LEFT JOIN md_members AS mdmb ON rel.member_id = mdmb.member_id LEFT JOIN md_members AS mdmb ON rel.member_id = mdmb.member_id
WHERE book.privately_owned = 0 AND book.label LIKE ? ORDER BY order_index DESC ,book.book_id DESC LIMIT ?,?` WHERE book.privately_owned = 0 AND book.label LIKE ? ORDER BY order_index DESC ,book.book_id DESC limit ? offset ?`
_, err = o.Raw(sql, keyword, offset, pageSize).QueryRows(&books) _, err = o.Raw(sql, keyword, pageSize, offset).QueryRows(&books)
return return

View File

@ -176,11 +176,11 @@ func (m *BookResult) FindToPager(pageIndex, pageSize int) (books []*BookResult,
FROM md_books AS book FROM md_books AS book
LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.role_id = 0 LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.role_id = 0
LEFT JOIN md_members AS m ON rel.member_id = m.member_id LEFT JOIN md_members AS m ON rel.member_id = m.member_id
ORDER BY book.order_index DESC ,book.book_id DESC LIMIT ?,?` ORDER BY book.order_index DESC ,book.book_id DESC limit ? offset ?`
offset := (pageIndex - 1) * pageSize offset := (pageIndex - 1) * pageSize
_, err = o.Raw(sql, offset, pageSize).QueryRows(&books) _, err = o.Raw(sql, pageSize, offset).QueryRows(&books)
return return
} }

View File

@ -165,9 +165,9 @@ func (m *DocumentHistory) FindToPager(docId, pageIndex, pageSize int) (docs []*D
FROM md_document_history AS history FROM md_document_history AS history
LEFT JOIN md_members AS m1 ON history.member_id = m1.member_id LEFT JOIN md_members AS m1 ON history.member_id = m1.member_id
LEFT JOIN md_members AS m2 ON history.modify_at = m2.member_id LEFT JOIN md_members AS m2 ON history.modify_at = m2.member_id
WHERE history.document_id = ? ORDER BY history.history_id DESC LIMIT ?,?;` WHERE history.document_id = ? ORDER BY history.history_id DESC limit ? offset ?;`
_, err = o.Raw(sql, docId, offset, pageSize).QueryRows(&docs) _, err = o.Raw(sql, docId, pageSize, offset).QueryRows(&docs)
if err != nil { if err != nil {
return return

View File

@ -96,7 +96,7 @@ WHERE book.privately_owned = 0 AND (book.book_name LIKE ? OR book.description LI
WHERE blog.blog_status = 'public' AND (blog.blog_release LIKE ? OR blog.blog_title LIKE ?) WHERE blog.blog_status = 'public' AND (blog.blog_release LIKE ? OR blog.blog_title LIKE ?)
) AS union_table ) AS union_table
ORDER BY create_time DESC ORDER BY create_time DESC
LIMIT ?, ?;` LIMIT ? OFFSET ?;`
err = o.Raw(sql1, keyword, keyword).QueryRow(&totalCount) err = o.Raw(sql1, keyword, keyword).QueryRow(&totalCount)
if err != nil { if err != nil {
@ -128,7 +128,7 @@ WHERE book.privately_owned = 0 AND (book.book_name LIKE ? OR book.description LI
totalCount += c totalCount += c
_, err = o.Raw(sql2, keyword, keyword, keyword, keyword, keyword, keyword, offset, pageSize).QueryRows(&searchResult) _, err = o.Raw(sql2, keyword, keyword, keyword, keyword, keyword, keyword, pageSize, offset).QueryRows(&searchResult)
if err != nil { if err != nil {
logs.Error("查询搜索结果失败 -> ", err) logs.Error("查询搜索结果失败 -> ", err)
return return
@ -224,7 +224,7 @@ FROM (
(blog.blog_release LIKE ? OR blog.blog_title LIKE ?) (blog.blog_release LIKE ? OR blog.blog_title LIKE ?)
) AS union_table ) AS union_table
ORDER BY create_time DESC ORDER BY create_time DESC
LIMIT ?, ?;` LIMIT ? OFFSET ?;`
err = o.Raw(sql1, memberId, memberId, keyword, keyword).QueryRow(&totalCount) err = o.Raw(sql1, memberId, memberId, keyword, keyword).QueryRow(&totalCount)
if err != nil { if err != nil {
@ -262,7 +262,7 @@ WHERE (book.privately_owned = 0 OR rel1.relationship_id > 0 or team.team_member_
totalCount += c totalCount += c
_, err = o.Raw(sql2, memberId, memberId, keyword, keyword, memberId, memberId, keyword, keyword, memberId, keyword, keyword, offset, pageSize).QueryRows(&searchResult) _, err = o.Raw(sql2, memberId, memberId, keyword, keyword, memberId, memberId, keyword, keyword, memberId, keyword, keyword, pageSize, offset).QueryRows(&searchResult)
if err != nil { if err != nil {
return return
} }

View File

@ -246,9 +246,9 @@ as t group by book_id) as team
LEFT JOIN md_relationship AS rel1 ON rel1.book_id = book.book_id AND rel1.role_id = 0 LEFT JOIN md_relationship AS rel1 ON rel1.book_id = book.book_id AND rel1.role_id = 0
LEFT JOIN md_members AS mdmb ON rel1.member_id = mdmb.member_id LEFT JOIN md_members AS mdmb ON rel1.member_id = mdmb.member_id
WHERE book.item_id = ? AND (book.privately_owned = 0 or rel.role_id >= 0 or team.role_id >= 0) WHERE book.item_id = ? AND (book.privately_owned = 0 or rel.role_id >= 0 or team.role_id >= 0)
ORDER BY order_index desc,book.book_id DESC LIMIT ?,?` ORDER BY order_index desc,book.book_id DESC limit ? offset ?`
_, err = o.Raw(sql2, memberId, memberId, item.ItemId, offset, pageSize).QueryRows(&books) _, err = o.Raw(sql2, memberId, memberId, item.ItemId, pageSize, offset).QueryRows(&books)
return return
@ -264,9 +264,9 @@ as t group by book_id) as team
sql := `SELECT book.*,rel.*,mdmb.account AS create_name FROM md_books AS book sql := `SELECT book.*,rel.*,mdmb.account AS create_name FROM md_books AS book
LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.role_id = 0 LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.role_id = 0
LEFT JOIN md_members AS mdmb ON rel.member_id = mdmb.member_id LEFT JOIN md_members AS mdmb ON rel.member_id = mdmb.member_id
WHERE book.item_id = ? AND book.privately_owned = 0 ORDER BY order_index desc,book.book_id DESC LIMIT ?,?` WHERE book.item_id = ? AND book.privately_owned = 0 ORDER BY order_index desc,book.book_id DESC limit ? offset ?`
_, err = o.Raw(sql, item.ItemId, offset, pageSize).QueryRows(&books) _, err = o.Raw(sql, item.ItemId, pageSize, offset).QueryRows(&books)
return return

View File

@ -72,7 +72,7 @@ func (m *MemberRelationshipResult) FindForUsersByBookId(lang string, bookId, pag
var members []*MemberRelationshipResult var members []*MemberRelationshipResult
sql1 := "SELECT * FROM md_relationship AS rel LEFT JOIN md_members as mdmb ON rel.member_id = mdmb.member_id WHERE rel.book_id = ? ORDER BY rel.relationship_id DESC LIMIT ?,?" sql1 := "SELECT * FROM md_relationship AS rel LEFT JOIN md_members as mdmb ON rel.member_id = mdmb.member_id WHERE rel.book_id = ? ORDER BY rel.relationship_id DESC limit ? offset ?"
sql2 := "SELECT count(*) AS total_count FROM md_relationship AS rel LEFT JOIN md_members as mdmb ON rel.member_id = mdmb.member_id WHERE rel.book_id = ?" sql2 := "SELECT count(*) AS total_count FROM md_relationship AS rel LEFT JOIN md_members as mdmb ON rel.member_id = mdmb.member_id WHERE rel.book_id = ?"
@ -86,7 +86,7 @@ func (m *MemberRelationshipResult) FindForUsersByBookId(lang string, bookId, pag
offset := (pageIndex - 1) * pageSize offset := (pageIndex - 1) * pageSize
_, err = o.Raw(sql1, bookId, offset, pageSize).QueryRows(&members) _, err = o.Raw(sql1, bookId, pageSize, offset).QueryRows(&members)
if err != nil { if err != nil {
return members, 0, err return members, 0, err

View File

@ -12,10 +12,10 @@ import (
//团队. //团队.
type Team struct { type Team struct {
TeamId int `orm:"column(team_id);pk;auto;unique;" json:"team_id"` TeamId int `orm:"column(team_id);pk;auto;unique;" json:"team_id"`
TeamName string `orm:"column(team_name);size(255);description(团队名称)" json:"team_name"` TeamName string `orm:"column(team_name);size(255);description(团队名称)" json:"team_name"`
MemberId int `orm:"column(member_id);type(int);description(创建人id)" json:"member_id"` MemberId int `orm:"column(member_id);type(int);description(创建人id)" json:"member_id"`
IsDelete bool `orm:"column(is_delete);default(0);description(是否删除 0否 1:是)" json:"is_delete"` IsDelete bool `orm:"column(is_delete);default(false);description(是否删除 false否 true:是)" json:"is_delete"`
CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add;description(创建时间)" json:"create_time"` CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add;description(创建时间)" json:"create_time"`
MemberCount int `orm:"-" json:"member_count"` MemberCount int `orm:"-" json:"member_count"`
BookCount int `orm:"-" json:"book_count"` BookCount int `orm:"-" json:"book_count"`
MemberName string `orm:"-" json:"member_name"` MemberName string `orm:"-" json:"member_name"`