func (n *NamespacedContextReader) Get(key string) (value.Value, bool) { left, right, has := expr.LeftRight(key) if !has || strings.ToLower(left) != n.namespace { return nil, false } return n.basereader.Get(right) }
func (m *Schema) Table(tableName string) (*Table, error) { tbl, ok := m.tableMap[tableName] if ok && tbl != nil { return tbl, nil } _, tableName, _ = expr.LeftRight(tableName) return m.findTable(strings.ToLower(tableName)) }
func (m *SqlDriverMessageMap) Get(key string) (value.Value, bool) { if idx, ok := m.ColIndex[key]; ok { return value.NewValue(m.Vals[idx]), true } //u.Debugf("could not find: %v in %#v", key, m.ColIndex) _, right, hasLeft := expr.LeftRight(key) if hasLeft { if idx, ok := m.ColIndex[right]; ok { return value.NewValue(m.Vals[idx]), true } } return nil, false }
func (m *Sqlbridge) parseSourceTable(req *SqlSelect) error { if m.Cur().T != lex.TokenIdentity { return fmt.Errorf("expected tablename but got: %v", m.Cur()) } src := SqlSource{} req.From = append(req.From, &src) src.Schema, src.Name, _ = expr.LeftRight(m.Next().V) if m.Cur().T == lex.TokenAs { m.Next() // Skip over "AS", we don't need it src.Alias = m.Next().V } return nil }
func parseColumns(m expr.TokenPager, fr expr.FuncResolver, buildVm bool, stmt ColumnsStatement) error { var col *Column for { //u.Debug(m.Cur()) switch m.Cur().T { case lex.TokenStar, lex.TokenMultiply: col = &Column{Star: true} m.Next() case lex.TokenUdfExpr: // we have a udf/functional expression column col = NewColumnFromToken(m.Cur()) funcName := strings.ToLower(m.Cur().V) tree := expr.NewTreeFuncs(m, fr) if err := tree.BuildTree(buildVm); err != nil { u.Errorf("could not parse: %v", err) return err } col.Expr = tree.Root col.SourceField = expr.FindIdentityField(col.Expr) if strings.Contains(col.SourceField, ".") { if _, right, hasLeft := expr.LeftRight(col.SourceField); hasLeft { col.SourceField = right } } col.Agg = expr.IsAgg(funcName) if m.Cur().T != lex.TokenAs { switch n := col.Expr.(type) { case *expr.FuncNode: // lets lowercase name n.Name = funcName col.As = expr.FindIdentityName(0, n, "") //u.Infof("col %#v", col) if col.As == "" { if strings.ToLower(n.Name) == "count" { //u.Warnf("count*") col.As = "count(*)" } else { col.As = n.Name } } case *expr.BinaryNode: //u.Debugf("udf? %T ", col.Expr) col.As = expr.FindIdentityName(0, n, "") if col.As == "" { u.Errorf("could not find as name: %#v", n) } } } else { switch n := col.Expr.(type) { case *expr.FuncNode: n.Name = funcName } } //u.Debugf("next? %v", m.Cur()) case lex.TokenIdentity: col = NewColumnFromToken(m.Cur()) tree := expr.NewTreeFuncs(m, fr) if err := tree.BuildTree(buildVm); err != nil { u.Errorf("could not parse: %v", err) return err } col.Expr = tree.Root case lex.TokenValue, lex.TokenInteger: // Value Literal col = NewColumnValue(m.Cur()) tree := expr.NewTreeFuncs(m, fr) if err := tree.BuildTree(buildVm); err != nil { u.Errorf("could not parse: %v", err) return err } col.Expr = tree.Root } //u.Debugf("after colstart?: %v ", m.Cur()) // since we can loop inside switch statement switch m.Cur().T { case lex.TokenAs: m.Next() switch m.Cur().T { case lex.TokenIdentity, lex.TokenValue: col.As = m.Cur().V col.originalAs = col.As col.asQuoteByte = m.Cur().Quote m.Next() continue } return fmt.Errorf("expected identity but got: %v", m.Cur().String()) case lex.TokenFrom, lex.TokenInto, lex.TokenLimit, lex.TokenEOS, lex.TokenEOF: // This indicates we have come to the End of the columns stmt.AddColumn(*col) //u.Debugf("Ending column ") return nil case lex.TokenIf: // If guard m.Next() //u.Infof("if guard: %v", m.Cur()) tree := expr.NewTreeFuncs(m, fr) if err := tree.BuildTree(buildVm); err != nil { u.Errorf("could not parse: %v", err) return err } col.Guard = tree.Root // Hm, we need to backup here? Parse Node went to deep? continue //u.Infof("if guard 2: %v", m.Cur()) //u.Debugf("after if guard?: %v ", m.Cur()) case lex.TokenCommentSingleLine: m.Next() col.Comment = m.Cur().V case lex.TokenRightParenthesis: // loop on my friend case lex.TokenComma: //u.Infof("? %#v", stmt) //u.Infof("col?%+v", col) stmt.AddColumn(*col) //u.Debugf("comma, added cols: %v", len(stmt.Columns)) default: return fmt.Errorf("expected column but got: %v", m.Cur().String()) } m.Next() } //u.Debugf("cols: %d", len(stmt.Columns)) return nil }