Skip to content

Commit

Permalink
handle boolean values in mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Raddon committed Dec 1, 2017
1 parent 34cbaab commit f77b451
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 10 additions & 0 deletions backends/mongo/mgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,16 @@ func TestSimpleRowSelect(t *testing.T) {
Deleted bool
//Category *datasource.StringArray
}{}

validateQuerySpec(t, tu.QuerySpec{
Sql: "select title, count, deleted from article WHERE deleted = true ",
ExpectRowCt: 3,
ValidateRowData: func() {
assert.Equal(t, true, data.Deleted)
},
RowData: &data,
})

validateQuerySpec(t, tu.QuerySpec{
Sql: "select title, count, deleted from article WHERE `author` = \"aaron\" ",
ExpectRowCt: 1,
Expand Down
10 changes: 9 additions & 1 deletion backends/mongo/sql_to_mgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ func (m *SqlToMgo) eval(arg expr.Node) (value.Value, bool, bool) {
val, ok := vm.Eval(nil, arg)
return val, ok, false
case *expr.IdentityNode:
if arg.IsBooleanIdentity() {
return value.NewBoolValue(arg.Bool()), true, false
}
return value.NewStringValue(arg.Text), true, true
}
return nil, false, false
Expand Down Expand Up @@ -518,6 +521,7 @@ func (m *SqlToMgo) walkFilterBinary(node *expr.BinaryNode, q *bson.M) (value.Val
lhval, lhok, isLident := m.eval(node.Args[0])
rhval, rhok, isRident := m.eval(node.Args[1])
if !lhok {
u.Warnf("not ok: %v l:%v r:%v", node, lhval, rhval)
return nil, fmt.Errorf("Could not evaluate left arg: %v", node.String())
}
if !rhok {
Expand All @@ -528,6 +532,7 @@ func (m *SqlToMgo) walkFilterBinary(node *expr.BinaryNode, q *bson.M) (value.Val
// comparison of left/right isn't mongos strong suit
// https://stackoverflow.com/questions/4442453/mongodb-query-condition-on-comparing-2-fields
// db.T.find( { $where : "this.Grade1 > this.Grade2" } );
u.Infof("identents? %v %v %v", lhval, rhval, node)
*q = bson.M{"$where": fmt.Sprintf("this.%s %s this.%s", lhval.ToString(), node.Operator.V, rhval.ToString())}
return nil, nil
}
Expand All @@ -537,7 +542,7 @@ func (m *SqlToMgo) walkFilterBinary(node *expr.BinaryNode, q *bson.M) (value.Val
// The $eq expression is equivalent to { field: <value> }.
if lhval != nil && rhval != nil {
*q = bson.M{lhval.ToString(): rhval.Value()}
//u.Infof("m=%#v type=%v", q, rhval.Type())
u.Infof("m=%#v type=%v", q, rhval.Type())
return nil, nil
}
if lhval != nil || rhval != nil {
Expand Down Expand Up @@ -736,6 +741,9 @@ func (m *SqlToMgo) walkAggFunc(node *expr.FuncNode) (q bson.M, _ error) {
func eval(cur expr.Node) (value.Value, bool) {
switch curNode := cur.(type) {
case *expr.IdentityNode:
if curNode.IsBooleanIdentity() {
return value.NewBoolValue(curNode.Bool()), true
}
return value.NewStringValue(curNode.Text), true
case *expr.StringNode:
return value.NewStringValue(curNode.Text), true
Expand Down

0 comments on commit f77b451

Please sign in to comment.