From 10f571263cccc148549f3bf9ea3acf9fd3b38ed8 Mon Sep 17 00:00:00 2001 From: Todd Persen Date: Tue, 8 Oct 2013 12:56:59 -0400 Subject: [PATCH] Simplify API. --- src/query/parser.go | 13 +++++++++++++ src/query/parser_test.go | 13 ++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/query/parser.go b/src/query/parser.go index 5434c72cc5..f0f343029d 100644 --- a/src/query/parser.go +++ b/src/query/parser.go @@ -53,6 +53,12 @@ type Query struct { groupByClause GroupByClause Limit int } +func (self *WhereCondition) GetBoolExpression() (*BoolExpression, bool) { + if self.isBooleanExpression { + return self.Left.(*BoolExpression), true + } + return nil, false +} func (self *Query) GetColumnNames() []*Value { if self.ColumnNames != nil { @@ -67,6 +73,13 @@ func (self *Query) GetFromClause() *Value { return GetValue(self.q.f) } +func (self *Expression) GetSimpleValue() (*Value, bool) { + if self.Operation == '\000' { + return self.Left, true + } + return nil, false +} + func GetValueArray(array *C.value_array) []*Value { if array == nil { return nil diff --git a/src/query/parser_test.go b/src/query/parser_test.go index 10ae3e4d5c..8e3750c500 100644 --- a/src/query/parser_test.go +++ b/src/query/parser_test.go @@ -32,15 +32,18 @@ func (self *QueryParserSuite) TestParseBasicSelectQuery(c *C) { w := q.GetWhereCondition() c.Assert(q.GetFromClause().Name, Equals, "t") - boolExpression := w.Left.(*BoolExpression) + boolExpression, ok := w.GetBoolExpression() + c.Assert(ok, Equals, true) + leftExpression := boolExpression.Left rightExpression := boolExpression.Right - leftValue := leftExpression.Left.Name - rightValue := rightExpression.Left.Name - c.Assert(leftValue, Equals, "c") + leftValue, ok := leftExpression.GetSimpleValue() // simple value is an expression with one value, e.g. it doesn't combine value using arithmetic operations + rightValue, ok := rightExpression.GetSimpleValue() + + c.Assert(leftValue.Name, Equals, "c") c.Assert(boolExpression.Operation, Equals, "==") - c.Assert(rightValue, Equals, "5") + c.Assert(rightValue.Name, Equals, "5") } func (self *QueryParserSuite) TestParseSelectWithoutWhereClause(c *C) {