schema.go 888 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package catalog
  2. import (
  3. "fmt"
  4. "github.com/kyleconroy/sqlc/internal/sql/ast"
  5. "github.com/kyleconroy/sqlc/internal/sql/sqlerr"
  6. )
  7. func (c *Catalog) createSchema(stmt *ast.CreateSchemaStmt) error {
  8. if stmt.Name == nil {
  9. return fmt.Errorf("create schema: empty name")
  10. }
  11. if _, err := c.getSchema(*stmt.Name); err == nil {
  12. if !stmt.IfNotExists {
  13. return sqlerr.SchemaExists(*stmt.Name)
  14. }
  15. }
  16. c.Schemas = append(c.Schemas, &Schema{Name: *stmt.Name})
  17. return nil
  18. }
  19. func (c *Catalog) dropSchema(stmt *ast.DropSchemaStmt) error {
  20. // TODO: n^2 in the worst-case
  21. for _, name := range stmt.Schemas {
  22. idx := -1
  23. for i := range c.Schemas {
  24. if c.Schemas[i].Name == name.Str {
  25. idx = i
  26. }
  27. }
  28. if idx == -1 {
  29. if stmt.MissingOk {
  30. continue
  31. }
  32. return sqlerr.SchemaNotFound(name.Str)
  33. }
  34. c.Schemas = append(c.Schemas[:idx], c.Schemas[idx+1:]...)
  35. }
  36. return nil
  37. }