@@ -370,6 +370,105 @@ func Update(c *pg.Catalog, stmt nodes.Node) error {
370370 Arguments : args ,
371371 ReturnType : join (n .ReturnType .Names , "." ),
372372 })
373+
374+ case nodes.CommentStmt :
375+ switch n .Objtype {
376+
377+ case nodes .OBJECT_SCHEMA :
378+ name := n .Object .(nodes.String ).Str
379+ schema , exists := c .Schemas [name ]
380+ if ! exists {
381+ return wrap (pg .ErrorSchemaDoesNotExist (name ), raw .StmtLocation )
382+ }
383+ if n .Comment != nil {
384+ schema .Comment = * n .Comment
385+ } else {
386+ schema .Comment = ""
387+ }
388+ c .Schemas [name ] = schema
389+
390+ case nodes .OBJECT_TABLE :
391+ fqn , err := ParseList (n .Object .(nodes.List ))
392+ if err != nil {
393+ return err
394+ }
395+ schema , exists := c .Schemas [fqn .Schema ]
396+ if ! exists {
397+ return wrap (pg .ErrorSchemaDoesNotExist (fqn .Schema ), raw .StmtLocation )
398+ }
399+ table , exists := schema .Tables [fqn .Rel ]
400+ if ! exists {
401+ return wrap (pg .ErrorRelationDoesNotExist (fqn .Rel ), raw .StmtLocation )
402+ }
403+ if n .Comment != nil {
404+ table .Comment = * n .Comment
405+ } else {
406+ table .Comment = ""
407+ }
408+ schema .Tables [fqn .Rel ] = table
409+
410+ case nodes .OBJECT_COLUMN :
411+ colParts := stringSlice (n .Object .(nodes.List ))
412+ var fqn pg.FQN
413+ var col string
414+ switch len (colParts ) {
415+ case 2 :
416+ col = colParts [1 ]
417+ fqn = pg.FQN {Schema : "public" , Rel : colParts [0 ]}
418+ case 3 :
419+ col = colParts [2 ]
420+ fqn = pg.FQN {Schema : colParts [0 ], Rel : colParts [1 ]}
421+ case 4 :
422+ col = colParts [3 ]
423+ fqn = pg.FQN {Catalog : colParts [0 ], Schema : colParts [1 ], Rel : colParts [2 ]}
424+ default :
425+ return fmt .Errorf ("column specifier %q is not the proper format, expected '[catalog.][schema.]colname.tablename'" , strings .Join (colParts , "." ))
426+ }
427+ schema , exists := c .Schemas [fqn .Schema ]
428+ if ! exists {
429+ return wrap (pg .ErrorSchemaDoesNotExist (fqn .Schema ), raw .StmtLocation )
430+ }
431+ table , exists := schema .Tables [fqn .Rel ]
432+ if ! exists {
433+ return wrap (pg .ErrorRelationDoesNotExist (fqn .Rel ), raw .StmtLocation )
434+ }
435+ idx := - 1
436+ for i , c := range table .Columns {
437+ if c .Name == col {
438+ idx = i
439+ }
440+ }
441+ if idx < 0 {
442+ return wrap (pg .ErrorColumnDoesNotExist (table .Name , col ), raw .StmtLocation )
443+ }
444+ if n .Comment != nil {
445+ table .Columns [idx ].Comment = * n .Comment
446+ } else {
447+ table .Columns [idx ].Comment = ""
448+ }
449+
450+ case nodes .OBJECT_TYPE :
451+ fqn , err := ParseList (n .Object .(nodes.TypeName ).Names )
452+ if err != nil {
453+ return err
454+ }
455+ schema , exists := c .Schemas [fqn .Schema ]
456+ if ! exists {
457+ return wrap (pg .ErrorSchemaDoesNotExist (fqn .Schema ), raw .StmtLocation )
458+ }
459+ enum , exists := schema .Enums [fqn .Rel ]
460+ if ! exists {
461+ return wrap (pg .ErrorRelationDoesNotExist (fqn .Rel ), raw .StmtLocation )
462+ }
463+ if n .Comment != nil {
464+ enum .Comment = * n .Comment
465+ } else {
466+ enum .Comment = ""
467+ }
468+ schema .Enums [fqn .Rel ] = enum
469+
470+ }
471+
373472 }
374473 return nil
375474}
0 commit comments