-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbAccount.go
113 lines (100 loc) · 3.02 KB
/
dbAccount.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
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 (
"errors"
"github.com/webx-top/com"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/nging-plugins/dbmanager/application/dbschema"
"github.com/nging-plugins/dbmanager/application/library/dbmanager/driver/mysql"
)
func NewDbAccount(ctx echo.Context) *DbAccount {
return &DbAccount{
NgingDbAccount: dbschema.NewNgingDbAccount(ctx),
}
}
type DbAccount struct {
*dbschema.NgingDbAccount
}
func (a *DbAccount) SetOptions() error {
options := echo.H{}
charset := a.Context().Formx(`charset`).String()
if len(charset) > 0 {
if !com.InSlice(charset, mysql.Charsets) {
return a.Context().E(`字符集charset值无效`)
}
options.Set(`charset`, charset)
}
b, _ := com.JSONEncode(options)
a.Options = com.Bytes2str(b)
return nil
}
func (a *DbAccount) setDefaultValue() {
if len(a.NgingDbAccount.Engine) == 0 {
a.NgingDbAccount.Engine = `mysql`
}
switch a.NgingDbAccount.Engine {
case `mysql`:
if len(a.NgingDbAccount.Host) == 0 {
a.NgingDbAccount.Host = `127.0.0.1:3306`
}
if len(a.NgingDbAccount.User) == 0 {
a.NgingDbAccount.User = `root`
}
case `redis`:
if len(a.NgingDbAccount.Host) == 0 {
a.NgingDbAccount.Host = `127.0.0.1:6379`
}
a.NgingDbAccount.Name = ``
/*
if len(a.NgingDbAccount.Name) == 0 {
a.NgingDbAccount.Name = `0`
}
*/
}
}
func (a *DbAccount) Add() (interface{}, error) {
if len(a.NgingDbAccount.Title) == 0 {
return nil, errors.New(a.Context().T(`请输入标题`))
}
num, err := a.Count(nil, db.And(db.Cond{`uid`: a.Uid}, db.Cond{`title`: a.Title}))
if err != nil {
return nil, err
}
if num > 0 {
return nil, errors.New(a.Context().T(`标题已存在,请设置为一个从未使用过的标题`))
}
a.setDefaultValue()
return a.NgingDbAccount.Insert()
}
func (a *DbAccount) Edit(id uint, mw func(db.Result) db.Result, args ...interface{}) error {
if len(a.NgingDbAccount.Title) == 0 {
return errors.New(a.Context().T(`请输入标题`))
}
num, err := a.Count(nil, db.And(
db.Cond{`uid`: a.Uid},
db.Cond{`title`: a.Title},
db.Cond{`id`: db.NotEq(id)},
))
if err != nil {
return err
}
if num > 0 {
return errors.New(a.Context().T(`标题已存在,请设置为一个从未使用过的标题`))
}
a.setDefaultValue()
return a.NgingDbAccount.Update(mw, args...)
}