From 88d44fee0c168170af9cc3c7878b9cebe4e11d6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Mon, 10 Dec 2018 12:10:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0vendor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yunion.io/x/pkg/util/secrules/secrules.go | 19 ++++++++---- vendor/yunion.io/x/sqlchemy/conditions.go | 15 +++++++++- vendor/yunion.io/x/sqlchemy/filter.go | 5 ++++ vendor/yunion.io/x/sqlchemy/functions.go | 10 +++++-- vendor/yunion.io/x/sqlchemy/insert.go | 2 +- vendor/yunion.io/x/sqlchemy/query.go | 1 + vendor/yunion.io/x/sqlchemy/update.go | 30 +++++++++++-------- 7 files changed, 61 insertions(+), 21 deletions(-) diff --git a/vendor/yunion.io/x/pkg/util/secrules/secrules.go b/vendor/yunion.io/x/pkg/util/secrules/secrules.go index 6437ff1ae9..037bb62d45 100644 --- a/vendor/yunion.io/x/pkg/util/secrules/secrules.go +++ b/vendor/yunion.io/x/pkg/util/secrules/secrules.go @@ -127,12 +127,21 @@ func ParseSecurityRule(pattern string) (*SecurityRule, error) { return nil, ErrInvalidAction } } else if status == SEG_IP { + // NOTE regutils.MatchCIDR actually also matches IP address without prefix length if regutils.MatchCIDR(seg) { - _, rule.IPNet, _ = net.ParseCIDR(seg) - } else if regutils.MatchIPAddr(seg) { - rule.IPNet = &net.IPNet{ - IP: net.ParseIP(seg), - Mask: net.CIDRMask(32, 32), + if idx := strings.Index(seg, "/"); idx > -1 { + if _, ipnet, err := net.ParseCIDR(seg); err != nil { + return nil, ErrInvalidNet + } else { + rule.IPNet = ipnet + } + } else if ip := net.ParseIP(seg); ip != nil { + rule.IPNet = &net.IPNet{ + IP: ip, + Mask: net.CIDRMask(32, 32), + } + } else { + return nil, ErrInvalidIPAddr } } else { rule.IPNet = &net.IPNet{ diff --git a/vendor/yunion.io/x/sqlchemy/conditions.go b/vendor/yunion.io/x/sqlchemy/conditions.go index 064d60c437..59fd7ed8fb 100644 --- a/vendor/yunion.io/x/sqlchemy/conditions.go +++ b/vendor/yunion.io/x/sqlchemy/conditions.go @@ -159,12 +159,25 @@ func IsEmpty(f IQueryField) ICondition { return &c } +type SIsNullOrEmptyCondition struct { + SSingleCondition +} + +func (c *SIsNullOrEmptyCondition) WhereClause() string { + return fmt.Sprintf("%s IS NULL OR LENGTH(%s) = 0", c.field.Reference(), c.field.Reference()) +} + +func IsNullOrEmpty(f IQueryField) ICondition { + c := SIsNullOrEmptyCondition{NewSingleCondition(f)} + return &c +} + type SIsNotEmptyCondition struct { SSingleCondition } func (c *SIsNotEmptyCondition) WhereClause() string { - return fmt.Sprintf("LENGTH(%s) > 0", c.field.Reference()) + return fmt.Sprintf("%s IS NOT NULL AND LENGTH(%s) > 0", c.field.Reference(), c.field.Reference()) } func IsNotEmpty(f IQueryField) ICondition { diff --git a/vendor/yunion.io/x/sqlchemy/filter.go b/vendor/yunion.io/x/sqlchemy/filter.go index 855fcba11f..9689d83492 100644 --- a/vendor/yunion.io/x/sqlchemy/filter.go +++ b/vendor/yunion.io/x/sqlchemy/filter.go @@ -107,6 +107,11 @@ func (q *SQuery) IsEmpty(f string) *SQuery { return q.Filter(cond) } +func (q *SQuery) IsNullOrEmpty(f string) *SQuery { + cond := IsNullOrEmpty(q.Field(f)) + return q.Filter(cond) +} + func (q *SQuery) IsNotEmpty(f string) *SQuery { cond := IsNotEmpty(q.Field(f)) return q.Filter(cond) diff --git a/vendor/yunion.io/x/sqlchemy/functions.go b/vendor/yunion.io/x/sqlchemy/functions.go index c55920f836..237cdb31f5 100644 --- a/vendor/yunion.io/x/sqlchemy/functions.go +++ b/vendor/yunion.io/x/sqlchemy/functions.go @@ -38,8 +38,14 @@ func NewFunctionField(name string, funcexp string, fields ...IQueryField) SFunct return ff } -func COUNT(name string) IQueryField { - ff := NewFunctionField(name, "COUNT(*)") +func COUNT(name string, field ...IQueryField) IQueryField { + var expr string + if len(field) == 0 { + expr = "COUNT(*)" + } else { + expr = "COUNT(%s)" + } + ff := NewFunctionField(name, expr, field...) return &ff } diff --git a/vendor/yunion.io/x/sqlchemy/insert.go b/vendor/yunion.io/x/sqlchemy/insert.go index c7442588f0..65b8100248 100644 --- a/vendor/yunion.io/x/sqlchemy/insert.go +++ b/vendor/yunion.io/x/sqlchemy/insert.go @@ -37,7 +37,7 @@ func (t *STableSpec) insertSqlPrep(dataFields map[string]interface{}) (string, [ createdAtFields = append(createdAtFields, k) names = append(names, fmt.Sprintf("`%s`", k)) format = append(format, "UTC_TIMESTAMP()") - } else if ov != nil && !c.IsZero(ov) && !isAutoInc { + } else if ov != nil && (!c.IsZero(ov) || !c.IsText()) && !isAutoInc { v := c.ConvertFromValue(ov) values = append(values, v) names = append(names, fmt.Sprintf("`%s`", k)) diff --git a/vendor/yunion.io/x/sqlchemy/query.go b/vendor/yunion.io/x/sqlchemy/query.go index b72b412856..60b2cd7cd5 100644 --- a/vendor/yunion.io/x/sqlchemy/query.go +++ b/vendor/yunion.io/x/sqlchemy/query.go @@ -450,6 +450,7 @@ func (q *SQuery) AllStringMap() ([]map[string]string, error) { if err != nil { return nil, err } + defer rows.Close() results := make([]map[string]string, 0) for rows.Next() { result, err := q.rowScan2StringMap(rows) diff --git a/vendor/yunion.io/x/sqlchemy/update.go b/vendor/yunion.io/x/sqlchemy/update.go index f88e1a6bde..3a6512d2fb 100644 --- a/vendor/yunion.io/x/sqlchemy/update.go +++ b/vendor/yunion.io/x/sqlchemy/update.go @@ -83,17 +83,16 @@ func (us *SUpdateSession) saveUpdate(dt interface{}) (map[string]SUpdateDiff, er setters := make(map[string]SUpdateDiff) for _, c := range us.tableSpec.columns { k := c.Name() - of, ok := ofields[k] - if !ok { - continue - } + of := ofields[k] nf := fields[k] - if c.IsPrimary() && !c.IsZero(of) { // skip update primary key - primaries[k] = of - continue - } else if c.IsKeyIndex() && !c.IsZero(of) { - keyIndexes[k] = of - continue + if !gotypes.IsNil(of) { + if c.IsPrimary() && !c.IsZero(of) { // skip update primary key + primaries[k] = of + continue + } else if c.IsKeyIndex() && !c.IsZero(of) { + keyIndexes[k] = of + continue + } } nc, ok := c.(*SIntegerColumn) if ok && nc.IsAutoVersion { @@ -108,6 +107,9 @@ func (us *SUpdateSession) saveUpdate(dt interface{}) (map[string]SUpdateDiff, er if reflect.DeepEqual(of, nf) { continue } + if c.IsZero(nf) && c.IsText() { + nf = nil + } setters[k] = SUpdateDiff{old: of, new: nf, col: c} } @@ -125,8 +127,12 @@ func (us *SUpdateSession) saveUpdate(dt interface{}) (map[string]SUpdateDiff, er } else { buf.WriteString(", ") } - buf.WriteString(fmt.Sprintf("`%s` = ?", k)) - vars = append(vars, v.col.ConvertFromValue(v.new)) + if gotypes.IsNil(v.new) { + buf.WriteString(fmt.Sprintf("`%s` = NULL", k)) + } else { + buf.WriteString(fmt.Sprintf("`%s` = ?", k)) + vars = append(vars, v.col.ConvertFromValue(v.new)) + } } for _, versionField := range versionFields { buf.WriteString(fmt.Sprintf(", `%s` = `%s` + 1", versionField, versionField))