5
5
"fmt"
6
6
7
7
"github.com/kyleconroy/sqlc/internal/sql/ast"
8
+ sqlerr "github.com/kyleconroy/sqlc/internal/sql/errors"
8
9
)
9
10
10
11
func Build (stmts []ast.Statement ) (* Catalog , error ) {
@@ -50,20 +51,13 @@ func stringSlice(list *ast.List) []string {
50
51
return items
51
52
}
52
53
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
-
60
54
func (c * Catalog ) getSchema (name string ) (* Schema , error ) {
61
55
for i := range c .Schemas {
62
56
if c .Schemas [i ].Name == name {
63
57
return c .Schemas [i ], nil
64
58
}
65
59
}
66
- return nil , ErrSchemaNotFound
60
+ return nil , sqlerr . SchemaNotFound ( name )
67
61
}
68
62
69
63
func (c * Catalog ) getTable (name * ast.TableName ) (* Schema , * Table , error ) {
@@ -79,7 +73,7 @@ func (c *Catalog) getTable(name *ast.TableName) (*Schema, *Table, error) {
79
73
}
80
74
}
81
75
if s == nil {
82
- return nil , nil , ErrSchemaNotFound
76
+ return nil , nil , sqlerr . SchemaNotFound ( ns )
83
77
}
84
78
t , _ , err := s .getTable (name )
85
79
if err != nil {
@@ -133,8 +127,7 @@ func (c *Catalog) alterTable(stmt *ast.AlterTableStmt) error {
133
127
}
134
128
}
135
129
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 )
138
131
}
139
132
// If a missing column is allowed, skip this command
140
133
if idx < 0 && cmd .MissingOk {
@@ -147,8 +140,7 @@ func (c *Catalog) alterTable(stmt *ast.AlterTableStmt) error {
147
140
case ast .AT_AddColumn :
148
141
for _ , c := range table .Columns {
149
142
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 )
152
144
}
153
145
}
154
146
table .Columns = append (table .Columns , & Column {
@@ -194,12 +186,10 @@ func (c *Catalog) createEnum(stmt *ast.CreateEnumStmt) error {
194
186
Name : stmt .TypeName .Name ,
195
187
}
196
188
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 )
199
190
}
200
191
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 )
203
193
}
204
194
schema .Types = append (schema .Types , Enum {
205
195
Name : stmt .TypeName .Name ,
@@ -214,8 +204,7 @@ func (c *Catalog) createSchema(stmt *ast.CreateSchemaStmt) error {
214
204
}
215
205
if _ , err := c .getSchema (* stmt .Name ); err == nil {
216
206
if ! stmt .IfNotExists {
217
- // return wrap(pg.ErrorSchemaAlreadyExists(name), raw.StmtLocation)
218
- return ErrRelationAlreadyExists
207
+ return sqlerr .SchemaExists (* stmt .Name )
219
208
}
220
209
}
221
210
c .Schemas = append (c .Schemas , & Schema {Name : * stmt .Name })
@@ -232,7 +221,7 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
232
221
return err
233
222
}
234
223
if _ , _ , err := schema .getTable (stmt .Name ); err != nil {
235
- if ! errors .Is (err , ErrRelationNotFound ) {
224
+ if ! errors .Is (err , sqlerr . NotFound ) {
236
225
return err
237
226
}
238
227
} else if stmt .IfNotExists {
@@ -263,7 +252,7 @@ func (c *Catalog) dropSchema(stmt *ast.DropSchemaStmt) error {
263
252
if stmt .MissingOk {
264
253
continue
265
254
}
266
- return ErrSchemaNotFound
255
+ return sqlerr . SchemaNotFound ( name . Str )
267
256
}
268
257
c .Schemas = append (c .Schemas [:idx ], c .Schemas [idx + 1 :]... )
269
258
}
@@ -277,14 +266,14 @@ func (c *Catalog) dropTable(stmt *ast.DropTableStmt) error {
277
266
ns = c .DefaultSchema
278
267
}
279
268
schema , err := c .getSchema (ns )
280
- if errors .Is (err , ErrSchemaNotFound ) && stmt .IfExists {
269
+ if errors .Is (err , sqlerr . NotFound ) && stmt .IfExists {
281
270
continue
282
271
} else if err != nil {
283
272
return err
284
273
}
285
274
286
275
_ , idx , err := schema .getTable (name )
287
- if errors .Is (err , ErrRelationNotFound ) && stmt .IfExists {
276
+ if errors .Is (err , sqlerr . NotFound ) && stmt .IfExists {
288
277
continue
289
278
} else if err != nil {
290
279
return err
@@ -319,7 +308,7 @@ func (s *Schema) getType(rel *ast.TypeName) (Type, error) {
319
308
}
320
309
}
321
310
}
322
- return nil , ErrRelationNotFound
311
+ return nil , sqlerr . TypeNotFound ( rel . Name )
323
312
}
324
313
325
314
func (s * Schema ) getTable (rel * ast.TableName ) (* Table , int , error ) {
@@ -328,7 +317,7 @@ func (s *Schema) getTable(rel *ast.TableName) (*Table, int, error) {
328
317
return s .Tables [i ], i , nil
329
318
}
330
319
}
331
- return nil , 0 , ErrRelationNotFound
320
+ return nil , 0 , sqlerr . RelationNotFound ( rel . Name )
332
321
}
333
322
334
323
type Table struct {
0 commit comments