65 Star 396 Fork 128

admpub/nging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
driver.go 5.20 KB
一键复制 编辑 原始数据 按行查看 历史
admpub 提交于 2019-09-21 15:43 . update
/*
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 driver
import (
"github.com/admpub/nging/application/library/common"
"github.com/admpub/nging/application/library/dbmanager/result"
"github.com/webx-top/echo"
)
var (
drivers = map[string]Driver{}
DefaultDriver = &BaseDriver{}
)
type Driver interface {
Init(echo.Context, *DbAuth)
SetURLGenerator(func(string, ...string) string) Driver
GenURL(string, ...string) string
Results() []result.Resulter
AddResults(...result.Resulter) Driver
SetResults(...result.Resulter) Driver
SaveResults() Driver
SavedResults() interface{}
IsSupported(string) bool
Login() error
Logout() error
ProcessList() error
Privileges() error
Info() error
CreateDb() error
ModifyDb() error
ListDb() error
CreateTable() error
ModifyTable() error
ListTable() error
ViewTable() error
ListData() error
CreateData() error
Indexes() error
Foreign() error
Trigger() error
RunCommand() error
Import() error
Export() error
Analysis() error
Name() string
}
func NewBaseDriver() *BaseDriver {
return &BaseDriver{}
}
type BaseDriver struct {
echo.Context
*DbAuth
results []result.Resulter
urlGenerator func(string, ...string) string
}
func (m *BaseDriver) SetURLGenerator(fn func(string, ...string) string) Driver {
m.urlGenerator = fn
return m
}
func (m *BaseDriver) GenURL(op string, args ...string) string {
return m.urlGenerator(op, args...)
}
func (m *BaseDriver) Results() []result.Resulter {
return m.results
}
func (m *BaseDriver) AddResults(rs ...result.Resulter) Driver {
if m.results == nil {
m.results = []result.Resulter{}
}
m.results = append(m.results, rs...)
return m
}
func (m *BaseDriver) SetResults(rs ...result.Resulter) Driver {
m.results = rs
return m
}
func (m *BaseDriver) SaveResults() Driver {
if m.results == nil {
return m
}
/*
if v, y := m.Flash(`dbMgrResults`).([]result.Resulter); y {
m.results = append(v, m.results...)
}
*/
m.Session().AddFlash(m.results, `dbMgrResults`)
return m
}
func (m *BaseDriver) SavedResults() interface{} {
if v := m.Flash(`dbMgrResults`); v != nil {
return v
}
return m.results
}
func (m *BaseDriver) Init(ctx echo.Context, auth *DbAuth) {
m.Context = ctx
m.DbAuth = auth
}
func (m *BaseDriver) IsSupported(operation string) bool {
return true
}
func (m *BaseDriver) Login() error {
return nil
}
func (m *BaseDriver) Logout() error {
return nil
}
func (m *BaseDriver) ProcessList() error {
return nil
}
func (m *BaseDriver) Privileges() error {
return nil
}
func (m *BaseDriver) Info() error {
return nil
}
func (m *BaseDriver) CreateDb() error {
return nil
}
func (m *BaseDriver) ModifyDb() error {
return nil
}
func (m *BaseDriver) ListDb() error {
return nil
}
func (m *BaseDriver) CreateTable() error {
return nil
}
func (m *BaseDriver) ModifyTable() error {
return nil
}
func (m *BaseDriver) ListTable() error {
return nil
}
func (m *BaseDriver) ViewTable() error {
return nil
}
func (m *BaseDriver) ListData() error {
return nil
}
func (m *BaseDriver) CreateData() error {
return nil
}
func (m *BaseDriver) Indexes() error {
return nil
}
func (m *BaseDriver) Foreign() error {
return nil
}
func (m *BaseDriver) Trigger() error {
return nil
}
func (m *BaseDriver) RunCommand() error {
return nil
}
func (m *BaseDriver) Import() error {
return nil
}
func (m *BaseDriver) Export() error {
return nil
}
func (m *BaseDriver) Analysis() error {
data := m.Data()
data.SetInfo(m.T(`不支持此功能`), 0)
return m.JSON(data)
}
func (m *BaseDriver) Name() string {
return `Base`
}
//========================================================
func (m *BaseDriver) CheckErr(err error) interface{} {
return common.Err(m.Context, err)
}
func (m *BaseDriver) SetOk(msg string) {
m.Session().AddFlash(common.Ok(msg))
}
func (m *BaseDriver) SetFail(msg string) {
m.Session().AddFlash(msg)
}
func (m *BaseDriver) ReturnTo(rets ...string) error {
returnTo := m.Form(`return_to`)
if len(returnTo) == 0 {
if len(rets) > 0 {
returnTo = rets[0]
} else {
returnTo = m.Request().Referer()
}
}
m.SaveResults()
return m.Redirect(returnTo)
}
func Register(name string, driver Driver) {
drivers[name] = driver
}
func Get(name string) (Driver, bool) {
d, y := drivers[name]
return d, y
}
func GetForce(name string) Driver {
d, y := drivers[name]
if !y {
d = DefaultDriver
}
return d
}
func Has(name string) bool {
_, y := drivers[name]
return y
}
func GetAll() map[string]Driver {
return drivers
}
func Unregister(name string) {
_, y := drivers[name]
if y {
delete(drivers, name)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/admpub/nging.git
git@gitee.com:admpub/nging.git
admpub
nging
nging
v2.0.5

搜索帮助

0d507c66 1850385 C8b1a773 1850385