65 Star 395 Fork 128

admpub/nging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dbSync.go 2.57 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package model
import (
"net/url"
"strings"
"github.com/admpub/nging/application/dbschema"
"github.com/admpub/nging/application/model/base"
"github.com/webx-top/db"
"github.com/webx-top/echo"
)
type DbSyncWithAccount struct {
*dbschema.DbSync
SrcAccount *dbschema.DbAccount `db:"-,relation=id:source_account_id"`
DstAccount *dbschema.DbAccount `db:"-,relation=id:destination_account_id"`
}
func NewDbSync(ctx echo.Context) *DbSync {
return &DbSync{
DbSync: &dbschema.DbSync{},
Base: base.New(ctx),
}
}
type DbSync struct {
*dbschema.DbSync
*base.Base
}
func (a *DbSync) ToDSNFromAccount(acc *dbschema.DbAccount) string {
return a.ToDSN(acc.User, acc.Password, acc.Host, acc.Name)
}
func (a *DbSync) ToDSN(user, pwd, host, db string) string {
//test:test@(127.0.0.1:3306)/test_0
if len(user) == 0 {
user = `root`
}
if len(pwd) == 0 {
pwd = `root`
}
if len(host) == 0 {
host = `127.0.0.1:3306`
}
return url.QueryEscape(user) + `:` + url.QueryEscape(pwd) + `@(` + host + `)/` + db
}
func (a *DbSync) ParseDSN(dsn string) (user string, pwd string, host string, dbName string) {
idx := strings.Index(dsn, `:`)
user, _ = url.QueryUnescape(dsn[0:idx])
dsn = dsn[idx+1:]
idx = strings.Index(dsn, `@`)
pwd, _ = url.QueryUnescape(dsn[0:idx])
dsn = dsn[idx+1:]
idx = strings.Index(dsn, `/`)
host = dsn[0:idx]
host = strings.TrimPrefix(host, `(`)
host = strings.TrimSuffix(host, `)`)
dbName = dsn[idx+1:]
return
}
func (a *DbSync) HidePassword(dsn string) string {
idx := strings.Index(dsn, `:`)
user, _ := url.QueryUnescape(dsn[0:idx])
dsn = dsn[idx+1:]
idx = strings.Index(dsn, `@`)
return user + `:***@` + dsn[idx+1:]
}
func (a *DbSync) Add() (interface{}, error) {
return a.DbSync.Add()
}
func (a *DbSync) Edit(mw func(db.Result) db.Result, args ...interface{}) error {
return a.DbSync.Edit(mw, args...)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/admpub/nging.git
git@gitee.com:admpub/nging.git
admpub
nging
nging
v2.1.1

搜索帮助