Skip to content

Commit

Permalink
Merge pull request #61 from jsixface/bug/NilStarSelect
Browse files Browse the repository at this point in the history
Add validation for fields in where clauses.
  • Loading branch information
luizperes authored Oct 30, 2017
2 parents 3618925 + 3dc161d commit 58a06e0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion runtime/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func getTableForQuery(query, directory string, t *testing.T) *TableData {
visitor := new(RuntimeVisitor)
err := visitor.Visit(ast)
failTestIfError(err, t)

findWalkType(ast)
tableData, err := walkCommits(ast, visitor)
failTestIfError(err, t)
return tableData
Expand Down
27 changes: 27 additions & 0 deletions runtime/visitor.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package runtime

import (
"fmt"
"reflect"

"github.com/cloudson/gitql/parser"
"github.com/cloudson/gitql/utilities"
)

func (v *RuntimeVisitor) Visit(n *parser.NodeProgram) error {
Expand All @@ -26,6 +28,11 @@ func (v *RuntimeVisitor) VisitSelect(n *parser.NodeSelect) error {
n.WildCard = false
}

err := testAllFieldsInExpr(n.Where, proxyTableName)
if err != nil {
return err
}

n.Tables[0] = proxy.table
var from, to string
for from, to = range proxy.fields {
Expand Down Expand Up @@ -60,6 +67,26 @@ func (v *RuntimeVisitor) VisitSelect(n *parser.NodeSelect) error {
// Because we will, at first, discover the current object
}

func testAllFieldsInExpr(expr parser.NodeExpr, tableName string) error {
var err error
if expr != nil {
switch expr.(type) {
case *parser.NodeAnd, *parser.NodeOr:
err = testAllFieldsInExpr(expr.LeftValue(), tableName)
if err == nil {
return testAllFieldsInExpr(expr.RightValue(), tableName)
}
case *parser.NodeEqual, *parser.NodeNotEqual, *parser.NodeGreater, *parser.NodeSmaller, *parser.NodeIn:
return testAllFieldsInExpr(expr.LeftValue(), tableName)
case *parser.NodeId:
field := expr.(*parser.NodeId).Value()
if !utilities.IsFieldPresentInArray(builder.possibleTables[tableName], field) {
return fmt.Errorf("Table '%s' has not field '%s'", tableName, field)
}
}
}
return err
}
func testAllFieldsFromTable(fields []string, table string) error {
for _, f := range fields {
err := builder.UseFieldFromTable(f, table)
Expand Down
50 changes: 50 additions & 0 deletions runtime/visitor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package runtime

import (
"path/filepath"
"testing"

"github.com/cloudson/gitql/parser"
"github.com/cloudson/gitql/semantical"
)

func TestTestAllFieldsInExprBranches(t *testing.T) {
query := "select * from branches where name = 'something' and somthing > 'name'"
err := parseAndVisitQuery(query, "../", t)
if err == nil {
t.Error("Expected error, received none")
}
}

func TestTestAllFieldsInExprRefs(t *testing.T) {
query := "select * from refs where name = 'something' or type = 'asdfasdfsd'"
err := parseAndVisitQuery(query, "../", t)
if err != nil {
t.Errorf("Unexpedted error %s", err)
}
}

func TestTestAllFieldsInExprTags(t *testing.T) {
query := "select * from tags where type = 'blah'"
err := parseAndVisitQuery(query, "../", t)
if err == nil {
t.Errorf("Unexpedted error %s", err)
}
}

func parseAndVisitQuery(query, dir string, t *testing.T) error {
parser.New(query)
ast, errGit := parser.AST()
failTestIfError(errGit, t)

folder, errFile := filepath.Abs(dir)
failTestIfError(errFile, t)
ast.Path = &folder
errGit = semantical.Analysis(ast)
failTestIfError(errGit, t)

builder = GetGitBuilder(ast.Path)
visitor := new(RuntimeVisitor)
err := visitor.Visit(ast)
return err
}

0 comments on commit 58a06e0

Please sign in to comment.