(ref *ast.ColumnRef, tables []*Table, targetList *ast.List)
| 723 | } |
| 724 | |
| 725 | func 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 | } |
no test coverage detected