comment_on.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package catalog
  2. import (
  3. "github.com/kyleconroy/sqlc/internal/sql/ast"
  4. "github.com/kyleconroy/sqlc/internal/sql/sqlerr"
  5. )
  6. func (c *Catalog) commentOnColumn(stmt *ast.CommentOnColumnStmt) error {
  7. _, t, err := c.getTable(stmt.Table)
  8. if err != nil {
  9. return err
  10. }
  11. for i := range t.Columns {
  12. if t.Columns[i].Name == stmt.Col.Name {
  13. if stmt.Comment != nil {
  14. t.Columns[i].Comment = *stmt.Comment
  15. } else {
  16. t.Columns[i].Comment = ""
  17. }
  18. return nil
  19. }
  20. }
  21. return sqlerr.ColumnNotFound(stmt.Table.Name, stmt.Col.Name)
  22. }
  23. func (c *Catalog) commentOnSchema(stmt *ast.CommentOnSchemaStmt) error {
  24. s, err := c.getSchema(stmt.Schema.Str)
  25. if err != nil {
  26. return err
  27. }
  28. if stmt.Comment != nil {
  29. s.Comment = *stmt.Comment
  30. } else {
  31. s.Comment = ""
  32. }
  33. return nil
  34. }
  35. func (c *Catalog) commentOnTable(stmt *ast.CommentOnTableStmt) error {
  36. _, t, err := c.getTable(stmt.Table)
  37. if err != nil {
  38. return err
  39. }
  40. if stmt.Comment != nil {
  41. t.Comment = *stmt.Comment
  42. } else {
  43. t.Comment = ""
  44. }
  45. return nil
  46. }
  47. func (c *Catalog) commentOnType(stmt *ast.CommentOnTypeStmt) error {
  48. t, _, err := c.getType(stmt.Type)
  49. if err != nil {
  50. return err
  51. }
  52. if stmt.Comment != nil {
  53. t.SetComment(*stmt.Comment)
  54. } else {
  55. t.SetComment("")
  56. }
  57. return nil
  58. }