55 "fmt"
66
77 "github.com/kyleconroy/sqlc/internal/sql/ast"
8+ sqlerr "github.com/kyleconroy/sqlc/internal/sql/errors"
89)
910
1011func Build (stmts []ast.Statement ) (* Catalog , error ) {
@@ -50,20 +51,13 @@ func stringSlice(list *ast.List) []string {
5051 return items
5152}
5253
53- // TODO: This need to be rich error types
54- var ErrRelationNotFound = errors .New ("relation not found" )
55- var ErrRelationAlreadyExists = errors .New ("relation already exists" )
56- var ErrSchemaNotFound = errors .New ("schema not found" )
57- var ErrColumnNotFound = errors .New ("column not found" )
58- var ErrColumnExists = errors .New ("column already exists" )
59-
6054func (c * Catalog ) getSchema (name string ) (* Schema , error ) {
6155 for i := range c .Schemas {
6256 if c .Schemas [i ].Name == name {
6357 return c .Schemas [i ], nil
6458 }
6559 }
66- return nil , ErrSchemaNotFound
60+ return nil , sqlerr . SchemaNotFound ( name )
6761}
6862
6963func (c * Catalog ) getTable (name * ast.TableName ) (* Schema , * Table , error ) {
@@ -79,7 +73,7 @@ func (c *Catalog) getTable(name *ast.TableName) (*Schema, *Table, error) {
7973 }
8074 }
8175 if s == nil {
82- return nil , nil , ErrSchemaNotFound
76+ return nil , nil , sqlerr . SchemaNotFound ( ns )
8377 }
8478 t , _ , err := s .getTable (name )
8579 if err != nil {
@@ -133,8 +127,7 @@ func (c *Catalog) alterTable(stmt *ast.AlterTableStmt) error {
133127 }
134128 }
135129 if idx < 0 && ! cmd .MissingOk {
136- // return wrap(pg.ErrorColumnDoesNotExist(table.Name, *cmd.Name), raw.StmtLocation)
137- return ErrColumnNotFound
130+ return sqlerr .ColumnNotFound (table .Rel .Name , * cmd .Name )
138131 }
139132 // If a missing column is allowed, skip this command
140133 if idx < 0 && cmd .MissingOk {
@@ -147,8 +140,7 @@ func (c *Catalog) alterTable(stmt *ast.AlterTableStmt) error {
147140 case ast .AT_AddColumn :
148141 for _ , c := range table .Columns {
149142 if c .Name == cmd .Def .Colname {
150- // return wrap(pg.ErrorColumnAlreadyExists(table.Name, *d.Colname), d.Location)
151- return ErrColumnExists
143+ return sqlerr .ColumnExists (table .Rel .Name , c .Name )
152144 }
153145 }
154146 table .Columns = append (table .Columns , & Column {
@@ -194,12 +186,10 @@ func (c *Catalog) createEnum(stmt *ast.CreateEnumStmt) error {
194186 Name : stmt .TypeName .Name ,
195187 }
196188 if _ , _ , err := schema .getTable (tbl ); err == nil {
197- // return wrap(pg.ErrorRelationAlreadyExists(fqn.Rel), raw.StmtLocation)
198- return ErrRelationAlreadyExists
189+ return sqlerr .RelationExists (tbl .Name )
199190 }
200191 if _ , err := schema .getType (stmt .TypeName ); err == nil {
201- // return wrap(pg.ErrorTypeAlreadyExists(fqn.Rel), raw.StmtLocation)
202- return ErrRelationAlreadyExists
192+ return sqlerr .TypeExists (tbl .Name )
203193 }
204194 schema .Types = append (schema .Types , Enum {
205195 Name : stmt .TypeName .Name ,
@@ -214,8 +204,7 @@ func (c *Catalog) createSchema(stmt *ast.CreateSchemaStmt) error {
214204 }
215205 if _ , err := c .getSchema (* stmt .Name ); err == nil {
216206 if ! stmt .IfNotExists {
217- // return wrap(pg.ErrorSchemaAlreadyExists(name), raw.StmtLocation)
218- return ErrRelationAlreadyExists
207+ return sqlerr .SchemaExists (* stmt .Name )
219208 }
220209 }
221210 c .Schemas = append (c .Schemas , & Schema {Name : * stmt .Name })
@@ -232,7 +221,7 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
232221 return err
233222 }
234223 if _ , _ , err := schema .getTable (stmt .Name ); err != nil {
235- if ! errors .Is (err , ErrRelationNotFound ) {
224+ if ! errors .Is (err , sqlerr . NotFound ) {
236225 return err
237226 }
238227 } else if stmt .IfNotExists {
@@ -263,7 +252,7 @@ func (c *Catalog) dropSchema(stmt *ast.DropSchemaStmt) error {
263252 if stmt .MissingOk {
264253 continue
265254 }
266- return ErrSchemaNotFound
255+ return sqlerr . SchemaNotFound ( name . Str )
267256 }
268257 c .Schemas = append (c .Schemas [:idx ], c .Schemas [idx + 1 :]... )
269258 }
@@ -277,14 +266,14 @@ func (c *Catalog) dropTable(stmt *ast.DropTableStmt) error {
277266 ns = c .DefaultSchema
278267 }
279268 schema , err := c .getSchema (ns )
280- if errors .Is (err , ErrSchemaNotFound ) && stmt .IfExists {
269+ if errors .Is (err , sqlerr . NotFound ) && stmt .IfExists {
281270 continue
282271 } else if err != nil {
283272 return err
284273 }
285274
286275 _ , idx , err := schema .getTable (name )
287- if errors .Is (err , ErrRelationNotFound ) && stmt .IfExists {
276+ if errors .Is (err , sqlerr . NotFound ) && stmt .IfExists {
288277 continue
289278 } else if err != nil {
290279 return err
@@ -319,7 +308,7 @@ func (s *Schema) getType(rel *ast.TypeName) (Type, error) {
319308 }
320309 }
321310 }
322- return nil , ErrRelationNotFound
311+ return nil , sqlerr . TypeNotFound ( rel . Name )
323312}
324313
325314func (s * Schema ) getTable (rel * ast.TableName ) (* Table , int , error ) {
@@ -328,7 +317,7 @@ func (s *Schema) getTable(rel *ast.TableName) (*Table, int, error) {
328317 return s .Tables [i ], i , nil
329318 }
330319 }
331- return nil , 0 , ErrRelationNotFound
320+ return nil , 0 , sqlerr . RelationNotFound ( rel . Name )
332321}
333322
334323type Table struct {
0 commit comments