cmd.go 693 B

1234567891011121314151617181920212223242526272829303132
  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. )
  7. func Cmd(n ast.Node, name, cmd string) error {
  8. // TODO: Convert cmd to an enum
  9. if !(cmd == ":many" || cmd == ":one") {
  10. return nil
  11. }
  12. var list *ast.List
  13. switch stmt := n.(type) {
  14. case *pg.SelectStmt:
  15. return nil
  16. case *pg.DeleteStmt:
  17. list = stmt.ReturningList
  18. case *pg.InsertStmt:
  19. list = stmt.ReturningList
  20. case *pg.UpdateStmt:
  21. list = stmt.ReturningList
  22. default:
  23. return nil
  24. }
  25. if list == nil || len(list.Items) == 0 {
  26. return fmt.Errorf("query %q specifies parameter %q without containing a RETURNING clause", name, cmd)
  27. }
  28. return nil
  29. }