param_ref.go 796 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package validate
  2. import (
  3. "fmt"
  4. "github.com/kyleconroy/sqlc/internal/sql/ast"
  5. "github.com/kyleconroy/sqlc/internal/sql/ast/pg"
  6. "github.com/kyleconroy/sqlc/internal/sql/astutils"
  7. "github.com/kyleconroy/sqlc/internal/sql/sqlerr"
  8. )
  9. func ParamRef(n ast.Node) error {
  10. var allrefs []*pg.ParamRef
  11. // Find all parameter references
  12. astutils.Walk(astutils.VisitorFunc(func(node ast.Node) {
  13. switch n := node.(type) {
  14. case *pg.ParamRef:
  15. allrefs = append(allrefs, n)
  16. }
  17. }), n)
  18. seen := map[int]struct{}{}
  19. for _, r := range allrefs {
  20. seen[r.Number] = struct{}{}
  21. }
  22. for i := 1; i <= len(seen); i += 1 {
  23. if _, ok := seen[i]; !ok {
  24. return &sqlerr.Error{
  25. Code: "42P18",
  26. Message: fmt.Sprintf("could not determine data type of parameter $%d", i),
  27. }
  28. }
  29. }
  30. return nil
  31. }