Skip to content

Commit

Permalink
Merge pull request #62 from jsixface/feature/WhereNotEquals
Browse files Browse the repository at this point in the history
Add not equals validation
  • Loading branch information
luizperes authored Nov 1, 2017
2 parents 58a06e0 + f0d7b3b commit fa64865
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lexical/lexical.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ func Token() (uint8, *TokenError) {
if lexeme == "=" {
state = S_SMALLER_OR_EQUAL
break
} else if lexeme == ">" {
char = nextChar()
return T_NOT_EQUAL, nil
}
return T_SMALLER, nil
case S_SMALLER_OR_EQUAL:
Expand Down
3 changes: 3 additions & 0 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ func (n *NodeEqual) LeftValue() NodeExpr {

// NOT EQUAL
func (n *NodeNotEqual) Assertion(lvalue string, rvalue string) bool {
if len(lvalue) == 40 {
return lvalue[0:len(rvalue)] != rvalue
}
return lvalue != rvalue
}

Expand Down
22 changes: 22 additions & 0 deletions runtime/commits_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runtime

import (
"fmt"
"path/filepath"
"testing"

Expand Down Expand Up @@ -82,3 +83,24 @@ func TestSelectedFieldsCount(t *testing.T) {
t.Errorf("Selected 'author' and 'hash'. Got %v", table.fields)
}
}

func TestNotEqualsInWhereLTGT(t *testing.T) {
queryData := "select committer, hash from commits limit 1"
table := getTableForQuery(queryData, "../", t)
firstCommitter := table.rows[0]["committer"].(string)
query := fmt.Sprintf("select committer, hash from commits where committer <> '%s' limit 1", firstCommitter)
table = getTableForQuery(query, "../", t)
if firstCommitter == table.rows[0]["committer"].(string) {
t.Errorf("Still got the same committer as the first one. - %s", firstCommitter)
}
}
func TestNotEqualsInWhere(t *testing.T) {
queryData := "select committer, hash from commits limit 1"
table := getTableForQuery(queryData, "../", t)
firstCommitter := table.rows[0]["committer"].(string)
query := fmt.Sprintf("select committer, hash from commits where committer != '%s' limit 1", firstCommitter)
table = getTableForQuery(query, "../", t)
if firstCommitter == table.rows[0]["committer"].(string) {
t.Errorf("Still got the same committer as the first one. - %s", firstCommitter)
}
}
11 changes: 9 additions & 2 deletions runtime/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ func testAllFieldsFromTable(fields []string, table string) error {
return err
}
}

return nil
}

Expand All @@ -118,7 +117,9 @@ func (v *RuntimeVisitor) VisitExpr(n parser.NodeExpr) error {
case reflect.TypeOf(new(parser.NodeIn)):
g := n.(*parser.NodeIn)
return v.VisitIn(g)

case reflect.TypeOf(new(parser.NodeNotEqual)):
g := n.(*parser.NodeNotEqual)
return v.VisitNotEqual(g)
}

return nil
Expand All @@ -128,7 +129,13 @@ func (v *RuntimeVisitor) VisitEqual(n *parser.NodeEqual) error {
lvalue := n.LeftValue().(*parser.NodeId).Value()
rvalue := n.RightValue().(*parser.NodeLiteral).Value()
boolRegister = n.Assertion(metadata(lvalue), rvalue)
return nil
}

func (v *RuntimeVisitor) VisitNotEqual(n *parser.NodeNotEqual) error {
lvalue := n.LeftValue().(*parser.NodeId).Value()
rvalue := n.RightValue().(*parser.NodeLiteral).Value()
boolRegister = n.Assertion(metadata(lvalue), rvalue)
return nil
}

Expand Down

0 comments on commit fa64865

Please sign in to comment.