insert_stmt.go 837 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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/sqlerr"
  6. )
  7. func InsertStmt(stmt *pg.InsertStmt) error {
  8. sel, ok := stmt.SelectStmt.(*pg.SelectStmt)
  9. if !ok {
  10. return nil
  11. }
  12. if sel.ValuesLists == nil {
  13. return nil
  14. }
  15. if len(sel.ValuesLists.Items) != 1 {
  16. return nil
  17. }
  18. sublist, ok := sel.ValuesLists.Items[0].(*ast.List)
  19. if !ok {
  20. return nil
  21. }
  22. colsLen := len(stmt.Cols.Items)
  23. valsLen := len(sublist.Items)
  24. switch {
  25. case colsLen > valsLen:
  26. return &sqlerr.Error{
  27. Code: "42601",
  28. Message: "INSERT has more target columns than expressions",
  29. }
  30. case colsLen < valsLen:
  31. return &sqlerr.Error{
  32. Code: "42601",
  33. Message: "INSERT has more expressions than target columns",
  34. }
  35. }
  36. return nil
  37. }