param_style.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package validate
  2. import (
  3. "github.com/kyleconroy/sqlc/internal/sql/ast"
  4. "github.com/kyleconroy/sqlc/internal/sql/ast/pg"
  5. "github.com/kyleconroy/sqlc/internal/sql/astutils"
  6. "github.com/kyleconroy/sqlc/internal/sql/named"
  7. "github.com/kyleconroy/sqlc/internal/sql/sqlerr"
  8. )
  9. // A query can use one (and only one) of the following formats:
  10. // - positional parameters $1
  11. // - named parameter operator @param
  12. // - named parameter function calls sqlc.arg(param)
  13. func ParamStyle(n ast.Node) error {
  14. positional := astutils.Search(n, func(node ast.Node) bool {
  15. _, ok := node.(*pg.ParamRef)
  16. return ok
  17. })
  18. namedFunc := astutils.Search(n, named.IsParamFunc)
  19. namedSign := astutils.Search(n, named.IsParamSign)
  20. for _, check := range []bool{
  21. len(positional.Items) > 0 && len(namedSign.Items)+len(namedFunc.Items) > 0,
  22. len(namedFunc.Items) > 0 && len(namedSign.Items)+len(positional.Items) > 0,
  23. len(namedSign.Items) > 0 && len(positional.Items)+len(namedFunc.Items) > 0,
  24. } {
  25. if check {
  26. return &sqlerr.Error{
  27. Code: "", // TODO: Pick a new error code
  28. Message: "query mixes positional parameters ($1) and named parameters (sqlc.arg or @arg)",
  29. }
  30. }
  31. }
  32. return nil
  33. }