MCPcopy
hub / github.com/sqlc-dev/sqlc / sourceTables

Method sourceTables

internal/compiler/output_columns.go:481–648  ·  view source on GitHub ↗

Compute the output columns for a statement. Return an error if column references are ambiguous Return an error if column references don't exist Return an error if a table is referenced twice Return an error if an unknown column is referenced

(qc *QueryCatalog, node ast.Node)

Source from the content-addressed store, hash-verified

479// Return an error if a table is referenced twice
480// Return an error if an unknown column is referenced
481func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, error) {
482 list := &ast.List{}
483 switch n := node.(type) {
484 case *ast.DeleteStmt:
485 if n.Relations != nil {
486 list = n.Relations
487 } else if n.FromClause != nil {
488 // Multi-table DELETE: walk FromClause to find tables
489 var tv tableVisitor
490 astutils.Walk(&tv, n.FromClause)
491 list = &tv.list
492 }
493 case *ast.InsertStmt:
494 list = &ast.List{
495 Items: []ast.Node{n.Relation},
496 }
497 case *ast.SelectStmt:
498 var tv tableVisitor
499 astutils.Walk(&tv, n.FromClause)
500 list = &tv.list
501 case *ast.TruncateStmt:
502 list = astutils.Search(n.Relations, func(node ast.Node) bool {
503 _, ok := node.(*ast.RangeVar)
504 return ok
505 })
506 case *ast.RefreshMatViewStmt:
507 list = astutils.Search(n.Relation, func(node ast.Node) bool {
508 _, ok := node.(*ast.RangeVar)
509 return ok
510 })
511 case *ast.UpdateStmt:
512 var tv tableVisitor
513 astutils.Walk(&tv, n.FromClause)
514 astutils.Walk(&tv, n.Relations)
515 list = &tv.list
516 }
517
518 var tables []*Table
519 for _, item := range list.Items {
520 item := item
521 switch n := item.(type) {
522
523 case *ast.RangeFunction:
524 var funcCall *ast.FuncCall
525 switch f := n.Functions.Items[0].(type) {
526 case *ast.List:
527 switch fi := f.Items[0].(type) {
528 case *ast.FuncCall:
529 funcCall = fi
530 case *ast.SQLValueFunction:
531 continue // TODO handle this correctly
532 default:
533 continue
534 }
535 case *ast.FuncCall:
536 funcCall = f
537 default:
538 return nil, fmt.Errorf("sourceTables: unsupported function call type %T", n.Functions.Items[0])

Callers 2

outputColumnsMethod · 0.95
expandStmtMethod · 0.95

Calls 6

outputColumnsMethod · 0.95
WalkFunction · 0.92
SearchFunction · 0.92
ParseTableNameFunction · 0.85
GetFuncMethod · 0.80
GetTableMethod · 0.45

Tested by

no test coverage detected