1
0

insert_stmt.go 789 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package validate
  2. import (
  3. "github.com/kyleconroy/sqlc/internal/sql/ast"
  4. "github.com/kyleconroy/sqlc/internal/sql/sqlerr"
  5. )
  6. func InsertStmt(stmt *ast.InsertStmt) error {
  7. sel, ok := stmt.SelectStmt.(*ast.SelectStmt)
  8. if !ok {
  9. return nil
  10. }
  11. if sel.ValuesLists == nil {
  12. return nil
  13. }
  14. if len(sel.ValuesLists.Items) != 1 {
  15. return nil
  16. }
  17. sublist, ok := sel.ValuesLists.Items[0].(*ast.List)
  18. if !ok {
  19. return nil
  20. }
  21. colsLen := len(stmt.Cols.Items)
  22. valsLen := len(sublist.Items)
  23. switch {
  24. case colsLen > valsLen:
  25. return &sqlerr.Error{
  26. Code: "42601",
  27. Message: "INSERT has more target columns than expressions",
  28. }
  29. case colsLen < valsLen:
  30. return &sqlerr.Error{
  31. Code: "42601",
  32. Message: "INSERT has more expressions than target columns",
  33. }
  34. }
  35. return nil
  36. }