sq/libsq/ast/join.go

205 lines
4.0 KiB
Go
Raw Normal View History

2016-10-17 07:14:01 +03:00
package ast
import (
"fmt"
"github.com/neilotoole/sq/libsq/ast/internal/slq"
"github.com/antlr/antlr4/runtime/Go/antlr/v4"
2016-10-17 07:14:01 +03:00
)
var _ Node = (*JoinNode)(nil)
2016-10-17 07:14:01 +03:00
// JoinNode models a SQL JOIN node. It has a child of type JoinConstraint.
type JoinNode struct {
seg *SegmentNode
2016-10-31 01:35:56 +03:00
ctx antlr.ParseTree
constraint *JoinConstraint
leftTbl *TblSelectorNode
rightTbl *TblSelectorNode
2016-10-17 07:14:01 +03:00
}
2020-08-06 20:58:47 +03:00
// LeftTbl is the selector for the left table of the join.
func (jn *JoinNode) LeftTbl() *TblSelectorNode {
2016-10-31 01:35:56 +03:00
return jn.leftTbl
2016-10-17 07:14:01 +03:00
}
2020-08-06 20:58:47 +03:00
// RightTbl is the selector for the right table of the join.
func (jn *JoinNode) RightTbl() *TblSelectorNode {
2016-10-31 01:35:56 +03:00
return jn.rightTbl
2016-10-17 07:14:01 +03:00
}
2020-08-06 20:58:47 +03:00
// Tabler implements the Tabler marker interface.
func (jn *JoinNode) tabler() {
2016-10-17 07:14:01 +03:00
// no-op
}
func (jn *JoinNode) Parent() Node {
2016-10-31 01:35:56 +03:00
return jn.seg
2016-10-17 07:14:01 +03:00
}
func (jn *JoinNode) SetParent(parent Node) error {
seg, ok := parent.(*SegmentNode)
2016-10-17 07:14:01 +03:00
if !ok {
return errorf("%T requires parent of type %s", jn, typeSegmentNode)
2016-10-17 07:14:01 +03:00
}
2016-10-31 01:35:56 +03:00
jn.seg = seg
2016-10-17 07:14:01 +03:00
return nil
}
func (jn *JoinNode) Children() []Node {
2016-10-31 01:35:56 +03:00
if jn.constraint == nil {
2016-10-17 07:14:01 +03:00
return []Node{}
}
2016-10-31 01:35:56 +03:00
return []Node{jn.constraint}
2016-10-17 07:14:01 +03:00
}
func (jn *JoinNode) AddChild(node Node) error {
2020-08-06 20:58:47 +03:00
jc, ok := node.(*JoinConstraint)
2016-10-17 07:14:01 +03:00
if !ok {
2020-08-06 20:58:47 +03:00
return errorf("JOIN() child must be *JoinConstraint, but got: %T", node)
2016-10-17 07:14:01 +03:00
}
2016-10-31 01:35:56 +03:00
if jn.constraint != nil {
2016-10-17 07:14:01 +03:00
return errorf("JOIN() has max 1 child: failed to add: %T", node)
}
2020-08-06 20:58:47 +03:00
jn.constraint = jc
2016-10-17 07:14:01 +03:00
return nil
}
func (jn *JoinNode) SetChildren(children []Node) error {
2016-10-17 07:14:01 +03:00
if len(children) == 0 {
2016-10-31 01:35:56 +03:00
jn.constraint = nil
2016-10-17 07:14:01 +03:00
return nil
}
if len(children) > 1 {
return errorf("JOIN() can have only one child: failed to add %d children", len(children))
}
2016-10-31 01:35:56 +03:00
expr, ok := children[0].(*JoinConstraint)
2016-10-17 07:14:01 +03:00
if !ok {
return errorf("JOIN() child must be *FnJoinExpr, but got: %T", children[0])
}
2016-10-31 01:35:56 +03:00
jn.constraint = expr
2016-10-17 07:14:01 +03:00
return nil
}
func (jn *JoinNode) Context() antlr.ParseTree {
2016-10-31 01:35:56 +03:00
return jn.ctx
2016-10-17 07:14:01 +03:00
}
func (jn *JoinNode) SetContext(ctx antlr.ParseTree) error {
2016-10-31 01:35:56 +03:00
jn.ctx = ctx
2016-10-17 07:14:01 +03:00
return nil
}
func (jn *JoinNode) Text() string {
2016-10-31 01:35:56 +03:00
return jn.ctx.GetText()
2016-10-17 07:14:01 +03:00
}
func (jn *JoinNode) Segment() *SegmentNode {
2016-10-31 01:35:56 +03:00
return jn.seg
2016-10-17 07:14:01 +03:00
}
func (jn *JoinNode) String() string {
2016-10-31 01:35:56 +03:00
text := nodeString(jn)
2016-10-17 07:14:01 +03:00
leftTblName := ""
rightTblName := ""
2016-10-31 01:35:56 +03:00
if jn.leftTbl != nil {
leftTblName, _ = jn.leftTbl.SelValue()
2016-10-17 07:14:01 +03:00
}
2016-10-31 01:35:56 +03:00
if jn.rightTbl != nil {
rightTblName, _ = jn.rightTbl.SelValue()
2016-10-17 07:14:01 +03:00
}
text += fmt.Sprintf(" | left_table: {%s} | right_table: {%s}", leftTblName, rightTblName)
2016-10-17 07:14:01 +03:00
return text
}
2020-08-06 20:58:47 +03:00
var _ Node = (*JoinConstraint)(nil)
2016-10-17 07:14:01 +03:00
2020-08-06 20:58:47 +03:00
// JoinConstraint models a join's constraint.
// For example the elements inside the parentheses
// in "join(.uid == .user_id)".
2016-10-31 01:35:56 +03:00
type JoinConstraint struct {
2020-08-06 20:58:47 +03:00
// join is the parent node
join *JoinNode
2016-10-17 07:14:01 +03:00
ctx antlr.ParseTree
children []Node
}
func (n *JoinConstraint) Parent() Node {
return n.join
2016-10-17 07:14:01 +03:00
}
func (n *JoinConstraint) SetParent(parent Node) error {
join, ok := parent.(*JoinNode)
2016-10-17 07:14:01 +03:00
if !ok {
return errorf("%T requires parent of type %s", n, typeJoinNode)
2016-10-17 07:14:01 +03:00
}
n.join = join
2016-10-17 07:14:01 +03:00
return nil
}
func (n *JoinConstraint) Children() []Node {
return n.children
2016-10-17 07:14:01 +03:00
}
func (n *JoinConstraint) AddChild(child Node) error {
2016-10-17 07:14:01 +03:00
nodeCtx := child.Context()
switch nodeCtx.(type) {
case *antlr.TerminalNodeImpl:
case *slq.SelectorContext:
default:
return errorf("cannot add child node %T to %T", nodeCtx, n.ctx)
2016-10-17 07:14:01 +03:00
}
n.children = append(n.children, child)
2016-10-17 07:14:01 +03:00
return nil
}
func (n *JoinConstraint) SetChildren(children []Node) error {
2016-10-17 07:14:01 +03:00
for _, child := range children {
nodeCtx := child.Context()
switch nodeCtx.(type) {
case *antlr.TerminalNodeImpl:
case *slq.SelectorContext:
default:
return errorf("cannot add child node %T to %T", nodeCtx, n.ctx)
2016-10-17 07:14:01 +03:00
}
}
2020-08-06 20:58:47 +03:00
if len(children) == 0 {
n.children = children
2020-08-06 20:58:47 +03:00
return nil
}
n.children = children
2016-10-17 07:14:01 +03:00
return nil
}
func (n *JoinConstraint) Context() antlr.ParseTree {
return n.ctx
2016-10-17 07:14:01 +03:00
}
func (n *JoinConstraint) SetContext(ctx antlr.ParseTree) error {
n.ctx = ctx // TODO: check for correct type
2016-10-17 07:14:01 +03:00
return nil
}
func (n *JoinConstraint) Text() string {
return n.ctx.GetText()
2016-10-17 07:14:01 +03:00
}
func (n *JoinConstraint) String() string {
return nodeString(n)
2016-10-17 07:14:01 +03:00
}