@@ -2,6 +2,7 @@ package catalog
22
33import (
44 "errors"
5+ "fmt"
56
67 "github.com/kyleconroy/sqlc/internal/sql/ast"
78)
@@ -23,8 +24,12 @@ func Build(stmts []ast.Statement) (*Catalog, error) {
2324 err = c .alterTable (n )
2425 case * ast.CreateEnumStmt :
2526 err = c .createEnum (n )
27+ case * ast.CreateSchemaStmt :
28+ err = c .createSchema (n )
2629 case * ast.CreateTableStmt :
2730 err = c .createTable (n )
31+ case * ast.DropSchemaStmt :
32+ err = c .dropSchema (n )
2833 case * ast.DropTableStmt :
2934 err = c .dropTable (n )
3035 }
@@ -203,6 +208,20 @@ func (c *Catalog) createEnum(stmt *ast.CreateEnumStmt) error {
203208 return nil
204209}
205210
211+ func (c * Catalog ) createSchema (stmt * ast.CreateSchemaStmt ) error {
212+ if stmt .Name == nil {
213+ return fmt .Errorf ("create schema: empty name" )
214+ }
215+ if _ , err := c .getSchema (* stmt .Name ); err == nil {
216+ if ! stmt .IfNotExists {
217+ // return wrap(pg.ErrorSchemaAlreadyExists(name), raw.StmtLocation)
218+ return ErrRelationAlreadyExists
219+ }
220+ }
221+ c .Schemas = append (c .Schemas , & Schema {Name : * stmt .Name })
222+ return nil
223+ }
224+
206225func (c * Catalog ) createTable (stmt * ast.CreateTableStmt ) error {
207226 ns := stmt .Name .Schema
208227 if ns == "" {
@@ -231,6 +250,26 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
231250 return nil
232251}
233252
253+ func (c * Catalog ) dropSchema (stmt * ast.DropSchemaStmt ) error {
254+ // TODO: n^2 in the worst-case
255+ for _ , name := range stmt .Schemas {
256+ idx := - 1
257+ for i := range c .Schemas {
258+ if c .Schemas [i ].Name == name .Str {
259+ idx = i
260+ }
261+ }
262+ if idx == - 1 {
263+ if stmt .MissingOk {
264+ continue
265+ }
266+ return ErrSchemaNotFound
267+ }
268+ c .Schemas = append (c .Schemas [:idx ], c .Schemas [idx + 1 :]... )
269+ }
270+ return nil
271+ }
272+
234273func (c * Catalog ) dropTable (stmt * ast.DropTableStmt ) error {
235274 for _ , name := range stmt .Tables {
236275 ns := name .Schema
0 commit comments