public.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package catalog
  2. import (
  3. "github.com/kyleconroy/sqlc/internal/sql/ast"
  4. "github.com/kyleconroy/sqlc/internal/sql/sqlerr"
  5. )
  6. func (c *Catalog) schemasToSearch(ns string) []string {
  7. if ns == "" {
  8. ns = c.DefaultSchema
  9. }
  10. return append(c.SearchPath, ns)
  11. }
  12. func (c *Catalog) ListFuncsByName(rel *ast.FuncName) ([]Function, error) {
  13. var funcs []Function
  14. for _, ns := range c.schemasToSearch(rel.Schema) {
  15. s, err := c.getSchema(ns)
  16. if err != nil {
  17. return nil, err
  18. }
  19. for i := range s.Funcs {
  20. if s.Funcs[i].Name == rel.Name {
  21. funcs = append(funcs, *s.Funcs[i])
  22. }
  23. }
  24. }
  25. return funcs, nil
  26. }
  27. func (c *Catalog) GetFuncN(rel *ast.FuncName, n int) (Function, error) {
  28. for _, ns := range c.schemasToSearch(rel.Schema) {
  29. s, err := c.getSchema(ns)
  30. if err != nil {
  31. return Function{}, err
  32. }
  33. for i := range s.Funcs {
  34. if s.Funcs[i].Name != rel.Name {
  35. continue
  36. }
  37. args := s.Funcs[i].InArgs()
  38. if len(args) == n {
  39. return *s.Funcs[i], nil
  40. }
  41. }
  42. }
  43. return Function{}, sqlerr.RelationNotFound(rel.Name)
  44. }
  45. func (c *Catalog) GetTable(rel *ast.TableName) (Table, error) {
  46. _, table, err := c.getTable(rel)
  47. if table == nil {
  48. return Table{}, err
  49. } else {
  50. return *table, err
  51. }
  52. }