parseConditionTree parses the condition tree passed to the WHERE clause.
()
| 12 | |
| 13 | // parseConditionTree parses the condition tree passed to the WHERE clause. |
| 14 | func (p *parser) parseConditionTree() (*query.ConditionNode, error) { |
| 15 | stack := lane.NewStack() |
| 16 | errFailedToParse := errors.New("failed to parse conditions") |
| 17 | |
| 18 | for { |
| 19 | if p.current = p.tokenizer.Next(); p.current == nil { |
| 20 | break |
| 21 | } |
| 22 | |
| 23 | switch p.current.Type { |
| 24 | |
| 25 | case tokenizer.Not: |
| 26 | // TODO: Handle NOT (...), for the time being we proceed with the other |
| 27 | // tokens and handle the negation when parsing the condition. |
| 28 | fallthrough |
| 29 | |
| 30 | case tokenizer.Identifier: |
| 31 | condition, err := p.parseCondition() |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | if condition == nil { |
| 36 | return nil, p.currentError() |
| 37 | } |
| 38 | |
| 39 | if condition.IsSubquery { |
| 40 | if err := p.parseSubquery(condition); err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | leafNode := &query.ConditionNode{Condition: condition} |
| 46 | if prevNode, ok := stack.Pop().(*query.ConditionNode); !ok { |
| 47 | stack.Push(leafNode) |
| 48 | } else if prevNode.Condition == nil { |
| 49 | prevNode.Right = leafNode |
| 50 | stack.Push(prevNode) |
| 51 | } else { |
| 52 | return nil, errFailedToParse |
| 53 | } |
| 54 | |
| 55 | case tokenizer.And, tokenizer.Or: |
| 56 | leftNode, ok := stack.Pop().(*query.ConditionNode) |
| 57 | if !ok { |
| 58 | return nil, errFailedToParse |
| 59 | } |
| 60 | |
| 61 | node := query.ConditionNode{ |
| 62 | Type: &p.current.Type, |
| 63 | Left: leftNode, |
| 64 | } |
| 65 | stack.Push(&node) |
| 66 | |
| 67 | case tokenizer.OpenParen: |
| 68 | stack.Push(nil) |
| 69 | |
| 70 | case tokenizer.CloseParen: |
| 71 | rightNode, ok := stack.Pop().(*query.ConditionNode) |