Permalink
Browse files

updated query specification

  • Loading branch information...
Fs02 committed Mar 4, 2018
1 parent b49bf23 commit 3935505998685f428eb045fc51b11ffd91f759e0
Showing with 717 additions and 2 deletions.
  1. +2 −0 .gitignore
  2. +27 −0 Gopkg.lock
  3. +30 −0 Gopkg.toml
  4. +84 −2 query/condition.go
  5. +574 −0 query/condition_test.go
@@ -0,0 +1,2 @@
vendor

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,30 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[prune]
go-tests = true
unused-packages = true
@@ -36,28 +36,110 @@ type Condition struct {
Inner []Condition
}

func (c Condition) None() bool {
return (c.Type == ConditionAnd ||
c.Type == ConditionOr ||
c.Type == ConditionXor ||
c.Type == ConditionNot) &&
len(c.Inner) == 0
}

func (c Condition) And(condition ...Condition) Condition {
if c.None() && len(condition) == 1 {
return condition[0]
} else if c.Type == ConditionAnd {
c.Inner = append(c.Inner, condition...)
return c
}

inner := append([]Condition{c}, condition...)
return And(inner...)
}

func (c Condition) Or(condition ...Condition) Condition {
if c.None() && len(condition) == 1 {
return condition[0]
} else if c.Type == ConditionOr || c.None() {
c.Type = ConditionOr
c.Inner = append(c.Inner, condition...)
return c
}

inner := append([]Condition{c}, condition...)
return Or(inner...)
}

func (c Condition) Xor(condition ...Condition) Condition {
if c.None() && len(condition) == 1 {
return condition[0]
} else if c.Type == ConditionXor || c.None() {
c.Type = ConditionXor
c.Inner = append(c.Inner, condition...)
return c
}

inner := append([]Condition{c}, condition...)
return Xor(inner...)
}

func And(inner ...Condition) Condition {
if len(inner) == 1 {
return inner[0]
}

return Condition{
Type: ConditionAnd,
Inner: inner,
}
}

func Or(inner ...Condition) Condition {
if len(inner) == 1 {
return inner[0]
}

return Condition{
Type: ConditionOr,
Inner: inner,
}
}

func Xor(inner ...Condition) Condition {
if len(inner) == 1 {
return inner[0]
}

return Condition{
Type: ConditionXor,
Inner: inner,
}
}

func Not(inner ...Condition) Condition {
if len(inner) == 1 {
c := inner[0]
switch c.Type {
case ConditionEq:
c.Type = ConditionNe
case ConditionLt:
c.Type = ConditionGte
case ConditionLte:
c.Type = ConditionGt
case ConditionGt:
c.Type = ConditionLte
case ConditionGte:
c.Type = ConditionLt
case ConditionNil:
c.Type = ConditionNotNil
case ConditionIn:
c.Type = ConditionNin
case ConditionLike:
c.Type = ConditionNotLike
}

return c
}

return Condition{
Type: ConditionNot,
Inner: inner,
@@ -160,9 +242,9 @@ func NotLike(col string, expr string) Condition {

func Fragment(col string, expr string, val interface{}) Condition {
return Condition{
Type: ConditionFrag,
Type: ConditionFragment,
Column: col,
Value: pattern,
Value: val,
Expr: expr,
}
}
Oops, something went wrong.

0 comments on commit 3935505

Please sign in to comment.