sq/libsq/ast/selector.go

119 lines
2.4 KiB
Go
Raw Normal View History

2016-10-17 07:14:01 +03:00
package ast
import (
"fmt"
2020-08-06 20:58:47 +03:00
"github.com/antlr/antlr4/runtime/Go/antlr"
"github.com/neilotoole/sq/libsq/ast/internal/slq"
2016-10-17 07:14:01 +03:00
)
2020-08-06 20:58:47 +03:00
const msgNodeNoAddChild = "%T cannot add children: failed to add %T"
const msgNodeNoAddChildren = "%T cannot add children: failed to add %d children"
var _ Node = (*Selector)(nil)
2016-10-17 07:14:01 +03:00
2020-08-06 20:58:47 +03:00
// Selector is a selector such as ".my_table" or ".my_col". The
// generic selector will typically be replaced with a more specific
// selector node such as TblSelector or ColSelector.
2016-10-17 07:14:01 +03:00
type Selector struct {
2020-08-06 20:58:47 +03:00
baseNode
2016-10-17 07:14:01 +03:00
}
func (s *Selector) String() string {
return nodeString(s)
}
func (s *Selector) SelValue() string {
return s.ctx.GetText()[1:]
}
2020-08-06 20:58:47 +03:00
var _ Node = (*TblSelector)(nil)
// TblSelector is a selector for a table, such as ".my_table"
// or "@my_src.my_table".
2016-10-17 07:14:01 +03:00
type TblSelector struct {
Selector
DSName string
TblName string
}
func newTblSelector(seg *Segment, tblName string, ctx antlr.ParseTree) *TblSelector {
tbl := &TblSelector{}
tbl.parent = seg
tbl.ctx = ctx
tbl.TblName = tblName
return tbl
}
2020-08-06 20:58:47 +03:00
// Selectable implements the Selectable marker interface.
2016-10-17 07:14:01 +03:00
func (s *TblSelector) Selectable() {
// no-op
}
func (s *TblSelector) SelValue() string {
return s.TblName
}
func (s *TblSelector) String() string {
text := nodeString(s)
2020-08-06 20:58:47 +03:00
text += fmt.Sprintf(" | table: %q | datasource: %q", s.SelValue(), s.DSName)
2016-10-17 07:14:01 +03:00
return text
}
2020-08-06 20:58:47 +03:00
var _ Node = (*ColSelector)(nil)
var _ ColExpr = (*ColSelector)(nil)
// ColSelector models a column selector such as ".user_id".
2016-10-17 07:14:01 +03:00
type ColSelector struct {
Selector
}
func newColSelector(parent Node, ctx antlr.ParseTree) *ColSelector {
col := &ColSelector{}
col.parent = parent
col.ctx = ctx
return col
}
// ColExpr returns the column name.
func (s *ColSelector) ColExpr() (string, error) {
// Drop the leading dot, e.g. ".user" -> "user"
return s.Text()[1:], nil
}
2020-08-06 20:58:47 +03:00
func (s *ColSelector) IsColName() bool {
return true
}
2016-10-17 07:14:01 +03:00
func (s *ColSelector) String() string {
return nodeString(s)
}
2020-08-06 20:58:47 +03:00
var _ Node = (*Cmpr)(nil)
// Cmpr models a comparison.
2016-10-17 07:14:01 +03:00
type Cmpr struct {
2020-08-06 20:58:47 +03:00
baseNode
2016-10-17 07:14:01 +03:00
}
func (c *Cmpr) String() string {
return nodeString(c)
}
2020-08-06 20:58:47 +03:00
func newCmnr(parent Node, ctx slq.ICmprContext) *Cmpr {
leaf, _ := ctx.GetChild(0).(*antlr.TerminalNodeImpl)
2016-10-17 07:14:01 +03:00
cmpr := &Cmpr{}
cmpr.ctx = leaf
cmpr.parent = parent
return cmpr
}
2020-08-06 20:58:47 +03:00
// Datasource models a source such as "@sakila_sl3".
2016-10-17 07:14:01 +03:00
type Datasource struct {
2020-08-06 20:58:47 +03:00
baseNode
2016-10-17 07:14:01 +03:00
}
func (d *Datasource) String() string {
return nodeString(d)
}