// pusheSelectRoutes is a convenience function that pushes all the select
// expressions and returns the list of resultColumns generated for it.
430403
func (pb *primitiveBuilder) pushSelectRoutes(selectExprs sqlparser.SelectExprs) ([]*resultColumn, error) {
	resultColumns := make([]*resultColumn, 0, len(selectExprs))
	for _, node := range selectExprs {
		switch node := node.(type) {
		case *sqlparser.AliasedExpr:
			pullouts, origin, expr, err := pb.findOrigin(node.Expr)
			if err != nil {
				return nil, err
			}
			node.Expr = expr
			rc, _, err := pb.bldr.PushSelect(node, origin)
			if err != nil {
				return nil, err
			}
			resultColumns = append(resultColumns, rc)
			pb.addPullouts(pullouts)
		case *sqlparser.StarExpr:
			var expanded bool
			var err error
			resultColumns, expanded, err = pb.expandStar(resultColumns, node)
			if err != nil {
				return nil, err
			}
			if expanded {
				continue
			}
			// We'll allow select * for simple routes.
			rb, ok := pb.bldr.(*route)
			if !ok {
				return nil, errors.New("unsupported: '*' expression in cross-shard query")
			}
			// Validate keyspace reference if any.
			if !node.TableName.IsEmpty() {
				if _, err := pb.st.FindTable(node.TableName); err != nil {
					return nil, err
				}
			}
			resultColumns = append(resultColumns, rb.PushAnonymous(node))
		case sqlparser.Nextval:
			rb, ok := pb.bldr.(*route)
			if !ok {
				// This code is unreachable because the parser doesn't allow joins for next val statements.
				return nil, errors.New("unsupported: SELECT NEXT query in cross-shard query")
			}
			for _, ro := range rb.routeOptions {
				if ro.eroute.Opcode != engine.SelectNext {
					return nil, errors.New("NEXT used on a non-sequence table")
				}
				ro.eroute.Opcode = engine.SelectNext
			}
			resultColumns = append(resultColumns, rb.PushAnonymous(node))
		default:
			panic(fmt.Sprintf("BUG: unexpceted select expression type: %T", node))
		}
	}
	return resultColumns, nil
}
87382
func (c CurlyRouter) SelectRoute(
	webServices []*WebService,
	httpRequest *http.Request) (selectedService *WebService, selected *Route, err error) {

	requestTokens := tokenizePath(httpRequest.URL.Path)

	detectedService := c.detectWebService(requestTokens, webServices)
	if detectedService == nil {
		if trace {
			traceLogger.Printf("no WebService was found to match URL path:%s\n", httpRequest.URL.Path)
		}
		return nil, nil, NewError(http.StatusNotFound, "404: Page Not Found")
	}
	candidateRoutes := c.selectRoutes(detectedService, requestTokens)
	if len(candidateRoutes) == 0 {
		if trace {
			traceLogger.Printf("no Route in WebService with path %s was found to match URL path:%s\n", detectedService.rootPath, httpRequest.URL.Path)
		}
		return detectedService, nil, NewError(http.StatusNotFound, "404: Page Not Found")
	}
	selectedRoute, err := c.detectRoute(candidateRoutes, httpRequest)
	if selectedRoute == nil {
		return detectedService, nil, err
	}
	return detectedService, selectedRoute, nil
}
111546
func (c *Column) SetSelectSQL(s string) {
	c.SelectSQL = s
	c.Writeable = false
	c.Readable = true
}
921226
func (c *Chain) selectRouteFor(addr string) (route *Chain, err error) {
	if c.IsEmpty() {
		return newRoute(), nil
	}
	if c.isRoute {
		return c, nil
	}

	route = newRoute()
	var nl []Node

	for _, group := range c.nodeGroups {
		var node Node
		node, err = group.Next()
		if err != nil {
			return
		}

		if node.Bypass.Contains(addr) {
			break
		}

		if node.Client.Transporter.Multiplex() {
			node.DialOptions = append(node.DialOptions,
				ChainDialOption(route),
			)
			route = newRoute() // cutoff the chain for multiplex node.
		}

		route.AddNode(node)
		nl = append(nl, node)
	}

	route.route = nl

	return
}
471947
func (p *pipelineChannel) PushFront(h HandlerTempl) {
	Handlers := make([]HandlerTempl, len(p.Handlers)+1)
	Handlers[0] = h
	copy(Handlers[1:], p.Handlers)
	p.Handlers = Handlers
}
633524
func (db *Database) buildSelect() string {
	if len(db.tables) == 0 {
		db.panicLog("Please set a table with Model [ + Joins ]")
	}

	var query bytes.Buffer
	query.WriteString(db.buildCTE())

	// Select
	var fields string
	query.WriteString("SELECT ")
	if len(db.selectFields) > 0 {
		fields = db.selectFields
	}

	query.WriteString(fields)
	query.WriteString(" FROM ")
	// Model && Join

	query.WriteString(strings.Join(db.tables, ","))
	query.WriteString(" ")

	// Join (optional)
	if len(db.joinTables) > 0 {
		query.WriteString(strings.Join(db.joinTables, " "))
		query.WriteString(" ")
	}

	// Where (optional)
	if len(db.whereFields) > 0 {
		query.WriteString(db.buildWhere())
	}

	// Order (optional)
	if db.order != "" {
		query.WriteString(" ORDER BY ")
		query.WriteString(db.order)
	}

	// Limit (optional)
	if db.limit != 0 {
		query.WriteString(" LIMIT ")
		query.WriteString(strconv.Itoa(db.limit))
	}

	// Offset (optional)
	if db.offset != 0 {
		query.WriteString(" OFFSET ")
		query.WriteString(strconv.Itoa(db.offset))
	}

	query.WriteString(";")
	qs := query.String()
	db.printLog(qs)
	return qs
}
1254209
func (oa *orderedAggregate) PushMisc(sel *sqlparser.Select) {
	oa.input.PushMisc(sel)
}
833557
func (srv *Server) AddRoute(path string, f Function) {
	if srv.rtable == nil {
		srv.rtable = make(map[string]Function)
	}
	srv.rtable[path] = f
}
280774
func (pb *primitiveBuilder) pushGroupBy(sel *sqlparser.Select, grouper groupByHandler) error {
	if sel.Distinct != "" {
		// We can be here only if the builder could handle a group by.
		if err := grouper.MakeDistinct(); err != nil {
			return err
		}
	}

	if len(sel.GroupBy) == 0 {
		return nil
	}
	if err := pb.st.ResolveSymbols(sel.GroupBy); err != nil {
		return fmt.Errorf("unsupported: in group by: %v", err)
	}

	// We can be here only if the builder could handle a group by.
	return grouper.SetGroupBy(sel.GroupBy)
}
403902
func (this *Router) executeRouteFuncs(req *Request, res *Response, next func(), route *Route) bool {
	for _, rfn := range route.Callbacks {
		rfn(req, res, next)
		// If the response has been "closed" by a "route" function then return.
		if res.Closed == true {
			return true
		}
	}
	return false
}
363735
func (list *List) PushAll(slice []string) *List {
	for _, s := range slice {
		list.Push(s)
	}
	return list
}
624841
func (s *DomainsService) ListPushes(accountID string, options *ListOptions) (*domainPushesResponse, error) {
	path := versioned(domainPushPath(accountID, 0))
	pushesResponse := &domainPushesResponse{}

	path, err := addURLQueryOptions(path, options)
	if err != nil {
		return nil, err
	}

	resp, err := s.client.get(path, pushesResponse)
	if err != nil {
		return nil, err
	}

	pushesResponse.HttpResponse = resp
	return pushesResponse, nil
}
476294
func (jb *join) PushMisc(sel *sqlparser.Select) {
	jb.Left.PushMisc(sel)
	jb.Right.PushMisc(sel)
}
888141
func (pb *primitiveBuilder) pushOrderBy(orderBy sqlparser.OrderBy) error {
	if oa, ok := pb.bldr.(*orderedAggregate); ok {
		return oa.PushOrderBy(pb, orderBy)
	}

	switch len(orderBy) {
	case 0:
		return nil
	case 1:
		// Special handling for ORDER BY NULL. Push it everywhere.
		if _, ok := orderBy[0].Expr.(*sqlparser.NullVal); ok {
			pb.bldr.PushOrderByNull()
			return nil
		} else if f, ok := orderBy[0].Expr.(*sqlparser.FuncExpr); ok {
			if f.Name.Lowered() == "rand" {
				pb.bldr.PushOrderByRand()
				return nil
			}
		}
	}

	firstRB, ok := pb.bldr.First().(*route)
	if !ok {
		return errors.New("unsupported: cannot order by on a cross-shard subquery")
	}
	for _, order := range orderBy {
		if node, ok := order.Expr.(*sqlparser.SQLVal); ok {
			// This block handles constructs that use ordinals for 'ORDER BY'. For example:
			// SELECT a, b, c FROM t1, t2 ORDER BY 1, 2, 3.
			num, err := ResultFromNumber(pb.st.ResultColumns, node)
			if err != nil {
				return err
			}
			target := pb.st.ResultColumns[num].column.Origin()
			if target != firstRB {
				return errors.New("unsupported: order by spans across shards")
			}
		} else {
			// Analyze column references within the expression to make sure they all
			// go to the same route.
			err := sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
				switch node := node.(type) {
				case *sqlparser.ColName:
					target, _, err := pb.st.Find(node)
					if err != nil {
						return false, err
					}
					if target != firstRB {
						return false, errors.New("unsupported: order by spans across shards")
					}
				case *sqlparser.Subquery:
					return false, errors.New("unsupported: order by has subquery")
				}
				return true, nil
			}, order.Expr)
			if err != nil {
				return err
			}
		}

		// There were no errors. We can push the order by to the left-most route.
		if err := firstRB.PushOrderBy(order); err != nil {
			return err
		}
	}
	return nil
}
590503
func (s *GetApiOutput) SetRouteSelectionExpression(v string) *GetApiOutput {
	s.RouteSelectionExpression = &v
	return s
}
94460
func (sq *subquery) PushSelect(expr *sqlparser.AliasedExpr, _ builder) (rc *resultColumn, colnum int, err error) {
	col, ok := expr.Expr.(*sqlparser.ColName)
	if !ok {
		return nil, 0, errors.New("unsupported: expression on results of a cross-shard subquery")
	}

	// colnum should already be set for subquery columns.
	inner := col.Metadata.(*column).colnum
	sq.esubquery.Cols = append(sq.esubquery.Cols, inner)

	// Build a new column reference to represent the result column.
	rc = newResultColumn(expr, sq)
	sq.resultColumns = append(sq.resultColumns, rc)

	return rc, len(sq.resultColumns) - 1, nil
}
292369
func (s *Selection) FindSelection(sel *Selection) *Selection {
	if sel == nil {
		return pushStack(s, nil)
	}
	return s.FindNodes(sel.Nodes...)
}