param_style.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package validate
  2. import (
  3. "github.com/kyleconroy/sqlc/internal/sql/ast"
  4. "github.com/kyleconroy/sqlc/internal/sql/astutils"
  5. "github.com/kyleconroy/sqlc/internal/sql/named"
  6. "github.com/kyleconroy/sqlc/internal/sql/sqlerr"
  7. )
  8. // A query can use one (and only one) of the following formats:
  9. // - positional parameters $1
  10. // - named parameter operator @param
  11. // - named parameter function calls sqlc.arg(param)
  12. func ParamStyle(n ast.Node) error {
  13. namedFunc := astutils.Search(n, named.IsParamFunc)
  14. for _, f := range namedFunc.Items {
  15. fc, ok := f.(*ast.FuncCall)
  16. if ok {
  17. switch val := fc.Args.Items[0].(type) {
  18. case *ast.FuncCall:
  19. return &sqlerr.Error{
  20. Code: "", // TODO: Pick a new error code
  21. Message: "Invalid argument to sqlc.arg()",
  22. Location: val.Location,
  23. }
  24. case *ast.ParamRef:
  25. return &sqlerr.Error{
  26. Code: "", // TODO: Pick a new error code
  27. Message: "Invalid argument to sqlc.arg()",
  28. Location: val.Location,
  29. }
  30. case *ast.A_Const, *ast.ColumnRef:
  31. default:
  32. return &sqlerr.Error{
  33. Code: "", // TODO: Pick a new error code
  34. Message: "Invalid argument to sqlc.arg()",
  35. }
  36. }
  37. }
  38. }
  39. return nil
  40. }