MCPcopy Index your code
hub / github.com/sqlc-dev/sqlc / findColumnForRef

Function findColumnForRef

internal/compiler/output_columns.go:725–779  ·  view source on GitHub ↗
(ref *ast.ColumnRef, tables []*Table, targetList *ast.List)

Source from the content-addressed store, hash-verified

723}
724
725func findColumnForRef(ref *ast.ColumnRef, tables []*Table, targetList *ast.List) error {
726 parts := stringSlice(ref.Fields)
727 var alias, name string
728 if len(parts) == 1 {
729 name = parts[0]
730 } else if len(parts) == 2 {
731 alias = parts[0]
732 name = parts[1]
733 }
734
735 var found int
736 for _, t := range tables {
737 if alias != "" && t.Rel.Name != alias {
738 continue
739 }
740
741 // Find matching column
742 for _, c := range t.Columns {
743 if c.Name == name {
744 found++
745 break
746 }
747 }
748 }
749
750 // Find matching alias if necessary
751 if found == 0 {
752 for _, c := range targetList.Items {
753 resTarget, ok := c.(*ast.ResTarget)
754 if !ok {
755 continue
756 }
757 if resTarget.Name != nil && *resTarget.Name == name {
758 found++
759 }
760 }
761 }
762
763 if found == 0 {
764 return &sqlerr.Error{
765 Code: "42703",
766 Message: fmt.Sprintf("column reference %q not found", name),
767 Location: ref.Location,
768 }
769 }
770 if found > 1 {
771 return &sqlerr.Error{
772 Code: "42703",
773 Message: fmt.Sprintf("column reference %q is ambiguous", name),
774 Location: ref.Location,
775 }
776 }
777
778 return nil
779}

Callers 1

findColumnForNodeFunction · 0.85

Calls 1

stringSliceFunction · 0.70

Tested by

no test coverage detected