Skip to content

Commit

Permalink
Merge pull request henson#58 from sndnvaps/update/2019.12.24
Browse files Browse the repository at this point in the history
修复ip地址唯一性问题。
  • Loading branch information
henson committed Dec 24, 2019
2 parents a68126d + 2b12aca commit 6b25a91
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
9 changes: 5 additions & 4 deletions pkg/models/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// IP struct
type IP struct {
ID int64 `xorm:"pk autoincr" json:"-"`
Data string `xorm:"NOT NULL" json:"ip"`
Data string `xorm:"NOT NULL unique" json:"ip"`
Type1 string `xorm:"NOT NULL" json:"type1"`
Type2 string `xorm:"NULL" json:"type2,omitempty"`
Speed int64 `xorm:"NOT NULL" json:"speed,omitempty"`
Expand Down Expand Up @@ -67,7 +67,8 @@ func DeleteIP(ip *IP) error {

func getOne(ip string) *IP {
var tmpIp IP
result, _ := x.Where("data=?", ip).Get(tmpIp)
//只获取第一条记录
result, _ := x.Where("data=?", ip).Limit(0, 1).Get(tmpIp)
if result {
return &tmpIp
}
Expand Down Expand Up @@ -143,10 +144,10 @@ func Update(ip *IP) error {
}

//Test if have https proxy in database
//just test on mysql database
//just test on MySQL/Mariadb database
// dbName: ProxyPool
// dbTableName: ip
// select distinct if(exists(select * from ip where type2='https'),1,0) as a from ip;
// select distinct if(exists(select * from ProxyPool.ip where type1='https'),1,0) as a from ProxyPool.ip;
func TestHttps() bool {
has, err := x.Exist(&IP{Type1: "https"})
if err != nil {
Expand Down
9 changes: 2 additions & 7 deletions pkg/storage/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,8 @@ func ProxyFind(value string) (ip *models.IP) {
// ProxyAdd .
func ProxyAdd(ip *models.IP) {

//need to check Ip is in the database or not
// when temp.Data == "", we just insert the ip into db, or update it
if temp := models.GetOne(ip.Data); temp.Data == "" {
models.InsertIps(ip)
} else {
ProxyUpdate(ip)
}
models.InsertIps(ip)

}

// ProxyUpdate .
Expand Down

0 comments on commit 6b25a91

Please sign in to comment.